Skip to content
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

[DebugInfo] Fix debug info round tripping of types inside functions #78328

Merged
merged 2 commits into from
Dec 21, 2024
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
10 changes: 2 additions & 8 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2591,15 +2591,9 @@ void ASTMangler::appendModule(const ModuleDecl *module,
StringRef ModName = module->getRealName().str();

// If RespectOriginallyDefinedIn is not set, ignore the ABI name only for
// _Concurrency and swift-syntax (which adds "Compiler" as a prefix when
// building swift-syntax as part of the compiler).
// TODO: Mangling for the debugger should respect originally defined in, but
// as of right now there is not enough information in the mangled name to
// reconstruct AST types from mangled names when using that attribute.
// _Concurrency.
if ((RespectOriginallyDefinedIn ||
(module->getName().str() != SWIFT_CONCURRENCY_NAME &&
!module->getABIName().str().starts_with(
SWIFT_MODULE_ABI_NAME_PREFIX))) &&
module->getName().str() != SWIFT_CONCURRENCY_NAME) &&
module->getABIName() != module->getName())
ModName = module->getABIName().str();

Expand Down
26 changes: 16 additions & 10 deletions lib/IRGen/IRGenDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "swift/AST/ASTMangler.h"
#include "swift/AST/Attr.h"
#include "swift/AST/Decl.h"
#include "swift/AST/DeclContext.h"
#include "swift/AST/Expr.h"
#include "swift/AST/GenericEnvironment.h"
#include "swift/AST/IRGenOptions.h"
Expand All @@ -34,6 +35,7 @@
#include "swift/AST/Pattern.h"
#include "swift/AST/TypeDifferenceVisitor.h"
#include "swift/AST/TypeWalker.h"
#include "swift/AST/Types.h"
#include "swift/Basic/Assertions.h"
#include "swift/Basic/Compiler.h"
#include "swift/Basic/SourceManager.h"
Expand Down Expand Up @@ -65,6 +67,7 @@
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/FileSystem.h"
Expand Down Expand Up @@ -2343,18 +2346,21 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
if (visitedOriginallyDefinedIn)
return TypeWalker::Action::Stop;

// A typealias inside a function used that function's signature as part of
DeclContext *D = nullptr;
if (auto *TAT = llvm::dyn_cast<TypeAliasType>(T))
D = TAT->getDecl()->getDeclContext();
else if (auto *NT = llvm::dyn_cast<NominalOrBoundGenericNominalType>(T))
D = NT->getDecl()->getDeclContext();

// A type inside a function uses that function's signature as part of
// its mangling, so check if any types in the generic signature are
// annotated with @_originallyDefinedIn.
if (auto *TAT = llvm::dyn_cast<TypeAliasType>(T)) {
auto D = TAT->getDecl()->getDeclContext();
if (auto AFD = llvm::dyn_cast<AbstractFunctionDecl>(D)) {
OriginallyDefinedInFinder InnerWalker;
AFD->getInterfaceType().walk(InnerWalker);
if (InnerWalker.visitedOriginallyDefinedIn) {
visitedOriginallyDefinedIn = true;
return TypeWalker::Action::Stop;
}
if (auto AFD = llvm::dyn_cast_or_null<AbstractFunctionDecl>(D)) {
OriginallyDefinedInFinder InnerWalker;
AFD->getInterfaceType().walk(InnerWalker);
if (InnerWalker.visitedOriginallyDefinedIn) {
visitedOriginallyDefinedIn = true;
return TypeWalker::Action::Stop;
}
}

Expand Down
13 changes: 13 additions & 0 deletions test/DebugInfo/local_type_originally_defined_in.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ public func localTypeAliasTest(horse: Horse) {
// CHECK: DIDerivedType(tag: DW_TAG_typedef, name: "$s32local_type_originally_defined_in0A13TypeAliasTest5horsey4Barn5HorseV_tF1AL_aD"
}


public func localTypeTest(horse: Horse) {
// The local type mangling for 'A' mentions 'Horse', which must
// be mangled using it's current module name, and not the
// original module name, for consistency with the debug info
// mangling.
struct LocalStruct {}

let info = UnsafeMutablePointer<LocalStruct>.allocate(capacity: 1)
_ = info
// CHECK: DICompositeType(tag: DW_TAG_structure_type, name: "$s32local_type_originally_defined_in0A8TypeTest5horsey4Barn5HorseV_tF11LocalStructL_VD"
}

public func localTypeAliasTest() -> Horse {
typealias B = Int

Expand Down