Commit Graph

108 Commits

Author SHA1 Message Date
Michael Gottesman
4da340a30d Merge pull request #84728 from gottesmm/pr-c10808b54e513cf6f212aa5f01ede2a97d5a32a6
[concurrency] Import getters with completion handlers as nonisolated(nonsending).
2025-10-07 04:57:53 -07:00
Michael Gottesman
08f108c23b [concurrency] Import getters with completion handlers as nonisolated(nonsending).
Specifically, this means importing getters defined via swift_async_name. This
just ensures that they are treated just like any other imported objc async
function with completion handler.

rdar://156985950
2025-10-06 19:57:01 -07:00
Henrik G. Olsson
cbc0ec3b88 Add -verify-ignore-unrelated where necessary (NFC)
These are tests that fail in the next commit without this flag. This
does not add -verify-ignore-unrelated to all tests with -verify, only
the ones that would fail without it. This is NFC since this flag is
currently a no-op.
2025-10-04 14:19:52 -07:00
Pavel Yaskevich
b046efccc1 [Concurrency] Downgrade errors when isolated member is referenced from a preconcurrency context
Such references used to be downgraded until Swift 6 but since the
context is `@preconcurrency` it should be possible for API authors
to introduce concurrency annotations such as `@Sendable` without
breaking clients even when they are compiled in Swift 6 mode.

Resolves: rdar://157061896
2025-07-30 11:02:40 -07:00
Michael Gottesman
3ed4059a60 [sema] Change non-sendable -> non-Sendable in diagnostics.
This matches send non sendable but importantly also makes it clear that we are
talking about something that doesn't conform to the Sendable protocol which is
capitalized.

rdar://151802975
2025-05-22 11:37:58 -07:00
Anthony Latsis
eeb991122a [NFC] Switch from backticks to quotes in several diagnostics 2025-01-29 07:49:27 +00:00
Michael Gottesman
784d1932db Fix another test.
I tried to port this behavior to another test that validated the behavior using
RBI. When I tried to do so I ran into the issue that the behavior in this test
only reproduces in targeted mode not in complete mode which is required for rbi
to run.
2024-12-03 14:26:07 -08:00
Daniel Rodríguez Troitiño
ba68faaed5 [test] Mark tests that use experimental/upcoming features as such
Find all the usages of `--enable-experimental-feature` or
`--enable-upcoming-feature` in the tests and replace some of the
`REQUIRES: asserts` to use `REQUIRES: swift-feature-Foo` instead, which
should correctly apply to depending on the asserts/noasserts mode of the
toolchain for each feature.

Remove some comments that talked about enabling asserts since they don't
apply anymore (but I might had miss some).

All this was done with an automated script, so some formatting weirdness
might happen, but I hope I fixed most of those.

There might be some tests that were `REQUIRES: asserts` that might run
in `noasserts` toolchains now. This will normally be because their
feature went from experimental to upcoming/base and the tests were not
updated.
2024-11-02 11:46:46 -07:00
Holly Borla
bd1653e497 [Concurrency] Split up the non-Sendable result diagnostics and improve
wording.
2024-08-05 09:56:11 -07:00
Holly Borla
d40663dd66 [Concurrency] Flip the implicitly async select in a concurrency error.
This diagnostic was presenting "implicitly async" for explicitly async calls,
and vice versa, because the `select` was incorrect.
2024-07-31 06:46:07 -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
Holly Borla
9ba481ad53 [Diagnostics] Clarify the wording of error_in_future_swift_version. 2024-03-01 12:05:51 -08:00
Slava Pestov
6027bf46a6 AST: Simplify collectExistentialConformances() 2024-02-10 09:36:37 -05:00
Pavel Yaskevich
6082d1c05b [Sema] Don't propagate allowsMissing to existential superclass check
`TypeChecker::containsProtocol` shouldn't propagate user-provided
`allowMissing` to existential superclass check because the type
could be `& Sendable` and `allowMissing` propagation would result
in invalid builtin conformance returned for a non-conforming
superclass.
2023-11-13 14:56:13 -08:00
Ben Barham
36686d720d [Sema] Move predates concurrency remarks to warnings
Remarks are intended to be enabled via eg. `-R...`, where as
`(add|remove)_predates_concurrency_import` is a diagnostic that's always
output without any `-R` flag. Move it to a warning instead.

Resolves rdar://114207080.
2023-10-25 14:37:25 -07:00
Holly Borla
c9bfe8c3dd [Concurrency] Don't allow subclasses of nonisolated classes to add global
actor isolation.

Adding global actor isolation via subclassing admits data races because
actor-isolated types are Sendable while nonisolated classes are not (unless
otherwise annotated), so this allowed bypassing Sendable checking.
2023-10-11 12:41:00 -07:00
Holly Borla
57214ce7fb [Concurrency] Try harder to downgrade preconcurrency errors to warnings in
Swift 5 mode.

When you annotate a ValueDecl with `@preconcurrency`, the compiler should allow
concurrency violations by downgrading errors in the actor isolation checker to
warnings in Swift 5 mode. Previously, the actor isolation checker only checked
whether the caller's context was preconcurrency when deciding to downgrade, so
referencing preconcurrency declarations directly remained errors. Preconcurrency
was also dropped when computing actor isolation for declarations imported from
clang, which are always preconcurrency.
2023-10-04 07:07:41 -07:00
Doug Gregor
740cd7bd61 [Distributed] Implement distributed actor semantics in call checking
The move to perform all call checking that can cross concurrency
domains into one place dropped the specific logic for distributed
actor calls. Introduce that logic, cleaning it up to consistently use
the "known to be local" semantics needed for distributed actors.
2023-06-26 13:39:56 -07:00
Allan Shortlidge
c13dd18ef8 Sema: Diagnose the availability of global actor attributes. This includes SPI and resilience diagnostics. 2022-08-20 17:57:25 -07:00
Anthony Latsis
9596dcd1df Gardening: Migrate test suite to GH issues: ClangImporter 2022-08-12 06:58:42 +03:00
Kavon Farvardin
4fcc58a3fe test coverage for rdar://97646309 2022-08-10 12:50:01 -07:00
Doug Gregor
9bd950477c Using --enable-experimental-feature requires an asserts-enabled compiler.
Fixes rdar://97281993.
2022-07-21 09:31:32 -07:00
Doug Gregor
c443b1a2d8 Downgrade "reference to captured var" to warn in preconcurrency code.
Fixes rdar://97228088.
2022-07-18 17:05:52 -07:00
Doug Gregor
28a0f83590 Turn @Sendable completion handlers into an experimental feature.
`@Sendable` on completion handlers imported from Objective-C has been
implemented for a while, but has been disabled in production builds
due to a number of problems we've encountered with rolling it out.

Introduce an experimental feature for `@Sendable` completion handlers
so we can iterate on this more before we enable it by default.

Part of rdar://85569247, which will cover re-landing this feature.
2022-07-18 12:29:28 -07:00
Kavon Farvardin
e26c16562b only warn for isolation mismatch in @preconcurrency context
Much like how we do for an isolation mismatch for a call within
a closure that is `@preconcurrency`, references should also only
warn about the isolation issue too.

The reason why there is a mismatch is that the completion-handlers
are considered `@Sendable`, which disables the isolation inheriting
behavior of other, non-Sendable closures.

resolves rdar://96830159
2022-07-13 19:55:09 -07:00
Doug Gregor
c9c50b4ae0 [Requirement machine] Ignore unavailable conformances on superclass constraints.
When determining whether a superclass conforms to a particular protocol,
skip unavailable conformances. This way, we don't minimize away a
constraint that might only apply to subclasses of the specified
superclass.

Fixes rdar://91853658.
2022-05-27 13:09:15 -07:00
Doug Gregor
001db3a2c4 Fix testcase now that we get warnings instead of errors 2022-04-23 00:21:14 -07:00
Doug Gregor
9df6394afe [Clang importer] Consider attributes on the typedef name for an anonymous tag.
In C, one can provide a typedef name for an anonymous tag declaration in
one shot, e.g.,

    typedef struct {
      double x, y
    } Point;

In this case, there are effectively two declarations at the C level:
the typedef and the struct. The Clang importer was only taking
attributes from the anonymous struct (i.e., the tag) and not from the
typedef. However, any attributes put before the `typedef` should apply
as well... so consider those, too.

For now, only do this for `swift_attr` attributes, because we're
seeing this primarily with `Sendable` annotations. In the future, we
can look to generalizing it, but that could have source-breaking
consequences.

Fixes rdar://91632960.
2022-04-22 17:40:13 -07:00
Doug Gregor
4116d7a3d7 Rename the -strict-concurrency= options to be more descriptive.
The three options are now:

* `explicit`: Enforce Sendable constraints where it has been explicitly adopted and perform actor-isolation checking wherever code has adopted concurrency. (This is the default)
* `targeted`: Enforce Sendable constraints and perform actor-isolation checking wherever code has adopted concurrency, including code that has explicitly adopted Sendable.
* `complete`: Enforce Sendable constraints and actor-isolation checking throughout the entire module.
2022-04-20 18:17:33 -07:00
Doug Gregor
d20064443f Use the strict concurrency level to make decisions.
`isConcurrencyChecked()` was being used as a proxy for
`-warn-concurrency` that didn't account for Swift 6. Replace
checks against it within the current module with checks against the
strict concurrency level, which subsumes the Swift 6 check and can
account for the difference between "limited" and "on".

`isConcurrencyChecked()` is used now used exclusively to mean "treat a
missing Sendable conformance as an explicitly-non-Sendable type".
2022-04-18 23:52:07 -07:00
Evan Wilde
3321c32456 Pass all the tests!
There were some tests that relied on the top-level code not being an
asynchronous context to emit certain error messages. Now that it is,
those tests weren't emitting the expected error message.

In other cases, the issue was that they were trying to initialize a
global variable and weren't really using top-level code as top-level
code, so adding `-parse-as-library` was sufficient for the testing
purposes.

To fix the objc_async test, parsing as a library was nearly sufficient.
Unfortunately, the little `if #available` trick that I was using stopped
working since it relied on being in top-level code. So that we emit the
unavailableFromAsync error message, I had to set the availability on
everything correctly because we can't just disable availability
checking.
2022-03-15 16:35:48 -07:00
Becca Royal-Gordon
51e5408eeb [ClangImporter] Allow @Sendable on more params
When `__attribute__((swift_attr(“@Sendable”)))` is applied to a clang parameter Swift now attempts to make the type `Sendable` in a more comprehensive way than before:

* If it is a function type, it adds `@Sendable` to it.
* If it is a protocol composition type, it adds `& Sendable` to it.
* If it is a class, protocol, or generic class, it inserts it into a protocol composition and adds `& Sendable`.
* If it is a typealias, it *may* desugar it to a modified version of the underlying type.
* In various other cases, it recurses into the children of the type.

This allows Objective-C methods and functions to require that their arguments have a Sendable type without specifying a particular type they must belong to.

Fixes rdar://87727549.
2022-03-10 16:03:37 -08:00
Kavon Farvardin
3c04f0a9f4 [SR-15694] make isolation inference + override more consistent
During actor isolation inference, we would unconditionally choose the
isolation of the overridden decl (when, say, there is no attribute on the decl).
The overridden decl is identified with `getOverriddenDecl`.

This works mostly fine, but initializers have some unusual overridden decls
returned by that method. For example, in the following code:

```swift
@objc class PictureFrame: NSObject {
  init(size: Int) { }
}

@MainActor
class FooFrame: PictureFrame {
  init() {
    super.init(size: 0)
  }
}
```

that method claims that `FooFrame.init()` overrides `PictureFrame.init()`, when
it really does not! So, if we were to unconditionally take the isolation from
`PictureFrame.init()` (and thus `NSObject.init()`), then we'd infer that
`FooFrame.init()` has unspecified isolation, despite the nominal it resides in
being marked as `MainActor`. This is in essence the problem in SR-15694, where
placing the isolation directly on the initializer fixes this issue.

If `FooFrame.init()` really does override, then why can it be `MainActor`? Well,
we have a rule in one part of the type-checker saying that if an ObjC-imported
decl has unspecified isolation, then overriding it with isolation is permitted.
But the other part of the type-checker dealing with the isolation inference was
not permitting that.

So, this patch unifies how actor-isolation inference is conducted to reuse that
same logic. In addition, the inference now effectively says that, if the decl
has no inferred isolation, or the inferred isolation is invalid according to the
overriding rules, then we use the isolation of the overridden decl. This
preserves the old behavior of the inference, while also fixing this issue with
ObjC declarations by expanding where isolation is allowed.

For example, as a consequence of this change, in the following code:

```swift
@MainActor
class FooFrame: NotIsolatedPictureFrame {
  override func rotate() {
    mainActorFn()
  }
}
```

if `NotIsolatedPictureFrame` is a plain-old not-isolated class imported from
Objective-C, then `rotate` will now have `MainActor` isolation, instead of
having unspecified isolation like it was inferred to have previously. This
helps make things consistent, because `rotate` is allowed to be `@MainActor` if
it were explicitly marked as such.

resolves rdar://87217618 / SR-15694
2022-03-07 16:41:43 -07:00
Doug Gregor
7149702d12 Rename @_predatesConcurrency to @preconcurrency.
Introduce the `@preconcurrency` attribute name for `@_predatesConcurrency`,
which has been the favored name in the pitch thread so far. Retain the
old name for now to help smooth migration.
2022-01-26 08:39:01 -08:00
Doug Gregor
ceedb019ca Update a test for the new add-predates-concurrency remark 2021-12-21 15:08:01 -08:00
Doug Gregor
5e9530429c Merge pull request #40488 from DougGregor/sendable-inherit-part-trois
Ensure that Sendable is inherited properly, take 3.
2021-12-09 19:33:10 -08:00
Doug Gregor
61e907498c Ensure that Sendable is inherited properly, take 3.
Extend the implicit Sendable request to produce an inherited
conformance when queried, rather than depending on the conformance
lookup table to always have the right entries. This acknowledges that
implicit Sendable needs to happen more at the type checking level than
the name-binding level, which is needed to eliminate cyclic
dependencies.
2021-12-09 11:06:13 -08:00
Evan Wilde
691cf1925d Merge branch 'main' into ewilde/concurrency/unavailablefromasync-messages 2021-12-08 11:41:14 -08:00
Evan Wilde
3a13721eae Add optional message to unavailablefromasync
Adding the ability to add an optional message to the unavailable from
async attribute. This can be used to indicate other possible API to use,
or help explain why it's unavailable.
2021-12-08 09:39:24 -08:00
Doug Gregor
600fd1775a Merge pull request #40434 from DougGregor/capture-sendable-checking
Diagnose non-sendable captures more directly.
2021-12-06 15:46:15 -08:00
Evan Wilde
cce3e8a7f5 Merge pull request #40149 from etcwilde/ewilde/concurrency/underscored-unavailablefromasync
Add `_unavailableFromAsync` attribute
2021-12-06 12:38:53 -08:00
Doug Gregor
9bd209d35b Diagnose non-sendable captures more directly.
Rather than only checking the sendability of captures at the point of
use, diagnose captures uniformly for the closure as a whole. Fixes a
case where we were missing a diagnostic due to an explicit capture,
rdar://85988937.
2021-12-06 11:32:29 -08:00
Evan Wilde
694fad4aef Adding unavailableFromAsync tests
This patch adds a bunch of tests to verify the behaviour of the
@_unavailableFromAsync attribute.

The attribute is only allowed on functions, so we verify that an error
is emitted when it is applied to structs, extensions, classes, and
actors. The attribute may be applied to constructors to disallow
construction in an async context, but cannot be applied to destructors
since a destructor must be callable from anywhere. Additionally, the
attribute cannot be applied to asynchronous functions since an async
function _must_ be called from an async context.

Specific checks include
 - Errors are emitted in an async context (global function, e.g.)
 - Errors are emitted when the async context is nested in a sync context
 - Errors are not emitted from a sync context nested in an async context

This patch also includes verification that the attribute is propagated
across module boundaries and is be imported from ObjC functions.

Lastly, this patch adds the IDE completion testing to verify that the
attribute is considered.
2021-11-30 14:20:21 -08:00
Evan Wilde
563d942fcd Enable availability-checking on objc_async.swift test
I'm re-enabling availability checking in the
test/ClangImporter/objc_async.swift test because I want to perform an
availability check in the future.

In re-enabling it, I came across a warning with a fixme and a test
failure on the line. Given that the expected warning has a fixme and is
currently filled in with "something about missing conformance", I think
this is the warning message that they were looking for. Supplying that
gets the test passing with availability-checking enabled.
2021-11-30 13:57:27 -08:00
Doug Gregor
0c9c8f92c9 Imported C declarations always predate concurrency 2021-11-29 15:03:33 -08:00
Becca Royal-Gordon
ad0e7096ba Make NS_ENUM &c. Sendable even when audited
An explicit swift_attr("@_nonSendable") will override it (except for ns_error_domain where the type is embedded in another type that's forced to be Sendable), but swift_attr("@_nonSendable(_assumed)") will not.
2021-11-19 11:34:02 -08:00
Becca Royal-Gordon
b9480f12d0 Allow imported types to add a Sendable conformance
...by using `__attribute__((swift_attr("@Sendable")))`. `@_nonSendable` will "beat" `@Sendable`, while `@_nonSendable(_assumed)` will not.

This commit also checks if `SwiftAttr` supports `#pragma clang attribute` and, if it does, defines `__SWIFT_ATTR_SUPPORTS_SENDABLE_DECLS` in imported headers so they know they can apply these attributes in an auditing style.
2021-11-19 11:34:01 -08:00
Saleem Abdulrasool
4d44953691 Revert "Support __available__((swift_attr("@Sendable")))" 2021-11-19 07:40:24 -08:00
Becca Royal-Gordon
3d2d4e1721 Merge pull request #40170 from beccadax/send-me-an-object
Support __available__((swift_attr("@Sendable")))
2021-11-19 01:26:10 -08:00
Doug Gregor
a77f05f627 Switch @_unsafeSendable and @_unsafeMainActor tests to @_predatesConcurrency 2021-11-18 09:01:31 -08:00