Commit Graph

28382 Commits

Author SHA1 Message Date
Pavel Yaskevich
c68a17b8c4 [Concurrency] Don't infer nonisolated(nonsending) on synchronous witnesses
If the requirement is `nonisolated(nonsending)` but witness is
synchronous, prevent actor isolation inference from requirements
because this isolation only applies to asynchronous declarations
at the moment.

Resolves: rdar://153680826
(cherry picked from commit a964282275)
2025-06-23 13:58:07 -07:00
Meghana Gupta
3e359e4774 Support lifetime dependence inference on enum elements 2025-06-23 13:48:57 -07:00
Slava Pestov
f9a6432ffd Sema: Expand isolated conformance inference to consider conformances to inherited protocols
Otherwise, if PDerived inherits from P and P's requirements are
witnessed by @MainActor-isolated members, we fail to infer
@MainActor isolation on the conformance to PDerived.

- Fixes https://github.com/swiftlang/swift/issues/82222.
- Fixes rdar://153219831.
2025-06-23 15:19:55 -04:00
Michael Gottesman
25c2359cca Merge pull request #82379 from gottesmm/release/6.2-152522631
[6.2][sema] Work around a double curry thunk actor isolation inference bug that is a knock on effect of ced96aa5cd.
2025-06-20 20:41:59 -07:00
Michael Gottesman
845e1bcd14 [sema] Work around a double curry thunk actor isolation inference bug that is a knock on effect of ced96aa5cd.
Specifically, there is currently a bug in TypeCheckConcurrency.cpp where we do
not visit autoclosures. This causes us to never set the autoclosure's
ActorIsolation field like all other closures. For a long time we were able to
get away with this just by relying on the isolation of the decl context of the
autoclosure... but with the introduction of nonisolated(nonsending), we found
cases where the generated single curry autoclosure would necessarily be
different than its decl context (e.x.: a synchronous outer curry thunk that is
nonisolated that returns an inner curry thunk that is
nonisolated(nonsending)). This problem caused us to hit asserts later in the
compiler since the inner closure was actually nonisolated(nonsending), but we
were thinking that it should have been concurrent.

To work around this problem, I changed the type checker in
ced96aa5cd to explicitly set the isolation of
single curry thunk autoclosures when it generates them. The reason why we did
this is that it made it so that we did not have to have a potential large source
break in 6.2 by changing TypeCheckConcurrency.cpp to visit all autoclosures it
has not been visiting.

This caused a follow on issue where since we were now inferring the inner
autoclosure to have the correct isolation, in cases where we were creating a
double curry thunk for an access to a global actor isolated field of a
non-Sendable non-global actor isolated nominal type, we would have the outer
curry thunk have unspecified isolation instead of main actor isolation. An
example of this is the following:

```swift
class A {
  var block:  @MainActor () -> Void = {}
}

class B {
  let a = A()

  func d() {
    a.block = c // Error! Passing task isolated 'self' to @MainActor closure.
  }

  @MainActor
  func c() {}
}
```

This was unintentional. To work around this, this commit changes the type
checker to explicitly set the double curry thunk isolation to the correct value
when the type checker generates the double curry thunk in the same manner as it
does for single curry thunks and validates that if we do set the value to
something explicitly that it has the same value as the single curry thunk.

rdar://152522631
(cherry picked from commit c28490b527)
2025-06-20 11:05:24 -07:00
Pavel Yaskevich
cb9925d556 [CSSimplify] VariadicGenerics: Don't attempt to wrap optional into one-element tuple
If we have a tuple with unresolved pack expansion on one side
and an optional type on the other, prevent `matchTypes` from
wrapping optional into a one-element tuple because the matching
should be handled as part of the optional injection.

Resolves: rdar://152940244
(cherry picked from commit 6cfbafd968)
2025-06-20 00:15:07 -07:00
Hamish Knight
8f66e610df Merge pull request #82340 from hamishknight/record-opened-6.2 2025-06-19 08:19:06 +01:00
Hamish Knight
612f7b213c Merge pull request #82319 from hamishknight/keyed-6.2
[6.2] [Sema] Tighten up function call check in `resolveKeyPathExpr`
2025-06-18 18:24:30 +01:00
Pavel Yaskevich
3d1c592456 Merge pull request #82324 from xedin/SE-0449-fixes-6.2
[6.2][Concurrency] SE-0449: Fix a few issues related to `nonisolated` type declarations
2025-06-18 09:14:07 -07:00
Hamish Knight
4d654b271f [CS] Distinguish locators for generic args in addSpecializationConstraint
Make sure we give each argument a different locator to ensure we
can correctly record any opened types.

rdar://153674889
2025-06-18 16:21:45 +01:00
Pavel Yaskevich
06d1224aec [Concurrency] SE-0449: nonisolated on a type should prevent isolation inference from protocol requirements
If a `nonisolated` type conforms to a global-isolated protocol
the witnesses to the protocol requirements should infer the
isolation from the protocol but instead be `nonisolated`.

Resolves: rdar://145519840
(cherry picked from commit e50acbf3d5)
2025-06-17 15:29:18 -07:00
Pavel Yaskevich
7d0e2b51fd [Concurrency] SE-0449: Implied conformances to nonisolated protocols make witnesses nonisolated
Even if the requirement is stated on an isolated protocol if the
conformance is implied by a nonisolated one all of the requirements
and witnesses should be nonisolated.

(cherry picked from commit 06be7bda39)
2025-06-17 15:29:18 -07:00
Sima Nerush
a0b0a0704d [Concurrency] Make sure to still infer global actor if the conformance kind is explicit.
(cherry picked from commit 7e42a19187)
2025-06-17 15:29:18 -07:00
Sima Nerush
0ed6c1cf41 [Concurrency] Look for explicit 'nonisolated' when getting isolation from protocol conformances.
(cherry picked from commit 4fca62a771)
2025-06-17 15:29:18 -07:00
Hamish Knight
f95eb6bf48 [Sema] Tighten up function call check in resolveKeyPathExpr
Check for `CallExpr` instead of `ApplyExpr`, we don't support
arbitrary postfix operators in key paths.
2025-06-17 23:07:55 +01:00
Hamish Knight
61e0326b7f [Sema] Add null check in createMemberwiseInitParameter
The property wrapper initializer info may be null if e.g we had a
request cycle, just use the property's interface type in that case.

rdar://82899428
2025-06-17 17:31:24 +01:00
Pavel Yaskevich
cd83345651 [Concurrency] NonisolatedNonsendingByDefault: Don't attempt to migrate $ prefixed declarations
These are special declarations that are synthesized by the compiler
or a macro and warnings about them are non-actionable.

(cherry picked from commit 17976c7775)
2025-06-16 13:44:02 -07:00
Pavel Yaskevich
93c955c4f9 [Concurrency] NonisolatedNonsendingByDefault: Don't attempt to migrate swift-testing test cases
The execution of these functions is controlled by the testing
framework and it's okay if their behavior changes when the feature
flag is enabled.

Resolves: rdar://152687527
(cherry picked from commit f2f5d234dc)
2025-06-16 13:44:02 -07:00
Slava Pestov
acb53d2050 Merge pull request #82132 from slavapestov/bind-extensions-macro-6.2
[6.2] Sema: Don't expand macros when binding extensions
2025-06-14 12:14:10 -04:00
Hamish Knight
c1db12a0d4 Merge pull request #82239 from hamishknight/cod-cycle-6.2
[6.2] [Sema] Scale back CodingKeys hack in `TypeChecker::lookupUnqualifiedType`
2025-06-14 12:54:54 +01:00
Slava Pestov
a10cc92bf7 Sema: Try to bind extensions without expanding macros
Macro expansion can call typeCheckExpr(), which performs qualified
lookups. So if we expand macros while binding extensions, these
qualified lookups can fail because they cannot find members of
extensions that have not been bound yet.

To fix this, try binding extensions without performing macro
expansion first. If any extensions remain at the end, we fall back
to the old behavior, and try to bind them again, this time
performing macro expansion.

Fixes rdar://149798059.
2025-06-13 16:00:16 -04:00
Slava Pestov
15f968f269 AST: Call setExtendedNominal() instead of cacheOutput(ExtendedNominalRequest...) 2025-06-13 16:00:16 -04:00
Hamish Knight
9ba1da9a51 [Sema] Scale back CodingKeys hack in TypeChecker::lookupUnqualifiedType
I previously added this hack to match the logic in
`TypeChecker::lookupUnqualified`, but it turns out that can introduce
request cycles for cases where `CodingKeys` is used in a generic
requirement for one of `Codable`'s potential value witnesses. Scale
back the hack such that it's only done when we get an initial empty
lookup result, ensuring we maintain source compatibility. Both these
lookup hacks should go away once we properly handle CodingKeys 
synthesis.

rdar://153096639
2025-06-13 19:56:02 +01:00
Pavel Yaskevich
c17d370158 Merge pull request #82219 from xedin/rdar-152687353-6.2
[6.2][Concurrency] NonisolatedNonsendingByDefault: Migration applies only to the current module declarations
2025-06-13 09:14:47 -07:00
Hamish Knight
19bc18f064 Merge pull request #82152 from hamishknight/fix-nested-arenas-6.2 2025-06-13 08:01:57 +01:00
Pavel Yaskevich
c0984d2bf5 [Concurrency] NonisolatedNonsendingByDefault/NFC: Move 'same module' check higher
This is a follow-up to https://github.com/swiftlang/swift/pull/82173
which moves the check higher to make it an early exist.

(cherry picked from commit f0502c2257)
2025-06-12 10:42:32 -07:00
Pavel Yaskevich
20e086cdc7 [Concurrency] NonisolatedNonsendingByDefault: Migration applies only to the current module declarations
Prevent migration from handling declarations that come from a
module different from the current one, this is primarily a
problem for swiftinterfaces that can get rebuilt when the module
is imported by a model that has migration mode enabled.

Resolves: rdar://152687353
(cherry picked from commit bcd6caaea4)
2025-06-12 10:19:46 -07:00
Pavel Yaskevich
36f376f50c [TypeChecker] Remove @_inheritActorContext effect from synchronous types
The compiler would previously accept use of `@_inheritActorContext`
on a parameter with a synchronous function type which wasn't marked
as `@isolated(any)`. That is incorrect because in such cases the
attribute has no effect and furthermore would prevent Sendable
and isolation checking.

Uses like that are currently diagnosed by the type-checker but we
need to go one step further and remove the effect in such case to
prevent invalid uses.

Resolves: rdar://143581268
(cherry picked from commit dbe19b6d5f)
2025-06-12 08:54:12 -07:00
Meghana Gupta
58b714b968 Introduce a new suppressible experimental feature to guard @_lifetime 2025-06-11 08:03:40 -07:00
Meghana Gupta
a9b831788d Update spelling for representing lifetime dependencies to @_lifetime 2025-06-11 08:03:39 -07:00
Anthony Latsis
5b71a7dd7a Merge pull request #82156 from AnthonyLatsis/camellia-sinensis-6.2
[6.2] Sema: Never record argument label mismatches for unlabeled trailing closures
2025-06-11 04:49:31 +01:00
Pavel Yaskevich
27cd5eef15 Merge pull request #82114 from xedin/rdar-150689994-6.2
[6.2][Diagnostics] Correctly diagnose situations when immutable value is passed to `inout` parameter
2025-06-10 09:11:10 -07:00
Pavel Yaskevich
dfd21ba699 Merge pull request #82130 from xedin/rdar-151943924-6.2
[6.2][TypeCheckEffects] Fix `AbstractFunction::getType` to look through al…
2025-06-10 08:44:12 -07:00
Hamish Knight
bd4c3adb92 [CS] Make CSRanking change slightly less risky for 6.2
Check for `isSolverAllocated` rather than
`hasTypeVariableOrPlaceholder`, and downgrade an `ASSERT` to
`CONDITIONAL_ASSERT`.
2025-06-10 16:18:24 +01:00
Hamish Knight
cc66fc886f [CS] Avoid solver-allocated inputs with typesSatisfyConstraint
Escaping solver-allocated types into a nested allocation arena is
problematic since we can e.g lazily compute the `ContextSubMap` for a
`NominalOrBoundGenericNominalType`, which is then destroyed when we
exit the nested arena. Ensure we don't pass any types with type
variables or placeholders to `typesSatisfyConstraint`.

rdar://152763265
2025-06-10 16:08:00 +01:00
Anthony Latsis
da1922f0bf Sema: Never record argument label mismatches for unlabeled trailing closures
Fixes a crash on invalid. The previous logic was causing a label
mismatch constraint fix to be recorded for an unlabeled trailing closure
argument matching a variadic paramater after a late recovery argument
claim in `matchCallArgumentsImpl`, because the recovery claiming skips
arguments matching defaulted parameters, but not variadic ones. We may
want to reconsider that last part, but currently it regresses the
quality of some diagnostics, and this is a targeted fix.

The previous behavior is fine because the diagnosis routine associate
with the constraint fix (`diagnoseArgumentLabelError`) skips unlabeled
trailing closures when tallying labeling issues — *unless* there are no
other issues and the tally is zero, which we assert it is not.

Fixes rdar://152313388.

(cherry picked from commit f2e1420a90)
2025-06-10 15:28:41 +01:00
Pavel Yaskevich
f8a3c3b1ff [TypeCheckEffects] Fix AbstractFunction::getType to look through all levels of optional
A member and a parameter could be wrapped in an arbitrary number
of `Optional`, we need to look through all of them to get to the
underlying function type.

Resolves: rdar://151943924
(cherry picked from commit 86390ab91f)
2025-06-09 17:18:11 -07:00
Pavel Yaskevich
a9e5a8a07a [Diagnostics] Correctly diagnose situations when immutable value is passed to inout parameter
Currently the note is going to point to the "callee" but that is
incorrect when the failure is related to an argument of a call.

Detect this situation in `RValueTreatedAsLValueFailure::diagnoseAsNote`
and produce a correct note.

Resolves: rdar://150689994
(cherry picked from commit 6bbc101a98)
2025-06-09 09:39:12 -07:00
Pavel Yaskevich
3ca6228246 [ConstraintSystem] Look through ArgumentAttribute element when computing a callee
This element is used purely for diagnostic purposes and locator
should behavior as if it's completely transparent.

(cherry picked from commit 622475bf15)
2025-06-09 09:39:12 -07:00
Pavel Yaskevich
47925188ba [Concurrency] Fix SendableMetatype conformance failures to behave like Sendable ones
No warnings with minimal checking, warnings with `strict-concurrency=complete` and
if declaration is `@preconcurrency` until next major swift version.

Resolves: rdar://151911135
Resolves: https://github.com/swiftlang/swift/issues/81739
(cherry picked from commit e326cd00930ff042ba1595e7793af9aaf0208b97)
2025-06-06 08:51:05 -07:00
Pavel Yaskevich
2a3e2d0800 Merge pull request #82037 from xedin/rdar-146027395-6.2
[6.2][Concurrency] Downgrade errors to warnings when Sendable requirement is inferred from a preconcurrency protocol
2025-06-06 08:49:20 -07:00
Allan Shortlidge
f82c0b71a2 Sema: Fix an unused variable warning. 2025-06-05 15:44:27 -07:00
Pavel Yaskevich
fe2c8ef00a [Diagnostics] Use contains instead of has when non-Sendable type appears inside of a member type
(cherry picked from commit 3495c61840)
2025-06-05 14:48:20 -07:00
Pavel Yaskevich
ace1e527d1 [Concurrency] Downgrade errors to warnings when Sendable requirement is inferred from a preconcurrency protocol
If a type gets `Sendable` conformace requirement through another
`@preconcurrency` protocol the error should be downgraded even
with strict concurrency checking to allow clients time to address
the new requirement.

Resolves: rdar://146027395
(cherry picked from commit 6d45229367)
2025-06-05 14:48:01 -07:00
Pavel Yaskevich
a947450a4d [Concurrency] NFC: Rename ImpliedByStandardProtocol to ImpliedByPreconcurrencyProtocol
The new name better reflects the intention for this Sendable check kind.

(cherry picked from commit 7cca7225a1)
2025-06-05 14:48:01 -07:00
Pavel Yaskevich
82f04e5b78 [Concurrency] Clean-up duplicate code in checkSendableInstanceStorage
(cherry picked from commit e24196e365)
2025-06-05 14:48:01 -07:00
Pavel Yaskevich
608a37ad04 Merge pull request #81997 from xedin/using-for-default-isolation-in-file-context-6.2
[6.2][AST/Sema] SE-0478: Implement using declaration under an experimental flag
2025-06-05 00:44:21 -07:00
Doug Gregor
3604f8918c Merge pull request #81985 from DougGregor/non-sendable-metatype-capture-diag-fixes-6.2
[6.2] Minor fixes for the non-sendable meta type capture diagnostics
2025-06-04 17:59:17 -07:00
Pavel Yaskevich
f72efd8327 [TypeChecker] Disable use of using in macro extensions
(cherry picked from commit 83ac0f5042)
2025-06-04 13:23:17 -07:00
Pavel Yaskevich
e5501d485a [AST] NFC: Capitalize UsingSpecifier::nonisolated for consistency
(cherry picked from commit 21ec5924f7)
2025-06-04 13:22:34 -07:00