Skip to content

Commit deec3e1

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 507c405 commit deec3e1

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
@@ -8681,6 +8681,9 @@ GROUPED_ERROR(isolated_conformance_wrong_domain,IsolatedConformances,none,
86818681
GROUPED_WARNING(isolated_conformance_will_become_nonisolated,IsolatedConformances,none,
86828682
"conformance of %0 to %1 should be marked 'nonisolated' to retain its behavior with upcoming feature 'InferIsolatedConformances'",
86838683
(const ValueDecl *, const ValueDecl *))
8684+
GROUPED_WARNING(isolated_conformance_to_sendable_metatype,IsolatedConformances,none,
8685+
"%0 conformance of %1 to SendableMetatype-inheriting %kind2 can never "
8686+
"be used with generic code", (ActorIsolation, Type, const ValueDecl *))
86848687
86858688
//===----------------------------------------------------------------------===//
86868689
// MARK: @_inheritActorContext

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8280,6 +8280,10 @@ RawConformanceIsolationRequest::evaluate(
82808280
if (conformance->getOptions().contains(ProtocolConformanceFlags::Nonisolated))
82818281
return ActorIsolation::forNonisolated(false);
82828282

8283+
auto dc = conformance->getDeclContext();
8284+
ASTContext &ctx = dc->getASTContext();
8285+
auto proto = conformance->getProtocol();
8286+
82838287
// If there is an explicitly-specified global actor on the isolation,
82848288
// resolve it and report it.
82858289
if (auto globalActorTypeExpr = conformance->getExplicitGlobalActorIsolation()) {
@@ -8299,16 +8303,27 @@ RawConformanceIsolationRequest::evaluate(
82998303
globalActorTypeExpr->setType(MetatypeType::get(globalActorType));
83008304
}
83018305

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

83058324
return ActorIsolation::forGlobalActor(globalActorType);
83068325
}
83078326

8308-
auto dc = conformance->getDeclContext();
8309-
ASTContext &ctx = dc->getASTContext();
8310-
auto proto = conformance->getProtocol();
8311-
83128327
// If the protocol itself is isolated, don't infer isolation for the
83138328
// conformance.
83148329
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)