Skip to content

Commit 27acad2

Browse files
committed
ConstantPropagation: fix dynamic cast from AnyHashable to a protocol.
rdar://problem/35364339
1 parent 6d9a4b5 commit 27acad2

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/SIL/DynamicCasts.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ classifyDynamicCastToProtocol(CanType source,
151151
return DynamicCastFeasibility::WillFail;
152152
}
153153

154+
// AnyHashable is a special case: although it's a struct, there maybe another
155+
// type conforming to it and to the TargetProtocol at the same time.
156+
if (SourceNominalTy == SourceNominalTy->getASTContext().getAnyHashableDecl())
157+
return DynamicCastFeasibility::MaySucceed;
158+
154159
// If we are in a whole-module compilation and
155160
// if the source type is internal or target protocol is internal,
156161
// then conformances cannot be changed at run-time, because only this
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %target-swift-frontend -O -emit-sil %s | %FileCheck %s
2+
3+
// REQUIRES: objc_interop
4+
5+
import Foundation
6+
7+
protocol FooProtocol : class {
8+
}
9+
10+
class FooProtocolImplementation : NSObject, FooProtocol {
11+
}
12+
13+
// CHECK-LABEL: sil @{{.*}}testit
14+
// CHECK: checked_cast_addr_br {{.*}} AnyHashable {{.*}} to FooProtocol
15+
public func testit(_ x: AnyHashable) -> Bool {
16+
return (x as? FooProtocol) != nil
17+
}
18+

0 commit comments

Comments
 (0)