Commit Graph

1232 Commits

Author SHA1 Message Date
John McCall
93b593bb5c Fix argument index calculations for isolated default arguments.
Fixes rdar://150060837
2025-05-07 20:04:38 -04:00
Slava Pestov
2fa49b0458 AST: SubstitutionMap overload of substOpaqueTypesWithUnderlyingTypes()
This replaces the oddly-named mapIntoTypeExpansionContext() method
on SubstitutionMap itself in favor of a global function, just like
the ones that take Type and ProtocolConformanceRef.
2025-04-28 13:48:35 -04:00
Anthony Latsis
925b72eaec Merge pull request #76337 from DePasqualeOrg/grammar-compound-modifiers
Grammatical corrections for compound modifiers
2025-04-25 13:50:52 +01:00
Anthony
c9b17383c8 Grammatical corrections for compound modifiers 2025-04-24 09:21:32 +02:00
Michael Gottesman
227ab376cf [silgen] When emitting a foreign async completion handler for a method, use merge isolation region to tie self and the block storage into the same region.
This is an extension of a similar problem that I had fixed earlier where due to
the usage of intermediate Sendable types we do not propagate regions correctly.

The previous issue I fixed was that we were not properly tieing the result of a
foreign async completion handler to the block storage since we used an
intervening UnsafeContinuation (which is Sendable) to propagate the result into
the block storage. I fixed this by changing SILGen to insert a
merge_isolation_region that explicitly ties the result to the block storage.

This new issue is that the block that we create and then pass as the completion
handler is an @Sendable block. Thus when we call the actual objc_method, the
block storage and self are not viewed as being in the same region. In this PR, I
change it so that we add a merge_isolation_region from self onto the block
storage.

The end result of this is that we have that self, the result of the call, and
the block storage are all in the same region meaning that we properly diagnose
that returning an NSObject from the imported Objective-C function is task
isolated and thus we cannot return it as a sending result.

rdar://131422332
2025-04-23 10:01:24 -07:00
Nate Chandler
7697a49ee9 [CoroutineAccessors] Only reference when available
Don't bind references to storage to use (new ABI) coroutine accessors
unless they're guaranteed to be available.  For example, when building
against a resilient module that has coroutine accessors, they can only
be used if the deployment target is >= the version of Swift that
includes the feature.

rdar://148783895
2025-04-15 21:13:57 -07:00
Nate Chandler
b76a76bdf1 [CoroutineAccessors] Unwind based on feature.
Now that coroutine kind (and consequently ABI) for the accessors is
keyed off a SIL option, it's no longer possible to read whether a given
SILFunction arose from a read/modify coroutine just by checking its
coroutine kind.  Regardless of ABI, read/modify coroutines may only
unwind (i.e. are only permitted not to "run to completion") if the
relevant experimental (soon to be deleted) feature is enabled.
2025-04-10 07:41:37 -07:00
Slava Pestov
ec0dfc8716 AST: Add new form of SubstitutionMap::getProtocolSubstitutions() 2025-04-03 17:35:33 -04:00
Joe Groff
6b605f41cb Type substitution eliminates dependencies with Escapable targets.
When a generic function has potentially Escapable outputs, those outputs
declare lifetime dependencies, which have no effect when substitution
leads to those types becoming `Escapable` in a concrete context.
This means that type substitution should canonically eliminate lifetime
dependencies targeting Escapable parameters or returns, and that
type checking should allow a function value with potentially-Escapable
lifetime dependencies to bind to a function type without those dependencies
when the target of the dependencies is Escapable.

Fixes rdar://147533059.
2025-04-02 08:54:45 -07:00
Amritpan Kaur
45a7b45ad9 [SILGen] Lower unapplied methods. 2025-03-19 10:54:09 -07:00
Amritpan Kaur
60e03a85d9 [SILGen] Lower applied keypath methods. 2025-03-19 10:54:09 -07:00
Amritpan Kaur
e5362c761d [NFC] Refactor subscript argument evaluation for reuse. 2025-03-19 10:54:09 -07:00
Amritpan Kaur
91c94a608d [NFC] Refactor callee formation for reuse. 2025-03-19 10:54:09 -07:00
Amritpan Kaur
e7e354d989 [NFC] Generalize subscript code for methods. 2025-03-19 10:54:09 -07:00
Joe Groff
da813458a6 SILGen: Emit an addressable representation for immutable bindings on demand.
To ensure that dependent values have a persistent-enough memory representation
to point into, when an immutable binding is referenced as an addressable
argument to a call, have SILGen retroactively emit a stack allocation and
materialization that covers the binding's scope.
2025-03-12 18:35:42 -07:00
Michael Gottesman
f6349aaf1e Revert "SILGen: Emit an addressable representation for immutable bindings on demand."
This reverts commit 19a99ea42b.

rdar://146864906
2025-03-12 13:24:33 -07:00
Joe Groff
2df3211690 Merge pull request #79903 from jckarter/addressable-representation-silgen
SILGen: Emit an addressable representation for immutable bindings on demand.
2025-03-12 08:02:05 -07:00
Joe Groff
19a99ea42b SILGen: Emit an addressable representation for immutable bindings on demand.
To ensure that dependent values have a persistent-enough memory representation
to point into, when an immutable binding is referenced as an addressable
argument to a call, have SILGen retroactively emit a stack allocation and
materialization that covers the binding's scope.
2025-03-11 15:15:48 -07:00
Michael Gottesman
2340dba27b [silgen] Place hop_to_executor emission for an apply within a formal evaluation scope.
Without this, the borrow of the hop_to_executor lasts after the apply. Beyond
being unnecessary this results in an OSSA violation if we are passing an actor
as an isolated parameter to an initializer since we hop_to_executor the actor
and then pass it as a +1 parameter to the initializer causing the actor to be
consumed before its borrow ends.

rdar://144994837
2025-03-07 11:51:22 -08:00
Gabor Horvath
00fa738209 [cxx-interop] Fix calling convention for rvalue reference params
In C++, we always expected to invoke the dtor for moved-from objects.
This is not the case for swift. Fortunately, @inCxx calling convention
is already expressing that the caller supposed to destroy the object.
This fixes the missing dtor calls when calling C++ functions taking
rvalue references. Fixes #77894.

rdar://140786022
2025-03-03 11:47:12 +00:00
Michael Gottesman
7e350bb4ce Revert "[concurrency] Add Concurrent/ConcurrentUnsafe and use it instead of ActorIsolation::Nonisolated."
This reverts commit 0cb64638d0.
2025-02-06 14:05:06 -08:00
Michael Gottesman
f05f08c2c0 Revert "[concurrency] Make ActorIsolation::Nonisolated -> CallerIsolationInheriting and delete CallerIsolationInheriting."
This reverts commit 543b1e6ca3.
2025-02-06 14:04:30 -08:00
Michael Gottesman
543b1e6ca3 [concurrency] Make ActorIsolation::Nonisolated -> CallerIsolationInheriting and delete CallerIsolationInheriting. 2025-02-03 10:56:08 -08:00
Michael Gottesman
0cb64638d0 [concurrency] Add Concurrent/ConcurrentUnsafe and use it instead of ActorIsolation::Nonisolated.
This is just the first part of a larger transition.
2025-02-03 10:56:06 -08:00
Joe Groff
44b431690d SILGen: Materialize addressor bases for the formal access scope of the entire access.
The return pointer may point into the materialized base value, so if the base needs
materialization, ensure that materialization covers any futher projection of the
value.
2025-01-31 18:08:53 -08:00
Joe Groff
23747bbb8e SILGen: Coordinate access strategy determination with actual codegen in findStorageReferenceExprForMoveOnly.
Looking at the AST-level `getReadImpl` doesn't always correspond to what
accessor SILGen prefers to use, due to resilience, ABI rules, and other
concerns. In findStorageReferenceExprForMoveOnly, when determining whether
a storage reference is borrowable, use the same logic as SILGenLValue actually
uses to determine what storage or accessor access strategy to use.

Fixes rdar://142509673
2025-01-10 16:12:09 -08:00
Joe Groff
a25edb2185 Make Builtin.addressForBorrow work with addressable parameters. 2025-01-02 21:33:55 -08:00
Joe Groff
5c4406b5e8 Add an @_addressableForDependencies type attribute.
This attribute makes it so that a parameter of the annotated type, as well as
any type structurally containing that type as a field, becomes passed as
if `@_addressable` if the return value of the function has a dependency on
the parameter. This allows nonescapable values to take interior pointers into
such types.
2025-01-02 21:33:51 -08:00
Michael Gottesman
56f38c4172 [concurrency] Add support in SILGen/SIL for emitting and calling implicit leading parameters for CallerInheritingIsolation isolation.
This does not change region isolation yet to recognize these as effectively
nonisolated.
2025-01-02 13:18:54 -08:00
Michael Gottesman
5d4239af57 [concurrency] Add new isolation kind CallerIsolationInheriting.
Right now it is basically a version of nonisolated beyond a few simple cases
like constructors/destructors where we are pretty sure we want to not support
this.

This is part of my bringup strategy for changing nonisolated/unspecified to be
caller isolation inheriting.
2025-01-02 13:18:30 -08:00
Andrew Trick
b56a787c74 SILGen: emit mark_dependence for unsafeAddress
Fixes a correctness issue with unsafe addressors: `unsafeAddress` and
`unsafeMutableAddress`. Previously, the resulting `Unsafe[Mutable]Pointer` did
not depend on `self`, meaning that the compiler is allowed to destroy `self`
before any uses of the pointer. This happens to be valid for
`UnsafePointer.pointee` because, in that case, `self` does not have a lifetime
anyway; the correctness burden was on the programmer to use
`withExtendedLifetime` around all uses of `self`.

Now, unsafe addressors can be used for arbitrary `Self` types.

This also enables lifetime dependence diagnostics when the addressor points to a
`~Escapable` type.

Addressors can now be used as an implementation of borrowed properties.
2024-12-17 09:53:02 -08:00
Joe Groff
3c0b08dbcb Prototype an @_addressable attribute that puts an argument at a stable address.
Many APIs using nonescapable types would like to vend interior pointers to their
parameter bindings, but this isn't normally always possible because of representation
changes the caller may do around the call, such as moving the value in or out of memory,
bridging or reabstracting it, etc. `@_addressable` forces the corresponding parameter
to be passed indirectly in memory, in its maximally-abstracted representation.
[TODO] If return values have a lifetime dependency on this parameter, the caller must
keep this in-memory representation alive for the duration of the dependent value's
lifetime.
2024-12-03 20:39:23 -08:00
Michael Gottesman
ebcab8f2ee [silgen] Change ArgEmitter to use an OptionSet before I add another option.
NFCI.
2024-11-15 15:14:32 -08:00
Nate Chandler
a3d607965b [CoroutineAccessors] Fix begin_apply for protos.
When the CoroutineAccessors feature is enabled, `begin_apply`
instructions produce an additional result representing the allocation
done by the callee.  Fix a couple of cases where this additional result
was not being handled.
2024-10-29 14:24:35 -07:00
Hamish Knight
7c3f965578 Merge pull request #76979 from hamishknight/regex-request
Requestify regex pattern parsing
2024-10-12 19:19:09 +01:00
Hamish Knight
6a435960b7 Requestify regex pattern parsing
Instead of doing the pattern parsing in both the
C++ parser and ASTGen, factor out the parsing into
a request that returns the pattern to emit, regex
type, and version. This can then be lazily run
during type-checking.
2024-10-11 19:25:58 +01:00
Nate Chandler
71239d6357 [CoroutineAccessors] SIL represents callee alloc.
When its operand has coroutine kind `yield_once_2`, a `begin_apply`
instruction produces an additional value representing the storage
allocated by the callee.  This storage must be deallocated by a
`dealloc_stack` on every path out of the function.  Like any other stack
allocation, it must obey stack discipline.
2024-10-11 08:25:03 -07:00
Nate Chandler
11b5d27594 [CoroutineAccessors] Add flag to observe errors.
Temporarily allow the legacy behavior of allowing caller coroutine
accessors to observe errors (i.e. by executing no code after the yield
if the caller threw an error) behind the
CoroutineAccessorsUnwindOnCallerError flag.
2024-10-11 08:25:02 -07:00
Nate Chandler
8972d6214c [CoroutineAccessors] Don't observe caller errors.
Experience with `_modify`/`_read` has shown that it is never desireable
to cleanup differently based on whether the caller has thrown.  Emit an
`end_apply` in either case.
2024-10-11 08:25:02 -07:00
John McCall
af8115ffa3 Hop to the current isolation properly in delegating async actor initializers.
This requires two major changes.

The first is that we need to teach SILGen that the isolation of an initializer
is essentially dynamic (as far as SILGen is concerned) --- that it needs to emit
code in order to get the isolation reference.  To make this work, I needed to
refactor how we store the expected executor of a function so that it's not
always a constant value; instead, we'll need to emit code that DI will lower
properly.  Fortunately, I can largely build on top of the work that Doug previously
did to support #isolation in these functions.  The SIL we emit here around delegating
initializer calls is not ideal --- the breadcrumb hop ends up jumping to the
generic executor, and then DI actually emits the hop to the actor.  This is a little
silly, but it's hard to eliminate without special-casing the self-rebinding, which
honestly we should consider rather than the weirdly global handling of that in
SILGen today.  The optimizer should eliminate this hop pretty reliably, at least.

The second is that we need to teach DI to handle the pattern of code we get in
delegating initializers, where the builtin actually has to be passed the self var
rather than a class reference.  This is because we don't *have* a class reference
that's consistently correct in these cases.  This ended up being a fairly
straightforward generalization.

I also taught the hop_to_executor optimizer to skip over the initialization of
the default-actor header; there are a lot of simple cases where we still do emit
the prologue generic-executor hop, but at least the most trivial case is handled.
To do this better, we'd need to teach this bit of the optimizer that the properties
of self can be stored to in an initializer prior to the object having escaped, and
we don't have that information easily at hand, I think.

Fixes rdar://87485045.
2024-10-04 22:23:00 -04:00
Nate Chandler
091368ba21 [CoroutineAccessors] Added read.
The name is a placeholder for the mutating single-yield coroutine
accessor.
2024-09-26 18:10:39 -07:00
Slava Pestov
375363a473 AST: Move global conformance lookup entry points to ConformanceLookup.h 2024-08-08 23:35:58 -04:00
Allan Shortlidge
14200e412c SILGen/stdlib: Remove _diagnoseUnavailableCodeReached_aeic().
It should no longer be necessary to provide an `@_alwaysEmitIntoClient` version
of `_diagnoseUnavailableCodeReached()`. This workaround was originally added to
provide compatibility with projects that were misconfigured to compile against
a newer stdlib but link against an older one.

Resolves rdar://119892482.
2024-07-11 14:53:03 -07:00
Slava Pestov
977b444eb3 AST: Add a new overload of getContextSubstitutionMap() 2024-07-10 13:28:26 -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
Akira Hatanaka
42bc49d3fe Add a new parameter convention @in_cxx for non-trivial C++ classes that are passed indirectly and destructed by the caller (#73019)
This corresponds to the parameter-passing convention of the Itanium C++
ABI, in which the argument is passed indirectly and possibly modified,
but not destroyed, by the callee.

@in_cxx is handled the same way as @in in callers and @in_guaranteed in
callees. OwnershipModelEliminator emits the call to destroy_addr that is
needed to destroy the argument in the caller.

rdar://122707697
2024-06-27 09:44:04 -07:00
Slava Pestov
bb97eaacf2 SILGen: Fix emission of 'modify' witness when all generic params are concrete
Fixes https://github.com/swiftlang/swift/issues/74648.
2024-06-24 17:38:59 -04:00
Michael Gottesman
f781ad366d [sending] Compensate in SILGen for a typechecker bug where we do not properly mark actor isolated async closures passed to an inheritActorContext argument as being Sendable.
The reason why I am fixing this is that otherwise, we get a warning when one
creates an actor isolated closure and pass it into a task, e.x.:

```swift
@MainActor func test() {
  // We would get a warning on the closure below saying that we are sending
  // a closure that is MainActor isolated.
  Task {
    ...
  }
}
```
2024-06-21 02:24:03 -07:00
Tim Kientzle
1098054291 Merge branch 'main' into tbkka-assertions2 2024-06-18 17:52:00 -07:00