Skip to content

Commit a32782b

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.
1 parent 799e0f8 commit a32782b

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
@@ -8619,6 +8619,9 @@ GROUPED_ERROR(isolated_conformance_with_sendable_simple,IsolatedConformances,
86198619
GROUPED_ERROR(isolated_conformance_wrong_domain,IsolatedConformances,none,
86208620
"%0 conformance of %1 to %2 cannot be used in %3 context",
86218621
(ActorIsolation, Type, DeclName, ActorIsolation))
8622+
GROUPED_WARNING(isolated_conformance_will_become_nonisolated,IsolatedConformances,none,
8623+
"conformance of %0 to %1 should be marked 'nonisolated' to retain its behavior with upcoming feature 'InferIsolatedConformances'",
8624+
(const ValueDecl *, const ValueDecl *))
86228625
86238626
//===----------------------------------------------------------------------===//
86248627
// 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
@@ -6675,6 +6675,49 @@ void TypeChecker::checkConformancesInContext(IterableDeclContext *idc) {
66756675
}
66766676
}
66776677

6678+
// If we are migrating to InferIsolatedConformances, and the
6679+
// nominal type is global-actor-isolated, look for conformances
6680+
// that are nonisolated but were not explicitly marked as such.
6681+
// These conformances will need to be marked 'nonisolated' to
6682+
// retain their current behavior.
6683+
if (Context.LangOpts
6684+
.getFeatureState(Feature::InferIsolatedConformances)
6685+
.isEnabledForMigration() &&
6686+
getActorIsolation(const_cast<NominalTypeDecl *>(nominal))
6687+
.isGlobalActor()) {
6688+
for (auto conformance : conformances) {
6689+
auto normal = dyn_cast<NormalProtocolConformance>(conformance);
6690+
if (!normal)
6691+
continue;
6692+
6693+
// Explicit nonisolated and @preconcurrency suppress this.
6694+
auto options = normal->getOptions();
6695+
if (options.contains(ProtocolConformanceFlags::Nonisolated) ||
6696+
options.contains(ProtocolConformanceFlags::Preconcurrency))
6697+
continue;
6698+
6699+
// Only consider conformances that were explicitly written in the source.
6700+
if (normal->getSourceKind() != ConformanceEntryKind::Explicit)
6701+
continue;
6702+
6703+
// Only consider conformances to non-marker, nonisolated protocols.
6704+
auto proto = normal->getProtocol();
6705+
if (proto->isMarkerProtocol() || getActorIsolation(proto).isActorIsolated())
6706+
continue;
6707+
6708+
// Only nonisolated conformances can be affected.
6709+
if (!conformance->getIsolation().isNonisolated())
6710+
continue;
6711+
6712+
auto nameLoc = normal->getProtocolNameLoc();
6713+
if (nameLoc.isValid()) {
6714+
Context.Diags.diagnose(
6715+
nameLoc, diag::isolated_conformance_will_become_nonisolated, nominal, proto)
6716+
.fixItInsert(nameLoc, "nonisolated ");
6717+
}
6718+
}
6719+
}
6720+
66786721
if (Context.TypeCheckerOpts.DebugGenericSignatures &&
66796722
!conformances.empty()) {
66806723
// 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)