Skip to content

Commit a3db8d4

Browse files
committed
AST: Fix debug info mangling of opaque result types with @_originallyDefinedIn
If a function's parameter or return types involve nominal types that have been moved across modules using @_originallyDefinedIn, we must take care to always mangle the opaque result type's name using the original module names and not the current module names. This was a problem with DWARF mangling, which normally disables @_originallyDefinedIn for other purposes. Make sure to always temporarily re-enable it when mangling an opaque result type. Fixes rdar://problem/93822207.
1 parent 27fd1bd commit a3db8d4

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class ASTMangler : public Mangler {
4343

4444
/// If enabled, non-canonical types are allowed and type alias types get a
4545
/// special mangling.
46-
bool DWARFMangling;
46+
bool DWARFMangling = false;
4747

4848
/// If enabled, entities that ought to have names but don't get a placeholder.
4949
///
@@ -72,6 +72,12 @@ class ASTMangler : public Mangler {
7272
/// function types.
7373
bool Preconcurrency = false;
7474

75+
/// If enabled, declarations annotated with @_originallyDefinedIn are mangled
76+
/// as if they're part of their original module. Disabled for debug mangling,
77+
/// because lldb wants to find declarations in the modules they're currently
78+
/// defined in.
79+
bool RespectOriginallyDefinedIn = true;
80+
7581
public:
7682
using SymbolicReferent = llvm::PointerUnion<const NominalTypeDecl *,
7783
const OpaqueTypeDecl *>;
@@ -110,9 +116,6 @@ class ASTMangler : public Mangler {
110116
BackDeploymentFallback,
111117
};
112118

113-
ASTMangler(bool DWARFMangling = false)
114-
: DWARFMangling(DWARFMangling) {}
115-
116119
void addTypeSubstitution(Type type, GenericSignature sig) {
117120
type = dropProtocolsFromAssociatedTypes(type, sig);
118121
addSubstitution(type.getPointer());

lib/AST/ASTMangler.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@ std::string ASTMangler::mangleTypeForDebugger(Type Ty, GenericSignature sig) {
633633
"mangling type for debugger", Ty);
634634

635635
DWARFMangling = true;
636+
RespectOriginallyDefinedIn = false;
636637
OptimizeProtocolNames = false;
637638
beginMangling();
638639

@@ -651,6 +652,7 @@ std::string ASTMangler::mangleTypeForTypeName(Type type) {
651652

652653
std::string ASTMangler::mangleDeclType(const ValueDecl *decl) {
653654
DWARFMangling = true;
655+
RespectOriginallyDefinedIn = false;
654656
beginMangling();
655657

656658
appendDeclType(decl);
@@ -742,6 +744,7 @@ std::string ASTMangler::mangleTypeAsContextUSR(const NominalTypeDecl *type) {
742744

743745
std::string ASTMangler::mangleTypeAsUSR(Type Ty) {
744746
DWARFMangling = true;
747+
RespectOriginallyDefinedIn = false;
745748
beginMangling();
746749

747750
Ty = getTypeForDWARFMangling(Ty);
@@ -758,6 +761,7 @@ std::string ASTMangler::mangleTypeAsUSR(Type Ty) {
758761

759762
std::string ASTMangler::mangleAnyDecl(const ValueDecl *Decl, bool prefix) {
760763
DWARFMangling = true;
764+
RespectOriginallyDefinedIn = false;
761765
if (prefix) {
762766
beginMangling();
763767
} else {
@@ -1032,6 +1036,14 @@ void ASTMangler::appendOpaqueDeclName(const OpaqueTypeDecl *opaqueDecl) {
10321036
if (canSymbolicReference(opaqueDecl)) {
10331037
appendSymbolicReference(opaqueDecl);
10341038
} else if (auto namingDecl = opaqueDecl->getNamingDecl()) {
1039+
// Set this to true temporarily, even if we're doing DWARF
1040+
// mangling for debug info, where it is false. Otherwise,
1041+
// the mangled opaque result type name will not be able to
1042+
// be looked up, since we rely on an exact match with the
1043+
// ABI name.
1044+
llvm::SaveAndRestore<bool> savedRespectOriginallyDefinedIn(
1045+
RespectOriginallyDefinedIn, true);
1046+
10351047
appendEntity(namingDecl);
10361048
appendOperator("QO");
10371049
} else {
@@ -2269,10 +2281,11 @@ void ASTMangler::appendModule(const ModuleDecl *module,
22692281
// Use the module real name in mangling; this is the physical name
22702282
// of the module on-disk, which can be different if -module-alias is
22712283
// used.
2284+
//
22722285
// For example, if a module Foo has 'import Bar', and '-module-alias Bar=Baz'
22732286
// was passed, the name 'Baz' will be used for mangling besides loading.
22742287
StringRef ModName = module->getRealName().str();
2275-
if (!DWARFMangling &&
2288+
if (RespectOriginallyDefinedIn &&
22762289
module->getABIName() != module->getName()) { // check if the ABI name is set
22772290
ModName = module->getABIName().str();
22782291
}
@@ -2281,7 +2294,7 @@ void ASTMangler::appendModule(const ModuleDecl *module,
22812294
if (ModName == STDLIB_NAME) {
22822295
if (useModuleName.empty()) {
22832296
appendOperator("s");
2284-
} else if (DWARFMangling) {
2297+
} else if (!RespectOriginallyDefinedIn) {
22852298
appendOperator("s");
22862299
} else {
22872300
appendIdentifier(useModuleName);
@@ -2298,11 +2311,11 @@ void ASTMangler::appendModule(const ModuleDecl *module,
22982311
return appendOperator("SC");
22992312
}
23002313

2301-
// Enabling DWARFMangling indicate the mangled names are not part of the ABI,
2302-
// probably used by the debugger or IDE (USR). These mangled names will not be
2303-
// demangled successfully if we use the original module name instead of the
2304-
// actual module name.
2305-
if (!useModuleName.empty() && !DWARFMangling)
2314+
// Disabling RespectOriginallyDefinedIn indicate the mangled names are not part
2315+
// of the ABI, probably used by the debugger or IDE (USR). These mangled names
2316+
// will not be demangled successfully if we use the original module name instead
2317+
// of the actual module name.
2318+
if (!useModuleName.empty() && RespectOriginallyDefinedIn)
23062319
appendIdentifier(useModuleName);
23072320
else
23082321
appendIdentifier(ModName);

lib/SIL/IR/SILType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ bool SILType::isNoReturnFunction(SILModule &M,
163163
}
164164

165165
std::string SILType::getMangledName() const {
166-
Mangle::ASTMangler mangler(false/*use dwarf mangling*/);
166+
Mangle::ASTMangler mangler;
167167
return mangler.mangleTypeWithoutPrefix(getASTType());
168168
}
169169

test/ModuleInterface/originally-defined-attr-opaque-result.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
// RUN: %target-swift-emit-module-interface(%t/CoreChef.swiftinterface) %s -module-name CoreChef -I %t -disable-availability-checking -DLIB
66
// RUN: %target-swift-typecheck-module-from-interface(%t/CoreChef.swiftinterface) -module-name CoreChef -I %t -disable-availability-checking
77

8+
// Also build the module itself with -g to exercise debug info round tripping.
9+
// RUN: %target-swift-frontend -emit-ir -g %s -I %t -disable-availability-checking
10+
811
// RUN: %FileCheck %s < %t/CoreChef.swiftinterface
912

1013
// REQUIRES: OS=macosx

0 commit comments

Comments
 (0)