Skip to content

Commit fc74fb0

Browse files
committed
IRGen: Set the mangler's generic signature correctly
Otherwise, we'll fall over if we try to mangle a retroactive conformance. Fixes <rdar://problem/70483417>.
1 parent 2a1b985 commit fc74fb0

File tree

5 files changed

+48
-31
lines changed

5 files changed

+48
-31
lines changed

lib/IRGen/GenReflection.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ getTypeRefImpl(IRGenModule &IGM,
308308
}
309309

310310
IRGenMangler Mangler;
311-
auto SymbolicName = Mangler.mangleTypeForReflection(IGM, type);
311+
auto SymbolicName = Mangler.mangleTypeForReflection(IGM, sig, type);
312312
return {IGM.getAddrOfStringForTypeRef(SymbolicName, role),
313313
SymbolicName.runtimeSizeInBytes()};
314314
}
@@ -628,25 +628,21 @@ class AssociatedTypeMetadataBuilder : public ReflectionMetadataBuilder {
628628
ArrayRef<std::pair<StringRef, CanType>> AssociatedTypes;
629629

630630
void layout() override {
631-
// If the conforming type is generic, we just want to emit the
632-
// unbound generic type here.
633-
auto *Nominal = Conformance->getType()->getAnyNominal();
634-
assert(Nominal && "Structural conformance?");
631+
PrettyStackTraceConformance DebugStack("emitting associated type metadata",
632+
Conformance);
635633

636-
PrettyStackTraceDecl DebugStack("emitting associated type metadata",
637-
Nominal);
638-
639-
addNominalRef(Nominal);
634+
auto *DC = Conformance->getDeclContext();
635+
addNominalRef(DC->getSelfNominalTypeDecl());
640636
addNominalRef(Conformance->getProtocol());
641637

642638
B.addInt32(AssociatedTypes.size());
643639
B.addInt32(AssociatedTypeRecordSize);
644640

641+
auto genericSig = DC->getGenericSignatureOfContext().getCanonicalSignature();
645642
for (auto AssocTy : AssociatedTypes) {
646643
auto NameGlobal = IGM.getAddrOfFieldName(AssocTy.first);
647644
B.addRelativeAddress(NameGlobal);
648-
addTypeRef(AssocTy.second,
649-
Nominal->getGenericSignature().getCanonicalSignature());
645+
addTypeRef(AssocTy.second, genericSig);
650646
}
651647
}
652648

lib/IRGen/IRGenMangler.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,10 @@ IRGenMangler::withSymbolicReferences(IRGenModule &IGM,
144144

145145
SymbolicMangling
146146
IRGenMangler::mangleTypeForReflection(IRGenModule &IGM,
147-
Type Ty) {
147+
CanGenericSignature Sig,
148+
CanType Ty) {
148149
return withSymbolicReferences(IGM, [&]{
150+
bindGenericParameters(Sig);
149151
appendType(Ty);
150152
});
151153
}
@@ -181,20 +183,6 @@ std::string IRGenMangler::mangleProtocolConformanceInstantiationCache(
181183
return finalize();
182184
}
183185

184-
SymbolicMangling
185-
IRGenMangler::mangleProtocolConformanceForReflection(IRGenModule &IGM,
186-
Type ty, ProtocolConformanceRef conformance) {
187-
return withSymbolicReferences(IGM, [&]{
188-
if (conformance.isConcrete()) {
189-
appendProtocolConformance(conformance.getConcrete());
190-
} else {
191-
// Use a special mangling for abstract conformances.
192-
appendType(ty);
193-
appendProtocolName(conformance.getAbstract());
194-
}
195-
});
196-
}
197-
198186
std::string IRGenMangler::mangleTypeForLLVMTypeName(CanType Ty) {
199187
// To make LLVM IR more readable we always add a 'T' prefix so that type names
200188
// don't start with a digit and don't need to be quoted.

lib/IRGen/IRGenMangler.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ class IRGenMangler : public Mangle::ASTMangler {
483483
std::string mangleOutlinedCopyFunction(CanType ty,
484484
CanGenericSignature sig) {
485485
beginMangling();
486+
bindGenericParameters(sig);
486487
appendType(ty);
487488
if (sig)
488489
appendGenericSignature(sig);
@@ -492,6 +493,7 @@ class IRGenMangler : public Mangle::ASTMangler {
492493
std::string mangleOutlinedConsumeFunction(CanType ty,
493494
CanGenericSignature sig) {
494495
beginMangling();
496+
bindGenericParameters(sig);
495497
appendType(ty);
496498
if (sig)
497499
appendGenericSignature(sig);
@@ -502,6 +504,7 @@ class IRGenMangler : public Mangle::ASTMangler {
502504
std::string mangleOutlinedRetainFunction(CanType t,
503505
CanGenericSignature sig) {
504506
beginMangling();
507+
bindGenericParameters(sig);
505508
appendType(t);
506509
if (sig)
507510
appendGenericSignature(sig);
@@ -511,6 +514,7 @@ class IRGenMangler : public Mangle::ASTMangler {
511514
std::string mangleOutlinedReleaseFunction(CanType t,
512515
CanGenericSignature sig) {
513516
beginMangling();
517+
bindGenericParameters(sig);
514518
appendType(t);
515519
if (sig)
516520
appendGenericSignature(sig);
@@ -521,6 +525,7 @@ class IRGenMangler : public Mangle::ASTMangler {
521525
std::string mangleOutlinedInitializeWithTakeFunction(CanType t,
522526
CanGenericSignature sig) {
523527
beginMangling();
528+
bindGenericParameters(sig);
524529
appendType(t);
525530
if (sig)
526531
appendGenericSignature(sig);
@@ -530,6 +535,7 @@ class IRGenMangler : public Mangle::ASTMangler {
530535
std::string mangleOutlinedInitializeWithCopyFunction(CanType t,
531536
CanGenericSignature sig) {
532537
beginMangling();
538+
bindGenericParameters(sig);
533539
appendType(t);
534540
if (sig)
535541
appendGenericSignature(sig);
@@ -539,6 +545,7 @@ class IRGenMangler : public Mangle::ASTMangler {
539545
std::string mangleOutlinedAssignWithTakeFunction(CanType t,
540546
CanGenericSignature sig) {
541547
beginMangling();
548+
bindGenericParameters(sig);
542549
appendType(t);
543550
if (sig)
544551
appendGenericSignature(sig);
@@ -548,6 +555,7 @@ class IRGenMangler : public Mangle::ASTMangler {
548555
std::string mangleOutlinedAssignWithCopyFunction(CanType t,
549556
CanGenericSignature sig) {
550557
beginMangling();
558+
bindGenericParameters(sig);
551559
appendType(t);
552560
if (sig)
553561
appendGenericSignature(sig);
@@ -557,6 +565,7 @@ class IRGenMangler : public Mangle::ASTMangler {
557565
std::string mangleOutlinedDestroyFunction(CanType t,
558566
CanGenericSignature sig) {
559567
beginMangling();
568+
bindGenericParameters(sig);
560569
appendType(t);
561570
if (sig)
562571
appendGenericSignature(sig);
@@ -571,11 +580,8 @@ class IRGenMangler : public Mangle::ASTMangler {
571580
}
572581

573582
SymbolicMangling mangleTypeForReflection(IRGenModule &IGM,
574-
Type Ty);
575-
576-
SymbolicMangling mangleProtocolConformanceForReflection(IRGenModule &IGM,
577-
Type Ty,
578-
ProtocolConformanceRef conformance);
583+
CanGenericSignature genericSig,
584+
CanType Ty);
579585

580586
std::string mangleTypeForLLVMTypeName(CanType Ty);
581587

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public protocol P {}
2+
3+
public enum E<First, Second> {
4+
case left(First)
5+
case right(Second)
6+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -emit-module-path %t/retroactive_conformance_other.swiftmodule %S/Inputs/retroactive_conformance_other.swift
3+
// RUN: %target-swift-frontend -emit-ir %s -I %t
4+
5+
import retroactive_conformance_other
6+
7+
public struct Foo<T: P> {}
8+
9+
public enum MyError : Error {
10+
case a
11+
}
12+
13+
extension E: P where First: P {
14+
}
15+
16+
public enum Bar<T: P> {
17+
case x(Foo<E<T, MyError>>)
18+
case y
19+
}
20+
21+

0 commit comments

Comments
 (0)