Commit Graph

28352 Commits

Author SHA1 Message Date
Michael Gottesman
d3de4a64c9 Merge pull request #81363 from gottesmm/release/6.2-rdar150209093
[6.2][concurrency] Ensure that we treat closures that are nonisolated(nonsending) via their ActorIsolation as nonisolated(nonsending).
2025-05-09 18:43:32 -07:00
Doug Gregor
2ea1421aba [SE-0470] Fix synthesized conformances with default main actor isolation
When defaulting to main-actor isolation, types that have synthesized
conformances (e.g., for Equatable, Hashable, Codable) were getting
nonisolated members by default. That would cause compiler errors
because the conformances themselves defaulted to main-actor isolation
when their types were.

Be careful to only mark these members as 'nonisolated' when it makes
sense, and leave them to get the isolation of their enclosing type
when the conformance might have isolation. This ensures that one can
use synthesis of these protocols along with default main-actor mode.

There is a one-off trick here to force the synthesized CodingKeys to
be nonisolated, because the CodingKey protocol requires Sendable.
We'll separately consider whether to generalize this rule.

More of rdar://150691429.
2025-05-09 14:40:21 -07:00
Becca Royal-Gordon
e7607e15c0 Merge pull request #81393 from beccadax/abi-let-you-graduate-6.2 2025-05-09 12:28:43 -07:00
Nate Cook
c8c035ec32 [6.2][stdlib] Allow a default for optional interpolations (#81360)
Cherry-pick of #80547 for the 6.2 release branch.

---

Explanation: This cherry picks the implementation of SE-0477 to add a
string interpolation method with a `default:` parameter for optional
interpolation values.
Main Branch PR: https://github.com/swiftlang/swift/pull/80547
Risk: Low.
Reviewed By: @stephentyrone 
Resolves: rdar://150865613
Testing: New tests for the string interpolations and fix-its.
2025-05-09 12:10:58 -05:00
Becca Royal-Gordon
6007e783ad Ban @abi on multi-var PBDs
Per a comment in the review thread that the comma in a multi-variable pattern binding makes it look like the `@abi` attribute has several arguments.
2025-05-08 18:27:58 -07:00
Becca Royal-Gordon
d59d219078 Forbid @_borrowed in @abi
It has indirect effects on the accessors, so it shouldn’t matter, but we can defensively redirect the query to the API counterpart anyway.

This was the last `InferredInABIAttr` attribute, so we can now remove all of the infrastructure involved in supporting attribute inference.
2025-05-08 18:27:58 -07:00
Becca Royal-Gordon
c8db046cfc Forbid lazy with @abi
It’s not clear how `@abi` would apply to the auxiliary decl used for `lazy`.
2025-05-08 18:27:57 -07:00
Becca Royal-Gordon
a103e21d11 Diagnose CustomAttrs as needed in @abi
CustomAttr backs four different features, each of which requires a different behavior in `@abi`:

• Global actors: Permitted (and permitted to vary) since they can affect mangling
• Result builders: Forbidden inside an `@abi` since they have no ABI impact
• Property wrappers: Forbidden both inside an `@abi` and on a decl with an `@abi` since it’s not clear how we would apply `@abi` to the auxiliary decls
• Attached macros: Forbidden inside an `@abi` since an ABI-only decl has no body, accessors, members, peers, extensions, or (currently) conformances

Implement these behaviors (outside of `ABIDeclChecker` since they can’t be described there).

Macro-related tests are not included in this commit; they require matching swift-syntax changes which are being negotiated.
2025-05-08 18:27:57 -07:00
Becca Royal-Gordon
23a7a5951e Tweak redecl checking’s handling of generated code
Macro expansions are now treated like a part of the source file they belong to, for purposes of the “second declaration is the one that’s diagnosed” rule. This helps stabilize a behavior that was easy to perturb.
2025-05-08 18:27:57 -07:00
Becca Royal-Gordon
10d49aad5d Typecheck ABI-only VarDecls
The decl checker was effectively not being run on these because we weren’t typechecking the PBD and typechecking the VarDecl itself is basically a no-op.
2025-05-08 18:27:57 -07:00
Pavel Yaskevich
9bd0a34599 Merge pull request #81356 from xedin/rdar-150777469-6.2
[6.2][CSFix] SE-0470: Warn about missing `@Sendable` for unapplied static …
2025-05-07 23:50:53 -07:00
Michael Gottesman
03d8c0ae6b [concurrency] Ensure that we treat closures that are nonisolated(nonsending) via their ActorIsolation as nonisolated(nonsending).
Some notes:

1. In most cases, I think we were getting lucky with this by just inferring the
closure's isolation from its decl context. In the specific case that we were
looking at here, this was not true since we are returning from an @concurrent
async function a nonisolated(nonsending) method that closes over self. This
occurs since even when NonisolatedNonsendingByDefault we want to start importing
objc async functions as nonisolated(nonsending).

2. I also discovered that in the ActorIsolationChecker we were not visiting the
inner autoclosure meaning that we never set the ActorIsolation field on the
closure. After some discussion with @xedin about potentially visiting the
function in the ActorIsolationChecker, we came to the conclusion that this was
likely to result in source stability changes. So we put in a targeted fix just
for autoclosures in this specific case by setting their actor isolation in the
type checker.

3. Beyond adding tests to objc_async_from_swift to make sure that when
NonisolatedNonsendingByDefault is disabled we do the right thing, I noticed that
we did not have any tests that actually tested the behavior around
objc_async_from_swift when NonisolatedNonsendingByDefault is enabled. So I added
the relevant test lines so we can be sure that we get correct behavior in such a
case.

rdar://150209093
(cherry picked from commit ced96aa5cd)
2025-05-07 13:03:45 -07:00
Rintaro Ishizaki
ee3c2ab49a [Macros] Don't include attr range when checking macro definition
For macro definition checking, we use the range of the `macro`
declaration and re-parse it with `SwiftParser`. Previously it uses the
range including the attributes, but that can result invalid code because
the attribute can be in a `#if ... #endif` region.

Since we don't use attributes for checking the definition, just use the
range without the attributes instead.

rdar://150805795
(cherry picked from commit 8519e71602)
2025-05-07 09:31:17 -07:00
Pavel Yaskevich
d517a26f25 [CSFix] SE-0470: Warn about missing @Sendable for unapplied static member references until future Swift version
Static member referenced were marked as `@Sendable` by `InferSendableFromCaptures`
because metatypes used to be always Sendable which is no longer the case, so in
order to maintain the source compatibility we need to downgrade missing `@Sendable`
to a warning for unapplied static member references.

This affects primarily operators at the moment because other static members
form a curry thunk with a call inside and would be diagnosed as a capture.

Resolves: rdar://150777469
(cherry picked from commit a57310b61d)
2025-05-07 09:13:08 -07:00
Michael Gottesman
322a376432 Merge pull request #81334 from gottesmm/release/6.2-rdar150695113
[6.2][swift-settings] Now that we aren't using it immediately, remove it from tree.
2025-05-06 22:41:46 -07:00
Doug Gregor
a3a17d0405 Disable conformance isolation inference for @preconcurrency conformances 2025-05-06 16:07:29 -07:00
Doug Gregor
89358d6a0e Make ConformanceIsolationRequest cache per-ProtocolConformance
This request was looking through to the root conformance, which could
mess with the caching bits. Sink the "is nonisolated conformance" bit
down into ProtocolConformance, and have the request for a non-root
conformance be defined in terms of the request for the root
conformance.
2025-05-06 16:07:25 -07:00
Doug Gregor
2ec6fbec4d [SE-0470] Prohibit inference of isolated conformances with nonisolated witnesses
If all of the witnesses to a conformance are nonisolated, then infer that
conformance as nonisolated rather than global-actor-isolated. This is
only relevant when InferIsolatedConformances is enabled, and prevents
that inference to help maintain source compatibility.
2025-05-06 16:07:22 -07:00
Michael Gottesman
e04e15f125 [swift-settings] Now that we aren't using it immediately, remove it from tree.
We can always get it back from the git history.

rdar://150695113
(cherry picked from commit 9d59dbed17)

Conflicts:
	include/swift/AST/DiagnosticsSema.def
	include/swift/Basic/Features.def
	test/abi/macOS/arm64/concurrency.swift
	test/abi/macOS/x86_64/concurrency.swift
2025-05-06 14:12:23 -07:00
Alejandro Alonso
12919a2300 Merge pull request #78248 from Azoy/value-generic-static-member
[NameLookup] Allow value generics to show up as static members
2025-05-05 18:42:15 -07:00
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