Skip to content

Commit 9e89d56

Browse files
committed
[ASTMangler] Do not crash when mangling a retroactive conformance
1 parent dbddb0d commit 9e89d56

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

lib/AST/ASTMangler.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,7 +1388,7 @@ void ASTMangler::appendRetroactiveConformances(Type type) {
13881388
module = Mod ? Mod : nominal->getModuleContext();
13891389
subMap = type->getContextSubstitutionMap(module, nominal);
13901390
}
1391-
1391+
13921392
appendRetroactiveConformances(subMap, module);
13931393
}
13941394

@@ -2528,7 +2528,7 @@ ASTMangler::appendProtocolConformance(const ProtocolConformance *conformance) {
25282528

25292529
auto conformingType = conformance->getType();
25302530
appendType(conformingType->getCanonicalType());
2531-
2531+
25322532
appendProtocolName(conformance->getProtocol());
25332533

25342534
bool needsModule = true;
@@ -2649,6 +2649,10 @@ void ASTMangler::appendDependentProtocolConformance(
26492649
void ASTMangler::appendConcreteProtocolConformance(
26502650
const ProtocolConformance *conformance) {
26512651
auto module = conformance->getDeclContext()->getParentModule();
2652+
if (!CurGenericSignature && conformance->getGenericSignature()) {
2653+
CurGenericSignature =
2654+
conformance->getGenericSignature()->getCanonicalSignature();
2655+
}
26522656

26532657
// Conforming type.
26542658
Type conformingType = conformance->getType();
@@ -2679,7 +2683,7 @@ void ASTMangler::appendConcreteProtocolConformance(
26792683
assert(CurGenericSignature &&
26802684
"Need a generic signature to resolve conformance");
26812685
auto conformanceAccessPath =
2682-
CurGenericSignature->getConformanceAccessPath(type, proto);
2686+
CurGenericSignature->getConformanceAccessPath(type, proto);
26832687
appendDependentProtocolConformance(conformanceAccessPath);
26842688
} else if (auto opaqueType = canType->getAs<OpaqueTypeArchetypeType>()) {
26852689
GenericSignature opaqueSignature = opaqueType->getBoundSignature();
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift -module-name=test %s -o %t/a.out
3+
// RUN: %target-run %t/a.out | %FileCheck %s
4+
// REQUIRES: executable_test
5+
// REQUIRES: CPU=arm64 || CPU=x86_64
6+
7+
// Check that the ASTMangler does not crash when mangling a retroactive conformance
8+
// and also do a executable test to make sure the code works as expected.
9+
10+
extension Result: RandomAccessCollection, BidirectionalCollection, Collection, Sequence where Success: RandomAccessCollection, Success.Index == Int {
11+
public typealias Element = Result<Success.Element, Failure>
12+
public typealias Index = Int
13+
public var startIndex: Int {
14+
switch self {
15+
case .success(let array):
16+
return array.startIndex
17+
case .failure:
18+
return 0
19+
}
20+
}
21+
public var endIndex: Int {
22+
switch self {
23+
case .success(let array):
24+
return array.endIndex
25+
case .failure:
26+
return 1
27+
}
28+
}
29+
public subscript(position: Int) -> Result<Success.Element, Failure> {
30+
switch self {
31+
case .success(let array):
32+
return .success(array[position])
33+
case .failure(let error):
34+
return .failure(error)
35+
}
36+
}
37+
}
38+
39+
let coll: [Int] = [4, 8, 15, 16, 23, 42]
40+
let result: Result<[Int], Error> = .success(coll)
41+
// CHECK: success(15)
42+
print(result[2])

0 commit comments

Comments
 (0)