Skip to content

Commit cb9cf69

Browse files
committed
[id as Any] Coerce keys to rvalues for checking Hashable conformance
lvalue Strings should first be coerced to rvalue Strings before checking for Hashable protocol conformance.
1 parent 650d0b4 commit cb9cf69

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

lib/Sema/CSApply.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5337,6 +5337,10 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
53375337
expr = coerceImplicitlyUnwrappedOptionalToValue(expr, objTy, locator);
53385338
}
53395339

5340+
// We want to check conformance on the rvalue, as that's what has
5341+
// the Hashable conformance
5342+
expr = tc.coerceToRValue(expr);
5343+
53405344
// Find the conformance of the source type to Hashable.
53415345
auto hashable = tc.Context.getProtocol(KnownProtocolKind::Hashable);
53425346
ProtocolConformance *conformance;

test/Constraints/anyhashable-collection-cast.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@ func test() {
1515
print(s)
1616
}
1717
}
18+
19+
func testLValueCoerce() {
20+
var lvalue = "lvalue"
21+
var map: [AnyHashable : Any] = [lvalue: lvalue]
22+
lvalue = map[lvalue] as! String
23+
}

0 commit comments

Comments
 (0)