mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[SE-0470] Fix synthesized conformances with default main actor isolation
When defaulting to main-actor isolation, types that have synthesized conformances (e.g., for Equatable, Hashable, Codable) were getting nonisolated members by default. That would cause compiler errors because the conformances themselves defaulted to main-actor isolation when their types were. Be careful to only mark these members as 'nonisolated' when it makes sense, and leave them to get the isolation of their enclosing type when the conformance might have isolation. This ensures that one can use synthesis of these protocols along with default main-actor mode. There is a one-off trick here to force the synthesized CodingKeys to be nonisolated, because the CodingKey protocol requires Sendable. We'll separately consider whether to generalize this rule. More of rdar://150691429.
This commit is contained in:
@@ -1728,12 +1728,52 @@ bool swift::hasLetStoredPropertyWithInitialValue(NominalTypeDecl *nominal) {
|
||||
});
|
||||
}
|
||||
|
||||
/// Determine whether a synth
|
||||
static bool synthesizedRequirementIsNonIsolated(
|
||||
const NormalProtocolConformance *conformance) {
|
||||
// @preconcurrency suppresses this.
|
||||
if (conformance->isPreconcurrency())
|
||||
return false;
|
||||
|
||||
// Explicit global actor isolation suppresses this.
|
||||
if (conformance->hasExplicitGlobalActorIsolation())
|
||||
return false;
|
||||
|
||||
// Explicit nonisolated forces this.
|
||||
if (conformance->getOptions()
|
||||
.contains(ProtocolConformanceFlags::Nonisolated))
|
||||
return true;
|
||||
|
||||
// When we are inferring conformance isolation, only add nonisolated if
|
||||
// either
|
||||
// (1) the protocol inherits from SendableMetatype, or
|
||||
// (2) the conforming type is nonisolated.
|
||||
ASTContext &ctx = conformance->getDeclContext()->getASTContext();
|
||||
if (!ctx.LangOpts.hasFeature(Feature::InferIsolatedConformances))
|
||||
return true;
|
||||
|
||||
// Check inheritance from SendableMetatype, which implies that the conformance
|
||||
// will be nonisolated.
|
||||
auto sendableMetatypeProto =
|
||||
ctx.getProtocol(KnownProtocolKind::SendableMetatype);
|
||||
if (sendableMetatypeProto &&
|
||||
conformance->getProtocol()->inheritsFrom(sendableMetatypeProto))
|
||||
return true;
|
||||
|
||||
auto nominalType = conformance->getType()->getAnyNominal();
|
||||
if (!nominalType)
|
||||
return true;
|
||||
|
||||
return !getActorIsolation(nominalType).isMainActor();
|
||||
}
|
||||
|
||||
bool swift::addNonIsolatedToSynthesized(DerivedConformance &derived,
|
||||
ValueDecl *value) {
|
||||
if (auto *conformance = derived.Conformance) {
|
||||
if (conformance && conformance->isPreconcurrency())
|
||||
if (!synthesizedRequirementIsNonIsolated(conformance))
|
||||
return false;
|
||||
}
|
||||
|
||||
return addNonIsolatedToSynthesized(derived.Nominal, value);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user