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

Account for different module ABI name when reconstructing a type #73291

Merged
merged 1 commit into from
Apr 29, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,11 @@ class ASTContext final {

ModuleDecl *getModuleByIdentifier(Identifier ModuleID);

/// Looks up an already loaded module by its ABI name.
///
/// \returns The module if found, nullptr otherwise.
ModuleDecl *getLoadedModuleByABIName(StringRef ModuleName);

/// Returns the standard library module, or null if the library isn't present.
///
/// If \p loadIfAbsent is true, the ASTContext will attempt to load the module
Expand Down
8 changes: 8 additions & 0 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2550,6 +2550,14 @@ ModuleDecl *ASTContext::getModuleByIdentifier(Identifier ModuleID) {
return getModule(builder.get());
}

ModuleDecl *ASTContext::getLoadedModuleByABIName(StringRef ModuleName) {
for (auto &[_, module] : getLoadedModules()) {
if (ModuleName == module->getABIName().str())
return module;
}
return nullptr;
}

ModuleDecl *ASTContext::getStdlibModule(bool loadIfAbsent) {
if (TheStdlibModule)
return TheStdlibModule;
Expand Down
13 changes: 6 additions & 7 deletions lib/AST/ASTDemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1082,16 +1082,15 @@ ASTBuilder::createTypeDecl(NodePointer node,
return dyn_cast<GenericTypeDecl>(DC);
}

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

return Ctx.getModuleByName(moduleName);
}
Expand Down
31 changes: 31 additions & 0 deletions test/TypeDecoder/different_abi_name.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Tests that reconstructing a type from a mangled name whose type is defined
// in a separate module which has a different ABI name compared to its regular
// name works.

// RUN: %empty-directory(%t)
// RUN: split-file %s %t
// RUN: cd %t

// RUN: %target-build-swift -emit-library -emit-module -parse-as-library -module-abi-name Other -g %t/TheModule.swift
// RUN: %target-build-swift -emit-executable -I %t -L %t -lTheModule %s -g -o %t/user -emit-module


// RUN: sed -ne '/\/\/ *DEMANGLE-TYPE: /s/\/\/ *DEMANGLE-TYPE: *//p' < %s > %t/input
// RUN: %lldb-moduleimport-test-with-sdk %t/user -qualify-types=1 -type-from-mangled=%t/input | %FileCheck %s --check-prefix=CHECK-TYPE

//--- TheModule.swift
public class Foo {
let i = 42
public init() {
}
}

//--- user.swift

import TheModule

let c = TheModule.Foo()

// DEMANGLE-TYPE: $s5Other3FooCD
// CHECK-TYPE: TheModule.Foo