Commit Graph

295 Commits

Author SHA1 Message Date
Slava Pestov
c0787f337f Sema: Fix soundness hole with variable initializers and opaque return types
Just because the type of the initializer expression is an opaque return type,
does not mean it is the opaque return type *for the variable being initialized*.

It looks like there is a bit of duplicated logic and layering violations going
on so I only fixed one caller of openOpaqueType(). This addresses the test case
in the issue. For the remaining calls I added FIXMEs to investigate what is
going on.

Fixes https://github.com/swiftlang/swift/issues/73245.
Fixes rdar://127180656.
2024-12-04 15:13:22 -05:00
Hamish Knight
73fb36f371 [AST] Split out "is compound" bit on FunctionRefInfo
FunctionRefKind was originally designed to represent
the handling needed for argument labels on function
references, in which the unapplied and compound cases
are effectively the same. However it has since been
adopted in a bunch of other places where the
spelling of the function reference is entirely
orthogonal to the application level.

Split out the application level from the
"is compound" bit. Should be NFC. I've left some
FIXMEs for non-NFC changes that I'll address in a
follow-up.
2024-12-02 14:11:33 +00:00
Hamish Knight
a4d51419ba [AST] NFC: Rename FunctionRefKind -> FunctionRefInfo 2024-12-02 14:11:32 +00:00
Doug Gregor
f89218941b [Result builder transform] Don't abort when we encounter a case with no statements
Remove code that aborts the result builder transform when we encounter
a case that has no statements in it. This can occur when the only
statements were behind a `#if` that evaluataed empty, so it should not
cause an abort.

Previously, the presence of an IfConfigDecl within the case statement
would have prevented us from aborting the traversal here. However, the
removal of IfConfigDecl from the AST turned this previously-accepted
code into a compiler crash.

Fixes rdar://139312426.
2024-11-07 22:58:18 -08:00
Allan Shortlidge
919ec930f9 Merge pull request #77384 from tshortli/rename-unmet-availability-requirement 2024-11-05 10:56:55 -08:00
Allan Shortlidge
161dc2e09e Sema: Rename getUnmetDeclAvailabilityRequirement().
Name it `getUnsatisfiedAvailabilityConstraint()` to match
`AvailabilityConstraint`.
2024-11-04 19:58:28 -08:00
Pavel Yaskevich
c5d0916746 [BuildTransform] Fix walkExplicitReturnStmts to walk implicit statements
Some statements introduce implicit braces and other things,
`walkExplicitReturnStmts` cannot ignore that while trying
to find explicit returns.

Resolves: rdar://139235128
2024-11-04 16:42:50 -08:00
Allan Shortlidge
7b5757b29b Sema: Replace TypeChecker::checkDeclarationAvailability().
Adopt `getUnmetDeclAvailabilityRequirement()` instead.
2024-11-03 11:37:01 -08:00
Hamish Knight
2bcffc56d0 [CS] Fix source range for for loop result builder transform
Ensure the implicit `do` statement has a source
range that covers the `for` loop by changing the
source location for the initial binding. This ensures
we correctly detect the code completion child and
avoid skipping it.
2024-10-29 20:35:40 +00:00
Anthony Latsis
41adfec8da [NFC] AST, Sema: Move TypeChecker::findReturnStatements into AnyFunctionRef
Also rename it to `getExplicitReturnStmts` for clarity and have it
take a `SmallVector` out parameter instead as a small optimization and
to discourage use of this new method as an alternative to
`AnyFunctionRef::bodyHasExplicitReturnStmt`.
2024-10-11 03:57:43 +03:00
Anthony Latsis
e31e8773d5 [NFC] Sema: Reimplement ReturnStmtFinder as a function with a callback
A callback enables the caller to control whether to abort the walk. This
is handy for `BraceHasExplicitReturnStmtRequest`.
2024-10-11 03:46:35 +03:00
Anthony Latsis
244185c4f4 [NFC] AST: Rename BraceHasReturnRequest to BraceHasExplicitReturnStmtRequest 2024-10-11 03:44:43 +03:00
Anthony Latsis
c7ea672463 [NFC] AST, Sema: Internalize BraceHasReturnRequest evaluation in AnyFunctionRef method 2024-10-11 03:44:43 +03:00
Slava Pestov
877c60e3d0 Sema: Rename applySolution() to replaySolution() 2024-10-08 16:16:01 -04:00
Slava Pestov
892e79cd70 Sema: Use an xmacro to clean up some duplication in CSTrail.cpp 2024-10-07 16:50:46 -04:00
Slava Pestov
c7edc3494e Sema: Record result builder transforms in the trail 2024-10-07 16:50:45 -04:00
Doug Gregor
5b2520e379 Remove IfConfigDecl from the AST
The swift-syntax tree retains information about the parsed #if
regions. Drop it from the semantic AST.
2024-09-18 20:51:54 -07:00
Hamish Knight
a73e44a78f [Sema] Split out result builder pre-checking
Use `preCheckTarget` to pre-check the body,
allowing us to replace `PreCheckResultBuilderRequest`
with a request that only checks the brace for
ReturnStmts.
2024-08-03 16:05:09 +01:00
Hamish Knight
816f0b97e9 [Sema] NFC: Remove SuppressDiagnostics from PreCheckResultBuilderRequest
This is always `false`.
2024-08-03 16:05:08 +01:00
Hamish Knight
22b08da0dd [Sema] Remove replaceInvalidRefsWithErrors param
Doesn't seem like anything is relying on setting
this to `false` anymore, remove it.
2024-07-21 15:27:15 +01:00
Tim Kientzle
1098054291 Merge branch 'main' into tbkka-assertions2 2024-06-18 17:52:00 -07:00
Hamish Knight
f7753de1c8 [Completion] Only skip result builder expressions in the same builder
Previously we would skip any expression in a
result builder that didn't contain the completion
token, but that would cause issues if e.g the
result builder was needed to infer the type of a
variable that we're completing on. Instead, only
skip expressions in a result builder if the
completion token is in the same builder and the
expression itself doesn't contain the completion.

rdar://127154780
2024-06-07 21:06:16 +01:00
Tim Kientzle
1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
Hamish Knight
cbbb1c9eca [CS] Avoid creating placeholder var if skipping for completion
Otherwise we'd crash in CSApply since we'd have no
type recorded for the var.

rdar://127838305
2024-05-17 15:46:40 +01:00
cui fliter
127077b3aa chore: fix some comments
Signed-off-by: cui fliter <imcusg@gmail.com>
2024-03-05 17:23:22 +08:00
Ben Barham
ef8825bfe6 Migrate llvm::Optional to std::optional
LLVM has removed llvm::Optional, move over to std::optional. Also
clang-format to fix up all the renamed #includes.
2024-02-21 11:20:06 -08: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
c97d80b1c3 [AST] NFC: Add convenience constructors for ReturnStmt
Add `ReturnStmt::createParsed` and `createImplict`.
2024-01-23 19:30:18 +00:00
Alex Hoppen
09ae4f5d5d [CodeCompletion] Fix issue in which parts of a result builder were incorrectly skipped
`getLoc` does not necesarrily return the start location of the location (e.g. for `a.b().c()` it returns the location of `c` because that’s the location of the call). But we used the location from `getLoc` as the start location of the synthesized `buildExpression` call. In the added test case, this means that the `buildExpression` call only contained `everlay() {}` and not the code completion token. We thus infered that we could skip it the entire `MyStack {}.pnTabGesture {}.everlay() {}` call for code completion, which isn’t correct.

rdar://120798355
2024-01-12 15:03:57 -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
Alex Hoppen
18979b304b [Sema] Delete ClosuresInResultBuilderDontParticipateInInference
Since we migrated all of code completion to solver-based, this flag is no longer needed.
2023-09-07 13:15:23 -07: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
Alex Hoppen
ed4dc1becd Change Optional -> llvm::Optional 2023-07-07 21:16:33 +02:00
Alex Hoppen
527a79e9f8 [IDE] Set constraint system options from solveForCodeCompletion in applyResultBuilderBodyTransform when solving for code completion 2023-07-07 19:51:01 +02:00
Alex Hoppen
1bacfe9cd4 [IDE] Ignore unspported constructs in result builders in code completion mode 2023-07-07 19:51:01 +02:00
Alex Hoppen
0c413109d3 [CodeComplete] Skip conjunction elements unrelated to the code completion token 2023-07-07 19:50:46 +02:00
Evan Wilde
250082df25 [NFC] Reformat all the LLVMs
Reformatting everything now that we have `llvm` namespaces. I've
separated this from the main commit to help manage merge-conflicts and
for making it a bit easier to read the mega-patch.
2023-06-27 09:03:52 -07: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
Holly Borla
cd752cca22 [NameLookup] Plumb source location arguments through all name lookup APIs.
This source location will be used to determine whether to add a name lookup
option to exclude macro expansions when the name lookup request is constructed.
Currently, the source location argument is unused.
2023-06-11 23:09:47 -07:00
Hamish Knight
21e787bae8 [Sema] Walk SyntacticElementTarget for completion
Instead of walking the single ASTNode from the
target, walk all AST nodes associated with the
target to find the completion expr. This is needed
to find the completion expr in a pattern for an
initialization target.
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
8a26df8166 [ConstraintSystem] NFC: Standardize the way of type holefication
Introduce `ConstraintSystem::recordTypeVariablesAsHoles` as a
standard way to record that unbound type variables found in a
type are holes in the given constraint system.
2023-04-21 15:22:13 -07:00
Pavel Yaskevich
1845b44cce [BuilderTransform] NFC: Inline now redundant base class into ResultBuilderTransform 2023-03-16 11:22:37 -07:00
Pavel Yaskevich
93ea9a0c0c [BuilderTransform] NFC: Remove previous result builder implementation 2023-03-16 10:51:46 -07:00
Alex Hoppen
cbb32aae86 [Sema] Don't abort the result builder transform if the builder contained an ErrorExpr
When matching a result builder in the constraint system, the closure has already been pre-checked so we don’t need to pre-check it again. Also, we can continue type checking even if the closure contained an `ErrorExpr` since result builders are now type checked like regular closures.
2023-03-13 15:51:39 -07:00
Hamish Knight
2976edbe20 [CS] Rename SolutionApplicationTarget -> SyntacticElementTarget 2023-03-06 20:54:06 +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
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