Skip to content

Commit 54d3930

Browse files
committed
[Runtime] Adjust to conforming type when instantiating witness table.
When we find a protocol conformance descriptor for a given type, make sure we adjust to the conforming type of that descriptor (following the superclass chain as needed) before instantiating the witness table. Fixes rdar://problem/49741838.
1 parent e43a9f5 commit 54d3930

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

stdlib/public/runtime/ProtocolConformance.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,8 @@ swift_conformsToProtocolImpl(const Metadata * const type,
615615
if (!description)
616616
return nullptr;
617617

618-
return description->getWitnessTable(type);
618+
return description->getWitnessTable(
619+
findConformingSuperclass(type, description));
619620
}
620621

621622
const ContextDescriptor *
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift -parse-stdlib %s -module-name main -o %t/a.out
3+
// RUN: %target-codesign %t/a.out
4+
// RUN: %target-run %t/a.out
5+
// REQUIRES: executable_test
6+
7+
import Swift
8+
import StdlibUnittest
9+
10+
class Key<T>: RawRepresentable {
11+
typealias RawValue = T
12+
13+
let rawValue: T
14+
15+
required init(rawValue: T) {
16+
self.rawValue = rawValue
17+
}
18+
}
19+
20+
extension Key: Hashable where T: Hashable {
21+
func hash(into hasher: inout Hasher) {
22+
hasher.combine(rawValue)
23+
}
24+
}
25+
26+
extension Key: Equatable where T: Equatable {
27+
static func == (lhs: Key, rhs: Key) -> Bool {
28+
return lhs.rawValue == rhs.rawValue
29+
}
30+
}
31+
32+
class SpecificKey: Key<String> { }
33+
34+
extension SpecificKey {
35+
static let name = SpecificKey(rawValue: "name")
36+
}
37+
38+
let AssociatedTypeDemangleTests = TestSuite("AssociatedTypeDemangle")
39+
40+
AssociatedTypeDemangleTests.test("superclass substitutions") {
41+
var dictionary: [SpecificKey: String] = [:]
42+
dictionary[SpecificKey.name] = "Hello"
43+
44+
expectEqual(["Hello"], dictionary.values.map { $0 })
45+
}
46+
47+
runAllTests()

0 commit comments

Comments
 (0)