Skip to content

Commit 4879700

Browse files
authored
Merge pull request #35493 from slavapestov/debug-info-mangling-generic-signature
IRGen: Plumb through a GenericSignature when mangling types for debug info
2 parents e843c72 + 4c72128 commit 4879700

File tree

5 files changed

+45
-24
lines changed

5 files changed

+45
-24
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ class ASTMangler : public Mangler {
222222
GenericSignature signature,
223223
ResilienceExpansion expansion);
224224

225-
std::string mangleTypeForDebugger(Type decl, const DeclContext *DC);
225+
std::string mangleTypeForDebugger(Type decl, GenericSignature sig);
226226

227227
/// Create a mangled name to be used for _typeName constant propagation.
228228
std::string mangleTypeForTypeName(Type type);
@@ -282,9 +282,7 @@ class ASTMangler : public Mangler {
282282
void appendOpWithGenericParamIndex(StringRef,
283283
const GenericTypeParamType *paramTy);
284284

285-
void bindGenericParameters(const DeclContext *DC);
286-
287-
void bindGenericParameters(CanGenericSignature sig);
285+
void bindGenericParameters(GenericSignature sig);
288286

289287
/// Mangles a sugared type iff we are mangling for the debugger.
290288
template <class T> void appendSugaredType(Type type,

lib/AST/ASTMangler.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ static Type getTypeForDWARFMangling(Type t) {
535535
SubstFlags::AllowLoweredTypes);
536536
}
537537

538-
std::string ASTMangler::mangleTypeForDebugger(Type Ty, const DeclContext *DC) {
538+
std::string ASTMangler::mangleTypeForDebugger(Type Ty, GenericSignature sig) {
539539
PrettyStackTraceType prettyStackTrace(Ty->getASTContext(),
540540
"mangling type for debugger", Ty);
541541

@@ -545,9 +545,7 @@ std::string ASTMangler::mangleTypeForDebugger(Type Ty, const DeclContext *DC) {
545545

546546
Ty = getTypeForDWARFMangling(Ty);
547547

548-
if (DC)
549-
bindGenericParameters(DC);
550-
548+
bindGenericParameters(sig);
551549
appendType(Ty);
552550
appendOperator("D");
553551
return finalize();
@@ -673,7 +671,9 @@ std::string ASTMangler::mangleDeclAsUSR(const ValueDecl *Decl,
673671
beginManglingWithoutPrefix();
674672
llvm::SaveAndRestore<bool> allowUnnamedRAII(AllowNamelessEntities, true);
675673
Buffer << USRPrefix;
676-
bindGenericParameters(Decl->getDeclContext());
674+
675+
auto Sig = Decl->getInnermostDeclContext()->getGenericSignatureOfContext();
676+
bindGenericParameters(Sig);
677677

678678
if (auto Ctor = dyn_cast<ConstructorDecl>(Decl)) {
679679
appendConstructorEntity(Ctor, /*isAllocating=*/false);
@@ -1374,15 +1374,9 @@ void ASTMangler::appendOpWithGenericParamIndex(StringRef Op,
13741374

13751375

13761376
/// Bind the generic parameters from the given signature.
1377-
void ASTMangler::bindGenericParameters(CanGenericSignature sig) {
1377+
void ASTMangler::bindGenericParameters(GenericSignature sig) {
13781378
if (sig)
1379-
CurGenericSignature = sig;
1380-
}
1381-
1382-
/// Bind the generic parameters from the given context and its parents.
1383-
void ASTMangler::bindGenericParameters(const DeclContext *DC) {
1384-
if (auto sig = DC->getGenericSignatureOfContext())
1385-
bindGenericParameters(sig.getCanonicalSignature());
1379+
CurGenericSignature = sig.getCanonicalSignature();
13861380
}
13871381

13881382
void ASTMangler::appendFlatGenericArgs(SubstitutionMap subs) {
@@ -2858,12 +2852,13 @@ void ASTMangler::appendAccessorEntity(StringRef accessorKindCode,
28582852
const AbstractStorageDecl *decl,
28592853
bool isStatic) {
28602854
appendContextOf(decl);
2861-
bindGenericParameters(decl->getDeclContext());
2862-
if (isa<VarDecl>(decl)) {
2855+
if (auto *varDecl = dyn_cast<VarDecl>(decl)) {
2856+
bindGenericParameters(varDecl->getDeclContext()->getGenericSignatureOfContext());
28632857
appendDeclName(decl);
28642858
appendDeclType(decl);
28652859
appendOperator("v", accessorKindCode);
2866-
} else if (isa<SubscriptDecl>(decl)) {
2860+
} else if (auto *subscriptDecl = dyn_cast<SubscriptDecl>(decl)) {
2861+
bindGenericParameters(subscriptDecl->getGenericSignature());
28672862
appendDeclType(decl);
28682863

28692864
StringRef privateDiscriminator = getPrivateDiscriminatorIfNecessary(decl);

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
816816
->getKey();
817817

818818
Type Ty = DbgTy.getType();
819-
if (!Ty->hasTypeParameter())
819+
if (Ty->hasArchetype())
820820
Ty = Ty->mapTypeOutOfContext();
821821

822822
// Strip off top level of type sugar (except for type aliases).
@@ -842,7 +842,8 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
842842
IGM.getSILModule());
843843

844844
Mangle::ASTMangler Mangler;
845-
std::string Result = Mangler.mangleTypeForDebugger(Ty, nullptr);
845+
GenericSignature Sig = IGM.getCurGenericContext();
846+
std::string Result = Mangler.mangleTypeForDebugger(Ty, Sig);
846847

847848
if (!Opts.DisableRoundTripDebugTypes) {
848849
// Make sure we can reconstruct mangled types for the debugger.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %target-swift-frontend -emit-ir -g -primary-file %s
2+
3+
// https://bugs.swift.org/browse/SR-14016
4+
5+
public struct PowerCollection<C : Collection> : Collection {
6+
public typealias Index = [C.Index]
7+
public typealias Element = [C.Element]
8+
9+
public var startIndex, endIndex: Index
10+
11+
public subscript(position: Index) -> [C.Element] {
12+
return []
13+
}
14+
15+
public func index(after i: Index) -> Index {
16+
return i
17+
}
18+
19+
}
20+
21+
extension Array : Comparable where Element : Comparable {
22+
public static func < (lhs: [Element], rhs: [Element]) -> Bool {
23+
return false
24+
}
25+
}

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,7 +2293,8 @@ static int doPrintLocalTypes(const CompilerInvocation &InitInvok,
22932293
for (auto LTD : LocalTypeDecls) {
22942294
Mangle::ASTMangler Mangler;
22952295
std::string MangledName = Mangler.mangleTypeForDebugger(
2296-
LTD->getDeclaredInterfaceType(), LTD->getDeclContext());
2296+
LTD->getDeclaredInterfaceType(),
2297+
LTD->getInnermostDeclContext()->getGenericSignatureOfContext());
22972298
MangledNames.push_back(MangledName);
22982299
}
22992300

@@ -3420,7 +3421,8 @@ class TypeReconstructWalker : public SourceEntityWalker {
34203421
private:
34213422
void tryDemangleType(Type T, const DeclContext *DC, CharSourceRange range) {
34223423
Mangle::ASTMangler Mangler;
3423-
std::string mangledName(Mangler.mangleTypeForDebugger(T, DC));
3424+
auto sig = DC->getGenericSignatureOfContext();
3425+
std::string mangledName(Mangler.mangleTypeForDebugger(T, sig));
34243426
Type ReconstructedType = DC->mapTypeIntoContext(
34253427
Demangle::getTypeForMangling(Ctx, mangledName));
34263428
Stream << "type: ";

0 commit comments

Comments
 (0)