Skip to content

Commit 763421c

Browse files
authored
Merge pull request #73291 from augusto2112/6.0-mangle-abi-mod
Account for different module ABI name when reconstructing a type
2 parents b42e726 + f6eba5a commit 763421c

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed

include/swift/AST/ASTContext.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,11 @@ class ASTContext final {
11811181

11821182
ModuleDecl *getModuleByIdentifier(Identifier ModuleID);
11831183

1184+
/// Looks up an already loaded module by its ABI name.
1185+
///
1186+
/// \returns The module if found, nullptr otherwise.
1187+
ModuleDecl *getLoadedModuleByABIName(StringRef ModuleName);
1188+
11841189
/// Returns the standard library module, or null if the library isn't present.
11851190
///
11861191
/// If \p loadIfAbsent is true, the ASTContext will attempt to load the module

lib/AST/ASTContext.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2550,6 +2550,14 @@ ModuleDecl *ASTContext::getModuleByIdentifier(Identifier ModuleID) {
25502550
return getModule(builder.get());
25512551
}
25522552

2553+
ModuleDecl *ASTContext::getLoadedModuleByABIName(StringRef ModuleName) {
2554+
for (auto &[_, module] : getLoadedModules()) {
2555+
if (ModuleName == module->getABIName().str())
2556+
return module;
2557+
}
2558+
return nullptr;
2559+
}
2560+
25532561
ModuleDecl *ASTContext::getStdlibModule(bool loadIfAbsent) {
25542562
if (TheStdlibModule)
25552563
return TheStdlibModule;

lib/AST/ASTDemangler.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,16 +1082,15 @@ ASTBuilder::createTypeDecl(NodePointer node,
10821082
return dyn_cast<GenericTypeDecl>(DC);
10831083
}
10841084

1085-
ModuleDecl *
1086-
ASTBuilder::findModule(NodePointer node) {
1085+
ModuleDecl *ASTBuilder::findModule(NodePointer node) {
10871086
assert(node->getKind() == Demangle::Node::Kind::Module);
10881087
const auto moduleName = node->getText();
1089-
// Respect the main module's ABI name when we're trying to resolve
1088+
// Respect the module's ABI name when we're trying to resolve
10901089
// mangled names. But don't touch anything under the Swift stdlib's
1091-
// umbrella.
1092-
if (Ctx.MainModule && Ctx.MainModule->getABIName().is(moduleName))
1093-
if (!Ctx.MainModule->getABIName().is(STDLIB_NAME))
1094-
return Ctx.MainModule;
1090+
// umbrella.
1091+
if (moduleName != STDLIB_NAME)
1092+
if (auto *Module = Ctx.getLoadedModuleByABIName(moduleName))
1093+
return Module;
10951094

10961095
return Ctx.getModuleByName(moduleName);
10971096
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Tests that reconstructing a type from a mangled name whose type is defined
2+
// in a separate module which has a different ABI name compared to its regular
3+
// name works.
4+
5+
// RUN: %empty-directory(%t)
6+
// RUN: split-file %s %t
7+
// RUN: cd %t
8+
9+
// RUN: %target-build-swift -emit-library -emit-module -parse-as-library -module-abi-name Other -g %t/TheModule.swift
10+
// RUN: %target-build-swift -emit-executable -I %t -L %t -lTheModule %s -g -o %t/user -emit-module
11+
12+
13+
// RUN: sed -ne '/\/\/ *DEMANGLE-TYPE: /s/\/\/ *DEMANGLE-TYPE: *//p' < %s > %t/input
14+
// RUN: %lldb-moduleimport-test-with-sdk %t/user -qualify-types=1 -type-from-mangled=%t/input | %FileCheck %s --check-prefix=CHECK-TYPE
15+
16+
//--- TheModule.swift
17+
public class Foo {
18+
let i = 42
19+
public init() {
20+
}
21+
}
22+
23+
//--- user.swift
24+
25+
import TheModule
26+
27+
let c = TheModule.Foo()
28+
29+
// DEMANGLE-TYPE: $s5Other3FooCD
30+
// CHECK-TYPE: TheModule.Foo
31+

0 commit comments

Comments
 (0)