Commit Graph

167 Commits

Author SHA1 Message Date
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
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
Kavon Farvardin
3e4bc82aa8 rename _forget to discard; deprecate _forget
SE-390 concluded with choosing the keyword discard rather than forget for
the statement that disables the deinit of a noncopyable type. This commit
adds parsing support for `discard self` and adds a deprecation warning for
`_forget self`.

rdar://108859077
2023-05-08 21:42:19 -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
1691f32d84 [CSGen] Type-check capture list together with closure body
Delay constraint generation for capture list until body of
the associated closure is resolved. This means that we can
unify capture checking with that of regular pattern bindings
for multi-statement closures.
2023-03-17 15:22:38 -07:00
Pavel Yaskevich
93ea9a0c0c [BuilderTransform] NFC: Remove previous result builder implementation 2023-03-16 10:51:46 -07:00
Hamish Knight
ae756d0235 [CS] NFC: Add a couple of parameters to createConjunction
Allow callers to customize the isolation and
referenced vars.
2023-03-14 11:56:01 +00:00
Pavel Yaskevich
c106705883 [CSSolver] NFC: Replace some uses of last() with endsWith() 2023-03-13 11:24:15 -07:00
Hamish Knight
2976edbe20 [CS] Rename SolutionApplicationTarget -> SyntacticElementTarget 2023-03-06 20:54:06 +00:00
Mishal Shah
e256b56545 Merge branch 'main' into rebranch 2023-03-02 18:25:09 -08:00
Ben Barham
59afba5bf9 Manually merge branch 'main' into rebranch 2023-03-01 14:13:50 -08:00
Hamish Knight
f7423b9a82 Merge pull request #63994 from hamishknight/context-switch
Resolves https://github.com/apple/swift/issues/63750
2023-03-01 22:06:37 +00:00
Kavon Farvardin
cc0b668efb Merge pull request #63922 from kavon/standard-issue-neuralyzer
Implement the `forget` statement (as `_forget`)
2023-03-01 11:10:25 -08:00
Hamish Knight
6a7878b90d [CS] Perform limited exhaustiveness checking if we couldn't apply the solution
Currently ExprPatterns are type-checked during
CSApply. As such, they can cause solution
application to fail with a pattern that isn't
well-formed. I'm planning on moving their
type-checking into the solver, but until then,
lets only do limited exhaustiveness checking for
the switch if there was an error.

rdar://105781521
2023-03-01 16:20:13 +00:00
Kavon Farvardin
f41ed5926b implement the forget statement
Currently, this is staged in as `_forget`,
as part of SE-390. It can only be used on
`self` for a move-only type within a consuming
method or accessor. There are other rules, see
Sema for the details.

A `forget self` really just consumes self and
performs memberwise destruction of its data.
Thus, the current expansion of this statement
just reuses what we inject into the end of a
deinit.

Parsing of `forget` is "contextual".
By contextual I mean that we do lookahead to
the next token and see if it's identifier-like.
If so, then we parse it as the `forget` statement.
Otherwise, we parse it as though "forget" is an
identifier as part of some expression.

This way, we won't introduce a source break for
people who wrote code that calls a forget
function.

This should make it seamless to change it from
`_forget` to `forget` in the future.

resolves rdar://105795731
2023-02-28 21:15:17 -08:00
Doug Gregor
200f2340d9 [Macros] Be deliberate about walking macro arguments vs. expansions
Provide ASTWalker with a customization point to specify whether to
check macro arguments (which are type checked but never emitted), the
macro expansion (which is the result of applying the macro and is
actually emitted into the source), or both. Provide answers for the
~115 different ASTWalker visitors throughout the code base.

Fixes rdar://104042945, which concerns checking of effects in
macro arguments---which we shouldn't do.
2023-02-28 17:48:23 -08:00
Guillaume Lessard
ab4177a351 Merge branch 'main' into rebranch
# Conflicts:
#	lib/Sema/CSSyntacticElement.cpp
2023-02-21 15:36:25 -08:00
Pavel Yaskevich
45edfc359b Merge pull request #63768 from xedin/issue-63764
[BuilderTransform] Rework missing `buildWithLimitedAvailability` detection
2023-02-21 14:38:50 -08:00
swift-ci
7c9fd4dc12 Merge remote-tracking branch 'origin/main' into rebranch 2023-02-18 06:58:04 -08:00
Pavel Yaskevich
c4ea02c2d7 [BuilderTransform] Rework missing buildWithAvailability detection
Since all of the branches of an `if` statement are joined together
and hence produce the same type, that type should be used to
check whether `buildWithAvailability` is required but missing
regardless of availability condition kind.

Resolves: https://github.com/apple/swift/issues/63764
2023-02-18 01:04:15 -08:00
Hamish Knight
f7191b35b6 [CS] Add contextual Bool type to switch-case where clause
Seems this was accidentally omitted, which would
crash in CSApply for any non-Bool-convertible type.
2023-02-17 20:58:46 +00:00
Hamish Knight
eac3fb4506 [CS] NFC: Default the allowFreeTypeVariables parameter
All but one client wants to set this to anything
other than `FreeTypeVariableBinding::Disallow`.
2023-02-17 20:58:46 +00:00
swift-ci
b4d199665b Merge remote-tracking branch 'origin/main' into rebranch 2023-02-14 06:54:42 -08:00
Alex Hoppen
395df14960 [CodeCompletion] Skip conjuction elements that are unrelated to the code completion token 2023-02-14 09:40:56 +01:00
swift-ci
4a149bfd3d Merge remote-tracking branch 'origin/main' into rebranch 2023-02-13 15:53:55 -08:00
Pavel Yaskevich
8e82f1d68b [CSGen] Detect nested out-of-scope variables in recursive declarations
Follow-up to https://github.com/apple/swift/pull/63505

Since, when the type is not stated, a variable assumes the
type of its initializer that enables out-of-scope variables
to be nested inside of some other concrete type i.e. Optional.

Resolves: https://github.com/apple/swift/issues/63455
2023-02-11 19:19:09 -08:00
swift-ci
9b8c8421c5 Merge remote-tracking branch 'origin/main' into rebranch 2023-02-10 21:54:43 -08:00
Pavel Yaskevich
bffb8d9ee2 Revert "[CSGen] Handle recursive use of variable declarations" 2023-02-10 16:45:59 -08:00
swift-ci
5127f32808 Merge remote-tracking branch 'origin/main' into rebranch 2023-02-09 11:33:33 -08:00
Pavel Yaskevich
9401926304 Merge pull request #63505 from xedin/issue-63455
[CSGen] Handle recursive use of variable declarations
2023-02-09 11:20:43 -08:00
Pavel Yaskevich
83dd93ad2e [CSGen] Handle recursive use of variable declarations
It's possible for out-of-scope type variable to be the type of
declaration if such declaration is recursively referenced in
the body of a closure located in its initializer expression.
In such cases type of the variable declaration cannot be connected
to the closure because its not known in advance (determined by the
initializer itself).

Resolves: https://github.com/apple/swift/issues/63455
2023-02-07 17:48:26 -08:00
swift-ci
586bfe5124 Merge remote-tracking branch 'origin/main' into rebranch 2023-02-07 01:13:57 -08:00
Pavel Yaskevich
f1051f1520 Merge pull request #63465 from xedin/issue-63264
[CSSyntacticElement] Correctly determine whether body is a "single ex…
2023-02-07 01:09:13 -08:00
swift-ci
b79d135f14 Merge remote-tracking branch 'origin/main' into rebranch 2023-02-06 12:13:44 -08:00
Pavel Yaskevich
e3ea597241 [CSSyntacticElement] Correctly determine whether body is a "single expression"
Result builder transformed bodies are always multi-statement.

Resolves: https://github.com/apple/swift/issues/63264
2023-02-06 10:46:27 -08:00
Hamish Knight
6c4b3daa23 [CS] Connect isolated conjunctions to referenced VarDecls
Currently we only consider ParamDecls, but isolated
conjunctions can reference external VarDecls too.

This fixes a spurious "cannot reference invalid declaration"
error when the result builder transform is disabled
in the test case rdar104687668.swift. It also fixes
the attached test case, where an if expression
references a pattern var in a for loop.

rdar://105080067
2023-02-06 14:25:13 +00:00
swift-ci
197114cb22 Merge remote-tracking branch 'origin/main' into rebranch 2023-02-03 08:55:08 -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
Hamish Knight
4e414b1cf7 [Sema] Avoid forming double braced single expression closures
For a single expression closure, just use the
expression as the body in the case where we're
coercing to Void, as the return is already
implied. This avoids crashing in
`ClosureExpr::getSingleExpressionBody` with a
double braced body.

Surprisingly it seems nothing is currently calling
`ClosureExpr::getSingleExpressionBody` after
type-checking, so no test case, but later commits
in this patch will exercise this case.
2023-02-01 15:30:16 +00:00
Pavel Yaskevich
51b2481091 [AST] Expand TypeJoin expression to support joining over a type
(cherry picked from commit 4efc35a76c)
2023-02-01 15:13:58 +00:00
Erik Eckstein
712fd7922b Merge remote-tracking branch 'origin/main' into rebranch 2023-01-09 08:48:47 +01: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
swift-ci
1a625f2706 Merge remote-tracking branch 'origin/main' into rebranch 2023-01-03 01:13:18 -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
Alex Hoppen
3c90c892e9 [CodeCompletion] Drop ForCodeCompletion constraint system option for syntactic elements that don't contain completion token 2022-12-21 12:17:56 -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