Commit Graph

6754 Commits

Author SHA1 Message Date
Slava Pestov
db6b4a9baf SILGen: Simplify logic in LocalArchetypeTransform 2024-05-21 13:52:58 -04: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
Slava Pestov
c7c244e16a Merge pull request #73712 from slavapestov/pack-expansion-closures-part-6
Pack expansion closures, part 6
2024-05-18 07:41:44 -04:00
Andrew Trick
79d2c8bcd4 Merge pull request #73687 from atrick/fix-bitwise-pointer-warning
Suppress pointer conversion warnings for BitwiseCopyable elements.
2024-05-17 15:57:40 -07:00
Slava Pestov
4aa0008d74 SILGen: Rewrite captured local archetypes into primary archetypes
This implements support for autoclosures, closures and local functions
nested within a pack iteration for loop.

The combination of explicit closure expressions and pack expansion
expressions still needs some work.

Fixes #66917.
Fixes #69947.
Fixes rdar://113505724.
Fixes rdar://122293832.
Fixes rdar://124329076.
2024-05-17 17:45:05 -04:00
Slava Pestov
05d39d4b61 SILGen: Use TypeConverter::getSubstitutionMapWithCapturedEnvironments() 2024-05-17 15:16:11 -04:00
Slava Pestov
c1ce0d72c2 SIL: Store captured environments in SILFunction 2024-05-16 23:00:44 -04:00
Andrew Trick
6bd0ef039a Suppress pointer conversion warnings for BitwiseCopyable elements.
Now that BitwiseCopyable is accepted, it should work as the recommended workaround for unsafe pointer conversion warnings:

Forming 'UnsafeMutableRawPointer' to a variable of type 'S'; this is likely incorrect because 'S' may contain an object reference.

The check for trivial element types is in SILGenExpr, diagnoseImplicitRawConversion. For now, we can hack SILGenExpr to specifically disable the warning for BitwiseCopyable, just as it was done for FixedWidthInteger in prior releases.

Fixes rdar://128229439 (Conversion from BitwiseCopyable to UnsafeRawPointer should not warn.)
2024-05-16 15:25:05 -07:00
Joe Groff
f984effd7f Merge pull request #73658 from jckarter/no-implicit-copy-strip-move-only-wrapper
SILGen: Bug fix handling `consuming`/`borrowing` parameters and optional chaining.
2024-05-16 07:39:45 -07:00
Joe Groff
38d4622d24 SILGen: Bug fix handling consuming/borrowing parameters and optional chaining.
Make sure we remove the `@moveOnly` marker while projecting the payload as expected
for the (no longer non-implicitly-copyable) projected result. Fixes rdar://116127887.
2024-05-15 16:57:40 -07:00
Doug Gregor
39f4e38027 Diagnose inout uses of 'lets' in constructors in the type checker.
Stored `let` properties of a struct, class, or actor permit
'inout' modification within the constructor body after they have been
initialized. Tentatively remove this rule, only allowing such `let`
properties to be initialized (assigned to) and not treated as `inout`.

Fixes rdar://127258363.
2024-05-14 15:59:50 -07:00
Joe Groff
2d61e3d26b Merge pull request #73531 from jckarter/forwarding-force-unwrap
SILGen: Treat Optional `x!` force unwrapping as a forwarding operation.
2024-05-09 08:35:29 -07:00
Joe Groff
82e566a23a SILGen: Treat Optional x! force unwrapping as a forwarding operation.
Like `?` or property access, `x!` can be borrowing, consuming, or mutated
through depending on the use site and the ownership of the base value.
Alter SILGen to emit `x!` as a borrowing operation when the result is only
used as a borrow. Fix the move-only checker not to treat the unreachable
branch as a dead path for values and try to destroy the value unnecessarily
and possibly out-of-order with cleanups on the value. Fixes rdar://127459955.
2024-05-08 15:35:07 -07:00
nate-chandler
bf6058b142 Merge pull request #73443 from nate-chandler/rdar127516085
[AST] Remove Builtin.copy.
2024-05-06 12:08:31 -07:00
Joe Groff
b107a5e0d7 Merge pull request #73436 from jckarter/c-function-pointer-conversion-sendable
Look through `@Sendable` conversions when considering C function pointer conversions.
2024-05-06 07:15:09 -07:00
Nate Chandler
c10d39e35f [AST] Remove Builtin.copy.
A vestigial remnant of it was left behind after
06921cfe84 in order to avoid a reverse
condfail when building old swiftinterfaces that define

```swift
func _copy<T>(_ value: T) -> T {
  #if $BuiltinCopy
    Builtin.copy(value)
  #else
    value
  #endif
}
```

If the language feature is removed, though, such interfaces should again
be buildable because the branch where the language feature isn't defined
should be expanded.

rdar://127516085
2024-05-04 11:35:17 -07:00
nate-chandler
768b3a4944 Merge pull request #73422 from nate-chandler/rdar127502242
[SIL] Hollow out Builtin.copy and deprecate _copy.
2024-05-03 22:19:47 -07:00
Joe Groff
d6e4b6bab8 Look through @Sendable conversions when considering C function pointer conversions.
Adding (or removing) `@Sendable` doesn't add context, so it shouldn't prevent
conversion to a C function pointer. Fix for rdar://127521718.
2024-05-03 17:47:53 -07:00
Nate Chandler
06921cfe84 [SIL] Hollow out Builtin.copy, deprecate _copy.
The copy operator has been implemented and doesn't use it.  Remove
`Builtin.copy` and `_copy` as much as currently possible.

Source compatibility requires that `_copy` remain in the stdlib.  It is
deprecated here and just uses the copy operator.

Handling old swiftinterfaces requires that `Builtin.copy` be defined.
Redefine it here as a passthrough--SILGen machinery will produce the
necessary copy_addr.

rdar://127502242
2024-05-03 15:56:25 -07:00
Joe Groff
cc1bf0d5a8 Merge pull request #73410 from jckarter/noncopyable-subscript-borrow-fix
SILGen: Correct handling of subscripts with addressors.
2024-05-03 12:58:36 -07:00
Joe Groff
d08f1c37dd SILGen: Correct handling of subscripts with addressors.
Remove improper special-case handling of subscripts in `findStorageReferenceExprForMoveOnly`.
The correct thing to do for any storage decl ref of noncopyable type is to emit it as a borrow
if it's implemented using storage, a read accessor, or an addressor. Fixes rdar://127335590.
2024-05-02 19:20:14 -07:00
Slava Pestov
625436af05 Merge pull request #73393 from slavapestov/pack-expansion-closures-part-3
SIL type lowering support for closures that capture pack element archetypes
2024-05-02 19:43:24 -04:00
Slava Pestov
af0f17e605 SILGen: Tiny cleanup 2024-05-02 14:28:30 -04:00
Slava Pestov
6690e86835 SILGen: Use the correct GenericEnvironment when lowering functions with pack element captures 2024-05-02 14:28:30 -04:00
Joe Groff
73ed03c827 Merge pull request #73380 from jckarter/mark-captured-no-implicit-copy-parameters
Consistently mark `borrowing` and `consuming` parameters for move-only checking when captured.
2024-05-02 08:56:10 -07:00
Joe Groff
90e1ecb864 Consistently mark borrowing and consuming parameters for move-only checking when captured.
When a `borrowing` or `consuming` parameter is captured by a closure,
we emit references to the binding within the closure as if it is non-implicitly
copyable, but we didn't mark the bindings inside the closure for move-only
checking to ensure the uses were correct, so improper consumes would go
undiagnosed and lead to assertion failures, compiler crashes, and/or
miscompiles. Fixes rdar://127382105
2024-05-01 13:41:28 -07:00
Joe Groff
bbd7863cb2 Merge pull request #73339 from jckarter/noncopyable-switch-return-no-expr
SILGen: Check whether return statement has expression before examining for compatibility.
2024-04-30 15:18:48 -07:00
Joe Groff
91bd3e6b60 SILGen: Check whether return statement has expression before examining for compatibility.
Avoid an assertion failure when a `return` with no expression appears inside of a noncopyable
switch. Fixes rdar://127294417
2024-04-30 08:24:57 -07:00
Adrian Prantl
82a26a59d8 Emit line table entries for closure setup instructions.
Currently the Swift compiler makes these instructions with SILLocations marked
as autgenerated. While this allows for somewhat smoother stepping in some cases,
it can also make some debugging tasks harder due to missing source location
information, for example, when attributing memory allocations.

This patch makes these locations available again, based on that a debug info
consumer could consider filtering them out by recognizing that a source location
is on the opening `{` of a closure, but inside the scope of the function the
closure is defined in.

rdar://127095833
2024-04-29 12:51:28 -07:00
Michael Gottesman
025902fa99 [sil] Add a SILDeclRef field to SILFunction, have SILGen set it, and teach SILFunction how to use the field to get the SourceFile associated with the SILFunction. 2024-04-23 11:45:36 -05:00
nate-chandler
1faeb50fff Merge pull request #73121 from nate-chandler/rdar126715654
[NoncopyablePartialConsumption] Promote to feature.
2024-04-22 07:01:10 -07:00
Joe Groff
81534a6c15 Merge pull request #73148 from jckarter/borrowing-switch-old-behavior-compat
SILGen: Provide compatibility with prior `BorrowingSwitch` behavior.
2024-04-21 11:21:27 -07:00
Slava Pestov
e342a38b87 Sema: Convert TypeChecker::computeCaptures() into two requests
We now compute captures of functions and default arguments
lazily, instead of as a side effect of primary file checking.

Captures of closures are computed as part of the enclosing
context, not lazily, because the type checking of a single
closure body is not lazy.

This fixes a specific issue with the `-experimental-skip-*` flags,
where functions declared after a top-level `guard` statement are
considered to have local captures, but nothing was forcing these
captures to be computed.

Fixes rdar://problem/125981663.
2024-04-20 22:16:25 -04:00
Joe Groff
cb5f1e20ae SILGen: Provide compatibility with prior BorrowingSwitch behavior.
To avoid breaking early adopters of this feature, accept attempts to `return`
a `let` binding in a noncopyable `switch` when it would be treated as a
borrow normally, with a warning that this behavior will change soon.
rdar://126775241
2024-04-19 15:20:31 -07:00
Slava Pestov
2f36a940b7 SILGen: Don't ask for captures of things that can't have captures 2024-04-19 17:59:57 -04:00
Nate Chandler
23e7c36c5f [NoncopyablePartialConsumption] Ungate feature.
Remove all checks for the feature flag.  It's on all the time.
2024-04-19 12:37:34 -07:00
Slava Pestov
3e637b0afb Merge pull request #73129 from slavapestov/local-function-isolation-change
Local functions inherit isolation from context instead of captures
2024-04-19 08:32:46 -04:00
Slava Pestov
7ef1701f6f Merge pull request #73109 from slavapestov/silgen-imported-superclass
SILGen: Avoid needless walk up ancestor chain of imported class
2024-04-19 08:31:25 -04:00
Slava Pestov
05bcc25f7b SIL: Local functions should inherit isolation from context
If the outer context is actor isolated, we need to force a capture the
actor variable, since otherwise it might not appear in the capture list.
2024-04-18 23:26:17 -04:00
Slava Pestov
c66de7752d SILGen: Avoid needless walk up ancestor chain of imported class 2024-04-18 13:14:49 -04:00
Joe Groff
532a7de23e Merge pull request #73092 from jckarter/silgen-loadable-error-cleanup-order
SILGen: Move error values into indirect error returns in proper order with cleanups.
2024-04-18 07:16:02 -07: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
John McCall
2dfbceb164 Merge pull request #73071 from rjmccall/map-fully-specialized-isolation
Map the isolated variable type into context correctly
2024-04-17 03:28:30 -04:00
John McCall
e7765ad090 Map the isolated variable type into context correctly.
The fix for #72484 didn't properly handle the case of a "generic" context
where all the type parameters had concrete assignments.  In this situation,
F.mapTypeIntoContext does not work because the function has no generic
environment.

Fixes rdar://126085573
2024-04-16 19:31:38 -04:00
Pavel Yaskevich
7b8f76e3c4 Merge pull request #73007 from xedin/promote-dynamic-actor-isolation-to-upcoming-feature
[SE-0423] Promote `DynamicActorIsolation` to an upcoming feature and add a way to disable checks
2024-04-16 11:46:41 -07:00
Joe Groff
15973e3b87 Merge pull request #73041 from jckarter/silgen-borrow-noncopyable-global-lets
SILGen: Emit references to noncopyable global storage directly as a borrow.
2024-04-16 07:51:50 -07:00
Joe Groff
4077c75f8a SILGen: Emit references to noncopyable global storage directly as a borrow.
Later analyses are too conservative to remove a copy, but it should be fairly safe to
elide the copy for noncopyable globals, since accesses are tightly scoped and dynamically checked,
so consumes aren't possible, and borrows and inout accesses of mutable globals are dynamically
guarded and not subject to exclusivity checks. rdar://114329759
2024-04-15 16:12:12 -07:00
Pavel Yaskevich
72eb8abadb [Sema/SILGen] Don't emit dynamic actor isolation checks when -disable-dynamic-actor-isolation flag is used 2024-04-15 09:19:22 -07:00
Konrad `ktoso` Malawski
460a030860 [Distributed] Avoid infinite recursion in distributed thunk on protocol extensions (#73032) 2024-04-15 03:43:49 -07:00
Joe Groff
e06ff61312 Merge pull request #72577 from jckarter/enable-borrowing-switch-backend
Use the `BorrowingSwitch` implementation for all noncopyable switches.
2024-04-10 07:38:12 -07:00