Commit Graph

28382 Commits

Author SHA1 Message Date
Pavel Yaskevich
d91b4647a7 [TypeChecker] Avoid checking lazy property accessors if they haven't been synthesized yet
Ttheir availability is going to match the property itself.
This is a workaround for lazy type-checking because synthesis
of accessors for such properties requires a reference to
`self` which won't be available because pattern binding
for the variable was skipped.

Resolves: rdar://129359362
Resolves: rdar://159463230
Resolves: https://github.com/swiftlang/swift/issues/84041
(cherry picked from commit 6e7de5c1e0)
2025-10-02 23:27:35 -07:00
Pavel Yaskevich
9ffb30354d Merge pull request #84495 from xedin/dar-157663660-6.2
[6.2][Concurrency] Prevent some extraneous function conversion sendability checking
2025-09-26 09:21:31 -07:00
Pavel Yaskevich
a886c5f38a [Concurrency] Prevent extraneous sendability checking when applying nonisolated(nonsending)
`nonisolated(nonsending)` is not presented in interface types
which means that references to declarations that have this attribute
require a function conversion to apply it. Function conversion
checking should detect that and avoid sendability checking for
situations like that but there is really no conversion there.

(cherry picked from commit ce3310050b)
2025-09-24 12:06:26 -07:00
Pavel Yaskevich
18809358e1 [Concurrency] Avoid Sendable checking for conversions that strip @Sendable from nonisolated(nonsending) function type
The isolation doesn't change in this cause and making function type
non-Sendable means that it woun't be able to leave the current concurrency
domain and the compiler won't generate a thunk.

(cherry picked from commit 40c9674dfd)
2025-09-24 12:06:25 -07:00
Pavel Yaskevich
4699419dab [Concurrency] Check function conversions after their sub-expressions have been checked
Explicit closure expressions gets their isolation inferred during
actor isolation checking, if they appear as a sub-expression to
a function conversion we need to delay function checking until
the closure was processed. It shouldn't matter for other cases.

(cherry picked from commit 61fbaa7af7)
2025-09-24 12:05:59 -07:00
Pavel Yaskevich
641cfe06be [Concurrency] Fix @Sendable closures not inferring nonisolated(nonsending)
If there are no explicit concurrency attributes, isolated parameters,
or captures associated with the closure it should infer `nonisolated(nonsending)`
for the parent conversion injected by the solver (this conversion is injected
because the solver cannot check captures to elide it).

The change pushes `isIsolationInferenceBoundaryClosure` check down
with added benefit of getting preconcurrency context from the parent.

(cherry picked from commit 3ae34e8e68)
2025-09-24 12:05:51 -07:00
Allan Shortlidge
8f61ec2c71 Sema: Don't diagnose isolated deinit availability in skipped function bodies.
As a follow-up fix for https://github.com/swiftlang/swift/pull/83545, avoid
incorrectly diagnosing isolated deinit availability during module emission
jobs, which skip type checking non-inlinable function bodies and therefore
don't build accurate availability scopes for those bodies. It's ok to skip
these diagnostics during module emission since they should still be emitted
during compilation jobs.

Resolves rdar://161178785.
2025-09-23 12:05:26 -07:00
Alexis Laferrière
7a6e8f0e4d Sema: Narrow fix to allow @_spi_available in extensions
Allow referencing an `@_spi_available` decl in extensions to
`@_spi_available` types. This is a narrow fix as it should really be
handled as part of the context check but that check is currently too
permissive.

Fow now let's narrowly allow legal code. And then we should look at
revisiting the SPI availability logic, separate it from normal SPI and
treat more like availability.

Adding a test comparing the behavior of `@_spi` with `@_spi_available`
to document the current implementation.

rdar://159292698
2025-09-16 12:37:32 -07:00
John McCall
7e601ad247 Make sure that we capture the isolation of a defer function that
implicitly uses the isolation.
2025-09-11 21:53:22 -04:00
John McCall
a6c40c75c8 Fix two bugs with the isolation of defer bodies.
The first bug is that we weren't computing isolation correctly for
nested defers. This is an unlikely pattern of code, but it's good to fix.

The second bug is that getActorIsolationOfContext was looking through
defers, but getActorIsolation itself was not. This was causing defer
bodies to be emitted in SILGen without an isolation parameter, which
meant that #isolation could not possibly provide the right value. Fixing
this involves teaching SILGen that non-async functions can have
nonisolated(nonsending) isolation, but that's relatively straightforward.

This commit doesn't fix #isolation or adequately test SILGen, but that'll
be handled in a follow-up.
2025-09-11 21:52:03 -04:00
John McCall
f72fd5aa8f Merge pull request #82930 from nickolas-pohilets/mpokhylets/6.2-weak-let
[6.2] Cherry-pick SE-0481 with source-incompatible changes to closure capture semantics still wrapped into a feature
2025-09-11 20:02:39 -04:00
Konrad `ktoso` Malawski
64e98e66d3 Merge pull request #83997 from ktoso/pick-nonisolatednonsending-by-default-and-distributed 2025-09-05 17:10:34 +09:00
Gábor Horváth
4a394fcfbb Merge pull request #84073 from Xazax-hun/func-ptr-is-not-fragile-on-6.2
[6.2][cxx-interop] Do not consider function types fragile
2025-09-04 10:47:19 +01:00
Gábor Horváth
95cf518be4 [6.2][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-03 11:18:05 +01:00
Konrad Malawski
838bc9ee67 [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 18:58:49 +09:00
Gábor Horváth
fa65cc9b2b [cxx-interop] Add attribute to hide Swift declarations from interop
Explanation: We generate declarations in the C++ interop header with
"unavailable" annotations when we cannot export something to C++. These
declarations can collide with existing names. Previously, there were no
ways to resolve these name collisions. This PR introduces a new
attribute to hide declarations from the interop header.
Issues: rdar://158843666
Original PRs: #82616
Risk: Low, this adds a new, straightforward code path.
Testing: Added a compiler test.
Reviewers: @egorzhdan
2025-09-02 13:28:27 +01:00
Konrad Malawski
011d2d2edc [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-08-29 16:38:10 +09:00
Mykola (Nickolas) Pokhylets
5d70bea757 Cherry-pick SE-0481 with source-incompatible changes to closure capture semantics still wrapped into a feature
See https://github.com/swiftlang/swift/pull/80440 and https://github.com/swiftlang/swift/pull/82732
2025-08-22 10:31:42 +02:00
Pavel Yaskevich
26ba69ab79 [CSSimplify] SE-0324: Use correct conversion when converting array types
If the argument type is an array and it's passed to an imported declaration
that accepts a raw pointer, the solver should use an "array-to-c-pointer"
conversion instead of the one for pointers.

Resolves: rdar://158629300
(cherry picked from commit 29b04f4a63)
2025-08-21 16:00:48 -07:00
Pavel Yaskevich
f408b2db4a Merge pull request #82922 from xedin/nonexhaustive-enums-6.2
[6.2][AST/Sema] SE-0487: Implement `@nonexhaustive` attribute and new enum exhaustivity checking rule
2025-08-20 20:31:07 -07:00
Egor Zhdan
92b7bc7c5e [cxx-interop] Prohibit weak references to foreign reference types
This fixes a runtime crash when a `weak` reference to a C++ foreign reference type is used.

Instead of a runtime crash, Swift would now emit a compiler error saying that `weak` keyword is incompatible with foreign reference types.

rdar://124040825 / resolves https://github.com/swiftlang/swift/issues/83080
(cherry picked from commit 9abadf5483)
2025-08-19 12:19:43 +01:00
Pavel Yaskevich
1d65a560d7 [AST/Sema] SE-0487: Remove @preEnumExtensibility attribute
(cherry picked from commit 0444d297b6)
2025-08-18 23:58:29 -07:00
Pavel Yaskevich
0286f4ab82 [AST/Sema] SE-0487: Expand @nonexhaustive attribute to support warn argument
The spelling `@nonexhaustive(warn)` replaces `@preEnumExtensibility`
attriubte.

(cherry picked from commit 43eec8fede)
2025-08-18 23:58:29 -07:00
Pavel Yaskevich
c9722333b9 [AST/Sema] SE-0487: Rename @extensible into @nonexhaustive
This is an accepted spelling for the attribute. This commit
also renames the feature flag from `ExtensibleAttribute` to
`NonexhaustiveAttribute` to match the spelling of the attribute.

(cherry picked from commit fe1ae75711)
2025-08-18 23:58:27 -07:00
Pavel Yaskevich
cefa0b2fbf [AST/Sema] Add @preEnumExtensibility attribute to downgrade warnings for extensible enums
Just like `@preconcurrency` for concurrency, this attribute is going
to allow exhaustiveness error downgrades for enums that were retroactively
marked as `@extensible`.

(cherry picked from commit 498430afaf)
2025-08-18 23:58:03 -07:00
Pavel Yaskevich
bcf38a1aef [AST/Sema] Add @extensible attribute on enum declarations
This attribute controls whether cross-module access to the declaration
needs `@unknown default:` because it's allowed to gain new cases even
if the module is non-resilient.

(cherry picked from commit a0ae93d3a8)
2025-08-18 23:56:49 -07:00
Gábor Horváth
037e348526 Merge pull request #83691 from Xazax-hun/check-safety-function-types-on-6.2
[6.2][StrictMemorySafety] Check the safety of return types of calls
2025-08-14 20:27:53 +01:00
Pavel Yaskevich
7df1fe7289 Merge pull request #83698 from xedin/rdar-156955193-6.2
[6.2][TypeCheckEffects] AbstractFunction: Parameter types should be mapped…
2025-08-13 22:34:27 -07:00
Allan Shortlidge
92220f223b Merge pull request #83658 from tshortli/isolated-deinit-inlining-availability-6.2
[6.2] Sema: Fix isolated deinit availability checking
2025-08-13 16:32:37 -07:00
Pavel Yaskevich
7f652915d9 [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)
2025-08-13 10:19:51 -07:00
Gábor Horváth
99f9c318ca [6.2][StrictMemorySafety] Check the safety of return types of calls
Explanation: There were some scenarios where we could call an unsafe
function without marking the expression as unsafe. These affect mostly
cases where the function's result is passed to another function or
returned. This PR makes sure we always flag functions with unsafe return
types, even if their result is not stored anywhere for later use.
Issues: rdar://157237301
Original PRs: #83520
Risk: Low, worst case scenario the user has to add redundant unsafe
keywords in strict memory safe mode.
Testing: Added a compiler test.
Reviewers: @DougGregor
2025-08-13 11:49:22 +01:00
Artem Chikin
fe4880072e Merge pull request #82939 from artemcm/62_DepScanExplicitInput
[6.2 🍒][Dependency Scanning] Consider `-swift-module-file` inputs when looking for dependencies
2025-08-12 23:00:13 -07:00
Allan Shortlidge
f613a40c85 Sema: Fix isolated deinit availability checking.
The availability of isolated deinits were being misdiagnosed for nominal types
that were available prior to the module's deployment target when
`-target-min-inlining-version` was specified. Only the body of an isolated
deinit uses runtime functionality that requires a minimum runtime version, so
availability must be checked in the context of the body of the deinit, not the
interface of the deinit.

Resolves rdar://157563752.
2025-08-11 16:53:38 -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
Artem Chikin
89268e9b4d [6.2 🍒][Dependency Scanning] Consider '-swift-module-file' inputs when looking for dependencies
Previously this flag was only used to pass explicit dependencies to compilation tasks. This change adds support for the dependency scanner to also consider these inputs when resolving dependencies.

Resolves https://github.com/swiftlang/swift-driver/issues/1951
2025-07-18 10:12:22 -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