Commit Graph

1216 Commits

Author SHA1 Message Date
Slava Pestov
14d1fcb51a AST: TypeChecker::conformsToProtocol() => ModuleDecl::checkConformance() 2024-01-16 17:08:00 -05:00
Pavel Yaskevich
e8b7a26eac [AST] Add a flag to indicate that the conformance is @preconcurrency 2024-01-16 11:51:42 -08:00
Pavel Yaskevich
e40d4e42f8 [Diagnostics] Adjust isMisplacedMissingArgument to handle unresolved closures when matching to new parameter
Cannot rely that closure type is always resolved, but if the
other parameter has a correct shape, consider it a match.
2024-01-08 14:43:28 -08:00
Pavel Yaskevich
859106eed1 [Diagnostics] Expand trailing closure failure to handle all closure arguments 2024-01-08 14:09:18 -08:00
Pavel Yaskevich
92577723fb [Diagnostics] Add a tailored note when passing a closure to a non-closure accepting parameter 2024-01-08 14:09:18 -08:00
Anthony Latsis
bd2f48d9d2 [NFC] AST: Introduce and use Identifier::isConstructor 2023-12-25 21:09:17 +03:00
Sima Nerush
b6d0afba1f Merge pull request #67594 from simanerush/simanerush/pack-iteration-impl
[SE-0408] Enable Pack Iteration
2023-12-07 17:09:48 -08:00
Sima Nerush
23485990e5 Diagnose that where clause is not supported 2023-12-03 21:51:41 -08:00
Pavel Yaskevich
1f42585fdd [AST] NFC: Rename KeyPathExpr::{get, set}RootType to {get, set}ExplicitRootType 2023-11-28 13:01:43 -08:00
Pavel Yaskevich
bd4ee4681b Merge pull request #70007 from xedin/keypath-application-improvements
[ConstraintSystem] Modernization of key path application handling
2023-11-28 12:44:42 -08:00
Slava Pestov
1b1963e904 Sema: Remove 'inferred result type requires explicit coercion' diagnostic 2023-11-27 14:05:36 -05:00
Pavel Yaskevich
8a0d61faac [CSDiagnostics] Add a diagnostic for an invalid key path subscript index argument 2023-11-21 14:53:46 -08:00
Pavel Yaskevich
9f21b95fcb [CSDiagnostics] A tailored diagnostic when passing key path as an argument to non-key path parameter
Produce a tailored diagnostic that omits a fully unresolved key path
type (`KeyPath<_, _>`) when key path without an explicit root type is
passed as an argument to non-keypath parameter type (i.e. `Int`).
2023-11-08 19:54:32 -08:00
Pavel Yaskevich
4f5123eaf5 [CSDiagnostics] Make contextual key path type mismatch diagnostic consistent 2023-11-08 19:54:31 -08:00
Pavel Yaskevich
8b4985054a [CSDiagnostics] Differentiate between key path type and value issues
Make sure that contextual mismatch uses a correct locator when
the issue is with key path value type instead of the key path
type.
2023-11-01 09:15:14 -07:00
Kavon Farvardin
5e6c23e989 [Sema] diagnostic for conditional Copyable
We were allowing the catch-all "X is not compatible with generics yet"
diagnostic to be emitted when there's a failure to satisfy a requirement
for a conditional Copyable conformance. Instead, allow the existing
infrastructure to diagnose such failures-to-conform to emit a diagnostic
.

This patch also adds a check to catch future catch-all diagnostics in
asserts builds, since I'd like to phase out that diagnostic when
NoncopyableGenerics is enabled.
2023-10-18 13:52:14 -07:00
Pavel Yaskevich
23701bbb6d Merge pull request #69174 from xedin/rdar-116122902
[CSDiagnostics] Adjust how requirement failures anchor type is computed
2023-10-17 21:08:36 -07:00
Pavel Yaskevich
b3da15e324 [CSDiagnostics] Use simplified locator to find owner type of a requirement failure
Only simplified locator points to the right underlying expression.

Resolves: rdar://116122902
2023-10-13 10:45:50 -07:00
Slava Pestov
e298fb5a04 Merge pull request #69116 from slavapestov/fix-apply-variadic-generic-args
Sema: Fix two bugs with type resolution of generic arguments
2023-10-11 15:00:07 -04:00
Slava Pestov
67308e6894 Sema: Fix crash-on-invalid with incorrect use of variadic generic type
Applying generic arguments to a variadic generic type uses the
PackMatcher to build a mapping between generic parameters and
arguments.

The PackMatcher is symmetric, so there was an unexpected failure
mode that wasn't handled: if the variadic generic type had some
non-pack parameters, but the argument list was a single pack
expansion type, the match would succeed, grouping all of the
generic parameters into a single match.

This is non-sensical, so we need to explicitly check for this
case and diagnose it. This requires a new diagnostic, since
otherwise the general diagnostic we emit for variadic generic
type mismatches doesn't make sense, since it's complaining
about there being too few generic parameters.

Fixes rdar://116713961 / https://github.com/apple/swift/issues/69012.
2023-10-11 10:39:26 -04:00
Hamish Knight
03b6623f72 Merge pull request #69074 from hamishknight/over-extended 2023-10-10 10:25:57 +01:00
Pavel Yaskevich
12630f4c5c Merge pull request #69031 from xedin/rdar-116376651
[ConstraintSystem] A few improvements to key path handling and diagnostics
2023-10-09 23:27:45 -07:00
Hamish Knight
ccd32eb452 NFC: Remove ClangModuleLoader.h include from ExtInfo.h
This was unnecessarily pulling in a whole bunch
of Clang headers when all was needed was a
forward declaration.
2023-10-09 20:29:03 +01:00
Pavel Yaskevich
0a9e960420 [CSDiagnostics] Don't suggest unwrapping a key path root without an explicit type
A key path without an explicit root cannot be unwrapped since the
optionality is inferred from context and we cannot suggest removing
that, adding explicit type is not going to change anything and
`!` and `?` after the leading dot is not supported.
2023-10-06 13:36:35 -07:00
Doug Gregor
4b3d2f47d9 [Typed throws] Handle function conversions involving different thrown errors
Teach the constraint solver about the subtyping rule that permits
converting one function type to another when the effective thrown error
type of one is a subtype of the effective thrown error type of the
other, using `any Error` for untyped throws and `Never` for
non-throwing.

With minor other fixes, this allows us to use typed throws for generic
functions that carry a typed error from their arguments through to
themselves, which is in effect a typed `rethrows`:

```swift
func mapArray<T, U, E: Error>(_ array: [T], body: (T) throws(E) -> U)
throws(E) -> [U] {
  var resultArray: [U] = .init()
  for value in array {
    resultArray.append(try body(value))
  }
  return resultArray
}
```
2023-10-05 11:55:05 -07:00
swift-ci
c1df6db3f0 Merge pull request #68881 from apple/prefix-smashing
Diagnose possible enum common-prefix mistakes
2023-10-02 21:48:14 -07:00
Becca Royal-Gordon
78127ce5ee Diagnose possible enum common-prefix mistakes
Clang Importer strips prefixes from enum and option set case names. The logic to do this computes a common prefix from the type name and all non-deprecated case names (to oversimplify), which means that adding, removing, or changing one case can change the prefix that is removed from *all* cases. This typically causes the prefix to become shorter, meaning that additional words are prepended to each existing case name.

Existing diagnostics make it look like the case has disappeared, when in fact it still exists under a different name. A little more information may help developers to figure out what happened.

Add a tailored diagnostic for this scenario which kicks in when (a) a missing member is diagnosed, (b) the base is an imported enum or option set’s metatype, and (c) an enum case or static property exists which has the name we attempted to look up as a suffix.

Fixes rdar://116251319.
2023-10-02 16:50:54 -07:00
Doug Gregor
54589e12f5 [Typed throws] Type-check thrown expressions against the thrown error type
Rather than always type-check the expression in a `throw` statement for
conformance to `Error`, check that it converts to the thrown error type.
2023-09-29 10:51:54 -07:00
Kavon Farvardin
68ae729584 _Copyable as a Requirement Against the Machine
An initial implementation of a rework in how
we prevent noncopyable types from being
substituted in places they are not permitted.

Instead of generating a constraint for every
generic parameter in the solver, we produce
real Copyable conformance requirements. This
is much better for our longer-term goal of
supporting `~Copyable` in more places.
2023-09-19 16:35:17 -07:00
Allan Shortlidge
01ecd81950 AST: Introduce conveniences for inheritance clause source locations.
Replace the `front()` and `back()` accessors on `InheritedTypes` with dedicated
functions for accessing the start and end source locations of the inheritance
clause. NFC.
2023-09-06 10:41:57 -07:00
Allan Shortlidge
0dd8f4c492 AST: Introduce abstraction for extension/type decl inheritance clauses.
Wrap the `InheritedEntry` array available on both `ExtensionDecl` and
`TypeDecl` in a new `InheritedTypes` class. This class will provide shared
conveniences for working with inherited type clauses. NFC.
2023-09-06 10:41:57 -07:00
Luciano Almeida
9c3f2aa2c1 Merge pull request #68301 from LucianoPAlmeida/coerce-function 2023-09-06 06:57:52 -03:00
Luciano Almeida
0300139257 [Sema][Diagnostics] Improving diagnostics for function type coerce failure 2023-09-03 15:18:04 -03:00
Hamish Knight
c0ae9b950b Move out-of-place ThenStmt checking to constraint generation 2023-09-01 14:32:15 +01:00
Hamish Knight
6ee44f09b4 Introduce then statements
These allow multi-statement `if`/`switch` expression
branches that can produce a value at the end by
saying `then <expr>`. This is gated behind
`-enable-experimental-feature ThenStatements`
pending evolution discussion.
2023-09-01 14:32:14 +01:00
Slava Pestov
9ebb5f2e03 AST: Rename VarDecl::getType() to VarDecl::getTypeInContext()
This is a futile attempt to discourage future use of getType() by
giving it a "scary" name.

We want people to use getInterfaceType() like with the other decl kinds.
2023-08-04 14:19:25 -04:00
Hamish Knight
2210ea6856 [CS] Avoid emitting invalid binding diagnostic in more cases
Also avoid emitting the diagnostic for an out of
place binding pattern if there's an existing fix
on a parent call expression, as that's the real
source of the failure.

rdar://113025351
2023-07-28 11:22:27 +01:00
Becca Royal-Gordon
fe6753485f [NFC] Adopt new diagnostic features across Sema 2023-07-20 15:23:47 -07:00
Becca Royal-Gordon
1b81c3de7b [NFC] Adopt ValueDecl * for decl_declared_here 2023-07-19 13:08:12 -07:00
Becca Royal-Gordon
fc2c66f674 [NFC] Adopt %kind for Sema ValueDecl diagnostics 2023-07-19 13:08:11 -07:00
Sophia Poirier
fa41015ae4 [ConstraintSystem] implement implicit pack materialization for abstract tuples instead of explicit '.element' 2023-07-14 10:32:38 -07:00
Egor Zhdan
6e7fb3263b [cxx-interop] Tweak C++ type semantics detection
`CxxRecordSemanticsKind::ExplicitlyUnsafe` and `CxxRecordSemanticsKind::UnsafePointerMember` were never directly used, and those do not indicate semantics: they indicate safety of the type when used from Swift, which should be handled by another request `IsSafeUseOfCxxDecl` instead of `CxxRecordSemantics`.

Having `ExplicitlyUnsafe` and `UnsafePointerMember` as semantics indicators was problematic, for instance, for types that are move-only and store a pointer at the same time. Swift allowed the usage of these types (under the rules for `UnsafePointerMember` types) when move-only types are disabled, and did not apply the move-only attribute on such types when move-only types are enabled.

rdar://110644300
2023-07-04 20:12:06 +01:00
Pavel Yaskevich
c50263c730 Merge pull request #66971 from xedin/fix-specialization-issues
[ConstraintSystem] Fix a couple of issues related to generic specialization
2023-06-29 12:25:11 -07:00
Erik Eckstein
6b1697eb06 use new llvm::Optional APIs to fix deprecation warnings 2023-06-28 14:28:38 +02:00
Pavel Yaskevich
be4df5afd2 [CSDiagnostics] Diagnose attempts to specialize with invalid number of generic arguments
```swift
struct Test<T, U> {}
_ = Test<Int>() // error
```
2023-06-27 17:24:39 -07:00
Pavel Yaskevich
40169c74ee [CSDiagnostics] Add a diagnostic for an attempt to specialize a concrete type
Diagnose attempts to specialize a concrete type or its alias:

```swift
struct Test {}
typealias X = Test

_ = X<Int>() // error
```
2023-06-27 17:24:38 -07:00
Evan Wilde
250082df25 [NFC] Reformat all the LLVMs
Reformatting everything now that we have `llvm` namespaces. I've
separated this from the main commit to help manage merge-conflicts and
for making it a bit easier to read the mega-patch.
2023-06-27 09:03:52 -07:00
Evan Wilde
f3ff561c6f [NFC] add llvm namespace to Optional and None
This is phase-1 of switching from llvm::Optional to std::optional in the
next rebranch. llvm::Optional was removed from upstream LLVM, so we need
to migrate off rather soon. On Darwin, std::optional, and llvm::Optional
have the same layout, so we don't need to be as concerned about ABI
beyond the name mangling. `llvm::Optional` is only returned from one
function in
```
getStandardTypeSubst(StringRef TypeName,
                     bool allowConcurrencyManglings);
```
It's the return value, so it should not impact the mangling of the
function, and the layout is the same as `std::optional`, so it should be
mostly okay. This function doesn't appear to have users, and the ABI was
already broken 2 years ago for concurrency and no one seemed to notice
so this should be "okay".

I'm doing the migration incrementally so that folks working on main can
cherry-pick back to the release/5.9 branch. Once 5.9 is done and locked
away, then we can go through and finish the replacement. Since `None`
and `Optional` show up in contexts where they are not `llvm::None` and
`llvm::Optional`, I'm preparing the work now by going through and
removing the namespace unwrapping and making the `llvm` namespace
explicit. This should make it fairly mechanical to go through and
replace llvm::Optional with std::optional, and llvm::None with
std::nullopt. It's also a change that can be brought onto the
release/5.9 with minimal impact. This should be an NFC change.
2023-06-27 09:03:52 -07:00
swift-ci
44ab03a90b Merge pull request #66871 from kavon/generics-error-noncopyable
Be more specific than "noncopyable type T can't be used with generics yet"
2023-06-26 18:32:26 -07:00
Kavon Farvardin
8072eef926 describe noncopyables in parameter packs 2023-06-26 16:55:06 -07:00