[SE-0470] Downgrade some isolated conformance-related errors to warnings in Swift 5

(cherry picked from commit e8b3065293)
This commit is contained in:
Doug Gregor
2025-04-09 15:42:37 -07:00
parent 49ebfcbab2
commit 69758f6213
4 changed files with 25 additions and 10 deletions

View File

@@ -55,6 +55,7 @@ struct WitnessIsolationError {
/// Describes an isolation error involving an associated conformance.
struct AssociatedConformanceIsolationError {
ProtocolConformance *isolatedConformance;
DiagnosticBehavior behavior = DiagnosticBehavior::Unspecified;
/// Diagnose this associated conformance isolation error.
void diagnose(const NormalProtocolConformance *conformance) const;

View File

@@ -8009,7 +8009,8 @@ namespace {
firstConformance->getIsolation(),
firstConformance->getType(),
firstConformance->getProtocol()->getName(),
getContextIsolation());
getContextIsolation())
.warnUntilSwiftVersion(6);
return true;
}
};

View File

@@ -4947,6 +4947,9 @@ static void diagnoseConformanceIsolationErrors(
hasIsolatedConformances = true;
}
// Take the least-restrictive behavior.
behavior = behavior.merge(assocConformanceError.behavior);
anyNonDistributedIssues = true;
}
@@ -5405,9 +5408,19 @@ static void ensureRequirementsAreSatisfied(ASTContext &ctx,
// If the isolation doesn't match, record an error.
if (!outerIsolation.isGlobalActor() ||
outerIsolation != innerIsolation) {
DiagnosticBehavior behavior = DiagnosticBehavior::Unspecified;
// If we're working with requirements imported from Clang, or with
// global actor isolation in general, use the default diagnostic
// behavior based on the conformance context.
if (proto->hasClangNode() ||
outerIsolation.isGlobalActor() ||
innerIsolation.isGlobalActor())
behavior = SendableCheckContext(dc).defaultDiagnosticBehavior();
ctx.getGlobalCache().conformanceIsolationErrors[conformance]
.push_back(
AssociatedConformanceIsolationError{isolatedConformance});
AssociatedConformanceIsolationError{
isolatedConformance, behavior});
return true;
}

View File

@@ -1,4 +1,4 @@
// RUN: %target-swift-frontend -typecheck -verify -target %target-swift-5.1-abi-triple -swift-version 6 -enable-experimental-feature IsolatedConformances %s
// RUN: %target-swift-frontend -typecheck -verify -target %target-swift-5.1-abi-triple -swift-version 5 -strict-concurrency=complete -enable-experimental-feature IsolatedConformances %s
// REQUIRES: swift_feature_IsolatedConformances
// REQUIRES: concurrency
@@ -11,7 +11,7 @@ protocol P {
// Definition of isolated conformances
// ----------------------------------------------------------------------------
// expected-error@+4{{conformance of 'CWithNonIsolated' to protocol 'P' crosses into main actor-isolated code and can cause data races}}
// expected-warning@+4{{conformance of 'CWithNonIsolated' to protocol 'P' crosses into main actor-isolated code and can cause data races}}
// expected-note@+3{{mark all declarations used in the conformance 'nonisolated'}}
// expected-note@+2{{isolate this conformance to the main actor with '@MainActor'}}{{25-25=@MainActor }}
@MainActor
@@ -54,7 +54,7 @@ protocol Q {
associatedtype A: P
}
// expected-error@+2{{conformance of 'SMissingIsolation' to protocol 'Q' crosses into main actor-isolated code and can cause data races}}
// expected-warning@+2{{conformance of 'SMissingIsolation' to protocol 'Q' crosses into main actor-isolated code and can cause data races}}
@MainActor
struct SMissingIsolation: Q {
// expected-note@-1{{conformance depends on main actor-isolated conformance of 'C' to protocol 'P'}}
@@ -66,7 +66,7 @@ struct PWrapper<T: P>: P {
func f() { }
}
// expected-error@+2{{conformance of 'SMissingIsolationViaWrapper' to protocol 'Q' crosses into main actor-isolated code and can cause data races}}
// expected-warning@+2{{conformance of 'SMissingIsolationViaWrapper' to protocol 'Q' crosses into main actor-isolated code and can cause data races}}
@MainActor
struct SMissingIsolationViaWrapper: Q {
// expected-note@-1{{conformance depends on main actor-isolated conformance of 'C' to protocol 'P'}}
@@ -84,7 +84,7 @@ struct S: @MainActor Q {
typealias A = C
}
// expected-error@+3{{conformance of 'SMismatchedActors' to protocol 'Q' crosses into global actor 'SomeGlobalActor'-isolated code and can cause data races}}
// expected-warning@+3{{conformance of 'SMismatchedActors' to protocol 'Q' crosses into global actor 'SomeGlobalActor'-isolated code and can cause data races}}
// expected-note@+2{{conformance depends on global actor 'SomeGlobalActor'-isolated conformance of 'C2' to protocol 'P'}}
@MainActor
struct SMismatchedActors: @MainActor Q {
@@ -149,9 +149,9 @@ func testIsolatedConformancesOfOtherGlobalActor(c: CMismatchedIsolation) {
}
func testIsolationConformancesFromOutside(c: C) {
acceptP(c) // expected-error{{main actor-isolated conformance of 'C' to 'P' cannot be used in nonisolated context}}
let _: any P = c // expected-error{{main actor-isolated conformance of 'C' to 'P' cannot be used in nonisolated context}}
let _ = PWrapper<C>() // expected-error{{main actor-isolated conformance of 'C' to 'P' cannot be used in nonisolated context}}
acceptP(c) // expected-warning{{main actor-isolated conformance of 'C' to 'P' cannot be used in nonisolated context}}
let _: any P = c // expected-warning{{main actor-isolated conformance of 'C' to 'P' cannot be used in nonisolated context}}
let _ = PWrapper<C>() // expected-warning{{main actor-isolated conformance of 'C' to 'P' cannot be used in nonisolated context}}
}
protocol HasAssociatedType {