Commit Graph

73 Commits

Author SHA1 Message Date
Pavel Yaskevich
9bd603bfc9 [Diagnostics] Apply "unhandled throw" diagnostic for for-in loop in closures
Extract diagnostic into a method and use it while type-checking
`for-in` in top-level code and in closures.
2021-12-03 10:56:32 -08:00
Pavel Yaskevich
248316536b [CSClosure] Warn about defer being the last element in the closure body 2021-12-03 10:56:07 -08:00
Pavel Yaskevich
0e6e058e7c [TypeChecker] Fix constraint solver to respect LeaveClosureBodyUnchecked flag 2021-12-03 10:54:07 -08:00
Pavel Yaskevich
46ff410a23 [ConstraintSystem] Warn about discarded expressions found in multi-statement closures 2021-12-03 10:53:50 -08:00
Pavel Yaskevich
bc54bc6bb7 Revert "[TypeChecker] SE-0326: Enable multi-statement closure inference by default" 2021-11-29 17:26:08 -08:00
Pavel Yaskevich
7b0f68c05f [Diagnostics] Apply "unhandled throw" diagnostic for for-in loop in closures
Extract diagnostic into a method and use it while type-checking
`for-in` in top-level code and in closures.
2021-11-15 16:42:05 -08:00
Pavel Yaskevich
8d1aeacc06 [CSClosure] Warn about defer being the last element in the closure body 2021-11-15 16:42:05 -08:00
Pavel Yaskevich
83033198c3 [TypeChecker] Fix constraint solver to respect LeaveClosureBodyUnchecked flag 2021-11-15 16:42:05 -08:00
Pavel Yaskevich
3927f56dbd [ConstraintSystem] Warn about discarded expressions found in multi-statement closures 2021-11-15 16:42:04 -08:00
Doug Gregor
45824befd2 Infer 'isolated' closure parameters from context.
When a closure is provided with a contextual type that has isolated
parameters, infer that the corresponding closure parameter is "isolated".

Fixes rdar://83732479.
2021-10-20 21:51:42 -07:00
Pavel Yaskevich
b865a7a451 [CSClosure] NFC: Remove outdated FIXME about SolutionApplicationTarget 2021-10-11 10:28:34 -07:00
Pavel Yaskevich
87f41a9b21 [CSClosure] Check multi-statement closure attrs only in if inference is enabled 2021-10-08 12:12:44 -07:00
Pavel Yaskevich
18b23aa50f [CSClosure] NFC: Add size to a SmallVector in ClosureConstraintGenerator::visitIfStmt 2021-10-08 10:08:04 -07:00
Pavel Yaskevich
b8cfaf778e [CSClosure] Make sure that partially resolved params/result gets their type variables referenced
It's possible to declare explicit parameter/result type without
providing generic arguments, in such situation they'd be "opened"
as type variables and inferred from the contextual type or the body.

Type variable collector needs to account for that and include
such inner type variables into scope of an isolated closure.
2021-10-08 10:08:04 -07:00
Pavel Yaskevich
e5db01b77d [CSClosure] Make sure that case body variables get types
Solution application needs to set interface types for variables
declared in a case statement, otherwise this would lead to crashes
in SILGen.
2021-10-08 10:08:04 -07:00
Pavel Yaskevich
b7b492ffd4 [CSClosure] Check parameters after solution is applied to the body of a closure 2021-10-08 10:08:03 -07:00
Pavel Yaskevich
50af68dca6 [CSClosure] Handle pattern and sequence of for-in loop together
Let `visitForEachPattern` handle both pattern and the sequence
expression associated with `for-in` loop because types in this
situation flow in both directions:

- From pattern to sequence, informing its element type e.g.
  `for i: Int8 in 0 ..< 8`

- From sequence to pattern, when pattern has no type information.
2021-10-08 10:08:03 -07:00
Pavel Yaskevich
29ec113d59 [CSClosures] Model return statements as solution application targets
Each return statement gets a contextual type from outer context,
so the best model for that is via solution application target.
2021-10-08 10:08:03 -07:00
Pavel Yaskevich
1dd76d83c6 [CSClosure] Avoid function conversion when closure result type is optional Void
In cases like:

```
func test<T>(_: () -> T?) -> [T] {
   ...
}

test {
  if ... {
     return
  }

  ...
}
```

Contextual return type would be first attempted as optional and
then unwrapped if the first attempt did not succeed. To avoid having
to solve the closure twice (which results in an implicit function conversion),
let's add an implicit empty tuple (`()`) to the `return` statement
and allow it be to injected into optional as many times as context
requires.
2021-10-08 10:08:03 -07:00
Pavel Yaskevich
bc14f00fe5 [CSClosure] Link closure conjunction with referenced outer parameters
If one or more outer closure parameters used in the body of an
inner closure have not been resolved when conjunction is created,
let's reference them in the conjunction constraint to avoid
disconnecting conjunction from its context.
2021-10-08 10:08:03 -07:00
Pavel Yaskevich
dfe6c3467f [CSClosure] Avoid creating conjunctions for empty bodies 2021-10-08 10:08:03 -07:00
Pavel Yaskevich
67a721485f [ConstraintSystem] Compute variables referenced by conjunction elements incrementally
Attempting to pre-compute a set of referenced type variables
upfront is incorrect because parameter(s) and/or result type
could be bound before conjunction is attempted. Let's compute
a set of referenced variables before each element gets attempted.
2021-10-08 10:08:03 -07:00
Pavel Yaskevich
fed705ddce [CSClosure] Account for partially resolved inner parameter types
It is possible that contextual type of a parameter
has been assigned to an anonymous of named argument
early, to facilitate closure type checking. Such a
type can have type variables inside e.g.

```swift
func test<T>(_: (UnsafePointer<T>) -> Void) {}

test { ptr in
  ...
}
```

Type variable representing `ptr` in the body of
this closure would be bound to `UnsafePointer<$T>`
in this case, where `$T` is a type variable for a
generic parameter `T`.
2021-10-08 10:08:03 -07:00
Pavel Yaskevich
8867a8d407 [CSApply] Delay solution application to multi-statement closure bodies
Let's delay solution application to multi-statement closure bodies,
because declarations found in the body of such closures may depend
on its `ExtInfo` flags e.g. no-escape is set based on parameter if
closure is used in a call.
2021-10-08 10:08:03 -07:00
Pavel Yaskevich
598b85006d [CSClosure] Allow all declarations to use typeCheckDecl
This is necessary to ensure that all of the information is
properly materialized, and access/availability and other
required checks are performed.
2021-10-08 10:08:03 -07:00
Pavel Yaskevich
fba80888ad [CSClosure] Mark conjunctions representing closure body as isolated
Bodies of multi-statement closures should be handled in isolation
because they don't dependent on anything expect parameter/result
types from outer context.
2021-10-08 10:08:03 -07:00
Pavel Yaskevich
ff4aca308d [ConstraintSystem/Closures] Ignore all unsupported declarations during constraint generation
Everything besides `PatternBindingDecl` is handled at solution application time.
2021-10-08 10:08:03 -07:00
Pavel Yaskevich
c68a74a0c6 [ConstraintSystem/Closures] Allow return without result expression 2021-10-08 10:08:02 -07:00
Pavel Yaskevich
dea609f78c [ConstraintSystem/Closures] Add support for do-catch statement 2021-10-08 10:08:02 -07:00
Pavel Yaskevich
1324d1fe3f [ConstraintSystem/Closures] Add support for switch statement 2021-10-08 10:08:02 -07:00
Pavel Yaskevich
0247652239 [ConstraintSystem/Closures] Add support for case statement 2021-10-08 10:08:02 -07:00
Pavel Yaskevich
b1d6d3a92e [ConstraintSystem/Closures] Add support for for-in statement 2021-10-08 10:08:02 -07:00
Pavel Yaskevich
8929d7e75e [ConstraintSystem/Closures] Add support for throw statement 2021-10-08 10:08:02 -07:00
Pavel Yaskevich
305437c475 [ConstraintSystem/Closures] Add support for #assert statement 2021-10-08 10:08:02 -07:00
Pavel Yaskevich
32b257bb0e [ConstraintSystem/Closures] Add support for continue statement 2021-10-08 10:08:02 -07:00
Pavel Yaskevich
f6398d6687 [ConstraintSystem/Closures] Add support for break statement 2021-10-08 10:08:02 -07:00
Pavel Yaskevich
880d862faf [ConstraintSystem/Closures] Add support for defer statement 2021-10-08 10:08:02 -07:00
Pavel Yaskevich
00e98ff0ce [ConstraintSystem/Closures] Add support for fallthrough statement 2021-10-08 10:08:02 -07:00
Pavel Yaskevich
9d89ae0773 [ConstraintSystem] Let closure body element simplification take advantage of contextual information 2021-10-08 10:08:02 -07:00
Pavel Yaskevich
55bae6150d [Constraint] Allow closure body elements to carry contextual information 2021-10-08 10:08:02 -07:00
Pavel Yaskevich
d74cb79ea3 [ConstraintSystem/Closures] Add support for repeat .. while statement 2021-10-08 10:08:02 -07:00
Pavel Yaskevich
34538ef037 [ConstraintSystem/Closures] Add support for do statement 2021-10-08 10:08:02 -07:00
Pavel Yaskevich
f999fa24e8 [ConstraintSystem/Closures] Add support for while statement 2021-10-08 10:08:02 -07:00
Pavel Yaskevich
391b0c6f31 [ConstraintSystem/Closures] Add support for guard statement 2021-10-08 10:08:02 -07:00
Pavel Yaskevich
cedb345878 [CSClosure] Further simplify conjunction creation for ASTNode elements 2021-10-08 10:08:02 -07:00
Pavel Yaskevich
a37b546b5e [CSClosure] Mark empty brace statements as non-viable elements
Such statements do not require any inference so could be safely
omitted from conjunctions.
2021-10-08 10:08:01 -07:00
Pavel Yaskevich
d81490516a [CSClosure] NFC: Simplify creation of non-isolated conjunctions for a set of ASTNodes 2021-10-08 10:08:01 -07:00
Pavel Yaskevich
a2d4ae3ba8 [ConstraintSystem/Closures] Add support for if statement 2021-10-08 10:08:01 -07:00
Pavel Yaskevich
2b7602e134 [CSClosure] Implement constraint generation for closure body elements 2021-10-08 10:08:01 -07:00
Pavel Yaskevich
91eecdb6d0 [CSClosure] Initial support for multi-statement closures
Implement constraint generation for brace statements and
refactor closure contraint generation to accept multi-statement
closures.
2021-10-08 10:08:01 -07:00