Skip to content

Commit a9d5262

Browse files
committed
Make InferIsolatedConformances a migratable upcoming feature
When migrating, provide warnings that add 'nonisolated' to nonisolated conformances that don't already have it and would end up being inferred to be isolated under the upcoming feature. (cherry picked from commit a32782b)
1 parent e39deba commit a9d5262

File tree

4 files changed

+80
-1
lines changed

4 files changed

+80
-1
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8545,6 +8545,9 @@ GROUPED_ERROR(isolated_conformance_with_sendable_simple,IsolatedConformances,
85458545
GROUPED_ERROR(isolated_conformance_wrong_domain,IsolatedConformances,none,
85468546
"%0 conformance of %1 to %2 cannot be used in %3 context",
85478547
(ActorIsolation, Type, DeclName, ActorIsolation))
8548+
GROUPED_WARNING(isolated_conformance_will_become_nonisolated,IsolatedConformances,none,
8549+
"conformance of %0 to %1 should be marked 'nonisolated' to retain its behavior with upcoming feature 'InferIsolatedConformances'",
8550+
(const ValueDecl *, const ValueDecl *))
85488551
85498552
//===----------------------------------------------------------------------===//
85508553
// MARK: @_inheritActorContext

include/swift/Basic/Features.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ UPCOMING_FEATURE(GlobalActorIsolatedTypesUsability, 0434, 6)
283283
MIGRATABLE_UPCOMING_FEATURE(ExistentialAny, 335, 7)
284284
UPCOMING_FEATURE(InternalImportsByDefault, 409, 7)
285285
UPCOMING_FEATURE(MemberImportVisibility, 444, 7)
286-
UPCOMING_FEATURE(InferIsolatedConformances, 470, 7)
286+
MIGRATABLE_UPCOMING_FEATURE(InferIsolatedConformances, 470, 7)
287287
MIGRATABLE_UPCOMING_FEATURE(NonisolatedNonsendingByDefault, 461, 7)
288288

289289
// Optional language features / modes

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6668,6 +6668,49 @@ void TypeChecker::checkConformancesInContext(IterableDeclContext *idc) {
66686668
}
66696669
}
66706670

6671+
// If we are migrating to InferIsolatedConformances, and the
6672+
// nominal type is global-actor-isolated, look for conformances
6673+
// that are nonisolated but were not explicitly marked as such.
6674+
// These conformances will need to be marked 'nonisolated' to
6675+
// retain their current behavior.
6676+
if (Context.LangOpts
6677+
.getFeatureState(Feature::InferIsolatedConformances)
6678+
.isEnabledForMigration() &&
6679+
getActorIsolation(const_cast<NominalTypeDecl *>(nominal))
6680+
.isGlobalActor()) {
6681+
for (auto conformance : conformances) {
6682+
auto normal = dyn_cast<NormalProtocolConformance>(conformance);
6683+
if (!normal)
6684+
continue;
6685+
6686+
// Explicit nonisolated and @preconcurrency suppress this.
6687+
auto options = normal->getOptions();
6688+
if (options.contains(ProtocolConformanceFlags::Nonisolated) ||
6689+
options.contains(ProtocolConformanceFlags::Preconcurrency))
6690+
continue;
6691+
6692+
// Only consider conformances that were explicitly written in the source.
6693+
if (normal->getSourceKind() != ConformanceEntryKind::Explicit)
6694+
continue;
6695+
6696+
// Only consider conformances to non-marker, nonisolated protocols.
6697+
auto proto = normal->getProtocol();
6698+
if (proto->isMarkerProtocol() || getActorIsolation(proto).isActorIsolated())
6699+
continue;
6700+
6701+
// Only nonisolated conformances can be affected.
6702+
if (!conformance->getIsolation().isNonisolated())
6703+
continue;
6704+
6705+
auto nameLoc = normal->getProtocolNameLoc();
6706+
if (nameLoc.isValid()) {
6707+
Context.Diags.diagnose(
6708+
nameLoc, diag::isolated_conformance_will_become_nonisolated, nominal, proto)
6709+
.fixItInsert(nameLoc, "nonisolated ");
6710+
}
6711+
}
6712+
}
6713+
66716714
if (Context.TypeCheckerOpts.DebugGenericSignatures &&
66726715
!conformances.empty()) {
66736716
// Now that they're filled out, print out information about the conformances
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// RUN: %target-swift-frontend -typecheck -verify -target %target-swift-5.1-abi-triple -swift-version 6 -enable-upcoming-feature InferIsolatedConformances:migrate %s
2+
3+
// REQUIRES: concurrency
4+
5+
6+
protocol P { }
7+
protocol Q: P { }
8+
9+
struct S: P { }
10+
11+
struct S2: Q { }
12+
13+
@MainActor
14+
struct MA1: P { }
15+
// expected-warning@-1{{conformance of 'MA1' to 'P' should be marked 'nonisolated' to retain its behavior with upcoming feature 'InferIsolatedConformances'}}{{13-13=nonisolated }}
16+
17+
@MainActor
18+
struct MA2: Q { }
19+
// expected-warning@-1{{conformance of 'MA2' to 'Q' should be marked 'nonisolated' to retain its behavior with upcoming feature 'InferIsolatedConformances'}}{{13-13=nonisolated }}
20+
21+
@MainActor
22+
struct MA3 { }
23+
24+
extension MA3: P, Q { }
25+
// expected-warning@-1{{conformance of 'MA3' to 'P' should be marked 'nonisolated' to retain its behavior with upcoming feature 'InferIsolatedConformances'}}{{16-16=nonisolated }}
26+
// expected-warning@-2{{conformance of 'MA3' to 'Q' should be marked 'nonisolated' to retain its behavior with upcoming feature 'InferIsolatedConformances'}}{{19-19=nonisolated }}
27+
28+
@MainActor
29+
struct MA4: @MainActor P { }
30+
31+
@MainActor
32+
struct MA5: nonisolated P { }
33+

0 commit comments

Comments
 (0)