Commit Graph

58 Commits

Author SHA1 Message Date
Michael Gottesman
348720b830 Revert "[rbi] Treat a partial_apply as nonisolated(unsafe) if all of its captures are nonisolated(unsafe)."
This reverts commit 6ce93c8a37.
2025-07-16 09:58:09 -07:00
Michael Gottesman
f920aba2f9 [rbi] Use interned StringRefs for diagnostics instead of SmallString<64>.
This makes the code easier to write and also prevents any lifetime issues from a
diagnostic outliving the SmallString due to diagnostic transactions.

(cherry picked from commit 010fa39f31)
2025-07-09 12:25:06 -07:00
Michael Gottesman
85fafa5b1e [rbi] Begin tracking if a function argument is from a nonisolated(nonsending) parameter and adjust printing as appropriate.
Specifically in terms of printing, if NonisolatedNonsendingByDefault is enabled,
we print out things as nonisolated/task-isolated and @concurrent/@concurrent
task-isolated. If said feature is disabled, we print out things as
nonisolated(nonsending)/nonisolated(nonsending) task-isolated and
nonisolated/task-isolated. This ensures in the default case, diagnostics do not
change and we always print out things to match the expected meaning of
nonisolated depending on the mode.

I also updated the tests as appropriate/added some more tests/added to the
SendNonSendable education notes information about this.

(cherry picked from commit 14634b6847)
2025-07-09 12:25:03 -07:00
Michael Gottesman
ca8cdc1fd7 [rbi] Thread through a SILFunction into print routines so we can access LangOpts.Features so we can change how we print based off of NonisolatedNonsendingByDefault.
We do not actually use this information yet though... This is just to ease
review.

(cherry picked from commit 4433ab8d81)
2025-07-09 12:24:17 -07:00
Michael Gottesman
0ec2527bdc [rbi] Wrap use ActorIsolation::printForDiagnostics with our own SILIsolationInfo::printActorIsolationForDiagnostics.
I am doing this so that I can change how we emit the diagnostics just for
SendNonSendable depending on if NonisolatedNonsendingByDefault is enabled
without touching the rest of the compiler.

This does not actually change any of the actual output though.

(cherry picked from commit 4ce4fc4f95)
2025-07-09 12:24:17 -07:00
Michael Gottesman
f24ff98f9a [nonisolated-nonsending] Make the AST not consider nonisolated(nonsending) to be an actor isolation crossing point.
We were effectively working around this previously at the SIL level. This caused
us not to obey the semantics of the actual evolution proposal. As an example of
this, in the following, x should not be considered main actor isolated:

```swift
nonisolated(nonsending) func useValue<T>(_ t: T) async {}
@MainActor func test() async {
  let x = NS()
  await useValue(x)
  print(x)
}
```

we should just consider this to be a merge and since useValue does not have any
MainActor isolated parameters, x should not be main actor isolated and we should
not emit an error here.

I also fixed a separate issue where we were allowing for parameters of
nonisolated(nonsending) functions to be passed to @concurrent functions. We
cannot allow for this to happen since the nonisolated(nonsending) parameters
/could/ be actor isolated. Of course, we have lost that static information at
this point so we cannot allow for it. Given that we have the actual dynamic
actor isolation information, we could dynamically allow for the parameters to be
passed... but that is something that is speculative and is definitely outside of
the scope of this patch.

rdar://154139237
(cherry picked from commit c12c99fb73)
2025-07-09 12:24:17 -07:00
Michael Gottesman
5361765212 Merge pull request #82428 from gottesmm/release/6.2-144111950
[6.2][rbi] Treat a partial_apply as nonisolated(unsafe) if all of its captures are nonisolated(unsafe).
2025-07-09 11:06:52 -07:00
Doug Gregor
1d86ed5f15 [Region analysis] Simplify logic for dynamic casts by making them less special
Extend and use SILIsolationInfo::getConformanceIsolation() for the dynamic
cast instructions.
2025-07-08 17:33:33 -07:00
Doug Gregor
066fa0b828 [Region isolation] Re-simplify handling of init_existential* instructions
Centralize the logic for figuring out the conformances for the various
init_existential* instructions in a SILIsolationInfo static method, and
always go through that when handling "assign" semantics. This way, we
can use CONSTANT_TRANSLATION again for these instructions, or a simpler
decision process between Assign and LookThrough.

The actually undoes a small change made earlier when we stopped looking
through `init_existential_value` instructions. Now we do when there are
no isolated conformances.
2025-07-08 17:33:33 -07:00
Doug Gregor
22ca6b98e5 [Region isolation] Factor SILIsolationInfo creation into static methods
Better match the style of SILIsolationInfo by moving the code for determining
SILIsolationInfo from conformances or dynamic casts to existentials into
static `getXYZ` methods on SILIsolationInfo.

Other than adding an assertion regarding disconnected regions, no
intended functionality change.
2025-07-08 17:33:33 -07:00
Doug Gregor
6637960bdc [SE-0470] Track the potential introduction of isolated conformances in regions
When we introduce isolation due to a (potential) isolated conformance,
keep track of the protocol to which the conformance could be
introduced. Use this information for two reasons:

1. Downgrade the error to a warning in Swift < 7, because we are newly
diagnosing these
2. Add a note indicating where the isolated conformance could be introduced.

(cherry picked from commit 02c34bb830)
2025-07-08 17:32:06 -07:00
Michael Gottesman
6ce93c8a37 [rbi] Treat a partial_apply as nonisolated(unsafe) if all of its captures are nonisolated(unsafe).
rdar://144111950
(cherry picked from commit 010443c854)
2025-07-08 13:35:52 -07:00
Allan Shortlidge
4c34f08b99 SILOptimizer: Fix an unused variable warning. 2025-06-05 15:40:30 -07:00
Michael Gottesman
7b434c2747 [rbi] Lookthrough an invocation of DistributedActor.asLocalActor when determining actor instances.
In this case, what is happening is that in SILGen, we insert implicit
DistributedActor.asLocalActor calls to convert a distributed actor to its local
any Actor typed form. The intention is that the actor parameter and result are
considered the same... but there is nothing at the SIL level to enforce that. In
this commit, I change ActorInstance (the utility that defines actor identity at
a value level) to look through such a call.

I implemented this by just recognizing the decl directly. We already do this in
parts of SILGen, so I don't really see a problem with doing this. It also
provides a nice benefit that we do not have to modify SILFunctionType to
represent this or put a @_semantic attribute on the getter.

NOTE: Generally, Sema prevents us from mixing together different actors. In this
case, Sema does not help us since this call is inserted implicitly by the
distributed actor implementation in SILGen. So this is not a problem in general.

rdar://152436817
(cherry picked from commit 331626e6fa)
2025-06-03 09:05:32 -07:00
Michael Gottesman
636aa31192 [sil-isolation-info] When determining isolation of a function arg, use its VarDecl.
Otherwise, we can be inconsistent with isolations returned by other parts of the
code. Previously we were just treating it always as self + nom decl, which is
clearly wrong if a type is not self (e.x.: if it is an isolated parameter).

rdar://135459885
(cherry picked from commit 0ece31e4f6)
2025-04-21 10:57:48 -07:00
Andrew Trick
19c8bd625f Add FIXMEs for calls to liveness that fail to check pointer escapes. 2025-03-02 23:51:34 -08:00
Michael Gottesman
f01d27514d [rbi] Convert an assert to an if check.
This also fixes a case where we would have inferred the wrong isolation (which
the assert caught). I think we missed this since it only comes up with final
classes.

rdar://142568522
2025-02-25 13:39:09 -08:00
Doug Gregor
37d71f362e Add StrictSendableMetatypes to require Sendable requirements on metatypes
Introduce a new experimental feature StrictSendableMetatypes that stops
treating all metatypes as `Sendable`. Instead, metatypes of generic
parameters and existentials are only considered Sendable if their
corresponding instance types are guaranteed to be Sendable.

Start with enforcing this property within region isolation. Track
metatype creation instructions and put them in the task's isolation
domain, so that transferring them into another isolation domain
produces a diagnostic. As an example:

    func f<T: P>(_: T.Type) {
      let x: P.Type = T.self
      Task.detached {
        x.someStaticMethod() // oops, T.Type is not Sendable
      }
    }
2025-02-12 20:21:57 -08: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
649952e3d9 Revert "[concurrency] Convert ActorIsolation::isConcurrentUnsafe -> isUnsafe."
This reverts commit 95623c691f.
2025-02-06 14:04:29 -08:00
Michael Gottesman
95623c691f [concurrency] Convert ActorIsolation::isConcurrentUnsafe -> isUnsafe.
This ensures it works for both nonisolated(unsafe) and concurrent(unsafe).
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
Michael Gottesman
c061ee72ec [rbi] Ensure that we infer isolation correctly for direct sending results.
I did the correct thing for indirect parameters, but did not do the correct
thing for direct parameters. This is now fixed with some tests to boot.

rdar://141631655
2025-01-07 14:33:39 -08:00
Michael Gottesman
b74be1433a [concurrency] Fix a small bug in RBI that caused us to not recognize that an @MainActor SILIsolationInfo is from an @MainActor. 2025-01-02 13:18:55 -08:00
Michael Gottesman
067dbadfef [region-isolation] Add a print command that emits errors of the form "*-isolated code" or "code in the current task"
This makes it so that one does not need to deal with the differences in text in
between the task isolated case and the actor isolated case. This is done by
swallowing the entire part of this message in one method rather than having the
caller do the work.
2024-10-25 16:57:55 -07:00
Michael Gottesman
a0088327d4 [concurrency] Represent a SILFunction without isolation as std::optional<ActorIsolation> instead of ActorIsolation::Unspecified.
The reason why is that we want to distinguish inbetween SILFunction's that are
marked as unspecified by SILGen and those that are parsed from textual SIL that
do not have any specified isolation. This will make it easier to write nice
FileCheck tests against SILGen output on what is the inferred isolation for
various items.

NFCI.
2024-09-18 11:23:22 -07:00
Slava Pestov
f2fff10cc9 SIL: Synchronous local functions don't need to capture the isolation parameter
Also, move this rule from the computation of lowered captures in SIL,
to the computation of AST captures in Sema. This allows us to
correctly handle the case where an async function nests inside a
sync function. It also removes a special case that was added recently
to cope with a generic `self` type.

Fixes rdar://129366819.
2024-08-28 17:03:49 -04:00
Michael Gottesman
00519669af Merge pull request #76076 from gottesmm/rdar134623227
[region-isolation] Treat sending indirect_results as disconnected even if it is a return value of an actor isolated function.
2024-08-25 08:02:25 -04:00
Michael Gottesman
3e27bfc03b [region-isolation] Treat sending indirect_results as disconnected even if it is a return value of an actor isolated function.
rdar://134623227
2024-08-24 14:02:41 -04:00
Michael Gottesman
49eee05647 [region-isolation] Treat as Sendable values that are meant to be ignored since they are marked with preconcurrency
rdar://133531625
2024-08-24 13:14:39 -04:00
Michael Gottesman
a75501e985 [region-isolation] Eliminate the last non-SIL test case usage of SILIsolationInfo::getWithIsolationCrossing().
The reason that I am changing this code is that getWithIsolationCrossing is a
bad API that was being used to infer actor isolation straight from an ApplyExpr
without adding an actor instance. This can cause us to reject programs
unnecessarily if we in other parts of the code correctly infer the SILValue
actor instance for the isolation.

Rather than allow for that, I am removing this code and I improved the rest of
the pattern matching here to ensure that we handled that with the normal actor
instance inferring code. This will prevent this type of mismerge from happening
by mistake. I fixed up the changes in the test cases.

The only usage of this left is for ApplyIsolationCrossings parsed straight from
SIL that we use only when testing. This is safe since if a test writer is using
the parsed SIL in this manner, they can make sure that mismerges do not happen.
2024-07-31 13:10:01 -07:00
Nate Chandler
0cad62521e [Test] Underscored sil_isolation_info_inference.
Use underscores rather than hyphens so that text editors understand the
name as a single word.
2024-07-25 13:52:21 -07:00
Michael Gottesman
75152e7834 [region-isolation] Teach region isolation how to handle cases where due to compiler bugs, we have a function isolated to self without an isolated parameter.
Closures generally only inherit actor instance isolation if they directly
capture state from the actor instance. In this case, for some reason that is not
true, so we hit an assert that assumes that we will only see a global actor
isolated isolation.

Region Isolation should be able to handle code even if the closure isolation
invariant is violated by the frontend. So to do this, I am introducing a new
singleton actor instance to represent the isolation of a defer or closure
created in an actor instance isolated method. The reason why I am using a
singleton is that closures and defer are not methods so we do not actually know
which parameter is 'self' since it isn't in the abi. But we still need some
value to represent the captured values as belonging to. To square this circle, I
just did what we have done in a similar situation where we did not have a value:
(ActorAccessorInit). In that case, we just use a sentinel to represent the
instance (NOTE: This is represented just via a kind so ActorInstances that are
operator== equal will not &value equal since we are just using a kind).
2024-07-18 21:28:22 -07:00
Michael Gottesman
c986af7695 [region-isolation] Be more aggressive about not looking through Sendable values when getting underlying objects.
Otherwise, in cases like the following, we look through the load to x.boolean
and think that the closure is actually capturing x instead of y:

```swift
func testBooleanCapture(_ x: inout NonSendableKlass) {
  let y = x.boolean
  Task.detached { @MainActor [z = y] in
    print(z)
  }
}
```

rdar://131369987
2024-07-09 14:38:47 -07:00
Michael Gottesman
20acc503c2 Fix three typos in comments. NFC. 2024-07-08 11:01:44 -07:00
Michael Gottesman
9f8e5e8998 Merge pull request #75016 from gottesmm/pr-d4640c7e2919faea7bc7eb96ebd8af643108e2d8
[region-isolation] When determining isolation from a class_method, check for global actor isolation first.
2024-07-07 09:58:24 -07:00
Michael Gottesman
62c6de7713 [region-isolation] Reuse ActorInstance::lookThroughInsts when computing the actor instance for SILIsolationInfo purposes.
We are already using this routine in other parts of TransferNonSendable to
ensure that we look through common insts that SILGen inserts that do not change
the actual underlying actor instance that we are using. In this case, I added
support for casts, optional formation, optional extraction, existential ref
initialization.

As an example of where this came up is the following test case where we fail to
look through an init_existential_ref.

```swift
public actor MyActor {
  private var intDict: [Int: Int] = [:]

  public func test() async {
    await withTaskGroup(of: Void.self) { taskGroup in
      for (_, _) in intDict {}
      await taskGroup.waitForAll() // Isolation merge failure happens here
    }
  }
}
```

I also added the ability to at the SIL level actual test out this merge
condition using the analysis test runner. I used this to validate that this
functionality works as expected in a precise way.

rdar://130113744
2024-07-06 23:02:11 -07:00
Michael Gottesman
6129f4e833 [region-isolation] Print out SILIsolationInfo's options on all printing routines for SILIsolationInfo.
Before we wouldn't print them in all situations and even more so a few of the
printing routines did not have it at all. This just adds a centralized
SILIsolationInfo::dumpOptions() method and then goes through all of the printing
helpers and changes them to use them as appropriate.
2024-07-06 23:02:11 -07:00
Michael Gottesman
0c254807bf [region-isolation] Allow for unapplied isolated parameter ownership.
Given a function or a partial_apply with an isolated parameter, we do not know
immediately what the actual isolation is of the function or partial_apply since
we do not know which instance will be applied to the function or partial_apply.

In this commit, I introduce a new bit into SILIsolationInfo that tracks this
information upon construction and allows for it to merge with ownership that has
the appropriate type and a specific instance. Since the values that created the
two isolations, will be in the same region this should ensure that the value is
only ever in a flow sensitive manner in a region with only one actor instance
(since regions with isolations with differing actor instances are illegal).
2024-07-06 23:02:11 -07:00
Michael Gottesman
0a06c00f47 [region-isolation] When determining isolation from a class_method, check for global actor isolation first.
Before this change in the following code, we would say that message is isolated to the actor instead of the global actor isolation of the actor's method:

```swift
class Message { ... }

actor MessageHolder {
  @MainActor func hold(_ message: Message) { ... }
}

@MainActor
func sendMessage() async {
    let messageHolder = MessageHolder()
    let message = Message()
    // We identified messageHolder.hold as being MessageHolder isolated
    // instead of main actor isolated.
    messageHolder.hold(message)
    Task { @MainActor in print(message) }
}
```

I also used this as an opportunity to simplify the logic in this part of the
code. Specifically, I had made it so that multiple interesting cases were
handled in the same conditional statement in a manner that it made it hard to
know which cases were actually being handled and why it was correct. Now I split
that into two separate if statements with comments that make it clear what we
are actually trying to pattern match against.

rdar://130980933
2024-07-06 23:01:21 -07:00
Michael Gottesman
6fe749626f [region-isolation] Add 'inout sending' diagnostics.
Specifically:

1. We error now if one transfers an 'inout sending' parameter and does not
reinitialize it before the end of the function.

2. We error now if one merges an 'inout sending' parameter into an actor
isolated region and do not reinitialize it with a non-actor isolated value
before the end of the function.

rdar://126303739
2024-07-02 16:21:44 -07:00
Michael Gottesman
f43911b30f [region-isolation] Given a .none #isolation parameter, infer the isolation from the callee rather than the isolated parameter.
Otherwise, we will have differing isolation from other parameters since
the isolations will look different since one will have the .none value
as an instance and the other will not have one and instead will rely on
the AST isolation info. That is the correct behavior here since we do
not actually have an actor here.

I also removed some undefined behavior in the merging code. The way the
code should work is that we should check if the merge fails and in such
a case emit an unknown pattern error... instead of not checking
appropriately on the next iteration and hitting undefined behavior.

rdar://130396399
2024-06-25 14:12:15 -07:00
Michael Gottesman
c01f551ebe [transfer-non-sendable] Teach SILIsolationInfo how to handle "look through instructions" when finding actor instances.
From the perspective of the IR, we are changing SILIsolationInfo such that
inferring an actor instance means looking at equivalence classes of values where
we consider operands to look through instructions to be equivalent to their dest
value. The result is that cases where the IR maybe puts in a copy_value or the
like, we consider the copy_value to have the same isolation info as using the
actor directly. This prevents a class of crashes due to merge failings. Example:

```swift
actor MyActor {
  init() async {

  init(ns: NonSendableKlass) async {
    self.k = NonSendableKlass()
    self.helper(ns)
  }

  func helper(_ newK: NonSendableKlass) {}
}
```

Incidently, we already had a failing test case from this behavior rather than
the one that was the original genesis. Specifically:

1. If a function's SILIsolationInfo is the same as the isolation info of a
SILValue, we assume that no transfer actually occurs.

2. Since we were taking too static of a view of actor instances when comparing,
we would think that a SILIsolationInfo of a #isolation parameter to as an
argument would be different than the ambient's function isolation which is also
that same one. So we would emit a transfer non transferrable error if we pass in
any parameters of the ambient function into another isolated function. Example:

```swift
actor Test {

  @TaskLocal static var local: Int?

  func withTaskLocal(isolation: isolated (any Actor)? = #isolation,
                     _ body: (consuming NonSendableValue, isolated (any Actor)?) -> Void) async {
    Self.$local.withValue(12) {

      // We used to get these errors here since we thought that body's isolation
      // was different than the body's isolation.
      //
      //  warning: sending 'body' risks causing data races
      //  note: actor-isolated 'body' is captured by a actor-isolated closure...
      body(NonSendableValue(), isolation)
    }
  }
}
```

rdar://129400019
2024-06-24 18:16:11 -07:00
Tim Kientzle
1098054291 Merge branch 'main' into tbkka-assertions2 2024-06-18 17:52:00 -07:00
Michael Gottesman
d032bc033a [region-isolation] Check if we have a self param before attempting to get the self type.
Otherwise in asserts builds we get an assert and in no-asserts we get a crash.

rdar://129975097
2024-06-18 13:39:20 -07:00
Michael Gottesman
f035590784 [region-isolation] Dont crash when processing global actor isolated init accessors.
This just means that I stopped treating it like an actor instance isolated
thing. This was fun to track down since ActorIsolation has a union in it that
was being misinterpreted, leading to memory corruption... my favorite! = ).

rdar://129256560
2024-06-11 22:15:37 -07: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
Michael Gottesman
c7124e431a [sending] Fix recent alloc_stack as indirect result isolation inference to infer disconnected if the alloc stack is used as a sending indirect result.
I also fixed an issue that I found where we were not substituting SILResultInfo
flags which was causing us to drop when substituting sil_sending. I added a
SILVerifier check to make sure that we do not break this again.
2024-06-01 23:25:16 -07:00
Michael Gottesman
0cd0868f52 [region-isolation] Recognize sil_isolated parameters that are global actors as global actors rather than actor instance.
The specific example I ran into was in sendable_continuation.swift where we were
passing in the @MainActor instance as a sil_isolated parameter. We were thinking
it was an actor instance so we emitted the wrong message.
2024-06-01 23:25:16 -07:00
Michael Gottesman
1d8ea84fa3 [region-isolation] When determining isolation of a full apply site... use the isolated parameter, not self.
This was ok in the small since most of the time we were processing a self
parameter as isolated... but that isn't always true...
2024-06-01 23:25:16 -07:00