Commit Graph

28382 Commits

Author SHA1 Message Date
Holly Borla
b684e716fe Merge pull request #81275 from jamieQ/invalid-isolated-any-conversion-6.2
[6.2][Sema]: ban @isolated(any) conversions to synchronous function types
2025-05-05 18:00:20 -07:00
Doug Gregor
431aeca5fc Merge pull request #81250 from DougGregor/observation-nonisolated-conformances-6.2
[6.2] [Observation] Create nonisolated conformances to Observable
2025-05-05 10:19:53 -07:00
Jamie
89b98a2cb5 [Sema]: ban @isolated(any) conversions to synchronous function types
per SE-0431, function conversions from an @isolated(any) function to a synchronous,
non-@isolated(any) function type should not be allowed. this adds a warning during
type checking to enforce this, which will be an error in a future major
language mode.

(cherry picked from commit 629e208970)
2025-05-03 12:51:39 -05:00
John Hui
b7812babdf Merge pull request #81212 from j-hui/base-6.2/fix-recursive-inheritance
🍒 [6.2] [cxx-interop] Avoid unchecked recursion when importing C++ classes with circular inheritance
2025-05-02 10:24:37 -07:00
Pavel Yaskevich
39cdc3ac81 Merge pull request #81238 from xedin/nonisolated-nonsending-inference-for-storage-6.2
[6.2][Concurrency] SE-0461: Allow `nonisolated(nonsending)` inference on properties…
2025-05-02 09:04:32 -07:00
Pavel Yaskevich
fb04803459 Merge pull request #81237 from xedin/rdar-150397825-6.2
[6.2][Concurrency] Account for explicit `@concurrent` on closures
2025-05-01 22:14:24 -07:00
Doug Gregor
a634e8d573 [Observation] Create nonisolated conformances to Observable
This is needed to make default-main-actor mode work well with
Observable. Fixes rdar://150419628.
2025-05-01 22:02:59 -07:00
Michael Gottesman
dec719d6e9 Merge pull request #81220 from gottesmm/release/6.2-150351351
[6.2][concurrency] Fix an off by one error in generic handling in local capture checking code.
2025-05-01 14:01:35 -07:00
Pavel Yaskevich
d606c94b93 [Concurrency] Allow nonisolated(nonsending) inference on properties with async getters
When `NonisolatedNonsendingByDefault` is enabled it should infer
`nonisolated(nonsending)` for both async functions and storage as
specified by the proposal.

(cherry picked from commit 8a9bbd5114)
2025-05-01 13:19:02 -07:00
Pavel Yaskevich
f50695ba32 [Concurrency] Account for explicit @concurrent on closures
If closure is stated as `@concurrent` is has to be treated as
nonisolated.

Resolves: rdar://150397825
(cherry picked from commit ef65f26457)
2025-05-01 13:08:35 -07:00
Doug Gregor
fcedb0b2a4 Merge pull request #81125 from DougGregor/unsafe-call-effects-6.2
[6.2] [Strict memory safety] Provide argument-specific diagnostics for calls
2025-04-30 15:09:50 -07:00
Michael Gottesman
55d802cd6a [concurrency] Fix an off by one error in generic handling in local capture checking code.
Otherwise, we break memory safety by accessing a generic parameter that does not
exist resulting in undefined behavior.

rdar://150351351
(cherry picked from commit fb3c710212)
2025-04-30 14:25:41 -07:00
John Hui
c4fed45f54 [cxx-interop] Avoid unchecked recursion when importing C++ classes with circular inheritance
It is possible for a C++ class template to inherit from a specialization
of itself. Normally, these are imported to Swift as separate (unrelated)
types, but when symbolic import is enabled, unspecialized templates are
imported in place of their specializations, leading to circularly
inheriting classes to seemingly inherit from themselves.

This patch adds a check to guard against the most common case of
circular inheritance, when a class template directly inherits from
itself. This pattern appears in a recent version of libc++,
necessitating this patch. However, the solution here is imperfect as it
does not handle more complex/contrived circular inheritance patterns.

This patch also adds a test case exercising this pattern. The
-index-store-path flag causes swift-frontend to index the C++ module
with symbolic import enabled, without the fix in this patch, that test
triggers an assertion failure due to the circular reference (and can
infinitely recurse in the StorageVisitor when assertions are disabled).

rdar://148026461
(cherry picked from commit 1f2107f357)
2025-04-30 13:40:19 -07:00
Slava Pestov
9bb6673bbd AST: Fix existential erasure of long member types
Suppose protocol P has a primary associated type A, and we have
a `any P<S>` value. We form the generalization signature <T>
with substitution map {T := S}, and the existential signature
<T, Self where T == Self.A>.

Now, if we call a protocol requirement that takes Self.A.A.A,
we see this is fixed concrete type, because the reduced type of
Self.A.A.A is T.A.A in the existential signature.

However, this type parameter is not formed from the
conformance requirements of the generalization signature
(there aren't any), so we cannot directly apply the outer
substitution map.

Instead, change the outer substitution conformance lookup
callback to check if the reduced type parameter is valid
in the generalization signature, and not just rooted in a
generic parameter of the generalization signature.

If it isn't, fall back to global conformance lookup.

A better fix would introduce new requirements into the
generalization signature to handle this, or store them
separately in the generic environment itself. But this is fine
for now.

- Fixes https://github.com/swiftlang/swift/issues/79763.
- Fixes rdar://problem/146111083.
2025-04-29 19:00:50 -04:00
Doug Gregor
25a9a154fa [Effects checking] Handle optional function types for dynamic @objc calls
We've been missing this case forever, but it hadn't come up until
we performed "unsafe" checking on calls.

(cherry picked from commit 60f7d20ed2)
2025-04-26 07:45:36 -07:00
Doug Gregor
dab3666aa7 [Strict memory safety] Temporarily disable warning about stray "unsafe" in _SwiftifyImport expansions
We need to improve on the macro so it doesn't generate unnecessary
"unsafe" expressions.

(cherry picked from commit 81b68e567b)
2025-04-26 07:45:34 -07:00
Doug Gregor
56c0ac51de [Strict memory safety] Provide argument-specific diagnostics for calls
Similar to what we do for 'throws' checking, perform argument-specific
checking for unsafe call arguments. This provides more detailed failures:

```
example.swift:18:3: warning: expression uses unsafe constructs but is not
marked with 'unsafe' [#StrictMemorySafety]
16 |   x.f(a: 0, b: 17, c: nil)
17 |
18 |   x.f(a: 0, b: 17, c: &i)
   |   |                   `- note: argument 'c' in call to instance
method 'f' has unsafe type 'UnsafePointer<Int>?'
   |   `- warning: expression uses unsafe constructs but is not marked
with 'unsafe' [#StrictMemorySafety]
19 |   unsafeF()
20 | }
```

It also means that we won't complain for `nil` or `Optional.none`
arguments passed to unsafe types, which eliminates some false
positives, and won't complain about unsafe result types when there is
a call---because we'd still get complaints later about the
actually-unsafe bit, which is using those results.

Fixes rdar://149629670.

(cherry picked from commit ee9487b86f)
2025-04-26 07:45:30 -07:00
Doug Gregor
0cdfbd45c7 [Effects] Keep track of whether we looked through a 'self' application
When we look through a "self" application while decomposing a function
call in effects checking, keep track of whether we did so. Use this to
ensure that we get the right parameter types when looking at the
"self" application vs. the outer call.

This has always been wrong, but the async/throws effects don't ever
matter for the innermost application (i.e., for `x.m`), so the
inconsistency didn't come up.

(cherry picked from commit 0b264e2d55)
2025-04-26 07:45:27 -07:00
Slava Pestov
2d76c38174 Merge pull request #80965 from slavapestov/fix-rdar149438520-6.2
[6.2] Sema: Fix case where witness thrown error type is a subtype of a type parameter
2025-04-25 21:52:52 -04:00
Pavel Yaskevich
995916a780 Merge pull request #81051 from xedin/rdar-rdar-148168219-6.2
[6.2][Sema] ConstnessChecker: Look through function conversions while chec…
2025-04-25 09:43:54 -07:00
Allan Shortlidge
4f61908b4c AST: Fix a typo in missingImportsForDefiningModule(). 2025-04-24 22:56:43 -07:00
Hamish Knight
726d78d011 [Sema] Add missing null check for Type
`getType` here can return a null type if the queried expression isn't
part of the solution, which can currently happen for code completion.
I'm working on a more principled fix for this, but until then this is
a low-risk fix that will unblock the stress tester and can be
cherry-picked to 6.2.

rdar://149759542
2025-04-24 20:37:38 +01:00
Slava Pestov
dc70a0e779 Sema: Fix case where witness thrown error type is a subtype of a type parameter
In b30006837e, I changed the `if`
condition here to check for the absence of type variables as well
as type parameters. This is incorrect; the type variables come up
in ValueWitnessRequest, and the type parameters come up in
associated type inference. We want the matching to be more lax
in the former case.

Fixes rdar://149438520.
2025-04-24 12:30:02 -04:00
Pavel Yaskevich
be0169042d [Sema] ConstnessChecker: Look through function conversions while checking arguments
In strict concurrency mode some calls could reference a declaration that
is  wrapped in one or more function conversion expressions to apply
concurrency related attributes or erase them (such as `@Sendable` or
`@MainActor`). This shouldn't impact constness checking and the checker
should look through such conversions.

Resolves: rdar://148168219
(cherry picked from commit b484e9645d)
2025-04-23 14:18:11 -07:00
Allan Shortlidge
5a557d5261 Merge pull request #80996 from tshortli/member-import-visibility-cross-import-overlay-6.2
[6.2] Sema: Improve MemberImportVisibility diagnostics for cross import overlays
2025-04-22 18:18:04 -07:00
Allan Shortlidge
43fd131762 Sema: Improve MemberImportVisibility diagnostics for cross import overlays.
When `MemberImportVisibility` is enabled and a declaration from a cross import
overlay is diagnosed because it has not been imported, suggest imports of the
declaring and bystanding modules instead of the cross import overlay module
(which is an implementation detail).

Resolves rdar://149307959.
2025-04-22 10:09:24 -07:00
Pavel Yaskevich
d007d7adfb [Frontend] Rename AsyncCallerExecution upcoming feature to NonisolatedNonsendingBeDefault
(cherry picked from commit c110941c27)
2025-04-22 00:33:45 -07:00
Pavel Yaskevich
4093171303 Merge pull request #80957 from xedin/rdar-145672343-6.2
[6.2][Sema/SILGen] Import ObjC async functions as nonisolated(nonsending) by default
2025-04-21 22:21:12 -07:00
Doug Gregor
28d2bef2d1 Merge pull request #80946 from DougGregor/safe-nested-in-unsafe-fixes-6.2
[6.2] [Strict memory safety] Improve handling of safe types nested within unsafe ones
2025-04-21 17:22:13 -07:00
Pavel Yaskevich
601c34cfcf [Concurrency] Infer isolation of methods that override caller isolated @objc async handlers to be `@concurrent
This is behavior pre-SE-0461 which is the safest option instead
of inferring `nonisolated(nonsending)` and changing the calling
convention and foregoing Sendable checking.

(cherry picked from commit c25d2fab50)
2025-04-21 11:22:19 -07:00
Pavel Yaskevich
d40c37e20d [Sema/SILGen] Import ObjC async functions as nonisolated(nonsending) by default
These functions already have special code generation that keeps them
in the caller's isolation context, so there is no behavior change here.

Resolves: rdar://145672343
(cherry picked from commit 07bad98f6d)
2025-04-21 11:22:18 -07:00
Pavel Yaskevich
cdc02a4c8d Merge pull request #80924 from xedin/rdar-149107104-6.2
[6.2][TypeChecker] Allow closures to assume `nonisolated(nonsending)`
2025-04-21 11:10:38 -07:00
Doug Gregor
2bc7b5747b [Strict memory safety] Treat the implicit call to a 'defer' function as implicitly 'unsafe'
This eliminates stray warnings.

(cherry picked from commit b7d41a55a5)
2025-04-21 09:03:32 -07:00
Doug Gregor
ac56b63bd3 [Strict memory safety] Type alias types are only unsafe if their underlying type is
(cherry picked from commit 53ca902267)
2025-04-21 09:03:28 -07:00
Doug Gregor
93fbce0ce2 [Strict memory safety] Nested types are safe/unsafe independent of their enclosing type
When determining whether a nested type is safe, don't consider whether
its enclosing type is safe. They're independent.

(cherry picked from commit 8ec52c825c)
2025-04-21 09:03:25 -07:00
Doug Gregor
4c5ba0c5cb [Strict memory safety] Only diagnose unsafe types in the canonical type
Typealiases involving unsafe types that resolve to safe types
should not be diagnosed.

(cherry picked from commit 0405f61207)
2025-04-21 09:03:21 -07:00
Pavel Yaskevich
7d77c615b3 Merge pull request #80922 from xedin/rdar-148996589-6.2
[6.2][Concurrency] Downgrade non-Sendable type captures to warnings if clo…
2025-04-19 19:07:03 -07:00
Pavel Yaskevich
134f5def36 [TypeChecker] Allow closures to assume nonisolated(nonsending)
Always infer `nonisolated(nonsending)` from context directly on
a closure unless the closure is marked as `@concurrent`, otherwise
the closure is not going to get correct isolation and going to hop
to the wrong executor in its preamble.

Resolves: rdar://149107104
(cherry picked from commit 3de72c5452)
2025-04-18 16:41:34 -07:00
Pavel Yaskevich
55c2af0886 [Concurrency] Downgrade non-Sendable type captures to warnings if closure is in @preconcurrency context
The original check examined only the immediate closure, but it's
possible that the closure happens to be in a preconcurrency context
which also requires a downgrade.

Resolves: rdar://148996589
(cherry picked from commit cad2df3d5c)
2025-04-18 16:09:39 -07:00
Hamish Knight
b2b37eb766 [Sema] Downgrade noasync diagnostic to warning for closure macro args
Downgrade to a warning until the next language mode. This is
necessary since we previously missed coercing macro arguments to
parameter types, resulting in cases where closure arguments weren't
being treated as `async` when they should have been.

rdar://149328745
2025-04-18 11:58:14 +01:00
Hamish Knight
15c7467f37 NFC: Abstract away the use of '7' to represent the next language mode
Introduce `Version::getFutureMajorLanguageVersion` to make it easier
to find clients that will need to be updated once we have a new
language mode.
2025-04-18 11:58:14 +01:00
Pavel Yaskevich
65f78f3c01 Merge pull request #80807 from xedin/se-0461-renamings-6.2
[6.2][SE-0461] Replace `@execution(...)` with `@concurrent` and `nonisolated(nonsending)`
2025-04-16 20:47:14 -07:00
Pavel Yaskevich
826176a28a [AST] NFC: Add a convenient way to create implicit NonisolatedAttrs 2025-04-16 13:20:16 -07:00
Pavel Yaskevich
031d3bcc2b [Sema/SILGen] NFC: Remove all mentions of @execution from comments 2025-04-16 13:18:57 -07:00
Pavel Yaskevich
06f880e65c [AST/ASTGen/Sema/Serialization] Remove @execution attribute
Complete the transition from `@execution` to `@concurrent` and `nonisolated(nonsending)`
2025-04-16 13:18:52 -07:00
Nate Chandler
de1d5ae894 [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-16 11:05:02 -07:00
Nate Chandler
85860f6960 [NFC] AST: Extract helper method.
Several callers of `AbstractStorageDecl::getAccessStrategy` only cared
about whether the the access would be via physical storage.  Before
adding more arguments to `getAccessStrategy` for which such callers
would have to pass a sentinel value, add a convenience method for this.
2025-04-16 11:05:01 -07:00
Pavel Yaskevich
fde841704c [AST/Parse] Implement nonisolated(nonsending) type modifier 2025-04-16 10:06:08 -07:00
Pavel Yaskevich
d8c64c1ff0 [AST/Sema] Intoduce nonisolated(nonsending) as a replacement for @execution(caller) 2025-04-16 10:06:08 -07:00
Pavel Yaskevich
4a973f7b4b [AST/Sema] Replace @execution(concurrent) with @concurrent 2025-04-16 10:06:08 -07:00