Commit Graph

105 Commits

Author SHA1 Message Date
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
Pavel Yaskevich
73dffb3125 [ConstraintSystem] Don't require a type variable per closure body element
Since each of the body elements is already going to have a type associated
with it there is no need to create yet another type variable.
2021-10-08 10:08:01 -07:00
Pavel Yaskevich
eb8eabfba7 [ConstraintSystem] Add a skeleton of ClosureBodyElement constraint 2021-10-08 10:08:00 -07:00
Pavel Yaskevich
18128eeeec [CSClosure] Use rewritten expression when applying solution to a brace statement
It went un-noticed because currently only single-statement closures
using this logic and their bodies are modeled as a single return statement.
2021-08-11 13:44:59 -07:00
Pavel Yaskevich
3e73f88512 [ConstraintSystem] NFC: Use stored result type while generating constraints for closures
Instead of passing/storing result type to constraint generator,
let's retrieve it from the constraint system when needed.
2021-07-14 16:28:26 -07:00
Holly Borla
31f7b1ac75 [ConstraintSystem] Change the type of an unapplied function reference
when it has property wrapper parameters.

The property wrapper type will be replaced with either the wrapped-value
or projected-value type, depending on the argument label/parameter name,
and CSApply will build a thunk to construct the property wrapper and call
the function.
2021-02-25 18:35:13 -08:00
maustinstar
3913c31688 [SR-11711] Single-expression returns for #if declarations within closures 2020-11-03 12:48:05 -05:00
Doug Gregor
6d41524fe6 [SE-0289] Finish renaming source code, tests to "result builders" 2020-10-20 22:18:51 -07:00
Doug Gregor
0d568a93d4 [SE-0289] Update diagnostics & many other strings to "result builders" 2020-10-20 21:44:09 -07:00
Pavel Yaskevich
461eafff54 [ConstraintSystem] NFC: Move ConstraintSystem.h to include/swift/Sema 2020-10-08 10:45:47 -07:00
Pavel Yaskevich
f94be56468 [Sema] Decouple ConstraintSystem and TypeChecker headers 2020-10-06 13:18:49 -07:00
Hamish Knight
a7a8d9fd26 [AST] Remove NewBodyKind default
A bunch of callers were accidentally misusing it.
2020-09-04 13:24:34 -07:00
Rintaro Ishizaki
8425e1cf4f [AST] Rename TypeCheckedAtOnce to TypeCheckedWithSignature
To clarify the meaning
2020-07-24 10:48:51 -07:00
Rintaro Ishizaki
b1eec26653 [AST] Add a enum for type checking state of ClosureExpr's body 2020-07-24 10:46:03 -07:00
Doug Gregor
a4239370c8 [Constraint solver] Wire up "parents" of closure return statements. 2020-06-05 14:02:46 -07:00
Doug Gregor
a115ad9f5c [AST] Record "separately checked" in ClosureExpr.
Reverse the polarity of the "checked in context" bit for ClosureExpr
to "separately checked", which simplifies the AST walker logic (to
"should we walk separately type-checked closure bodies?") and
eliminates single-expression closures as a separate case to consider.
2020-06-05 00:05:11 -07:00
Doug Gregor
1d36224e74 [ClosureExpr] Remove hasAppliedFunctionBuilder.
There are no longer any clients of the "has applied function builder" bit
in ClosureExpr, so remove it.
2020-06-04 23:16:51 -07:00
Doug Gregor
836bc57fe5 [AST Walker] Stop visiting the bodies of closures as expressions.
Single-expression closures have always been traversed differently
from multi-statement closures. The former were traversed as if the
expression was their only child, skipping the BraceStmt and implicit
return, while the later was traversed as a normal BraceStmt.
Unify on the latter treatment, so that traversal

There are a few places where we unintentionally relied on this
expression-as-child behavior. Clean those up to work with arbitrary
closures, which is an overall simplification in the logic.
2020-06-04 23:06:32 -07:00
Doug Gregor
f55e7643fa [Constaint system] Add a predicate for when to check closures in enclosing expr
Introduce a new predicate, shouldTypeCheckInEnclosingExpression(), to
determine when the body of a closure should be checked as part of the
enclosing expression rather than separately, and use it in the various
places where "hasSingleExpressionBody()" was used for that purpose.
2020-06-03 22:48:58 -07:00