Commit Graph

62 Commits

Author SHA1 Message Date
Slava Pestov
3196164341 Sema: Fix request cycle with isolation inference
Fixes rdar://129024926.
2024-06-03 13:41:42 -04:00
Michael Gottesman
06c32d74ff [region-isolation] Teach SIL isolation inference how to infer applies isolation from their callee's isolation.
This fixes a few issues I missed in the past bit of commits.

I need to fix one issue around async let, but I am going to fix it when I do a
sweep across async let.
2024-05-28 17:31:09 -07:00
Holly Borla
1ec5808119 [Availability] Diagnose unavailable conformances in UnderlyingToOpaqueExpr.
Change the availability checker to check substitution maps of underlying
values for opaque result types to diagnose unavailable conformances. This
change also makes sure `Sendable` availability diagnostics are errors in
Swift 6 mode.
2024-05-16 21:14:06 -07:00
Holly Borla
e044e3d390 [Concurrency] Implicit global actor attributes imply Sendable.
Sendable derivation was only checking for explicit global actor
attributes. It should also account for inferred global actor
attributes, e.g. from protocol conformances.
2024-05-15 22:28:47 -07:00
Holly Borla
29b8e6e6b0 [Concurrency] Un-gate implicit nonisolated access to struct vars and isolated
subclassing.

These features are additive, and they don't need to be gated behind the
`GlobalActorIsolatedTypesUsability` upcoming feature. The other inference
changes, including `@Sendable` inference for global-actor-isolated function
types, and global-actor inference on protocol refinements, remain gated
behind the upcoming flag.
2024-05-14 11:43:25 -07:00
Michael Gottesman
50c2d678f2 [region-isolation] When inferring isolation for an argument, handle non-self isolated parameters as well as self parameters that are actor isolated.
As part of this I went through how we handled inference and rather than using a
grab-bag getActorIsolation that was confusing to use, I created split APIs for
specific use cases (actor instance, global actor, just an apply expr crossing)
that makes it clearer inside the SILIsolationInfo::get* APIs what we are
actually trying to model. I found a few issues as a result and fixed most of
them if they were small. I also fixed one bigger one around computed property
initializers in the next commit. There is a larger change I didn't fix around allowing function
ref/partial_apply with isolated self parameters have a delayed flow sensitive
actor isolation... this will be fixed in a subsequent commit.

This also fixes a bunch of cases where we were printing actor-isolated instead
of 'self' isolated.

rdar://127295657
2024-05-10 15:33:44 -07:00
Michael Gottesman
f64f2529fb [region-isolation] Some more diagnostic wordsmithing.
rdar://127580781
2024-05-06 19:20:07 -07:00
Michael Gottesman
e4db879112 [region-isolation] Some more diagnostic wordsmithing.
rdar://127580781
2024-05-06 12:09:10 -07:00
Michael Gottesman
f02172a323 [region-isolation] Change terminology to use the term 'risk' instead of could. 2024-05-05 18:01:05 -07:00
Michael Gottesman
a933c14b77 [region-isolation] Only print the type of region that a value is in if it is not disconnected.
Just another diagnostic tweak.
2024-05-05 18:01:05 -07:00
Michael Gottesman
0b761109e2 [region-isolation] If we can infer the callee's name, use that instead of just saying 'callee'.
I also wordsmithed the error message to use the term 'risk' instead of less
negative terms.
2024-05-05 18:01:05 -07:00
Michael Gottesman
699692bd39 [region-isolation] Change diagnostics from using the term transferring -> sending.
rdar://127580781
2024-05-05 18:00:54 -07:00
Holly Borla
38cc9511ee [Concurrency] Stage in new Async{Throwing}Stream.init(unfolding:) errors
as warnings.

Marking the closure parameter to these inits as `@Sendable` changed the
inferred isolation of closure arguments in actor-isolated contexts, which
caused new effects checker errors when accessing isolated properties and
methods without `await`. Mark these `init`s as `@preconcurrency`, and fix
the effects checker to downgrade those errors to warnings when the context
of the call is `@preconcurrency`.
2024-04-26 18:09:37 -07:00
Michael Gottesman
a56d0f5ded [region-isolation] Tweak the main transferring diagnostic.
Specifically, I am transforming it from "may cause a race" -> "may cause a data
race". Adding data is a small thing, but it adds a bunch of nice clarity.
2024-04-06 22:50:26 -07:00
Michael Gottesman
b3e837c16c [region-isolation] Enable region isolation by default with strict-concurrency.
I added a disable flag -disable-region-based-isolation-with-strict-concurrency
so that we do not need to update the current tests. It is only available when
asserts are enabled to ensure users cannot use it.

rdar://125918028
2024-04-04 13:07:32 -07:00
Michael Gottesman
2f9b519758 [region-isolation] Wordsmith "{access,use} here could race".
I am doing this since it isn't always going to be an access. We may not have
memory. We are talking about uses here!

I was able to just use sed so it was an easy fix.
2024-03-23 17:19:09 -07:00
Michael Gottesman
357a53ab48 [region-isolation] Clean up use after transfer error to use the dynamic isolation information of the transfered operand value in its diagnostic message.
As an example of the change:

-  // expected-note @-1 {{'x' is transferred from nonisolated caller to main actor-isolated callee. Later uses in caller could race with potential uses in callee}}
+  // expected-note @-1 {{transferring disconnected 'x' to main actor-isolated callee could cause races in between callee main actor-isolated and local nonisolated uses}}

Part of the reason I am doing this is that I am going to be ensuring that we
handle a bunch more cases and I wanted to fix this diagnostic before I added
more incaranations of it to the tests.
2024-03-22 13:12:51 -07:00
Michael Gottesman
7368de2a20 [region-isolation] Remove variable_defined_here from named use after transfer error.
This is just a pseudo-why are these two things part of the same region error. I
am going to remove this for now and the proper form of this diagnostic will come
back when I land the region history functionality.
2024-03-19 13:19:08 -07:00
Michael Gottesman
afbcf85727 [region-isolation] Change named transfer non transferable error to use the dynamic merged IsolationRegionInfo found during dataflow.
I also eliminated the very basic "why is this task isolated" part of the warning
in favor of the larger, better, region history impl that I am going to land
soon. The diagnostic wasn't that good in the first place and also was fiddly. So
I removed it for now.

rdar://124960994
2024-03-18 12:13:36 -07:00
Michael Gottesman
8f149b4d0e [Concurrency] Change TypeBase::isSendable() to return false for unavailable conformances.
rdar://122174496
2024-03-11 11:29:52 -07:00
Michael Gottesman
99e3f7fb13 [region-isolation] Make RegionBasedIsolation an upcoming feature for swift 6.
To make the tests pass, I had to teach sil-opt how to setup upcoming features
since it did not know how to parse them.

rdar://124100266
2024-03-05 15:15:14 -08:00
Holly Borla
75858cd211 [Concurrency] Allow capturing nonisolated(unsafe) local variables in
`@Sendable` closures.
2024-03-04 15:04:46 -08:00
Holly Borla
7cc2afd0d4 [Concurrency] Fix an issue where nonisolated(unsafe) did not suppress
`Sendable` warnings on stored properties of `Sendable` types.
2024-03-04 14:46:24 -08:00
Michael Gottesman
f296529ee0 [region-isolation] Change the main race error to be "transferring {could,may} cause a race"
As I was updating tests in the previous commit, I noticed that this would read better to me.
2024-02-22 13:50:06 -08:00
Michael Gottesman
1c193caedb [region-isolation] Eliminate more "call site passes self" warnings
I just did a full pass through. There were some cases around nonisolated
closures defined in methods and global actor isolated things where we are now
emitting the wrong message. I am going to fix that in subsequent commits.
2024-02-22 13:50:06 -08:00
Michael Gottesman
7fe100a762 [region-isolation] Clean up the named value transfer warning. 2024-02-22 13:50:06 -08:00
Michael Gottesman
c25b39b003 [region-isolation] Fix a test.
This is because in 1e4fe6c4ea, we stopped
transfering the function callee as well.
2024-02-06 22:50:34 -08:00
Michael Gottesman
f87dbb5181 [region-isolation] Eliminate term "binding" from diagnostic. 2024-02-06 16:18:43 -08:00
Michael Gottesman
fef1b5e3df [region-isolation] Use VariableNameUtils to emit name diagnostics when possible.
A name diagnostic looks as follows:

Some notes:

1. VariableNameUtils still doesn't pattern match all cases. In the cases where
we fail to pattern match, I fall back to the old diagnostics. I am going to be
adding tests/etc specific to VariableNameUtils in a later patch.

2. I took this as an opportunity to begin preparing to change the diagnostics
into diags of the following form:

a. The main diagnostic is changed to something short "transferring non-Sendable
%0 could yield races with later accesses".

b. I added a longer note at the same location that explains that our caller is
in a specific isolation domain and our callee is isolated differently.

c. I added a note on the definition location of the identified value.
2024-02-06 13:42:35 -08:00
Michael Gottesman
f077e4a9d7 [region-isolation] Fix the call site or self error for values used in the same region as a function argument.
This is just good to do and also makes it so that in my test case for
assumeIsolated, I get a better msg.
2024-01-25 20:40:56 -08:00
Michael Gottesman
3641050675 [region-isolation] Wordsmith some diagnostics.
I tried to make them shorter and standardized on "; later accesses could race"
as the mark on a diagnostic that the problem was that a race was occuring
(before we had several small different variations).
2024-01-19 15:38:41 -08:00
Holly Borla
85cced7a14 [Concurrency] Diagnose concurrency violations in derived conformances at valid
source locations.
2023-12-20 06:55:10 -08:00
Michael Gottesman
d1870c1ae3 [region-isolation] Fix return vs break logic error in the evaluator.
I am not sure if this would ever actually ever cause a bug... but we should be
consistent. The issue here is that in the evaluator switch when processing
transfers in certain cases when we emitted an early error due to an actor
derived value we were performing a transfer and other times we weren't. With
this patch:

1. We now consistently do not actually transfer the value if it is actor
derived. It is always illegal to transfer an actor derived value... so we know
that if we always just error on transferring it, we have a safe model.

2. The switch we breaking so that we could do an assert that we canonicalized
the modified PartitionOp. Rather than do that and allow for people to
potentially return, I moved the assert into a SWIFT_DEFER and added an assert in
the continuation of the switch. This means that if an author ever breaks out of
the switch, they will hit the assert implying that the author in all cases must
explicitly return within the case statement guaranteeing this mistake does not
happen again.
2023-11-28 09:48:44 -08:00
Michael Gottesman
c0b3efedbf [region-isolation] Instead of using the complex logic to emit all possible requires... just emit a diagnostic on the first require that we see.
This involved me removing the complex logic for emitting diagnostics we have
today in favor of a simple diagnostic that works by:

1. Instead of searching for transfer instructions, we use the transfer
instruction that we propagated by the dataflow... so there is no way for us to
possible not identify a transfer instruction... we always have it.

2. Instead of emitting diagnostics for all requires, just emit a warning for the
first require we see along a path. The reason that we need to do this is that in
certain cases we will have multiple "require" instructions from slightly
different source locations (e.x.: differing by column) but on the same line. I
saw this happen specifically with print where we treat stores into the array as
a require as well as the actual call to print itself where we pass the array.

An additional benefit of this is that this allowed me to get rid of the
cache of already seen require instructions. By doing this, we now emit errors
when the same apply needs to be required by different transfer instructions for
different arguments.

NOTE: I left in the original implementation code to make it easier to review
this new code. I deleted it in the next commit. Otherwise the git diff for this
patch is too difficult to read.
2023-11-15 18:58:06 -08:00
Michael Gottesman
389078d4fa [region-isolation] Remove count of emitted diagnostics from main diagnostic.
These are not actionable to the user.
2023-11-10 12:48:45 -08:00
Michael Gottesman
dc8e4794a8 [region-isolation] Make sure that we treat Sendable functions as Sendable.
The reason for this issue is that we were originally checking in our NonSendable
type oracle just if a type conformed to Sendable. But function types do not
conform to protocols, so this would fail for protocols. To fix this, I added
some helper methods to call swift::isSendableType on SILType, called that in our
oracle, and then I added support in swift::isSendableType for both
SILFunctionType and AnyFunctionType so that we correctly handle them depending
on their sendability.

There was also a question if we also handled function conversions correctly. I
added a test case that shows that we do not error in either of the cases.

Another nice aspect of this change is that we no longer need to stash a pointer
to a looked up Sendable protocol to perform the check since that just happens
naturally inside SILType::isSendable() when it calls isSendableType. This is a
better separation of concerns.

rdar://116525224
2023-10-27 11:13:35 -07:00
Michael Gottesman
cb46851194 [region-isolation] Rename the experimental feature to RegionBasedIsolation.
This ensures that the pass is called TransferNonSendable but the experimental
feature is RegionBasedIsolation.
2023-10-26 12:01:44 -07:00
Michael Gottesman
0bad8f9b67 [region-isolation] Rename SendNonSendable.cpp -> TransferNonSendable.cpp. 2023-10-26 12:01:44 -07:00
Pavel Yaskevich
445b9c7a61 [Tests] NFC: Add tests to make sure that pointers are non-Sendable 2023-10-10 21:42:55 -07:00
Holly Borla
fb06817d1d [NFC][Concurrency] Separate out an error test case from sendable_checking.swift to
allow testing diagnostics from the SendNonSendable SIL pass.
2023-10-05 08:35:20 -07:00
Holly Borla
2159c0c58a [Concurrency] Diagnose Sendable violations in captures of local functions. 2023-09-21 21:11:34 -07:00
Holly Borla
553fe45def [Concurrency] Don't check parameter and result types of function references for
Sendability.

Sendability of function references depends only on captures, and applies when the
function itself is passed across isolation boundaries. Parameter and result
values can only cross isolation boundaries when the function is called.
2023-09-09 16:27:47 -07:00
Holly Borla
9b16d47dae [Concurrency] Add a test case for passing a non-Sendable argument to a
nonisolated closure in an actor-isolated context.
2023-09-09 15:52:37 -07:00
Michael Gottesman
b53af9419c [send-non-sendable] Add REQUIRES: asserts to concurrency tests that use SendNonSendable. 2023-08-31 19:25:23 -07:00
Michael Gottesman
026f1735b5 [send-non-sendable] Update concurrency tests so that we run them in all concurrency modes as appropriate.
This means that:

1. In test cases where minimal is the default (swift 5 without
-warn-concurrency), I added RUN lines for targeted, complete, and complete +
sns.

2. In test cases where complete is the default (swift 6, -warn-concurrency,
specified complete with -strict-concurrency), I added a send non-sendable run
line.

In each of these cases, I added additional expected-* lines as appropriate so
the tests can compile in each mode successfully.
2023-08-30 13:40:17 -07:00
Holly Borla
84eaeb766f [Concurrency] Diagnose non-Sendable 'self' arguments crossing actor isolation
boundaries in member reference expressions.
2023-08-22 21:28:33 -07:00
Holly Borla
6a4c7891b8 [Concurrency] Diagnose non-sendable 'self' arguments crossing isolation boundaries. 2023-08-04 09:32:41 -07:00
Becca Royal-Gordon
84592b8f8e Adopt %kind in Sendable checking diagnostic
Causes minor changes in diagnostic text.
2023-07-19 13:06:51 -07:00
Doug Gregor
7c45c7d850 [Concurrency] Check sendability of the original argument expressions in a call
When checking for the sendability in a call, use the sendability of the
original argument expressions rather than the parameter type, looking
through any implicit conversions that might remove `Sendable`.

Fixes rdar://110763694 / FB12343467.
2023-06-26 18:25:24 -07:00
Doug Gregor
d78a5edb99 Handle all isolation checking for function calls in one place
Isolation checking for calls had two separate implementation places:
one that looked at the declaration being called (for member
declarations) and one that worked on the actual call expression. Unify
on the latter implementation, which is more general and has access to
the specific call arguments. Improve diagnostics here somewher so we
don't regress in that area.

This refactoring shouldn't change the actual semantics, but it makes
upcoming semantic changes easier.
2023-06-26 13:39:56 -07:00