Skip to content

Make derived hashValue/hash(into:) nonisolated. #38620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions lib/Sema/DerivedConformanceEquatableHashable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,12 @@ deriveHashable_hashInto(
hashDecl->copyFormalAccessFrom(derived.Nominal,
/*sourceIsParentContext=*/true);

// The derived hash(into:) for an actor must be non-isolated.
if (derived.Nominal->isActor() ||
getActorIsolation(derived.Nominal) == ActorIsolation::GlobalActor) {
hashDecl->getAttrs().add(new (C) NonisolatedAttr(/*IsImplicit*/true));
}

derived.addMembersToConformanceContext({hashDecl});

return hashDecl;
Expand Down Expand Up @@ -904,10 +910,9 @@ static ValueDecl *deriveHashable_hashValue(DerivedConformance &derived) {
hashValueDecl->copyFormalAccessFrom(derived.Nominal,
/*sourceIsParentContext*/ true);

if (derived.Nominal->isDistributedActor()) {
// While distributed actors implement hash(into:) explicitly, the hashValue
// is still synthesized as usual. We must make it nonisolated in order
// for the hashValue to be able to witness the protocol requirement.
// The derived hashValue of an actor must be nonisolated.
if (derived.Nominal->isActor() ||
getActorIsolation(derived.Nominal) == ActorIsolation::GlobalActor) {
hashValueDecl->getAttrs().add(new (C) NonisolatedAttr(/*IsImplicit*/true));
}

Expand Down
30 changes: 30 additions & 0 deletions test/decl/protocol/conforms/actor_derived.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// RUN: %target-typecheck-verify-swift
// REQUIRES: concurrency

@available(SwiftStdlib 5.5, *)
actor A1: Hashable {
nonisolated func hash(into hasher: inout Hasher) { }
static func ==(lhs: A1, rhs: A1) -> Bool { true }
}

@available(SwiftStdlib 5.5, *)
actor A2: Hashable {
nonisolated var hashValue: Int { 0 } // expected-warning{{'Hashable.hashValue' is deprecated as a protocol requirement; conform type 'A2' to 'Hashable' by implementing 'hash(into:)' instead}}
static func ==(lhs: A2, rhs: A2) -> Bool { true }
}


@available(SwiftStdlib 5.5, *)
@MainActor
class C1: Hashable {
nonisolated func hash(into hasher: inout Hasher) { }
nonisolated static func ==(lhs: C1, rhs: C1) -> Bool { true }
}

@available(SwiftStdlib 5.5, *)
@MainActor
class C2: Hashable {
nonisolated var hashValue: Int { 0 } // expected-warning{{'Hashable.hashValue' is deprecated as a protocol requirement; conform type 'C2' to 'Hashable' by implementing 'hash(into:)' instead}}
nonisolated static func ==(lhs: C2, rhs: C2) -> Bool { true }
}

2 changes: 1 addition & 1 deletion test/decl/protocol/special/Actor.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// RUN: %target-typecheck-verify-swift -enable-experimental-concurrency
// REQUIRES: concurrency

// Synthesis of for actores.
// Synthesis of conformances for actors.

@available(SwiftStdlib 5.5, *)
actor A1 {
Expand Down