Skip to content

Commit 7b369d0

Browse files
committed
[SE-0470] Warn about isolated conformance to SendableMetatype-inheriting protocol
An isolated conformance to a SendableMetatype-inheriting protocol cannot actually be used in generic code, because the SendableMetatype requirement itself prevents it. Warn about this case so folks aren't surprised at runtime. This is a part of issue #82550 / rdar://154437489.
1 parent 35cbdd8 commit 7b369d0

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8591,6 +8591,9 @@ GROUPED_ERROR(isolated_conformance_wrong_domain,IsolatedConformances,none,
85918591
GROUPED_WARNING(isolated_conformance_will_become_nonisolated,IsolatedConformances,none,
85928592
"conformance of %0 to %1 should be marked 'nonisolated' to retain its behavior with upcoming feature 'InferIsolatedConformances'",
85938593
(const ValueDecl *, const ValueDecl *))
8594+
GROUPED_WARNING(isolated_conformance_to_sendable_metatype,IsolatedConformances,none,
8595+
"%0 conformance of %1 to SendableMetatype-inheriting %kind2 can never "
8596+
"be used with generic code", (ActorIsolation, Type, const ValueDecl *))
85948597
85958598
//===----------------------------------------------------------------------===//
85968599
// MARK: @_inheritActorContext

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8276,6 +8276,10 @@ RawConformanceIsolationRequest::evaluate(
82768276
if (rootNormal->getOptions().contains(ProtocolConformanceFlags::Nonisolated))
82778277
return ActorIsolation::forNonisolated(false);
82788278

8279+
auto dc = conformance->getDeclContext();
8280+
ASTContext &ctx = dc->getASTContext();
8281+
auto proto = conformance->getProtocol();
8282+
82798283
// If there is an explicitly-specified global actor on the isolation,
82808284
// resolve it and report it.
82818285
if (auto globalActorTypeExpr = rootNormal->getExplicitGlobalActorIsolation()) {
@@ -8295,16 +8299,27 @@ RawConformanceIsolationRequest::evaluate(
82958299
globalActorTypeExpr->setType(MetatypeType::get(globalActorType));
82968300
}
82978301

8302+
// Isolated conformance to a SendableMetatype-inheriting protocol can
8303+
// never be used generically. Warn about it.
8304+
if (auto sendableMetatype =
8305+
ctx.getProtocol(KnownProtocolKind::SendableMetatype)) {
8306+
if (proto->inheritsFrom(sendableMetatype) &&
8307+
!getActorIsolation(proto).preconcurrency()) {
8308+
ctx.Diags.diagnose(
8309+
conformance->getLoc(),
8310+
diag::isolated_conformance_to_sendable_metatype,
8311+
ActorIsolation::forGlobalActor(globalActorType),
8312+
conformance->getType(),
8313+
proto);
8314+
}
8315+
}
8316+
82988317
// FIXME: Make sure the type actually is a global actor type, map it into
82998318
// context, etc.
83008319

83018320
return ActorIsolation::forGlobalActor(globalActorType);
83028321
}
83038322

8304-
auto dc = rootNormal->getDeclContext();
8305-
ASTContext &ctx = dc->getASTContext();
8306-
auto proto = rootNormal->getProtocol();
8307-
83088323
// If the protocol itself is isolated, don't infer isolation for the
83098324
// conformance.
83108325
if (getActorIsolation(proto).isActorIsolated())

test/Concurrency/isolated_conformance.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,15 @@ struct PSendableS: @MainActor PSendable { // expected-note{{requirement specifie
147147
func f() { }
148148
}
149149

150+
protocol R: SendableMetatype {
151+
func f()
152+
}
153+
154+
// expected-warning@+1{{main actor-isolated conformance of 'RSendableSMainActor' to SendableMetatype-inheriting protocol 'R' can never be used with generic code}}
155+
@MainActor struct RSendableSMainActor: @MainActor R {
156+
func f() { }
157+
}
158+
150159
// ----------------------------------------------------------------------------
151160
// Use checking of isolated conformances.
152161
// ----------------------------------------------------------------------------

0 commit comments

Comments
 (0)