Skip to content

[3.0] Sema: Make Hashable-to-AnyHashable a Subtype rather than Conversion. #4351

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
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
20 changes: 11 additions & 9 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1579,6 +1579,17 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
conversionsOrFixes.push_back(ConversionRestrictionKind::Superclass);
}

// T -> AnyHashable.
if (isAnyHashableType(desugar2)) {
// Don't allow this in operator contexts or we'll end up allowing
// 'T() == U()' for unrelated T and U that just happen to be Hashable.
// We can remove this special case when we implement operator hiding.
if (kind != TypeMatchKind::OperatorArgumentConversion) {
conversionsOrFixes.push_back(
ConversionRestrictionKind::HashableToAnyHashable);
}
}

// Metatype to object conversion.
//
// Class and protocol metatypes are interoperable with certain Objective-C
Expand Down Expand Up @@ -1649,15 +1660,6 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
} else if (isSetType(desugar1) && isSetType(desugar2)) {
conversionsOrFixes.push_back(
ConversionRestrictionKind::SetUpcast);
// T -> AnyHashable.
} else if (isAnyHashableType(desugar2)) {
// Don't allow this in operator contexts or we'll end up allowing
// 'T() == U()' for unrelated T and U that just happen to be Hashable.
// We can remove this special case when we implement operator hiding.
if (kind != TypeMatchKind::OperatorArgumentConversion) {
conversionsOrFixes.push_back(
ConversionRestrictionKind::HashableToAnyHashable);
}
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions test/Constraints/anyhashable-collection-cast.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %target-swift-frontend -parse -verify %s

func dict() -> [AnyHashable: Any] {
return ["x": "y"]
}
func set() -> Set<AnyHashable> {
return ["x"]
}

func test() {
if let d = dict() as? [String: String] {
print(d)
}
if let s = set() as? Set<String> {
print(s)
}
}