Skip to content

[Sema] Allow weak/unowned types when checking Hashable/Equatable conformance #22143

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
Jan 28, 2019
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
3 changes: 2 additions & 1 deletion lib/Sema/DerivedConformanceEquatableHashable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ static bool allStoredPropertiesConformToProtocol(DeclContext *DC,
if (!propertyDecl->hasType())
return false;

auto type = propertyDecl->getType()->mapTypeOutOfContext();
auto type = propertyDecl->getValueInterfaceType();

if (!TypeChecker::conformsToProtocol(DC->mapTypeIntoContext(type),
protocol, DC,
ConformanceCheckFlags::Used)) {
Expand Down
22 changes: 22 additions & 0 deletions test/Sema/struct_equatable_hashable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,28 @@ class MixedClass: Hashable {
var hashValue: Int { return -9000 }
}

// Ensure equatable and hashable works with weak/unowned properties as well
struct Foo: Equatable, Hashable {
weak var foo: Bar?
unowned var bar: Bar
}

class Bar {
let bar: String

init(bar: String) {
self.bar = bar
}
}

extension Bar: Equatable, Hashable {
static func == (lhs: Bar, rhs: Bar) -> Bool {
return lhs.bar == rhs.bar
}

func hash(into hasher: inout Hasher) {}
}

// FIXME: Remove -verify-ignore-unknown.
// <unknown>:0: error: unexpected error produced: invalid redeclaration of 'hashValue'
// <unknown>:0: error: unexpected note produced: candidate has non-matching type '(Foo, Foo) -> Bool'
Expand Down