Skip to content

Commit 6bce1d4

Browse files
authored
Merge pull request #4336 from jckarter/anyhashable-dynamic-cast
Sema: Make Hashable-to-AnyHashable a Subtype rather than Conversion.
2 parents b9462c2 + b9bc62d commit 6bce1d4

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,6 +1579,17 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
15791579
conversionsOrFixes.push_back(ConversionRestrictionKind::Superclass);
15801580
}
15811581

1582+
// T -> AnyHashable.
1583+
if (isAnyHashableType(desugar2)) {
1584+
// Don't allow this in operator contexts or we'll end up allowing
1585+
// 'T() == U()' for unrelated T and U that just happen to be Hashable.
1586+
// We can remove this special case when we implement operator hiding.
1587+
if (kind != TypeMatchKind::OperatorArgumentConversion) {
1588+
conversionsOrFixes.push_back(
1589+
ConversionRestrictionKind::HashableToAnyHashable);
1590+
}
1591+
}
1592+
15821593
// Metatype to object conversion.
15831594
//
15841595
// Class and protocol metatypes are interoperable with certain Objective-C
@@ -1649,15 +1660,6 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
16491660
} else if (isSetType(desugar1) && isSetType(desugar2)) {
16501661
conversionsOrFixes.push_back(
16511662
ConversionRestrictionKind::SetUpcast);
1652-
// T -> AnyHashable.
1653-
} else if (isAnyHashableType(desugar2)) {
1654-
// Don't allow this in operator contexts or we'll end up allowing
1655-
// 'T() == U()' for unrelated T and U that just happen to be Hashable.
1656-
// We can remove this special case when we implement operator hiding.
1657-
if (kind != TypeMatchKind::OperatorArgumentConversion) {
1658-
conversionsOrFixes.push_back(
1659-
ConversionRestrictionKind::HashableToAnyHashable);
1660-
}
16611663
}
16621664
}
16631665
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %target-swift-frontend -parse -verify %s
2+
3+
func dict() -> [AnyHashable: Any] {
4+
return ["x": "y"]
5+
}
6+
func set() -> Set<AnyHashable> {
7+
return ["x"]
8+
}
9+
10+
func test() {
11+
if let d = dict() as? [String: String] {
12+
print(d)
13+
}
14+
if let s = set() as? Set<String> {
15+
print(s)
16+
}
17+
}

0 commit comments

Comments
 (0)