Commit Graph

28352 Commits

Author SHA1 Message Date
Gábor Horváth
816290f0cd [6.2.1][cxx-interop] Do not consider function types fragile
Explanationion: Function pointer types wee always considered fragile in C++ mode,
this manifested as a regression when interfacing with glibc.
Issues: rdar://159184118
Original PRs: #84040
Risk: Low, this only removes a spurious error for library evolution.
Testing: Added a compiler test.
Reviewers: @egorzhdan
2025-09-09 09:55:52 +01:00
Konrad Malawski
9524c10a43 [Concurrency/Distributed] ensure distributed thunks are @concurrent
Otherwise the "nonisolated nonsending by default" mode blows up as
distributed thunk signatures dont match expectations.

This undoes the fix from https://github.com/swiftlang/swift/pull/83940
and applies the fix on the synthesis side of the distributed thunks,
such that they are @concurrent always -- which keeps their old semantics
basically, regardless of what "default" mode we have.
2025-09-03 19:00:03 +09:00
Konrad Malawski
4c283e5959 [Concurrency/Distributed] nonisolated-nonsending by default breaks distributed thunks
the new NonisolatedNonsendingByDefault upcoming feature breaks remote
calls in distributed actors, because the expected isolation doesn't
match and the runtime swift_distributed_execute_target_resume will
crash.

This is a short term fix to unblock adopters, however preferably we
should mark the thunks as nonisolated(nonsending), though that seems to
be more involved.

resolves rdar://159247975
2025-09-03 08:21:05 +09:00
Pavel Yaskevich
bbb11ec272 [TypeCheckEffects] AbstractFunction: Parameter types should be mapped into context
Parameter type could be represented by an associated type which is
bound to a concrete type by an extension, `AbstractFunction::getType()`
should map it into context before returning because the construct is
that it always produces a function type.

Resolves: rdar://156955193
(cherry picked from commit 32b97d0e2a)
(cherry picked from commit 7f652915d9)
2025-08-27 10:35:50 -07:00
Pavel Yaskevich
c04eb7c482 [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
(cherry picked from commit 5ee673f358c9dd7dbbfe58f02bc78af60e0377b8)
2025-07-30 11:03:30 -07:00
John McCall
787cca9e37 Rewrite the type of nonisolated(nonsending) closures.
The constraint solver does not reliably give closures a function type
that includes `nonisolated(noncaller)`, even when the immediate context
requires a conversion to such a type. We were trying to work around this
in SILGen, but the peephole only kicked in if the types matched exactly,
so a contextual conversion that e.g. added `throws` was still emitting
the closure as `@concurrent`, which is of course the wrong semantics.
It's relatively easy to avoid all this by just rewriting the closure's
type to include `nonisolated(nonsending)` at a point where we can reliably
decide that, and then SILGen doesn't have to peephole anything for
correctness.

Fixes rdar://155313349
2025-07-28 22:22:54 -04:00
Doug Gregor
20a10c618e [SE-0466] Treat explicit "nonisolated" like implicit "nonisolated" on protocols
Make explicit "nonisolated" also not special on protocols, so a
nonisolated protocol does not suppress default isolation.
SendableMetatype is the proper way to suppress default isolation for a
protocol.

Unfortunately, these rules made it appear like issue #82168
was fixed, when in fact it was not. Keep the test case, but as a
failing test, and we'll investigate separately.
2025-07-25 12:21:38 -07:00
Doug Gregor
5abbf2e4c9 [SE-0466] Under main actor default isolation, explicit nonisolated is not special
Given an explicitly-nonisolated type such as

    nonisolated struct S { }

all extensions of S were also being treated as nonisolated. This meant
that being implicitly nonisolated (i.e., when you're using nonisolated
default isolation) was different from explicitly-writing nonisolated,
which is unfortunate and confusing. Align the rules, such that an
extension of S will get default isolation:

    extension S {
      func f() { } // @MainActor if we're in main actor default isolation
    }
2025-07-25 12:21:35 -07:00
Doug Gregor
4ffe06ed7c [SE-0466] Extensions and members thereof can apply default isolation
Extensions of nonisolated types can get default isolation from context
(e.g., @MainActor) independently. This was previously true for members,
but not conformances, and recently regressed for members. Fix the logic
to consistently allow such extensions to be default main-actor
isolated, which affects both their members and conformances.

Fixes rdar://156644976.
2025-07-25 12:21:08 -07:00
Doug Gregor
2db51c9159 Merge pull request #82923 from DougGregor/sendable-prohibits-mainactor-by-default-6.2
[6.2] [SE-0466] Enable SendableProhibitsMainActorInference by default
2025-07-23 20:04:56 -07:00
Holly Borla
276e5de462 [Concurrency] Local storage is always default nonisolated.
(cherry picked from commit 7b0ba7b5ed)
2025-07-22 21:36:59 -07:00
Holly Borla
2697773f21 [Concurrency] Only apply default main actor to local and nested
decls that are in a main actor isolated context.

This prevents `@MainActor` from being inferred in a context where
it cannot be used, while still allowing main actor code to be used
in local contexts that are also main actor isolated.

(cherry picked from commit 858e145069)
2025-07-22 21:15:26 -07:00
Pavel Yaskevich
61157cdd36 Merge pull request #83165 from xedin/nonisolated-nonseding-with-special-decls-6.2
[6.2][Concurrency] NonisolatedNonsendingByDefault: Infer `nonisolated(nonsending)` on declaration with special special semantics
2025-07-18 09:40:08 -07:00
Pavel Yaskevich
fb0daaa78a [Concurrency] NonisolatedNonsendingByDefault: Extend nonisolated(nonsending) to _openExistential
`_openExistential` is type-checked in a special way which
means that we need to explicitly inject `nonisolated(nonsending)`
isolation when forming a reference to this builtin.

(cherry picked from commit 358869ff54)
2025-07-17 18:08:30 -07:00
Pavel Yaskevich
18817b823f [Concurrency] NonisolatedNonsendingByDefault: Extend nonisolated(nonsending) to withoutActuallyEscaping
`withoutActuallyEscaping` is type-checked in a special way which
means that we need to explicitly inject `nonisolated(nonsending)`
isolation when forming a reference to this builtin.

(cherry picked from commit 48f4d7b688)
2025-07-17 18:08:30 -07:00
Holly Borla
94df8da676 [Concurrency] Downgrade isolated conformances to SendableMetatype
protocols to a warning for implicitly `@preconcurrency` protocols.

(cherry picked from commit 4b50238ebf)
2025-07-17 10:28:27 -07:00
Anthony Latsis
ffa408d9b8 Merge pull request #83110 from swiftlang/jepa-release
[6.2] Sema: Make `@concurrent` not imply `async` on closures
2025-07-17 16:44:44 +01:00
Doug Gregor
5797130e90 Merge pull request #83119 from DougGregor/unsafe-covers-unsafe-6.2 2025-07-17 02:02:18 -07:00
Doug Gregor
b6ea29613e [Strict memory safety] "unsafe" expression never propagates unsafe outward
In the effects checker, we were propagating the "has an unsafe use
site" outside of an `unsafe` expression. The result of this is that we
would not produce a warning for silly expressions like `unsafe unsafe
ptr.pointee`, where the first (outer) `unsafe` is unnecessary. Stop
propagating that bit so we properly diagnose the spurious "unsafe".

Fixes issue #82315 / rdar://153672668.
2025-07-16 15:43:59 -07:00
Anthony Latsis
dc1458762e Sema: Make @concurrent not imply async on closures
The present approach is not prudent because `@concurrent` synchronous
functions, a natural extension, are a likely-to-happen future direction,
whereas the current inference rule is entirely grounded on `@concurrent`
being exclusive to async functions.

If we were to ship this rule, we would have to keep the promise for
backwards compatibility when implementing the aforementioned future
direction, replacing one inconsistency with another, and possibly
introducing new bug-prone expression checking code.

```swift
func foo(_: () -> Void) {}
func foo(_: () async -> Void) {}

// In a future without this change and  `@concurrent` synchronous
// functions accepted, the first call resolves to the first overload,
// and the second call resolves to the second, despite `@concurrent` no
// longer implying `async`.
foo { }
foo { @concurrent in }
```

This change also drops the fix-it for removing `@concurrent` when used
on a synchronous closure. With the inference rule gone, and the
diagnosis delayed until after solution application, this error raises a
fairly balanced choice between removing the attribute and being
explicit about the effect, where a unilateral suggestion is quite
possibly more harmful than useful.

(cherry picked from commit 58d5059617)
2025-07-16 20:49:37 +01:00
Doug Gregor
6e419b6345 Revert "[6.2] Accept @cdecl global functions and enums, behind experimental feature flags" 2025-07-16 10:05:35 -07:00
Doug Gregor
73530bf957 Merge pull request #83088 from DougGregor/se-0466-nested-no-inference-6.2
[6.2] [SE-0466] Nested types of nonisolated types don't infer main-actor isolation
2025-07-16 09:44:00 -07:00
Alexis Laferrière
ed65bb9a46 Merge pull request #82791 from xymus/cdecl-6.2
[6.2] Accept `@cdecl` global functions and enums, behind experimental feature flags
2025-07-16 09:27:04 -07:00
Doug Gregor
a4a39f2a98 [SE-0466] Nested types of nonisolated types don't infer main-actor isolation
Under discussion as part of an amendment to SE-0466, limit default main
actor inference so it doesn't apply to a nested type within a
nonisolated type.
2025-07-15 20:15:35 -07:00
Xi Ge
6f4d55a901 Merge branch 'release/6.2' into cdecl-6.2 2025-07-15 14:50:10 -07:00
Pavel Yaskevich
84f70f3792 [Concurrency] Replace nonisolated with nonisolated(nonsending) when isolation is inferred
With `NonisolatedNonsendingByDefault` an explicit `nonisolated`
attribute in declaration context is inferred to mean `nonisolated(nonsending)`
and it should be printed as such in interface files and other places.

The inference logic that did didn't remove the original attribute
which meant that it would be printed twice i.e.
`nonisolated nonisolated(nonsending) func test() async` which is
incorrect and would fail swift interface validation.

Resolves: rdar://155847011
(cherry picked from commit b519c07428)
2025-07-15 09:20:33 -07:00
Hamish Knight
234a41567f Merge pull request #83036 from hamishknight/origami-6.2
[6.2] [Sema] Avoid folding sequences multiple times for completion
2025-07-15 09:15:17 +01:00
Doug Gregor
e5658e0087 Merge pull request #83020 from DougGregor/prohibit-isolated-conformance-capture-check-6.2 2025-07-14 15:37:31 -07:00
Michael Gottesman
187b1eedf7 Merge pull request #82776 from gottesmm/release/6.2-154139237
[6.2] [nonisolated-nonsending] Make the AST not consider nonisolated(nonsending) to be an actor isolation crossing point.
2025-07-14 13:19:55 -07:00
Hamish Knight
e653a22431 [Sema] Make change less risky for 6.2
Revert assertion changes and flip the condition to only apply when
doing completion.
2025-07-14 17:49:30 +01:00
Hamish Knight
5d93006d4c [Sema] Avoid folding sequences multiple times for completion
Completion can end up calling into pre-checking multiple times in
certain cases, make sure we don't attempt to fold a SequenceExpr
multiple times since its original AST is in a broken state
post-folding. Instead, just return the already-folded expression.

rdar://133717866
2025-07-14 17:49:23 +01:00
Pavel Yaskevich
c4a3d31ad0 Merge pull request #82998 from xedin/rdar-155589753-6.2
[6.2][Concurrency] Don't downgrade explicit isolation attribute clashes
2025-07-13 08:22:58 -07:00
Doug Gregor
74b54fea5c Use the "prohibits isolated conformances" check for capture metatypes
When checking whether a capture of a metatype is Sendable, we had an
incomplete and incorrect check for SendableMetatype conformance.
Replace that with the proper "prohibits isolated conformances" check
on the generic signature, which matches what we do on the caller side.

Fixes issue #82905 / rdar://155399531.
2025-07-12 08:47:14 -07:00
Pavel Yaskevich
4e93e74898 [Concurrency] Don't downgrade explicit isolation attribute clashes
The compiler shouldn't accept mismatch in explicit isolation attributes
because it could lead to incorrect isolation selection.

Resolves: rdar://155589753

(cherry picked from commit a9373c0f3f)
2025-07-10 22:29:20 -07:00
Michael Gottesman
35c3b9e12c [concurrency] Perform some fixes so that transfernonsendable_closureliterals_isolationinference.swift now passes.
The reason why this failed is that concurrently to @xedin landing
79af04ccc4, I enabled
NonisolatedNonsendingByDefault on a bunch of other tests. That change broke the
test and so we needed to fix it.

This commit fixes a few issues that were exposed:

1. We do not propagate nonisolated(nonsending) into a closure if its inferred
context isolation is global actor isolated or if the closure captures an
isolated parameter. We previously just always inferred
nonisolated(nonsending). Unfortunately since we do not yet have capture
information in CSApply, this required us to put the isolation change into
TypeCheckConcurrency.cpp and basically have function conversions of the form:

```
(function_conversion_expr type="nonisolated(nonsending) () async -> Void"
   (closure_expr type="() async -> ()" isolated_to_caller_isolation))
```

Notice how we have a function conversion to nonisolated(nonsending) from a
closure expr that has an isolation that is isolated_to_caller.

2. With this in hand, we found that this pattern caused us to first thunk a
nonisolated(nonsending) function to an @concurrent function and then thunk that
back to nonisolated(nonsending), causing the final function to always be
concurrent. I put into SILGen a peephole that recognizes this pattern and emits
the correct code.

3. With that in hand, we found that we were emitting nonisolated(nonsending)
parameters for inheritActorContext functions. This was then fixed by @xedin in

With all this in hand, closure literal isolation and all of the other RBI tests
with nonisolated(nonsending) enabled pass.

rdar://154969621
(cherry picked from commit 648bb8fe30)
2025-07-10 17:24:48 -07:00
Doug Gregor
8c34d57874 [Strict memory safety] Eliminate spurious warnings with synthesized Codable
When synthesizing code for Codable conformances involving unsafe types,
make sure to wrap the resulting expressions in "unsafe" when strict memory safety is enabled.

Tweak the warning-emission logic to suppress warnings about spurious
"unsafe" expressions when the compiler generated the "unsafe" itself,
so we don't spam the developer with warnings they can't fix. Also make
the checking for other suppression considerations safe when there are
no source locations, eliminating a potential assertion.

Fixes rdar://153665692.
2025-07-10 09:12:49 -07:00
Doug Gregor
d6deeccc81 [Strict memory safety] Fix "unsafe" checking for the for..in loop
The `$generator` variable we create for the async for..in loop is
`nonisolated(unsafe)`, so ensure that we generate an `unsafe`
expression when we use it. This uncovered some inconsistencies in how
we do `unsafe` checking for for..in loops, so fix those.

Fixes rdar://154775389.

(cherry picked from commit 35628cb503)
2025-07-10 09:12:07 -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
Doug Gregor
457d44c624 [SE-0466] Enable SendableProhibitsMainActorInference by default
This implements the SE-0466 amendment under discussion for all code
with default main-actor isolation.

Finalizes rdar://151029300.
2025-07-09 10:49:03 -07:00
Nate Cook
4f87475f73 [6.2] Allow inout parameters in expression macros (#82824)
Cherry pick of #82370 to 6.2 branch.

---

* **Explanation**: Resolves an issue where sema is flagging inout
expressions in expression macros as invalid, setting up a catch 22 where
Sema emits an error when a parameter has the '&' sigil and type checking
fails when it doesn't.
* **Risk**: Low. It allows inout expressions in one additional context.
* **Testing**: Tested by lit tests, including new tests for the newly
allowed expression.
* **Issue**: rdar://153840234
* **Reviewer**:  @slavapestov
* **Main branch PR**:  https://github.com/swiftlang/swift/pull/82370
2025-07-08 15:00:53 -05:00
Doug Gregor
719fb9ca26 Merge pull request #82848 from DougGregor/isolated-conformance-to-sendablemetatype-proto-error-6.2
[6.2] Diagnose isolated conformance to SendableMetatype-inheriting protocol
2025-07-08 09:09:07 -07:00
John McCall
c0db6b64f6 Merge pull request #82868 from rjmccall/dont-capture-defer-isolation-6.2
[6.2] Don't force a capture of an isolated parameter in defer bodies
2025-07-08 11:58:33 -04:00
Konrad `ktoso` Malawski
0bf7b29acf Merge pull request #82826 from ktoso/pick-wip-correct-isolated-handling-with-caller-isolation 2025-07-08 20:16:41 +09:00
John McCall
fb9e730fc9 Don't force a capture of an isolated parameter in defer bodies.
SILGen already has an exception for this from -enable-actor-data-race-checks,
so there's no need for it, and it causes problems in actor inits.

Fixes rdar://155239032
2025-07-07 23:30:22 -04:00
Doug Gregor
6b4271c3a8 Fixup for 6.2 branch 2025-07-07 14:17:59 -07:00
Doug Gregor
3755a6b7e1 [SE-0470] Promote isolated-conformance-to-sendable-metatype protocol to error
Forming an isolated conformance to a SendableMetatype-inherting
protocol opens up a soundness hole any time the conformance is used.
Reword the recently-introduced diagnostic for this case and promote it
to an error (except when it's preconcurrency).

Fixes rdar://154808002.
2025-07-07 13:17:12 -07:00
Doug Gregor
7b369d02dc [SE-0470] Warn about isolated conformance to SendableMetatype-inheriting protocol
An isolated conformance to a SendableMetatype-inheriting protocol
cannot actually be used in generic code, because the SendableMetatype
requirement itself prevents it. Warn about this case so folks aren't
surprised at runtime.

This is a part of issue #82550 / rdar://154437489.
2025-07-07 13:17:04 -07:00
Hamish Knight
ee5f4df5ee Merge pull request #82775 from hamishknight/simple-fix-6.2
[6.2] [CS] Ensure type variables are eliminated by `Solution::simplifyType`
2025-07-04 10:56:34 +01:00
Alexis Laferrière
1c5c76f2b5 Sema: Type-check @cdecl enums
@cdecl enums are Swift enums representable in C. These enums must have
an integer raw type. They can be referenced from @cdecl functions and
@objc methods. @objc enums are still rejected from @cdecl functions.
2025-07-03 14:11:27 -07:00
Alexis Laferrière
076ae9edb3 Sema: Reject enums marked with both @cdecl and @objc 2025-07-03 14:11:27 -07:00