Skip to content

Commit 7ad2eef

Browse files
martinboehmezoecarver
authored andcommitted
Only import constructors marked noexcept.
1 parent a5e953b commit 7ad2eef

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3893,7 +3893,12 @@ namespace {
38933893

38943894
AbstractFunctionDecl *result = nullptr;
38953895
if (auto *ctordecl = dyn_cast<clang::CXXConstructorDecl>(decl)) {
3896-
// TODO: Is failable, throws etc. correct?
3896+
// For the time being, we only import `noexcept` constructors.
3897+
// TODO: Import throwing constructors too.
3898+
auto *prototype = decl->getType()->getAs<clang::FunctionProtoType>();
3899+
if (!prototype || !prototype->hasNoexceptExceptionSpec())
3900+
return nullptr;
3901+
38973902
DeclName ctorName(Impl.SwiftContext, DeclBaseName::createConstructor(),
38983903
bodyParams);
38993904
result = Impl.createDeclWithClangNode<ConstructorDecl>(

test/CXXInterop/Inputs/cxx_constructors.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
struct ExplicitDefaultConstructor {
2-
ExplicitDefaultConstructor() : x(42) {}
2+
ExplicitDefaultConstructor() noexcept : x(42) {}
33
int x;
44
};
55

@@ -17,6 +17,10 @@ struct DefaultConstructorDeleted {
1717
};
1818

1919
struct ConstructorWithParam {
20-
ConstructorWithParam(int val) : x(val) {}
20+
ConstructorWithParam(int val) noexcept : x(val) {}
2121
int x;
2222
};
23+
24+
struct PotentiallyThrowingConstructor {
25+
PotentiallyThrowingConstructor() {}
26+
};

test/CXXInterop/cxx-constructors-typecheck.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ let deletedImplicitly = ConstructorWithParam() // expected-error {{missing argum
1111
let deletedExplicitly = DefaultConstructorDeleted() // expected-error {{cannot be constructed because it has no accessible initializers}}
1212

1313
let withArg = ConstructorWithParam(42)
14+
15+
// For the time being, we only import constructors marked `noexcept`.
16+
let potentiallyThrowing = PotentiallyThrowingConstructor() // expected-error {{cannot be constructed because it has no accessible initializers}}

0 commit comments

Comments
 (0)