Commit Graph

113 Commits

Author SHA1 Message Date
Hamish Knight
61a4148925 [CS] Generalize implied result handling
Track the implied result exprs in the constraint
system, and allow arbitrary propagation of
implied results down if/switch expression
branches. This is required for allowing implied
results in non-single-expression closures.
2024-02-07 18:14:22 +00:00
Hamish Knight
16cfca4186 [ASTWalker] NFC: Rename SkipChildren -> SkipNode
This better describes what the action currently
does, and allows us to re-introduce `SkipChildren`
with the correct behavior.
2024-02-05 15:27:25 +00:00
Hamish Knight
2e20aa5195 Merge pull request #71285 from hamishknight/fix-loc
[CS] Fix locator for `if` expressions
2024-02-01 09:39:43 +00:00
Hamish Knight
956d6d23f2 Merge pull request #71272 from hamishknight/cleanup-return
[CS] Unify ReturnStmt handling
2024-02-01 01:25:08 +00:00
Hamish Knight
185a7d5103 [CS] Fix locator for if expressions
Currently we don't insert an intermediate
`SyntacticElement` locator element for the `if`
statement in a SingleValueStmtExpr, which leads
to an assertion failure when attempting to simplify
a following `TernaryBranch` element.

Move the insertion of the `SyntacticElement` locator
element for the statement into the
SingleValueStmtExpr constraint generation function,
and remove it from the statement visitors themselves.

For the other cases, the `SyntacticElement` was
already being inserted for children of a BraceStmt,
so we were actually previously ending up with
duplicated elements in a couple of places.
2024-01-31 21:37:56 +00:00
Hamish Knight
deaf2dd372 [CS] Unify ReturnStmt handling
Unify the implementation between single-expression
and multi-statement closures. Because we're now
storing a contextual type for single expression
closure returns, update a code completion test to
bring its behavior inline with the multi-statement
case.
2024-01-31 20:26:20 +00:00
Hamish Knight
d9fd4c75b1 [CS] Remove isInputExpression parameter
This wasn't consistently used, and consequently
could result in some expressions getting their
parents invalidated. Instead, replace it with a
query to make sure we don't try and add an
expression we've already computed the parent info
for.
2024-01-31 20:26:20 +00:00
Hamish Knight
8773521d86 [CS] Don't walk into local decls in TypeVariableRefFinder
This could result in us incorrectly walking into
local functions and picking up type variables that
weren't being referenced.
2024-01-31 15:34:34 +00:00
Hamish Knight
0a4c029cfc [AST] Introduce UnreachableExpr
This models the conversion from an uninhabited
value to any type, and allows us to get rid of
a couple of places where we'd attempt to drop
the return statement instead.
2024-01-30 14:08:54 +00:00
Hamish Knight
9b64990d24 [AST] Remove the "single expression body" bit
Remove this bit from function decls and closures.
Instead, for closures, infer it from the presence
of a single return or single expression AST node
in the body, which ought to be equivalent, and
automatically takes result builders into
consideration. We can also completely drop this
query from AbstractFunctionDecl, replacing it
instead with a bit on ReturnStmt.
2024-01-30 14:08:54 +00:00
Hamish Knight
3a8e31d7f6 Merge pull request #71094 from hamishknight/return-tweaks
A couple of ReturnStmt tweaks
2024-01-24 10:07:27 +00:00
Hamish Knight
c97d80b1c3 [AST] NFC: Add convenience constructors for ReturnStmt
Add `ReturnStmt::createParsed` and `createImplict`.
2024-01-23 19:30:18 +00:00
Alex Hoppen
dd5486cda8 [Sema] Remove ConjunctionElement::mightContainCodeCompletionToken
Isn’t used anymore (I think it stopped being used after the new result builder transform was enabled).
2024-01-22 19:52:33 -08:00
Hamish Knight
990d15567d [AST] Split up PatternBindingDecl::setPattern
Most clients only want to set one of the two
parameters, split it into `setPattern` and
`setInitContext` (the latter of which now
handles calling `setBinding`).
2024-01-17 16:02:33 +00:00
Hamish Knight
848574080a [CS] Use correct DeclContext when solving initializations
Use the PatternBindingInitializer context if we
have one. This also uncovered a parser issue where
we would mistakenly create a
PatternBindingInitializer in top-level code after
parsing the initializers.
2024-01-17 16:02:33 +00:00
Doug Gregor
8ad137fb51 [Typed throws] Infer thrown error type for do..catch blocks within closures.
Start classifying all potential throw sites within a constraint
system and associate them with the nearest enclosing catch node. Then,
determine the thrown error type for a given catch node by taking the
union of the thrown errors at each potential throw site. Use this to
compute the error type thrown from the body of a `do..catch` block
within a closure.

This behavior is limited to the upcoming feature `FullTypedThrows`.
2023-12-13 14:59:23 -08:00
Doug Gregor
e1be9c312b Eliminate the DeclContext from ExplicitCaughtTypeRequest
Correctly determining the DeclContext needed for an
ExplicitCaughtTypeRequest is tricky for a number of callers, and
mistakes here can easily lead to redundant computation of the caught
type, redundant diagnostics, etc.

Instead, put a `DeclContext` into `DoCatchStmt`, because that's the
only catch node that needs a `DeclContext` but does not have one.
2023-12-13 11:42:56 -08:00
Doug Gregor
05a4a822ba Tighten CatchNode from an AbstractClosureExpr to a ClosureExpr.
Only real closures can have thrown error types, so we can use the
tighter bound for closures and simplify various CatchNode operations.
2023-12-12 22:30:18 -08:00
Doug Gregor
010a41243e Fix minor regressions from refactoring of caught error types 2023-12-12 15:39:56 -08:00
Doug Gregor
3369e3349f [Typed throws] Handle explicitly-specified throws on do..catch in closures
Teach the constraint system to use the same primitives as elsewhere to
determine the type context for a `throw` statement and the caught
error type within a `do..catch` statement. This makes
explicitly-specified `throws` work on `do..catch` that occurs in
closures.

Cleans up some redundant computations for caught error types.
2023-12-12 00:06:17 -08:00
Doug Gregor
91df336a4d [Typed throws] Unify ThrownTypeRequest and DoCatchExplicitThrownTypeRequest
These two requests are effectively doing the same thing to two
different cases within CatchNode. Unify the requests into a single
request, ExplicitCaughtTypeRequest, which operates on a CatchNode.

This also moves the logic for closures with explicitly-specified throws
clauses into the same request, taking it out of the constraint system.
2023-12-12 00:06:17 -08:00
Hamish Knight
f4e09c5531 [AST] Tighten up invariants around IfStmt
The 'then' statement must be a BraceStmt, and
the 'else' must either be a BraceStmt or an IfStmt.
2023-12-04 11:09:01 +00:00
Alex Hoppen
b603a5e9a0 [Sema] Remove LeaveClosureBodiesUnchecked
This shouldn’t be needed anymore since we migrated code completion to be solver-based.

rdar://91403086
2023-11-23 11:41:29 -08:00
Hamish Knight
5cbdfb8233 [CS] Consolidate conjunction join injection 2023-10-06 11:17:48 +01:00
Hamish Knight
33f94bc874 Introduce do expressions 2023-10-06 11:17:48 +01:00
Pavel Yaskevich
49a63fcc7d Merge pull request #68787 from xedin/separate-solving-for-for-loop-condition
[ConstraintSystem] Solve `where` clauses of for-in loops separately
2023-10-02 09:22:53 -07:00
Pavel Yaskevich
0b4a9f5484 [CSSyntacticElement] NFC: Update comments related to for-in where clause checking 2023-09-29 16:03:21 -07:00
Pavel Yaskevich
72caaa8cfe [ConstraintSystem] Solve where clauses of for-in loops separately
Doing so fits better into conjunction model which leads to more
granular control over what variables are brought into scope during
`where` clause expression checking.

These changes also remove "one-way bind" flag from "for-in" statement
target.
2023-09-29 13:28:46 -07:00
Doug Gregor
7f82b2a9aa [Typed throws] Enable checking of thrown types on closures.
Enable typed throws on explicit closures, either due to contextual
type information or due to explicit specification.
2023-09-29 10:51:54 -07:00
Pavel Yaskevich
fdd67a6bde [Sema] NFC: Remove obsolete isInResultBuilderContext method
Type-checking of result builder transformed closure is enabled
by default now, so this method is no longer used.
2023-09-18 17:59:30 +01:00
Hamish Knight
0760290387 [CS] Connect conjunctions with ReturnStmts to return type
Augment the TypeVarRefCollector such that it
picks up any type variables present in the result
type for a closure DeclContext when visiting a
ReturnStmt. This ensures we correctly handle
if/switch expressions that contain `return`
statements.

rdar://114402042
2023-09-05 21:25:05 +01:00
Hamish Knight
5851c59113 [CS] NFC: Introduce a convenience createConjunction 2023-09-05 21:25:04 +01:00
Hamish Knight
cc507fc74b [CS] NFC: Rename VarRefCollector -> TypeVarRefCollector 2023-09-05 21:25:03 +01: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
Hamish Knight
c48b77f486 [CodeComplete] Don't skip single-expression AssignExpr bodies of an if/switch expr
Fix an issue uncovered by the stress tester where
the brace element skipping logic could still attempt
to skip a single-expression body of an if/switch
expr.
2023-08-01 15:21:30 +01:00
Hamish Knight
1dd86fccdb [CodeComplete] More efficient skipping for completions in if/switch exprs
Skip type-checking multi-statement branches if the
completion is in a single-expression branch, and
skip type-checking the expression as a whole if
the completion is in a multi-statement branch.
2023-08-01 15:21:29 +01:00
Hamish Knight
a64ba23d7b [CS] NFC: Store ContextualTypeInfo in SyntacticElementTarget
Move the contextual type locator onto
ContextualTypeInfo, and consolidate the separate
fields in SyntacticElementTarget into storing a
ContextualTypeInfo. This then lets us plumb down
the locator for the branch contextual type of an
if/switch expression from the initial constraint
generation, rather than introducing it later. This
should be NFC.
2023-07-26 16:46:54 +01:00
Hamish Knight
067583a1e3 [CS] Merge VarRefCollector & UnresolvedVarCollector
These now do basically the same thing, merge their
implementations.
2023-07-17 17:32:47 +01:00
Hamish Knight
09bb346b03 [CS] Walk UnresolvedDeclRefExprs in UnresolvedVarCollector
This matches what we do in VarRefCollector, and is
needed because we currently delay the pre-checking
of patterns due to the fact that we don't resolve
them until CSGen. We ought to consider changing
this, but until then, adjust the logic here to
ensure we properly connect an ExprPattern that
references an outer var with any type variables
it may involve.

rdar://112264204
2023-07-17 17:32:46 +01:00
Alex Hoppen
0c413109d3 [CodeComplete] Skip conjunction elements unrelated to the code completion token 2023-07-07 19:50:46 +02:00
Pavel Yaskevich
77bd8ad746 [CSGen/SyntaticElement] Remove logic that connects tap expressions to closures
Both single- and multi-statement closures now use variable reference
collector to identify variables used in the interpolation body, which
means that it's not longer necessary to connect to the closure explicitly
(if interpolation is contained in one).
2023-07-07 19:50:46 +02:00
Pavel Yaskevich
ff0c942aa9 [CSSyntacticElement] Fix solution applicator to identify context correctly
`TapExpr` do not form their own declaration context which means
that using `context.getAsDeclContext` is incorrect for them.
2023-07-07 19:50:46 +02:00
Pavel Yaskevich
60fa909387 [CSSyntacticElement] Make sure that body of tap is solved in isolation
It doesn't need access to anything besides external declarations
which could be brought into the scope by context analyzer.
2023-07-07 19:50:46 +02:00
Pavel Yaskevich
e337a3947d [ConstraintSystem] Implement tap expression checking in the solver
Generate a conjunction for each tap expression body as soon as it
gets a contextual type instead of separate post-factum type-checking
via `typeCheckTapBody`.
2023-07-07 19:50:46 +02:00
Pavel Yaskevich
2ba70dff88 [CSClosure] Add TapExpr as a member of SyntacticElementContext 2023-07-07 19:50:46 +02:00
Erik Eckstein
6b1697eb06 use new llvm::Optional APIs to fix deprecation warnings 2023-06-28 14:28:38 +02: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
Hamish Knight
f18293bf7c [CS] Walk ExprPattern conjunction elements when finding type variables
Apply the same logic that we apply to other
conjunction elements, to make sure that e.g
property wrapper projected values work correctly.

rdar://110649179
2023-06-12 18:27:26 +01:00
Hamish Knight
0695917bbd [CS] Don't form conversion between switch subject and pattern
This is wrong because there's nowhere to put any
conversion that is introduced, meaning that we'll
likely crash in SILGen. Change the constraint to
equality, which matches what we do outside of the
constraint system.

rdar://107709341
2023-06-07 00:35:01 +01:00