Commit Graph

561 Commits

Author SHA1 Message Date
Holly Borla
84eaeb766f [Concurrency] Diagnose non-Sendable 'self' arguments crossing actor isolation
boundaries in member reference expressions.
2023-08-22 21:28:33 -07:00
Alex Hoppen
baa20aec52 Merge pull request #67909 from nishithshah2211/imperative-fixits
[Diagnostics] Use imperative msg for protocol conformance & switch-case fixits
2023-08-21 14:40:21 -07:00
Pavel Yaskevich
b34f9720e4 [CSSimplify] Adjust {Any, Partial}KeyPath bindings right before resolving
A type variable that represents a key path literal cannot be bound
directly to `AnyKeyPath` or `PartialKeyPath`, such types could only
be used for conversions.
It used to be the task of the binding inference to infer `AnyKeyPath`
and `PartiaKeyPath` as a `KeyPath` using previously generated type
variables for a root and value associated with key path literal
(previously stored in the locator).

Recently we switched over to storing key path information in the
constraint system and introduced `resolveKeyPath` method to gain
more control over how key path type variable gets assigned.

Getting information from the constraint system creates a problem
for the inference because "undo" for some bindings would be run
after solver scope has been erased, so instead of modifying bindings
in `inferFromRelational`, let's do that in `resolveKeyPath` right
before the key path type variable gets bound which seems to be a
better place for that logic anyway.

Resolves: rdar://113760727
2023-08-15 13:50:54 -07:00
Nishith Shah
8e2e625543 [Diagnostics] Use imperative msg for protocol conformance & switch-case fixits
This commit changes fixit messages from a question/suggestion to an
imperative message for protocol conformances and switch-case. Addresses
https://github.com/apple/swift/issues/67510.
2023-08-13 22:34:26 -07:00
Pavel Yaskevich
27413f5624 [CSSimplify] Relax isBindable requirements for pack expansion variables
If type variable we are about to bind represents a pack
expansion type, allow the binding to happen regardless of
what the \c type is, because contextual type is just a hint
in this situation and type variable would be bound to its
opened type instead.

Resolves: rdar://112617922
2023-08-04 16:49:20 -07:00
Pavel Yaskevich
5a5edcafc3 [CSSimplify] Extend same-shape detection to account for pack archetypes
`same-shape` mismatch detection logic shouldn't expect that types are
always packs because they could be either invalid (i.e. Void) or pack
archetypes too.

Resolves: rdar://112090069
2023-07-20 22:50:22 -07:00
Hamish Knight
7a137d6756 [CS] Allow ExprPatterns to be type-checked in the solver
Previously we would wait until CSApply, which
would trigger their type-checking in
`coercePatternToType`. This caused a number of
bugs, and hampered solver-based completion, which
does not run CSApply. Instead, form a conjunction
of all the ExprPatterns present, which preserves
some of the previous isolation behavior (though
does not provide complete isolation).

We can then modify `coercePatternToType` to accept
a closure, which allows the solver to take over
rewriting the ExprPatterns it has already solved.

This then sets the stage for the complete removal
of `coercePatternToType`, and doing all pattern
type-checking in the solver.
2023-06-07 00:35:01 +01:00
Hamish Knight
c0e013907e [AST] Allocate PlaceholderTypes in the correct arena
We shouldn't be allocating placeholders for type
variables in the permanent arena, and we should be
caching them such that equality works.

To achieve this, we need to introduce a new
"solver allocated" type property. This is required
because we don't want to mark placeholder types
with type variable originators as themselves having
type variables, as it's not part of their structural
type. Also update ErrorType to use this bit, though
I don't believe we currently create ErrorTypes
with type variable originators.
2023-05-06 20:42:02 +01:00
Hamish Knight
6ae5f1c535 [test] Update test for placeholder revert 2023-05-05 19:43:10 +01:00
Hamish Knight
b07f7b336e [CS] Improve handling of holes for Named/AnyPatterns
Rather than eagerly binding them to holes if the
sequence element type ends up being Any, let's
record the CollectionElementContextualMismatch fix,
and then if the patterns end up becoming holes,
skip penalizing them if we know the fix was
recorded. This avoids prematurely turning type
variables for ExprPatterns into holes, which
should be able to get better bindings from the
expression provided. Also this means we'll apply
the logic to non-Any sequence types, which
previously we would give a confusing diagnostic
to.
2023-05-04 14:53:58 +01:00
Pavel Yaskevich
366213d90c Merge pull request #65479 from xedin/rdar-108534555
[CSDiagnostics] Teach `diagnoseConflictingGenericArguments` about holes
2023-04-28 17:18:25 -07:00
Pavel Yaskevich
0b7aeed4b8 [CSDiagnostics] Teach diagnoseConflictingGenericArguments about holes
Only fully resolved substitutions are eligible to be considered
as conflicting, if holes are involved in any position that automatically
disqualifies a generic parameter.

Resolves: rdar://108534555
Resolves: https://github.com/apple/swift/issues/63450
2023-04-27 14:58:26 -07:00
Slava Pestov
dd0531e890 Merge pull request #65400 from slavapestov/generic-param-shadowing
Sema: Ban shadowing generic parameters from outer scopes
2023-04-26 12:08:51 -04:00
Slava Pestov
290701cb4d Sema: Ban shadowing generic parameters from outer scopes
Code like that is usually indicative of programmer error, and does not
round-trip through module interface files since there is no source
syntax to refer to an outer generic parameter.

For source compatibility this is a warning, but becomes an error with
-swift-version 6.

Fixes rdar://problem/108385980 and https://github.com/apple/swift/issues/62767.
2023-04-25 17:41:23 -04:00
Pavel Yaskevich
20d7642ca4 [CSGen] Allow expression pattern types to be holes 2023-04-24 10:02:00 -07:00
Pavel Yaskevich
b081bdbf57 [CSGen] Allow is pattern types to be holes 2023-04-21 12:14:41 -07:00
Pavel Yaskevich
976c0b17c3 [CSSyntaticElement] Canonicalize type before collecting "in scope" variables
Follow-up to https://github.com/apple/swift/pull/65048

`getDesugaredType` unwraps sugar types that appear in sequence,
to remove sugar from nested positions we need to get a canonical type.

Thanks to @slavapestov for pointing it out.
2023-04-11 11:53:31 -07:00
Pavel Yaskevich
ca14ab7157 [CSSyntaticElement] Desugar types before collecting "in scope" type variables
Generic type aliases, unless desugared, could bring unrelated type variables
into the scope i.e. `TypeAlias<$T, $U>.Context` is actually `_Context<$U>`.
These variables could be inferrable only after the the body the closure is
solved. To avoid that, let's adjust `TypeVariableRefFinder` to desugar types
before collecting referenced type variables.

Resolves: rdar://107835060
2023-04-10 15:33:03 -07:00
Pavel Yaskevich
36c61bdd3e [Tests] NFC: Remove ResultBuilderASTTransform references from tests 2023-03-15 14:29:04 -07:00
Allan Shortlidge
4058ad1421 AST: Improved inferred availability accuracy.
Previously, when creating availability attributes for synthesized declarations
we would identify some set of reference declarations that the synthesized
declaration should be as-available-as. The availability attributes of the
reference declarations would then be merged together to create the attributes
for the synthesized declaration. The problem with this approach is that the
reference declarations themselves may implicitly inherit availability from their
enclosing scopes, so the resulting merged attributes could be incomplete. The
fix is to walk though the enclosing scopes of the reference declarations,
merging the availability attributes found at each level.

Additionally, the merging algorithm that produces inferred availability
attributes failed to deal with conflicts between platform specific and platform
agnostic availability. Now the merging algorithm notices when platform agnostic
availability should take precedence and avoids generating conflicting platform
specific attributes.

Resolves rdar://106575142
2023-03-13 18:27:03 -07:00
Pavel Yaskevich
e450047c6b [ConstraintSystem] Dependent member simplification should be attempted if it has type variable in any position
Current logic attempts simplification of the base type only if it's
a type variable or a dependent member type. That's valid for correct
code but not for invalid code e.g. `(($T) -> Void).Element` is not
going to be simplified even if `$T` is bound, which causes crashes
in diagnostic mode because `matchTypes` is going to re-introduce
constraint with partially resolved dependent member types and by
doing so make it stale forever which trips constraint system state
verification logic.

Resolves: rdar://105149979
2023-02-27 12:41:42 -08:00
Pavel Yaskevich
29725e8209 [Sema/Tests] NFC: Limit a couple of perf tests to macOS
Helps to avoid flakiness on simulators/devices.

Resolves: rdar://105752467
2023-02-22 00:30:39 -08:00
Hamish Knight
a40f1abaff Introduce if/switch expressions
Introduce SingleValueStmtExpr, which allows the
embedding of a statement in an expression context.
This then allows us to parse and type-check `if`
and `switch` statements as expressions, gated
behind the `IfSwitchExpression` experimental
feature for now. In the future,
SingleValueStmtExpr could also be used for e.g
`do` expressions.

For now, only single expression branches are
supported for producing a value from an
`if`/`switch` expression, and each branch is
type-checked independently. A multi-statement
branch may only appear if it ends with a `throw`,
and it may not `break`, `continue`, or `return`.

The placement of `if`/`switch` expressions is also
currently limited by a syntactic use diagnostic.
Currently they're only allowed in bindings,
assignments, throws, and returns. But this could
be lifted in the future if desired.
2023-02-01 15:30:18 +00:00
Kavon Farvardin
3f6a0ccb90 Allow a global-actor to be dropped for some non-async functions.
It's ok to drop the global-actor qualifier `@G` from a function's type if:

- the cast is happening in a context isolated to global-actor `G`
- the function value will not be `@Sendable`
- the function value is not `async`

It's primarily safe to drop the attribute because we're already in the
same isolation domain. So it's OK to simply drop the global-actor
if we prevent the value from later leaving that isolation domain.
This means we no longer need to warn about code like this:

```
@MainActor func doIt(_ x: [Int], _ f: @MainActor (Int) -> ()) {
  x.forEach(f)
// warning: converting function value of type '@MainActor (Int) -> ()' to '(Int) throws -> Void' loses global actor 'MainActor'
}
```

NOTE: this implementation is a bit gross in that the constraint solver
might emit false warnings about casts it introduced that are actually
safe. This is mainly because closure isolation is only fully determined
after constraint solving. See the FIXME's for more details.

resolves rdar://94462333
2023-01-05 17:54:00 -08:00
Pavel Yaskevich
f8bf74b457 Merge pull request #62773 from xedin/rdar-83418797
[CSClosure] Fix handling of non-representivate variables
2023-01-04 09:48:54 -08:00
Pavel Yaskevich
2e25c2fc80 Merge pull request #62791 from xedin/rdar-103739206
[Diagnostics] Relax contextual type presence from assertion to a check
2023-01-04 09:47:19 -08:00
Pavel Yaskevich
eaf0532a04 [Diagnostics] Relax contextual type presence from assertion to a check
In some situations `getContextualType` for a contextual type
locator is going to return then empty type. This happens because
e.g. optional-some patterns and patterns with incorrect type don't
have a contextual type for initialization expression but use
a conversion with contextual locator nevertheless to indicate
the purpose. This doesn't affect non-ambiguity diagnostics
because mismatches carry both `from` and `to` types.

Resolves: rdar://problem/103739206
2023-01-02 21:11:07 -08:00
Pavel Yaskevich
47893695be [CSClosure] Fix handling of non-representivate variables
All of the type variables referenced by a type had to be
handled by `inferVariables` otherwise it would to possible
to bring non-representative variable into scope which in
turn later would get simplied into a variable that doesn't
exist in the active scope and solver would crash.

Resolves: rdar://83418797
2022-12-23 20:08:09 -08:00
Pavel Yaskevich
36a50c7070 [ResultBuilder] Don't run syntactic diagnostics on transformed do statements
Visiting of transformed `do` shouldn't run syntatic diagnostics because
they are going to be performed by `visit` that called `visitDoStmt`.
2022-12-21 10:31:36 -08:00
Pavel Yaskevich
697dfbae96 [TypeChecker] Enable result builder AST transform by default 2022-12-21 10:31:30 -08:00
Pavel Yaskevich
7166f193b5 [ConstraintSystem] Reject property wrapper transform on func parameters in pattern context
The transform is not currently supported on anything but regular calls.
2022-12-09 15:47:57 -05:00
Pavel Yaskevich
7588434007 [CSSimplify] Account for tuple splat when resolving closure parameters
SE-0110 allows tuple slat between function types. The solver needs to
account for that when trying to infer parameter types from context
while resolving a closure in order to propagate types and speed up
type-checking.

Resolves: https://github.com/apple/swift/issues/62390
2022-12-05 00:28:52 -08:00
Pavel Yaskevich
2e0cc04ba1 Merge pull request #62227 from xedin/result-builder-ast-transform-direct-buildBlock
[ResultBuilder] ASTTransform: Don't capture buildBlock into a separat…
2022-11-28 10:18:53 -08:00
Pavel Yaskevich
c17d35dc3e Merge pull request #62146 from xedin/rdar-102085039
[AST] `getParameterAt` should check index before retrieving it
2022-11-28 10:01:51 -08:00
Pavel Yaskevich
8060fa0844 [ResultBuilder] ASTTransform: Don't capture buildBlock into a separate variable
Inject `buildBlock` call into `return`, `buildFinalResult`, and
`build{Optional, Either}` to take advantage of contextual information
and maintain compatibility with current inference behavior.
2022-11-22 11:42:18 -08:00
Pavel Yaskevich
4e3a4ffbe9 Merge pull request #62120 from xedin/rdar-100753270
[CSSyntaxElement] Detect type variables in sequence expressions of `for-in` loops
2022-11-17 09:54:22 -08:00
Pavel Yaskevich
7bc1dc59ab [AST] getParameterAt should check index before retrieving it
`getParameterAt` needs to be more defensive about the input
because caller cannot always perform all the checks upfront.

Resolves: rdar://102085039
2022-11-16 15:34:05 -08:00
Pavel Yaskevich
cab0cb52ab [CSSyntaxElement] Detect type variables in sequence expressions of for-in loops
Invalid syntax could introduce type variables into sequence which
have to be brought into scope otherwise solver is going to crash.

Resolves: rdar://100753270
2022-11-15 11:56:36 -08:00
Holly Borla
0708e0efd4 Revert "Tests: Disable failing SwiftUI validation tests" 2022-11-15 09:10:23 -08:00
Allan Shortlidge
b2421aa463 Tests: Disable failing SwiftUI validation tests until rdar://102298208 is fixed. 2022-11-13 16:56:20 -08:00
Pavel Yaskevich
0a75a07962 [CSSolver] Don't drop bindings with type variables while applying solutions
If a (partial) solution has a type variable it could only be unbound
if `FreeTypeVariableBinding` is set to `Allow`, in all other cases
solution would either have a fully resolved type or a hole.

`applySolution` shouldn't second guess `finalize()` and just apply
a solution as given. This is very important for multi-statement closures
because their elements are solved in isolation and opaque value types
inferred for their result could contain not-yet-resolved type variables
 from outer context in their substitution maps (which it totally legal
under bi-directional inference).
2022-11-10 10:12:38 -08:00
Pavel Yaskevich
8ab37ca449 [Diagnostics] Diagnose type with mismatching generic arguments in ternary expr
Fixes a missing diagnostic related to types with mismatched
generic arguments in one of both ternary branches.

Resolves: rdar://98862079
2022-10-27 15:56:50 -07:00
Anthony Latsis
6637c18954 Address a few stale FIXMEs & replace some commented-out test cases with verifications 2022-10-06 16:39:18 +03:00
Pavel Yaskevich
77f9c01b77 [CSClosure] Don't try to infer types for implicitly wrapped parameters
Update `applyPropertyWrapperToParameter` to set types to projected
and wrapped values and allow `TypeVariableRefFinder` to skip decls
with implicit property wrappers that are not yet resolved.
2022-09-30 12:53:01 -07:00
Anthony Latsis
7778505bc6 Gardening: Migrate test suite to GH issues: validation-test/Sema (2/2) 2022-09-19 23:12:45 +03:00
Anthony Latsis
26791bc21e Gardening: Migrate test suite to GH issues: validation-test/Sema (1/2) 2022-09-19 23:12:12 +03:00
Pavel Yaskevich
c798a7fb72 [CSSimplify] Member ref decays into value witness for makeIterator in for-in
Reference to `makeIterator` in for-in loop context needs to be
treated as a reference to a witness of `Sequence#makeIterator`
requirement, otherwise it could lead to problems with retroactive
conformances.
2022-09-14 11:35:27 -07:00
Pavel Yaskevich
3e791dd169 [CSClosure] Use correct type for implicit wrapper variables
Using `computeWrappedValueType` is incorrect because
that return a type of the wrapped variable and not
the *wrapper* variable (one that starts with `_`).

Resolves: https://github.com/apple/swift/issues/61017
2022-09-12 09:53:24 -07:00
Luciano Almeida
cc11e1938f [test] Add regression test for fn builders 2022-09-06 01:39:30 -03:00
Allan Shortlidge
b5066bbcaf Sema: Check the availability of property wrappers attached to VarDecls in named patterns.
The availability of property wrappers was previously checked for typed patterns but not named patterns.

Resolves rdar://86698223.
2022-08-22 16:51:27 -07:00