Skip to content

Commit de21ba6

Browse files
authored
Merge pull request swiftlang#39035 from slavapestov/irgen-wrong-signature
IRGen: Mangle DWARF types with the right generic signature
2 parents 3be3a36 + 685f3f2 commit de21ba6

File tree

6 files changed

+36
-19
lines changed

6 files changed

+36
-19
lines changed

lib/AST/ASTMangler.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,9 @@ void ASTMangler::appendType(Type type, const ValueDecl *forDecl) {
11161116
if (tryMangleTypeSubstitution(tybase))
11171117
return;
11181118

1119+
auto outerGenericSig = CurGenericSignature;
11191120
appendAnyGenericType(decl);
1121+
CurGenericSignature = outerGenericSig;
11201122
bool isFirstArgList = true;
11211123
appendBoundGenericArgs(type, isFirstArgList);
11221124
appendRetroactiveConformances(type);
@@ -1249,7 +1251,9 @@ void ASTMangler::appendType(Type type, const ValueDecl *forDecl) {
12491251
appendType(GenArgs[0], forDecl);
12501252
appendOperator("Sg");
12511253
} else {
1254+
auto outerGenericSig = CurGenericSignature;
12521255
appendAnyGenericType(Decl);
1256+
CurGenericSignature = outerGenericSig;
12531257
bool isFirstArgList = true;
12541258
appendBoundGenericArgs(type, isFirstArgList);
12551259
appendRetroactiveConformances(type);
@@ -3224,17 +3228,6 @@ void ASTMangler::appendConcreteProtocolConformance(
32243228
const ProtocolConformance *conformance) {
32253229
auto module = conformance->getDeclContext()->getParentModule();
32263230

3227-
// It's possible that we might not have a generic signature here to get
3228-
// the conformance access path (for example, when mangling types for
3229-
// debugger). In that case, we can use the generic signature of the
3230-
// conformance (if it's present).
3231-
auto conformanceSig = conformance->getGenericSignature();
3232-
auto shouldUseConformanceSig = !CurGenericSignature && conformanceSig;
3233-
llvm::SaveAndRestore<CanGenericSignature> savedSignature(
3234-
CurGenericSignature, shouldUseConformanceSig
3235-
? conformanceSig.getCanonicalSignature()
3236-
: CurGenericSignature);
3237-
32383231
// Conforming type.
32393232
Type conformingType = conformance->getType();
32403233
if (conformingType->hasArchetype())

lib/IRGen/GenType.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,9 +1441,6 @@ TypeConverter::~TypeConverter() {
14411441
}
14421442

14431443
void TypeConverter::setGenericContext(CanGenericSignature signature) {
1444-
if (!signature)
1445-
return;
1446-
14471444
CurGenericSignature = signature;
14481445

14491446
// Clear the dependent type info cache since we have a new active signature

lib/IRGen/GenType.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,6 @@ class GenericContextScope {
277277
{}
278278

279279
~GenericContextScope() {
280-
if (!newSig)
281-
return;
282280
assert(TC.CurGenericSignature == newSig);
283281
TC.setGenericContext(oldSig);
284282
}

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "GenType.h"
2222
#include "swift/AST/ASTMangler.h"
2323
#include "swift/AST/Expr.h"
24+
#include "swift/AST/GenericEnvironment.h"
2425
#include "swift/AST/IRGenOptions.h"
2526
#include "swift/AST/Module.h"
2627
#include "swift/AST/ModuleLoader.h"
@@ -825,9 +826,29 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
825826
return MetadataTypeDeclCache.find(DbgTy.getDecl()->getName().str())
826827
->getKey();
827828

829+
// This is a bit of a hack. We need a generic signature to use for mangling.
830+
// If we started with an interface type, just use IGM.getCurGenericContext(),
831+
// since callers that use interface types typically push a signature that way.
832+
//
833+
// Otherwise, if we have a contextual type, find an archetype and ask it for
834+
// it's generic signature. The context generic signature from the IRGenModule
835+
// is unlikely to be useful here.
836+
GenericSignature Sig;
828837
Type Ty = DbgTy.getType();
829-
if (Ty->hasArchetype())
838+
if (Ty->hasArchetype()) {
839+
Ty.findIf([&](Type t) -> bool {
840+
if (auto *archetypeTy = t->getAs<PrimaryArchetypeType>()) {
841+
Sig = archetypeTy->getGenericEnvironment()->getGenericSignature();
842+
return true;
843+
}
844+
845+
return false;
846+
});
847+
830848
Ty = Ty->mapTypeOutOfContext();
849+
} else {
850+
Sig = IGM.getCurGenericContext();
851+
}
831852

832853
// Strip off top level of type sugar (except for type aliases).
833854
// We don't want Optional<T> and T? to get different debug types.
@@ -852,7 +873,6 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
852873
IGM.getSILModule());
853874

854875
Mangle::ASTMangler Mangler;
855-
GenericSignature Sig = IGM.getCurGenericContext();
856876
std::string Result = Mangler.mangleTypeForDebugger(Ty, Sig);
857877

858878
if (!Opts.DisableRoundTripDebugTypes) {

test/IRGen/retroactive_conformance_path.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-build-swift -module-name=test %s -o %t/a.out
2+
// RUN: %target-build-swift -module-name=test %s -o %t/a.out -requirement-machine=verify
33
// RUN: %target-run %t/a.out | %FileCheck %s
44
// REQUIRES: executable_test
55
// REQUIRES: CPU=arm64 || CPU=x86_64
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %target-swift-frontend -emit-ir -g -primary-file %s -requirement-machine=verify
2+
3+
public struct TestType<T: Error> { }
4+
5+
extension Array: Error where Element: Error { }
6+
7+
public struct GenericType<G> {
8+
public func test<T>(_ value: TestType<[T]>) { }
9+
}

0 commit comments

Comments
 (0)