Skip to content

IRGen: Plumb through a GenericSignature when mangling types for debug info #35493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions include/swift/AST/ASTMangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class ASTMangler : public Mangler {
GenericSignature signature,
ResilienceExpansion expansion);

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

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

void bindGenericParameters(const DeclContext *DC);

void bindGenericParameters(CanGenericSignature sig);
void bindGenericParameters(GenericSignature sig);

/// Mangles a sugared type iff we are mangling for the debugger.
template <class T> void appendSugaredType(Type type,
Expand Down
27 changes: 11 additions & 16 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ static Type getTypeForDWARFMangling(Type t) {
SubstFlags::AllowLoweredTypes);
}

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

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

Ty = getTypeForDWARFMangling(Ty);

if (DC)
bindGenericParameters(DC);

bindGenericParameters(sig);
appendType(Ty);
appendOperator("D");
return finalize();
Expand Down Expand Up @@ -673,7 +671,9 @@ std::string ASTMangler::mangleDeclAsUSR(const ValueDecl *Decl,
beginManglingWithoutPrefix();
llvm::SaveAndRestore<bool> allowUnnamedRAII(AllowNamelessEntities, true);
Buffer << USRPrefix;
bindGenericParameters(Decl->getDeclContext());

auto Sig = Decl->getInnermostDeclContext()->getGenericSignatureOfContext();
bindGenericParameters(Sig);

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


/// Bind the generic parameters from the given signature.
void ASTMangler::bindGenericParameters(CanGenericSignature sig) {
void ASTMangler::bindGenericParameters(GenericSignature sig) {
if (sig)
CurGenericSignature = sig;
}

/// Bind the generic parameters from the given context and its parents.
void ASTMangler::bindGenericParameters(const DeclContext *DC) {
if (auto sig = DC->getGenericSignatureOfContext())
bindGenericParameters(sig.getCanonicalSignature());
CurGenericSignature = sig.getCanonicalSignature();
}

void ASTMangler::appendFlatGenericArgs(SubstitutionMap subs) {
Expand Down Expand Up @@ -2858,12 +2852,13 @@ void ASTMangler::appendAccessorEntity(StringRef accessorKindCode,
const AbstractStorageDecl *decl,
bool isStatic) {
appendContextOf(decl);
bindGenericParameters(decl->getDeclContext());
if (isa<VarDecl>(decl)) {
if (auto *varDecl = dyn_cast<VarDecl>(decl)) {
bindGenericParameters(varDecl->getDeclContext()->getGenericSignatureOfContext());
appendDeclName(decl);
appendDeclType(decl);
appendOperator("v", accessorKindCode);
} else if (isa<SubscriptDecl>(decl)) {
} else if (auto *subscriptDecl = dyn_cast<SubscriptDecl>(decl)) {
bindGenericParameters(subscriptDecl->getGenericSignature());
appendDeclType(decl);

StringRef privateDiscriminator = getPrivateDiscriminatorIfNecessary(decl);
Expand Down
5 changes: 3 additions & 2 deletions lib/IRGen/IRGenDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
->getKey();

Type Ty = DbgTy.getType();
if (!Ty->hasTypeParameter())
if (Ty->hasArchetype())
Ty = Ty->mapTypeOutOfContext();

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

Mangle::ASTMangler Mangler;
std::string Result = Mangler.mangleTypeForDebugger(Ty, nullptr);
GenericSignature Sig = IGM.getCurGenericContext();
std::string Result = Mangler.mangleTypeForDebugger(Ty, Sig);

if (!Opts.DisableRoundTripDebugTypes) {
// Make sure we can reconstruct mangled types for the debugger.
Expand Down
25 changes: 25 additions & 0 deletions test/DebugInfo/retroactive_conformance_witness_thunk.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// RUN: %target-swift-frontend -emit-ir -g -primary-file %s

// https://bugs.swift.org/browse/SR-14016

public struct PowerCollection<C : Collection> : Collection {
public typealias Index = [C.Index]
public typealias Element = [C.Element]

public var startIndex, endIndex: Index

public subscript(position: Index) -> [C.Element] {
return []
}

public func index(after i: Index) -> Index {
return i
}

}

extension Array : Comparable where Element : Comparable {
public static func < (lhs: [Element], rhs: [Element]) -> Bool {
return false
}
}
6 changes: 4 additions & 2 deletions tools/swift-ide-test/swift-ide-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2293,7 +2293,8 @@ static int doPrintLocalTypes(const CompilerInvocation &InitInvok,
for (auto LTD : LocalTypeDecls) {
Mangle::ASTMangler Mangler;
std::string MangledName = Mangler.mangleTypeForDebugger(
LTD->getDeclaredInterfaceType(), LTD->getDeclContext());
LTD->getDeclaredInterfaceType(),
LTD->getInnermostDeclContext()->getGenericSignatureOfContext());
MangledNames.push_back(MangledName);
}

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