Skip to content

Commit ac557c5

Browse files
authored
Merge pull request #22143 from theblixguy/fix/look-through-reference-storage-type-equatable
[Sema] Allow weak/unowned types when checking Hashable/Equatable conformance
2 parents 8458f10 + 8928431 commit ac557c5

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/Sema/DerivedConformanceEquatableHashable.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ static bool allStoredPropertiesConformToProtocol(DeclContext *DC,
7878
if (!propertyDecl->hasType())
7979
return false;
8080

81-
auto type = propertyDecl->getType()->mapTypeOutOfContext();
81+
auto type = propertyDecl->getValueInterfaceType();
82+
8283
if (!TypeChecker::conformsToProtocol(DC->mapTypeIntoContext(type),
8384
protocol, DC,
8485
ConformanceCheckFlags::Used)) {

test/Sema/struct_equatable_hashable.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,28 @@ class MixedClass: Hashable {
315315
var hashValue: Int { return -9000 }
316316
}
317317

318+
// Ensure equatable and hashable works with weak/unowned properties as well
319+
struct Foo: Equatable, Hashable {
320+
weak var foo: Bar?
321+
unowned var bar: Bar
322+
}
323+
324+
class Bar {
325+
let bar: String
326+
327+
init(bar: String) {
328+
self.bar = bar
329+
}
330+
}
331+
332+
extension Bar: Equatable, Hashable {
333+
static func == (lhs: Bar, rhs: Bar) -> Bool {
334+
return lhs.bar == rhs.bar
335+
}
336+
337+
func hash(into hasher: inout Hasher) {}
338+
}
339+
318340
// FIXME: Remove -verify-ignore-unknown.
319341
// <unknown>:0: error: unexpected error produced: invalid redeclaration of 'hashValue'
320342
// <unknown>:0: error: unexpected note produced: candidate has non-matching type '(Foo, Foo) -> Bool'

0 commit comments

Comments
 (0)