Commit Graph

296 Commits

Author SHA1 Message Date
Kavon Farvardin
4964a66277 silgen: inherit eval scope for OpenExistentialExpr
We were not able to use an existential as the base
of an access that strictly borrows the existential,
because SILGen's RValue emission would establish
a fresh evaluation scope just for the existential's
opening, and then copy the opened value out.

This is problematic for noncopyable existentials.

So this patch moves & adds FormalEvaluationScope's
around so they're broad enough to enable a
borrow of an existential. The idea behind this
refactoring is to establish top-level
FormalEvaluationScopes when initially creating
RValue's for Expr's in SILGen. Any more-tightly
scoped operations will already establish their own
nested scope, so this is mostly adding safe-guards.

I've limited the existentials fix to noncopyables
for now.

part of rdar://159079818
2025-08-29 13:30:42 -07:00
John McCall
bee053f1f0 Switch InitializationPtr to use PossiblyUniquePtr so that we can just
forward existing initializations around when necessary.
2025-08-02 02:13:25 -04:00
Anthony Latsis
fec049e5e4 Address llvm::PointerUnion::{is,get} deprecations
These were deprecated in
https://github.com/llvm/llvm-project/pull/122623.
2025-07-29 18:37:48 +01: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
Slava Pestov
375363a473 AST: Move global conformance lookup entry points to ConformanceLookup.h 2024-08-08 23:35:58 -04:00
Slava Pestov
3fcda140bb AST: ModuleDecl::checkConformance() is a static method 2024-07-06 12:05:46 -04:00
Slava Pestov
fae01d9776 AST: Remove ModuleDecl parameter from more places 2024-07-06 12:05:46 -04:00
Tim Kientzle
1098054291 Merge branch 'main' into tbkka-assertions2 2024-06-18 17:52:00 -07:00
Erik Eckstein
2200632a95 SILGen: ignore unreachable var decls
Fixes a crash in case a lazy var is declared after a return statement

https://github.com/apple/swift/issues/73736
2024-06-10 16:19:47 +02: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
Jamie
58166fc162 [SILGen]: diagnose unreachable opened existentials
updates unreachable code handling in SILGenStmt.cpp to diagnose
opened existentials that were previously ignored.

resolves: https://github.com/apple/swift/issues/73649
2024-05-19 23:26:54 -05:00
Joe Groff
ba4f42420b SILGen: Move error values into indirect error returns in proper order with cleanups.
There's an unfortunate layering difference in the cleanup order between address-only
and loadable error values during `catch` pattern matching: for address-only values,
the value is copied into a temporary stack slot, and the stack slot is cleaned up
on exit from the pattern match, meaning the value must be moved into the error return
slot on the "no catch" case before cleanups run. But if it's a loadable value, then
we borrow it for the duration of the switch, and the borrow is released during cleanup
on exit from the pattern match, so the value must be forwarded after running cleanups.

The way the code is structured, it handles these cases properly when the convention of
the function being emitted is in sync with the fundamental properties of the error type
(when the error type is loadable and the error return is by value, or when the error
type is address-only and the error return is indirect, in other words). But when
a closure literal with a loadable error type is emitted in an argument context that
expects a function with an indirect error return, we would try to forward the loadable
error value into the error return slot while a borrow is still active on it, leading
to verifier errors. Defer forwarding the value into memory until after cleanups are
popped, fixing rdar://126576356.

A tidier solution might be to always emit the function body to use a bbarg on the
throw block to pass the error value from the body emission to the epilog when the
type is loadable, deferring the move into memory to the epilog block. This would
make the right behavior fall out of the existing implementation, but would require
a bit more invasive changes (pretty much everywhere that checks IndirectErrorReturn
would need to check a different-tracked AddressOnlyErrorType bit instead or in
addition). This change is more localized.
2024-04-17 14:13:42 -07:00
nate-chandler
ec6d207c89 Merge pull request #72538 from nate-chandler/opaque-values/20240322/1
[OpaqueValues] Pass direct to willThrowTyped.
2024-03-25 07:06:17 -07:00
Nate Chandler
7887912a94 [OpaqueValues] Pass direct to willThrowTyped.
The runtime function `swift_willThrowTyped` takes its argument
`@in_guaranteed`.  In opaque values SIL, that's passed directly.  Don't
store non-address errors before passing them to the function.
2024-03-22 18:01:12 -07:00
John McCall
c7d3e8f559 [NFC] Generalize how Conversion works with reabstraction conversions and
force callers to specify the input lowered type as well.
2024-03-21 12:47:56 -04:00
John McCall
7b3415aa23 Properly erase closure isolation to @isolated(any).
We do this by pushing the conversion down to the emission of the
closure expression, then teaching closure emission to apply the isolation
to the closure.  Ideally, we combine the isolation along with the rest of
the conversion peephole, but if necessary, we make sure we emit the
isolation.
2024-02-26 22:50:58 -05:00
Doug Gregor
e4d6bee153 Merge pull request #71400 from DougGregor/will-throw-typed
Implement `swift_willThrow` variant for typed throws.
2024-02-06 03:22:22 -08:00
Doug Gregor
3fa07a0e7a Implement swift_willThrow variant for typed throws.
`swift_willThrow` is called with an error right before it is thrown.
This existing entrypoint requires an already-boxed error existential;
with typed errors, we don't have the error existential on hand, so we
would need to allocate the box to throw a typed error. That's not okay.

Introduce a new `swift_willThrowTypedImpl` entry point into the runtime
that will first check for the presence of an error handler and, if one
is present, box the error to provide to the error handler. This
maintains the no-allocations path for typed errors while still
allowing existing error handlers to work.

This new entrypoint isn't available on older Swift runtimes, so create
a back-deployable shim called by the compiler. On new-enough platforms,
this will call through to `swift_willThrowTypedImpl`. On older
platforms, we drop the error and don't call the registered will-throw
handler at all. This is a compromise that avoids boxing when throwing
typed errors, at the cost of a slightly different experience for this
new feature on older runtimes.

Fixes rdar://119828459.
2024-02-05 15:06:55 -08:00
Sima Nerush
8aa2ef83f7 Merge pull request #71375 from simanerush/pack-iteration-sil-rename
[SILGen] Rename `loopDest` and `endDest` to `continueDest` and `breakDest` to make more sense to the reader.
2024-02-04 10:27:45 -08:00
Sima Nerush
457a2263d3 [SILGen] Rename loopDest and endDest to continueDest and breakDest to make more sense to the reader. 2024-02-03 14:23:28 -08: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
Slava Pestov
14d1fcb51a AST: TypeChecker::conformsToProtocol() => ModuleDecl::checkConformance() 2024-01-16 17:08:00 -05:00
Sima Nerush
2fd5843a79 Merge pull request #70355 from simanerush/pack-iteration-fixes
[SE-0408] Pack iteration review feedback!
2023-12-16 09:50:31 -08:00
Sima Nerush
92d81f340e [SILGen] Improve documentation of SE-0408, put back the documentation that was misplaced. 2023-12-15 20:39:47 -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
Sima Nerush
b6d0afba1f Merge pull request #67594 from simanerush/simanerush/pack-iteration-impl
[SE-0408] Enable Pack Iteration
2023-12-07 17:09:48 -08:00
Doug Gregor
00d7e41513 [Typed throws] Always convert error to destination type in calls
An attempted SILGen optimization caused assertions and SIL verification
errors. Always convert the error appropriately.

Fixes rdar://119214492.
2023-12-06 15:32:29 -08:00
Sima Nerush
be212badbd Add break/continue support 2023-12-03 21:51:40 -08:00
Sima Nerush
af56beb60e Add for case support and tests 2023-12-03 21:51:40 -08:00
Sima Nerush
6ab831043d SIL 2023-12-03 21:51:40 -08:00
Doug Gregor
cfe2b3c87d [Typed throws] Implement support for do throws(...) syntax
During the review of SE-0413, typed throws, the notion of a `do throws`
syntax for `do..catch` blocks came up. Implement that syntax and
semantics, as a way to explicitly specify the type of error that is
thrown from the `do` body in `do..catch` statement.
2023-12-02 07:37:47 -08:00
Hamish Knight
49ad980b86 [Profiler] Map regions for error-throwing AST nodes
Map a counter for the error branch of a given
potentially-throwing expression, and subtract it
from the following region count.

rdar://34244637
2023-11-16 17:28:57 +00:00
Doug Gregor
927e2420a0 [Typed throws] Handle closure reabstraction
This peephole optimization in SILGen requires us to use the thrown
error for the context of a closure type rather than the thrown error
for the closure AST node itself.
2023-11-08 12:08:01 -08:00
Doug Gregor
0ba605a4b9 [Typed throws] Implement reabstraction thunks that change the error
Introduce SILGen support for reabstractions thunks that change the
error, between indirect and direct errors as well as conversions
amongst error types (e.g., from concrete to `any Error`).
2023-11-07 11:39:56 -08:00
Slava Pestov
204bb0c2f2 SILGen: Lowering 'try?' and 'try!' with typed throws 2023-10-31 22:55:48 -04:00
Slava Pestov
079c5c4a1c SILGen: Support for 'throw' and 'try_apply' with indirect error result 2023-10-31 16:58:54 -04:00
Doug Gregor
2d4e8fda3e [Typed throws] Compute and use the caught error type of a do..catch block.
The type that is caught by the `catch` clauses in a `do..catch` block is
determined by the union of the thrown error types in the `do`
statement. Compute this type and use it for the catch clauses. This
does several things at once:

* Makes the type of the implicit `error` be a more-specific concrete
type when all throwing sites throw that same type
* When there's a concrete type for the error, one can use patterns
like `.cancelled`
* Check that this error type can be rethrown in the current context
* Verify that SIL generation involving do..catch with typed errors
doesn't require any existentials.
2023-10-04 17:20:36 -07:00
Doug Gregor
b6b999abd4 [Typed throws] Basic SIL lowering and SIL generation for typed throws
Lower the thrown error type into the SIL function type. This requires
very little code because the thrown error type was already modeled as
a SILResultInfo, which carries type information. Note that this
lowering does not yet account for error types that need to passed
indirectly, but we will need to do so for (e.g.) using resilient error
types.

Teach a few places in SIL generation not to assume that thrown types
are always the existential error type, which primarily comes down to
ensuring that rethrow epilogues have the thrown type of the
corresponding function or closure.

Teach throw emission to implicitly box concrete thrown errors in the
error existential when needed to satisfy the throw destination. This
is a temporary solution that helps translate typed throws into untyped
throws, but it should be replaced by a better modeling within the AST
of the points at which thrown errors are converted.
2023-09-29 10:51:55 -07:00
Hamish Knight
41dc6e2e04 [SILGen] Emit unreachable for uninhabited if/switch expr branches
If we have an uninhabited branch, emit it as an
ignored expr followed by an unreachable.
Previously we would omit the unreachable and rely
on the SILOptimizer to infer it, but we ought to
just emit it here. Also check `isUninhabited()`
instead of `isStructurallyUninhabited` since this
better matches what we allow in Sema. For tuples
of uninhabited values, we can do a regular
initialization without issue.
2023-09-22 18:44:43 +01:00
Kuba Mracek
a3ee08fbc9 [embedded] Use cond_fail in throw-as-traps mode 2023-09-21 07:17:19 -07:00
Kuba Mracek
a31c3388e4 [embedded] Add a temporary flag that turns throws into traps so that programs that use throwing can at least be compiled for now 2023-09-19 22:00:51 -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
Zak Kent
3657cbafc9 [SILGen] Move all top-level emission code to SILGenTopLevel.cpp 2023-08-08 11:25:11 -07:00
Zak Kent
7dae2e6905 [SILGen] Implement SILGenTopLevel
Implement SILGenTopLevel, a class that walks a file
run in script mode to generate all toplevel code
at once.
2023-08-08 11:25:11 -07:00
Slava Pestov
9ebb5f2e03 AST: Rename VarDecl::getType() to VarDecl::getTypeInContext()
This is a futile attempt to discourage future use of getType() by
giving it a "scary" name.

We want people to use getInterfaceType() like with the other decl kinds.
2023-08-04 14:19:25 -04:00
Joe Groff
c52ae08c7d SILGen: Don't reuse the Initialization across branches of an if or switch expression.
`Initialization` is stateful and not meant to be emitted into multiple times across different contexts.
If emitting into an initialization causes it to be split or aborted, that will carry over into
further uses of the initialization. This was happening during `if` and `switch` expression
emission, leading to miscompiles or compiler crashes. Fix this by saving only the buffer when
we prepare emission for a statement expression, and creating the initialization in the scope
where the expression for a branch actually gets emitted. Fixes rdar://112213253.
2023-07-14 14:21:57 -07:00
John McCall
e14f2bc0c7 [NFC] Add a method to just ask if a tuple AP vanishes under substitution 2023-06-29 19:39:51 -04:00
Erik Eckstein
6b1697eb06 use new llvm::Optional APIs to fix deprecation warnings 2023-06-28 14:28:38 +02:00
John McCall
acbd4a6022 Fix the emission of closures into reabstracted contexts with
variadic-tuple results.  There are three parts to this.

First, fix the emission of indirect result parameters to do a
proper abstraction-pattern-aware traversal of tuple patterns.
There was a FIXME here and everything.

Second, fix the computation of substituted abstraction
patterns to properly handle vanishing tuples.  The previous code
was recursively destructuring tuples, but only when it saw a
tuple as the substituted type, which of course breaks on vanishing
tuples.

Finally, fix the emission of returns into vanishing tuple
patterns by allowing the code to not produce a TupleInitialization
when the tuple pattern vanishes.  We should always get a singleton
element initializer in this case.

Fixes rdar://109843932, plus a closely-related test case for
vanishing tuples that I added myself.
2023-06-14 21:29:22 -04:00
Andrew Trick
2f200a6caa [move-only] Fix drop_deinit OSSA lowering
drop_deinit ultimately only affects the semantics of its
destroy_value. Avoid generating releases for destroys in which the
deinit has been dropped. Instead, individually release the members.
2023-06-06 09:17:53 -07:00