Skip to content

Commit 7782f86

Browse files
committed
Sema: Avoid adding nonisolated twice to synthesized Hashable methods.
#42041 introduced a centralized mechanism for adding the `nonisolated` attribute to synthesized decls but did not clean up the existing code that was already doing so for `Hashable` conformances. Resolves rdar://102106591
1 parent 0592979 commit 7782f86

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

lib/Sema/CodeSynthesis.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,13 +1605,14 @@ bool swift::hasLetStoredPropertyWithInitialValue(NominalTypeDecl *nominal) {
16051605
});
16061606
}
16071607

1608-
void swift::addNonIsolatedToSynthesized(
1609-
NominalTypeDecl *nominal, ValueDecl *value) {
1608+
bool swift::addNonIsolatedToSynthesized(NominalTypeDecl *nominal,
1609+
ValueDecl *value) {
16101610
if (!getActorIsolation(nominal).isActorIsolated())
1611-
return;
1611+
return false;
16121612

16131613
ASTContext &ctx = nominal->getASTContext();
16141614
value->getAttrs().add(new (ctx) NonisolatedAttr(/*isImplicit=*/true));
1615+
return true;
16151616
}
16161617

16171618
static std::pair<BraceStmt *, /*isTypeChecked=*/bool>

lib/Sema/CodeSynthesis.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ ValueDecl *getProtocolRequirement(ProtocolDecl *protocol, Identifier name);
8585
// with an initial value.
8686
bool hasLetStoredPropertyWithInitialValue(NominalTypeDecl *nominal);
8787

88-
/// Add 'nonisolated' to the synthesized declaration when needed.
89-
void addNonIsolatedToSynthesized(NominalTypeDecl *nominal, ValueDecl *value);
88+
/// Add 'nonisolated' to the synthesized declaration when needed. Returns true
89+
/// if an attribute was added.
90+
bool addNonIsolatedToSynthesized(NominalTypeDecl *nominal, ValueDecl *value);
9091

9192
} // end namespace swift
9293

lib/Sema/DerivedConformanceEquatableHashable.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -554,15 +554,13 @@ deriveHashable_hashInto(
554554
/*Throws=*/false,
555555
/*GenericParams=*/nullptr, params, returnType, parentDC);
556556
hashDecl->setBodySynthesizer(bodySynthesizer);
557-
addNonIsolatedToSynthesized(derived.Nominal, hashDecl);
558557
hashDecl->copyFormalAccessFrom(derived.Nominal,
559558
/*sourceIsParentContext=*/true);
560559

561560
// The derived hash(into:) for an actor must be non-isolated.
562-
if (derived.Nominal->isActor() ||
563-
getActorIsolation(derived.Nominal) == ActorIsolation::GlobalActor) {
564-
hashDecl->getAttrs().add(new (C) NonisolatedAttr(/*IsImplicit*/true));
565-
}
561+
if (!addNonIsolatedToSynthesized(derived.Nominal, hashDecl) &&
562+
derived.Nominal->isActor())
563+
hashDecl->getAttrs().add(new (C) NonisolatedAttr(/*IsImplicit*/ true));
566564

567565
derived.addMembersToConformanceContext({hashDecl});
568566

@@ -891,7 +889,6 @@ static ValueDecl *deriveHashable_hashValue(DerivedConformance &derived) {
891889
SourceLoc(), C.Id_hashValue, parentDC);
892890
hashValueDecl->setInterfaceType(intType);
893891
hashValueDecl->setSynthesized();
894-
addNonIsolatedToSynthesized(derived.Nominal, hashValueDecl);
895892

896893
ParameterList *params = ParameterList::createEmpty(C);
897894

@@ -918,10 +915,9 @@ static ValueDecl *deriveHashable_hashValue(DerivedConformance &derived) {
918915
/*sourceIsParentContext*/ true);
919916

920917
// The derived hashValue of an actor must be nonisolated.
921-
if (derived.Nominal->isActor() ||
922-
getActorIsolation(derived.Nominal) == ActorIsolation::GlobalActor) {
923-
hashValueDecl->getAttrs().add(new (C) NonisolatedAttr(/*IsImplicit*/true));
924-
}
918+
if (!addNonIsolatedToSynthesized(derived.Nominal, hashValueDecl) &&
919+
derived.Nominal->isActor())
920+
hashValueDecl->getAttrs().add(new (C) NonisolatedAttr(/*IsImplicit*/ true));
925921

926922
Pattern *hashValuePat = NamedPattern::createImplicit(C, hashValueDecl);
927923
hashValuePat->setType(intType);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-emit-module-interface(%t/Library.swiftinterface) %s -disable-availability-checking -module-name Library
3+
// RUN: %target-swift-typecheck-module-from-interface(%t/Library.swiftinterface) -disable-availability-checking -module-name Library
4+
// RUN: %FileCheck %s < %t/Library.swiftinterface
5+
6+
// REQUIRES: concurrency
7+
8+
// CHECK: @_Concurrency.MainActor public struct X1 : Swift.Equatable, Swift.Hashable, Swift.Codable
9+
@MainActor public struct X1: Equatable, Hashable, Codable {
10+
let x: Int
11+
let y: String
12+
13+
// CHECK: nonisolated public static func == (a: Library.X1, b: Library.X1) -> Swift.Bool
14+
// CHECK: nonisolated public func hash(into hasher: inout Swift.Hasher)
15+
// CHECK: nonisolated public func encode(to encoder: any Swift.Encoder) throws
16+
// CHECK: nonisolated public var hashValue: Swift.Int
17+
// CHECK: nonisolated public init(from decoder: any Swift.Decoder) throws
18+
}

0 commit comments

Comments
 (0)