Skip to content

Commit 3380331

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 e8b3065 commit 3380331

19 files changed

+68
-76
lines changed

CHANGELOG.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,34 @@
3232
strategy, given that the code would eventually become ambiguous anyways when
3333
the deployment target is raised.
3434

35+
* [SE-0470][]:
36+
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:
37+
38+
```swift
39+
protocol P {
40+
func f()
41+
}
42+
43+
@MainActor
44+
class MyType: @MainActor P {
45+
/*@MainActor*/ func f() {
46+
// must be called on the main actor
47+
}
48+
}
49+
```
50+
51+
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:
52+
53+
```swift
54+
func acceptP<T: P>(_ value: T) { }
55+
56+
/*nonisolated*/ func useIsolatedConformance(myType: MyType) {
57+
acceptP(myType) // error: main actor-isolated conformance of 'MyType' to 'P' cannot be used in nonisolated context
58+
}
59+
```
60+
61+
To address such issues, only use an isolated conformance from code that executes on the same global actor.
62+
3563
* [SE-0419][]:
3664
Introduced the new `Runtime` module, which contains a public API that can
3765
generate backtraces, presently supported on macOS and Linux. Capturing a
@@ -10759,6 +10787,7 @@ using the `.dynamicType` member to retrieve the type of an expression should mig
1075910787
[SE-0442]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0442-allow-taskgroup-childtaskresult-type-to-be-inferred.md
1076010788
[SE-0444]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0444-member-import-visibility.md
1076110789
[SE-0458]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0458-strict-memory-safety.md
10790+
[SE-0470]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0470-isolated-conformances.md
1076210791
[#64927]: <https://github.com/apple/swift/issues/64927>
1076310792
[#42697]: <https://github.com/apple/swift/issues/42697>
1076410793
[#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
@@ -8526,10 +8526,6 @@ ERROR(attr_abi_failable_mismatch,none,
85268526
//===----------------------------------------------------------------------===//
85278527
// MARK: Isolated conformances
85288528
//===----------------------------------------------------------------------===//
8529-
GROUPED_ERROR(isolated_conformance_experimental_feature,IsolatedConformances,
8530-
none,
8531-
"isolated conformances require experimental feature "
8532-
" 'IsolatedConformances'", ())
85338529
NOTE(note_isolate_conformance_to_global_actor,none,
85348530
"isolate this conformance to the %select{global actor %0|main actor}1 "
85358531
"with '@%2'", (Type, bool, StringRef))

include/swift/Basic/Features.def

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ 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")
257257
LANGUAGE_FEATURE(AsyncExecutionBehaviorAttributes, 0, "@concurrent and nonisolated(nonsending)")
258+
LANGUAGE_FEATURE(IsolatedConformances, 407, "Global-actor isolated conformances")
258259

259260
// Swift 6
260261
UPCOMING_FEATURE(ConciseMagicFile, 274, 6)
@@ -277,6 +278,7 @@ UPCOMING_FEATURE(GlobalActorIsolatedTypesUsability, 0434, 6)
277278
ADOPTABLE_UPCOMING_FEATURE(ExistentialAny, 335, 7)
278279
UPCOMING_FEATURE(InternalImportsByDefault, 409, 7)
279280
UPCOMING_FEATURE(MemberImportVisibility, 444, 7)
281+
UPCOMING_FEATURE(InferIsolatedConformances, 470, 7)
280282

281283
// Optional language features / modes
282284

@@ -492,12 +494,6 @@ ADOPTABLE_EXPERIMENTAL_FEATURE(AsyncCallerExecution, false)
492494
/// Allow custom availability domains to be defined and referenced.
493495
EXPERIMENTAL_FEATURE(CustomAvailability, true)
494496

495-
/// Allow isolated conformances.
496-
EXPERIMENTAL_FEATURE(IsolatedConformances, true)
497-
498-
/// Infer conformance isolation on global-actor-conforming types.
499-
EXPERIMENTAL_FEATURE(InferIsolatedConformances, true)
500-
501497
/// Allow SwiftSettings
502498
EXPERIMENTAL_FEATURE(SwiftSettings, false)
503499

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
@@ -167,9 +167,7 @@ ParserResult<TypeRepr> Parser::parseTypeSimple(
167167
Diag<> MessageID, ParseTypeReason reason) {
168168
ParserResult<TypeRepr> ty;
169169

170-
if (isParameterSpecifier() &&
171-
!(!Context.LangOpts.hasFeature(Feature::IsolatedConformances) &&
172-
Tok.isContextualKeyword("isolated"))) {
170+
if (isParameterSpecifier()) {
173171
// Type specifier should already be parsed before here. This only happens
174172
// for construct like 'P1 & inout P2'.
175173
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();
@@ -7917,8 +7916,6 @@ ConformanceIsolationRequest::evaluate(Evaluator &evaluator, ProtocolConformance
79177916

79187917
auto dc = rootNormal->getDeclContext();
79197918
ASTContext &ctx = dc->getASTContext();
7920-
if (!ctx.LangOpts.hasFeature(Feature::IsolatedConformances))
7921-
return ActorIsolation::forNonisolated(false);
79227919

79237920
// If the protocol itself is isolated, don't infer isolation for the
79247921
// 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
@@ -4994,8 +4987,7 @@ static void diagnoseConformanceIsolationErrors(
49944987
}
49954988

49964989
// Suggest isolating the conformance, if possible.
4997-
if (ctx.LangOpts.hasFeature(Feature::IsolatedConformances) &&
4998-
potentialIsolation && potentialIsolation->isGlobalActor() &&
4990+
if (potentialIsolation && potentialIsolation->isGlobalActor() &&
49994991
!conformance->isIsolated()) {
50004992
bool isMainActor = false;
50014993
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)