Skip to content

[6.2][cxx-interop] Fix not importing return type for certain functions #80585

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 1 commit into from
Apr 8, 2025
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: 9 additions & 1 deletion lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3730,6 +3730,13 @@ namespace {
return nullptr;
}

static bool isClangNamespace(const DeclContext *dc) {
if (const auto *ed = dc->getSelfEnumDecl())
return isa<clang::NamespaceDecl>(ed->getClangDecl());

return false;
}

Decl *importFunctionDecl(
const clang::FunctionDecl *decl, ImportedName importedName,
std::optional<ImportedName> correctSwiftName,
Expand Down Expand Up @@ -3865,7 +3872,8 @@ namespace {

bool importFuncWithoutSignature =
isa<clang::CXXMethodDecl>(decl) && Impl.importSymbolicCXXDecls;
if (!dc->isModuleScopeContext() && !isa<clang::CXXMethodDecl>(decl)) {
if (!dc->isModuleScopeContext() && !isClangNamespace(dc) &&
!isa<clang::CXXMethodDecl>(decl)) {
// Handle initializers.
if (name.getBaseName().isConstructor()) {
assert(!accessorInfo);
Expand Down
7 changes: 7 additions & 0 deletions test/Interop/Cxx/foreign-reference/Inputs/inheritance.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
// A wrapper around C++'s static_cast(), which allows Swift to get around interop's current lack of support for inheritance.
template <class I, class O> O cxxCast(I i) { return static_cast<O>(i); }

namespace Foo {
template <class I, class O>
O cxxCast(I i) {
return static_cast<O>(i);
}
} // namespace Foo

// A minimal foreign reference type.
struct
__attribute__((swift_attr("import_reference")))
Expand Down
4 changes: 3 additions & 1 deletion test/Interop/Cxx/foreign-reference/inheritance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ InheritanceTestSuite.test("Templated cast to base") {
let sc: BaseT = cast(s)
expectFalse(sc.isBase)
let sx: BaseT = cxxCast(s) // should instantiate I to SubT and O to BaseT
expectFalse(sc.isBase)
expectFalse(sx.isBase)
let sy: BaseT = Foo.cxxCast(s) // should instantiate I to SubT and O to BaseT
expectFalse(sy.isBase)
}

InheritanceTestSuite.test("Templated cast to itself") {
Expand Down