Skip to content

Commit 3b06438

Browse files
authored
Merge pull request #35962 from zoecarver/cxx/fix-invalid-decls
[cxx-interop] Don't crash when importing invalid decl.
2 parents 3ca1292 + 794c0ea commit 3b06438

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3747,6 +3747,16 @@ namespace {
37473747
if (isSpecializationDepthGreaterThan(def, 8))
37483748
return nullptr;
37493749

3750+
// If we have an inline data member, it won't get eagerly instantiated
3751+
// when we instantiate the class. So, make sure we do that now to catch
3752+
// any instantiation errors.
3753+
for (auto member : decl->decls()) {
3754+
if (auto varDecl = dyn_cast<clang::VarDecl>(member)) {
3755+
Impl.getClangSema()
3756+
.InstantiateVariableDefinition(varDecl->getLocation(), varDecl);
3757+
}
3758+
}
3759+
37503760
return VisitCXXRecordDecl(def);
37513761
}
37523762

@@ -8321,6 +8331,10 @@ ClangImporter::Implementation::importDeclImpl(const clang::NamedDecl *ClangDecl,
83218331
bool &HadForwardDeclaration) {
83228332
assert(ClangDecl);
83238333

8334+
// If this decl isn't valid, don't import it. Bail now.
8335+
if (ClangDecl->isInvalidDecl())
8336+
return nullptr;
8337+
83248338
// Private and protected C++ class members should never be used, so we skip
83258339
// them entirely (instead of importing them with a corresponding Swift access
83268340
// level) to remove clutter from the module interface.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
template <class T>
2+
struct GetTypeValue {
3+
static constexpr const bool value = T::value;
4+
};
5+
6+
using Invalid1 = GetTypeValue<int>;
7+
8+
template <class T>
9+
struct GetTypeValueInline {
10+
inline static constexpr const bool value = T::value;
11+
};
12+
13+
using Invalid2 = GetTypeValueInline<int>;

test/Interop/Cxx/static/Inputs/module.modulemap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,8 @@ module StaticMemberFunc {
2222
header "static-member-func.h"
2323
requires cplusplus
2424
}
25+
26+
module ConstexprStaticMemberVarErrors {
27+
header "constexpr-static-member-var-errors.h"
28+
requires cplusplus
29+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %target-swift-ide-test -print-module -module-to-print=ConstexprStaticMemberVarErrors -I %S/Inputs -source-filename=x -enable-cxx-interop 2>&1 | %FileCheck %s
2+
3+
// Check that we properly report the error and don't crash when importing an
4+
// invalid decl.
5+
6+
// CHECK: error: type 'int' cannot be used prior to '::' because it has no members
7+
// CHECK: {{note: in instantiation of template class 'GetTypeValue<int>' requested here|note: in instantiation of static data member 'GetTypeValue<int>::value' requested here}}
8+
9+
// CHECK: error: type 'int' cannot be used prior to '::' because it has no members
10+
// CHECK: note: in instantiation of static data member 'GetTypeValueInline<int>::value' requested here

0 commit comments

Comments
 (0)