Skip to content

Commit d89333f

Browse files
authored
Merge pull request #83825 from egorzhdan/egorzhdan/constexpr-in-namespace
[cxx-interop] Import constexpr globals in a namespace
2 parents ed70a1b + 6bdd44e commit d89333f

File tree

5 files changed

+33
-1
lines changed

5 files changed

+33
-1
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4599,7 +4599,7 @@ namespace {
45994599
isStatic = true;
46004600

46014601
// For now we don't import static constexpr. TODO: Lift this restriction.
4602-
if (isStatic && decl->isConstexpr())
4602+
if (isStatic && !isClangNamespace(dc) && decl->isConstexpr())
46034603
return nullptr;
46044604

46054605
auto introducer = Impl.shouldImportGlobalAsLet(decl->getType())
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
namespace NS1 {
2+
constexpr int myConstexprInt = 123;
3+
static constexpr int myStaticConstexprInt = 456;
4+
} // namespace NS1

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ module ClassesSecondHeader {
1212
requires cplusplus
1313
}
1414

15+
module ConstexprInNamespace {
16+
header "constexpr-in-namespace.h"
17+
requires cplusplus
18+
}
19+
1520
module ExternWithinNamespace {
1621
header "extern-within-namespace.h"
1722
export *
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %target-swift-ide-test -print-module -module-to-print=ConstexprInNamespace -I %S/Inputs -source-filename=x -cxx-interoperability-mode=upcoming-swift | %FileCheck %s
2+
3+
// CHECK: enum NS1 {
4+
// CHECK: static var myConstexprInt: Int32
5+
// CHECK: static var myStaticConstexprInt: Int32
6+
// CHECK: }
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %target-run-simple-swift(-I %S/Inputs -cxx-interoperability-mode=upcoming-swift)
2+
// REQUIRES: executable_test
3+
4+
import StdlibUnittest
5+
import ConstexprInNamespace
6+
7+
var NamespacesTestSuite = TestSuite("Constexpr in namespace")
8+
9+
NamespacesTestSuite.test("constexpr int") {
10+
expectEqual(NS1.myConstexprInt, 123)
11+
}
12+
13+
NamespacesTestSuite.test("static constexpr int") {
14+
expectEqual(NS1.myStaticConstexprInt, 456)
15+
}
16+
17+
runAllTests()

0 commit comments

Comments
 (0)