Skip to content

Commit 7266ff4

Browse files
committed
[SE-0470] Enable isolated conformances by default
The IsolatedConformances feature moves to a normal, supported feature. Remove all of the experimental-feature flags on test cases and such. The InferIsolatedConformances feature moves to an upcoming feature for Swift 7. This should become an adoptable feature, adding "nonisolated" where needed.
1 parent a0ba18d commit 7266ff4

19 files changed

+68
-76
lines changed

CHANGELOG.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,34 @@
55
66
## Swift 6.2
77

8+
* [SE-0470][]:
9+
A protocol conformance can be isolated to a specific global actor, meaning that the conformance can only be used by code running on that actor. Isolated conformances are expressed by specifying the global actor on the conformance itself:
10+
11+
```swift
12+
protocol P {
13+
func f()
14+
}
15+
16+
@MainActor
17+
class MyType: @MainActor P {
18+
/*@MainActor*/ func f() {
19+
// must be called on the main actor
20+
}
21+
}
22+
```
23+
24+
Swift will produce diagnostics if the conformance is directly accessed in code that isn't guaranteed to execute in the same global actor. For example:
25+
26+
```swift
27+
func acceptP<T: P>(_ value: T) { }
28+
29+
/*nonisolated*/ func useIsolatedConformance(myType: MyType) {
30+
acceptP(myType) // error: main actor-isolated conformance of 'MyType' to 'P' cannot be used in nonisolated context
31+
}
32+
```
33+
34+
To address such issues, only use an isolated conformance from code that executes on the same global actor.
35+
836
* [SE-0419][]:
937
Introduced the new `Runtime` module, which contains a public API that can
1038
generate backtraces, presently supported on macOS and Linux. Capturing a
@@ -10732,6 +10760,7 @@ using the `.dynamicType` member to retrieve the type of an expression should mig
1073210760
[SE-0442]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0442-allow-taskgroup-childtaskresult-type-to-be-inferred.md
1073310761
[SE-0444]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0444-member-import-visibility.md
1073410762
[SE-0458]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0458-strict-memory-safety.md
10763+
[SE-0470]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0470-isolated-conformances.md
1073510764
[#64927]: <https://github.com/apple/swift/issues/64927>
1073610765
[#42697]: <https://github.com/apple/swift/issues/42697>
1073710766
[#42728]: <https://github.com/apple/swift/issues/42728>

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8513,10 +8513,6 @@ ERROR(attr_abi_failable_mismatch,none,
85138513
//===----------------------------------------------------------------------===//
85148514
// MARK: Isolated conformances
85158515
//===----------------------------------------------------------------------===//
8516-
GROUPED_ERROR(isolated_conformance_experimental_feature,IsolatedConformances,
8517-
none,
8518-
"isolated conformances require experimental feature "
8519-
" 'IsolatedConformances'", ())
85208516
NOTE(note_isolate_conformance_to_global_actor,none,
85218517
"isolate this conformance to the %select{global actor %0|main actor}1 "
85228518
"with '@%2'", (Type, bool, StringRef))

include/swift/Basic/Features.def

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ SUPPRESSIBLE_LANGUAGE_FEATURE(MemorySafetyAttributes, 458, "@unsafe attribute")
254254
LANGUAGE_FEATURE(ValueGenerics, 452, "Value generics feature (integer generics)")
255255
LANGUAGE_FEATURE(RawIdentifiers, 451, "Raw identifiers")
256256
LANGUAGE_FEATURE(SendableCompletionHandlers, 463, "Objective-C completion handler parameters are imported as @Sendable")
257+
LANGUAGE_FEATURE(IsolatedConformances, 407, "Global-actor isolated conformances")
257258

258259
// Swift 6
259260
UPCOMING_FEATURE(ConciseMagicFile, 274, 6)
@@ -276,6 +277,7 @@ UPCOMING_FEATURE(GlobalActorIsolatedTypesUsability, 0434, 6)
276277
ADOPTABLE_UPCOMING_FEATURE(ExistentialAny, 335, 7)
277278
UPCOMING_FEATURE(InternalImportsByDefault, 409, 7)
278279
UPCOMING_FEATURE(MemberImportVisibility, 444, 7)
280+
UPCOMING_FEATURE(InferIsolatedConformances, 470, 7)
279281

280282
// Optional language features / modes
281283

@@ -495,12 +497,6 @@ ADOPTABLE_EXPERIMENTAL_FEATURE(AsyncCallerExecution, false)
495497
/// Allow custom availability domains to be defined and referenced.
496498
EXPERIMENTAL_FEATURE(CustomAvailability, true)
497499

498-
/// Allow isolated conformances.
499-
EXPERIMENTAL_FEATURE(IsolatedConformances, true)
500-
501-
/// Infer conformance isolation on global-actor-conforming types.
502-
EXPERIMENTAL_FEATURE(InferIsolatedConformances, true)
503-
504500
/// Allow SwiftSettings
505501
EXPERIMENTAL_FEATURE(SwiftSettings, false)
506502

lib/AST/ConformanceLookup.cpp

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -386,33 +386,30 @@ static ProtocolConformanceRef getBuiltinMetaTypeTypeConformance(
386386
// All metatypes are Copyable, Escapable, and BitwiseCopyable.
387387
if (auto kp = protocol->getKnownProtocolKind()) {
388388
switch (*kp) {
389-
case KnownProtocolKind::Sendable:
389+
case KnownProtocolKind::Sendable: {
390390
// Metatypes are generally Sendable, but with isolated conformances we
391391
// cannot assume that metatypes based on type parameters are Sendable.
392392
// Therefore, check for conformance to SendableMetatype.
393-
if (ctx.LangOpts.hasFeature(Feature::IsolatedConformances)) {
394-
auto sendableMetatypeProto =
395-
ctx.getProtocol(KnownProtocolKind::SendableMetatype);
396-
if (sendableMetatypeProto) {
397-
Type instanceType = metatypeType->getInstanceType();
398-
399-
// If the instance type is a type parameter, it is not necessarily
400-
// Sendable. There will need to be a Sendable requirement.
401-
if (instanceType->isTypeParameter())
402-
break;
403-
404-
// If the instance type conforms to SendableMetatype, then its
405-
// metatype is Sendable.
406-
auto instanceConformance = lookupConformance(
407-
instanceType, sendableMetatypeProto);
408-
if (instanceConformance.isInvalid() ||
409-
instanceConformance.hasMissingConformance())
410-
break;
411-
}
412-
413-
// Every other metatype is Sendable.
393+
auto sendableMetatypeProto =
394+
ctx.getProtocol(KnownProtocolKind::SendableMetatype);
395+
if (sendableMetatypeProto) {
396+
Type instanceType = metatypeType->getInstanceType();
397+
398+
// If the instance type is a type parameter, it is not necessarily
399+
// Sendable. There will need to be a Sendable requirement.
400+
if (instanceType->isTypeParameter())
401+
break;
402+
403+
// If the instance type conforms to SendableMetatype, then its
404+
// metatype is Sendable.
405+
auto instanceConformance = lookupConformance(
406+
instanceType, sendableMetatypeProto);
407+
if (instanceConformance.isInvalid() ||
408+
instanceConformance.hasMissingConformance())
409+
break;
414410
}
415411
LLVM_FALLTHROUGH;
412+
}
416413

417414
case KnownProtocolKind::Copyable:
418415
case KnownProtocolKind::Escapable:

lib/Parse/ParseType.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,7 @@ ParserResult<TypeRepr> Parser::parseTypeSimple(
163163
Diag<> MessageID, ParseTypeReason reason) {
164164
ParserResult<TypeRepr> ty;
165165

166-
if (isParameterSpecifier() &&
167-
!(!Context.LangOpts.hasFeature(Feature::IsolatedConformances) &&
168-
Tok.isContextualKeyword("isolated"))) {
166+
if (isParameterSpecifier()) {
169167
// Type specifier should already be parsed before here. This only happens
170168
// for construct like 'P1 & inout P2'.
171169
diagnose(Tok.getLoc(), diag::attr_only_on_parameters, Tok.getRawText());

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3020,10 +3020,9 @@ namespace {
30203020
// FIXME: When passing to a sending parameter, should this be handled
30213021
// by region isolation? Or should it always be handled by region
30223022
// isolation?
3023-
if (ctx.LangOpts.hasFeature(Feature::IsolatedConformances) &&
3024-
(mayExecuteConcurrentlyWith(
3023+
if (mayExecuteConcurrentlyWith(
30253024
localFunc.getAsDeclContext(), getDeclContext()) ||
3026-
(explicitClosure && explicitClosure->isPassedToSendingParameter()))) {
3025+
(explicitClosure && explicitClosure->isPassedToSendingParameter())) {
30273026
GenericSignature genericSig;
30283027
if (auto afd = localFunc.getAbstractFunctionDecl())
30293028
genericSig = afd->getGenericSignature();
@@ -7922,8 +7921,6 @@ ConformanceIsolationRequest::evaluate(Evaluator &evaluator, ProtocolConformance
79227921

79237922
auto dc = rootNormal->getDeclContext();
79247923
ASTContext &ctx = dc->getASTContext();
7925-
if (!ctx.LangOpts.hasFeature(Feature::IsolatedConformances))
7926-
return ActorIsolation::forNonisolated(false);
79277924

79287925
// If the protocol itself is isolated, don't infer isolation for the
79297926
// conformance.

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2567,13 +2567,6 @@ checkIndividualConformance(NormalProtocolConformance *conformance) {
25672567
ComplainLoc, diag::unchecked_conformance_not_special, ProtoType);
25682568
}
25692569

2570-
// Complain if the global-actor-isolated conformances are not enabled.
2571-
if (conformance->isIsolated() &&
2572-
!Context.LangOpts.hasFeature(Feature::IsolatedConformances)) {
2573-
Context.Diags.diagnose(
2574-
ComplainLoc, diag::isolated_conformance_experimental_feature);
2575-
}
2576-
25772570
bool allowImpliedConditionalConformance = false;
25782571
if (Proto->isSpecificProtocol(KnownProtocolKind::Sendable)) {
25792572
// In -swift-version 5 mode, a conditional conformance to a protocol can imply
@@ -4971,8 +4964,7 @@ static void diagnoseConformanceIsolationErrors(
49714964
}
49724965

49734966
// Suggest isolating the conformance, if possible.
4974-
if (ctx.LangOpts.hasFeature(Feature::IsolatedConformances) &&
4975-
potentialIsolation && potentialIsolation->isGlobalActor() &&
4967+
if (potentialIsolation && potentialIsolation->isGlobalActor() &&
49764968
!conformance->isIsolated()) {
49774969
bool isMainActor = false;
49784970
Type globalActorIsolation = potentialIsolation->getGlobalActor();

test/Concurrency/Runtime/isolated_conformance.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
// RUN: %target-run-simple-swift(-enable-experimental-feature IsolatedConformances -target %target-swift-5.1-abi-triple) | %FileCheck %s
1+
// RUN: %target-run-simple-swift(-target %target-swift-5.1-abi-triple) | %FileCheck %s
22

33
// REQUIRES: executable_test
44
// REQUIRES: concurrency
55
// REQUIRES: concurrency_runtime
6-
// REQUIRES: swift_feature_IsolatedConformances
76
// UNSUPPORTED: back_deployment_runtime
87

98
// FIXME: WebAssembly doesn't currently have a good way to install the

test/Concurrency/isolated_conformance.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
// RUN: %target-swift-frontend -typecheck -verify -target %target-swift-5.1-abi-triple -swift-version 5 -strict-concurrency=complete -enable-experimental-feature IsolatedConformances %s
1+
// RUN: %target-swift-frontend -typecheck -verify -target %target-swift-5.1-abi-triple -swift-version 5 -strict-concurrency=complete %s
22

3-
// REQUIRES: swift_feature_IsolatedConformances
43
// REQUIRES: concurrency
54

65
protocol P {

test/Concurrency/isolated_conformance_default_actor.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
// RUN: %target-swift-frontend -typecheck -verify -target %target-swift-5.1-abi-triple -swift-version 6 -enable-experimental-feature IsolatedConformances -default-isolation MainActor %s
1+
// RUN: %target-swift-frontend -typecheck -verify -target %target-swift-5.1-abi-triple -swift-version 6 -default-isolation MainActor %s
22

3-
// REQUIRES: swift_feature_IsolatedConformances
43
// REQUIRES: concurrency
54

65
nonisolated

test/Concurrency/isolated_conformance_inference.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
// RUN: %target-swift-frontend -typecheck -verify -target %target-swift-5.1-abi-triple -swift-version 6 -enable-experimental-feature IsolatedConformances -enable-experimental-feature InferIsolatedConformances %s
1+
// RUN: %target-swift-frontend -typecheck -verify -target %target-swift-5.1-abi-triple -swift-version 6 -enable-upcoming-feature InferIsolatedConformances %s
22

3-
// REQUIRES: swift_feature_IsolatedConformances
4-
// REQUIRES: swift_feature_InferIsolatedConformances
53
// REQUIRES: concurrency
64

75
protocol P {

test/Concurrency/sendable_metatype.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
// RUN: %target-typecheck-verify-swift -swift-version 6 -enable-experimental-feature IsolatedConformances -emit-sil -o /dev/null
1+
// RUN: %target-typecheck-verify-swift -swift-version 6 -emit-sil -o /dev/null
22

33
// REQUIRES: concurrency
4-
// REQUIRES: swift_feature_IsolatedConformances
54

65

76
protocol Q {

test/Concurrency/sendable_metatype_typecheck.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
// RUN: %target-typecheck-verify-swift -swift-version 6 -enable-experimental-feature IsolatedConformances
1+
// RUN: %target-typecheck-verify-swift -swift-version 6
22

33
// REQUIRES: concurrency
4-
// REQUIRES: swift_feature_IsolatedConformances
54

65
// This test checks for typecheck-only diagnostics involving non-sendable
76
// metatypes.

test/IRGen/isolated_conformance.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
// RUN: %target-swift-frontend -primary-file %s -emit-ir -swift-version 6 -enable-experimental-feature IsolatedConformances | %FileCheck %s -DINT=i%target-ptrsize
1+
// RUN: %target-swift-frontend -primary-file %s -emit-ir -swift-version 6 | %FileCheck %s -DINT=i%target-ptrsize
22

33
// REQUIRES: PTRSIZE=64
44
// REQUIRES: concurrency
5-
// REQUIRES: swift_feature_IsolatedConformances
65
// UNSUPPORTED: CPU=arm64e
76

87
protocol P {

test/ModuleInterface/isolated_conformance.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
// RUN: %target-swift-frontend -typecheck -swift-version 6 -enable-library-evolution -module-name isolated_conformance -enable-experimental-feature IsolatedConformances -emit-module-interface-path - %s | %FileCheck %s
1+
// RUN: %target-swift-frontend -typecheck -swift-version 6 -enable-library-evolution -module-name isolated_conformance -emit-module-interface-path - %s | %FileCheck %s
22

3-
// REQUIRES: swift_feature_IsolatedConformances
43
// REQUIRES: concurrency
54

65
public protocol MyProtocol {

test/SILOptimizer/constant_propagation_casts_ossa.sil

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
// RUN: %target-sil-opt -enable-sil-verify-all %s -diagnostic-constant-propagation -enable-experimental-feature IsolatedConformances | %FileCheck %s
1+
// RUN: %target-sil-opt -enable-sil-verify-all %s -diagnostic-constant-propagation | %FileCheck %s
22

3-
// REQUIRES: swift_feature_IsolatedConformances
43
// REQUIRES: concurrency
54

65
sil_stage canonical

test/SILOptimizer/isolated_conformances.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-build-swift -parse-as-library -O %s -enable-experimental-feature IsolatedConformances -o %t/a.out
2+
// RUN: %target-build-swift -parse-as-library -O %s -o %t/a.out
33
// RUN: %target-codesign %t/a.out
44
// RUN: %target-run %t/a.out | %FileCheck %s --check-prefix=CHECK-OUTPUT
55

6-
// RUN: %target-build-swift -parse-as-library -O %s -enable-experimental-feature IsolatedConformances -Xllvm -sil-disable-pass=function-signature-opts -emit-sil | %FileCheck %s
6+
// RUN: %target-build-swift -parse-as-library -O %s -Xllvm -sil-disable-pass=function-signature-opts -emit-sil | %FileCheck %s
77

88
// REQUIRES: concurrency
99
// REQUIRES: executable_test
1010
// REQUIRES: concurrency_runtime
11-
// REQUIRES: swift_feature_IsolatedConformances
1211
// REQUIRES: OS=macosx || OS=linux-gnu
1312

1413
// UNSUPPORTED: back_deployment_runtime

test/SILOptimizer/simplify_unconditional_check_cast.sil

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
// RUN: %target-sil-opt -enable-sil-verify-all %s -simplification -simplify-instruction=unconditional_checked_cast -enable-experimental-feature IsolatedConformances | %FileCheck %s
1+
// RUN: %target-sil-opt -enable-sil-verify-all %s -simplification -simplify-instruction=unconditional_checked_cast | %FileCheck %s
22

3-
// REQUIRES: swift_feature_IsolatedConformances
43
// REQUIRES: concurrency
54

65
import Swift

test/Serialization/isolated_conformance.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-swift-frontend -emit-module -target %target-swift-5.1-abi-triple -swift-version 6 -enable-experimental-feature IsolatedConformances -o %t/def_isolated_conformance.swiftmodule %S/Inputs/def_isolated_conformance.swift
2+
// RUN: %target-swift-frontend -emit-module -target %target-swift-5.1-abi-triple -swift-version 6 -o %t/def_isolated_conformance.swiftmodule %S/Inputs/def_isolated_conformance.swift
33

4-
// RUN: %target-swift-frontend -typecheck -verify -target %target-swift-5.1-abi-triple -swift-version 6 -enable-experimental-feature IsolatedConformances %s -I %t
4+
// RUN: %target-swift-frontend -typecheck -verify -target %target-swift-5.1-abi-triple -swift-version 6 %s -I %t
55

6-
// REQUIRES: swift_feature_IsolatedConformances
76
// REQUIRES: concurrency
87

98
import def_isolated_conformance

0 commit comments

Comments
 (0)