Skip to content

Commit 8df9fca

Browse files
committed
[cxx-interop] Don't import constructors of foreign reference types.
The logic is here for this in the importer, which means the module interface tests make it appear that the behavior is correct, but lazy member lookup still finds the constructors, so this patch fixes that part.
1 parent 590caf9 commit 8df9fca

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4940,6 +4940,10 @@ TinyPtrVector<ValueDecl *> ClangRecordMemberLookup::evaluate(
49404940
auto named = found.get<clang::NamedDecl *>();
49414941
if (dyn_cast<clang::Decl>(named->getDeclContext()) ==
49424942
recordDecl->getClangDecl()) {
4943+
// Don't import constructors on foreign reference types.
4944+
if (isa<clang::CXXConstructorDecl>(named) && isa<ClassDecl>(recordDecl))
4945+
continue;
4946+
49434947
if (auto import = clangModuleLoader->importDeclDirectly(named))
49444948
result.push_back(cast<ValueDecl>(import));
49454949
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: rm -rf %t
2+
// RUN: split-file %s %t
3+
// RUN: not %target-swift-frontend -typecheck -I %t/Inputs %t/test.swift -enable-experimental-cxx-interop 2>&1 | %FileCheck %s
4+
5+
//--- Inputs/module.modulemap
6+
module Test {
7+
header "test.h"
8+
requires cplusplus
9+
}
10+
11+
//--- Inputs/test.h
12+
struct
13+
__attribute__((swift_attr("import_reference")))
14+
__attribute__((swift_attr("retain:immortal")))
15+
__attribute__((swift_attr("release:immortal")))
16+
HasCtor {
17+
HasCtor(int a) {}
18+
};
19+
20+
//--- test.swift
21+
22+
import Test
23+
24+
// CHECK: error: 'HasCtor' cannot be constructed because it has no accessible initializers
25+
let x = HasCtor(42)

0 commit comments

Comments
 (0)