Skip to content

Commit 52a55a7

Browse files
committed
[clang][Interp] Allow zero-init of primitives with an empty init list
Differential Revision: https://reviews.llvm.org/D158595
1 parent ce031fc commit 52a55a7

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

clang/lib/AST/Interp/ByteCodeExprGen.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,10 @@ bool ByteCodeExprGen<Emitter>::VisitInitListExpr(const InitListExpr *E) {
571571

572572
// Primitive values.
573573
if (std::optional<PrimType> T = classify(E->getType())) {
574-
assert(E->getNumInits() == 1);
575574
assert(!DiscardResult);
575+
if (E->getNumInits() == 0)
576+
return this->visitZeroInitializer(E->getType(), E);
577+
assert(E->getNumInits() == 1);
576578
return this->delegate(E->inits()[0]);
577579
}
578580

clang/test/AST/Interp/literals.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ static_assert(-(1 << 31), ""); // expected-error {{not an integral constant expr
9191
// ref-error {{not an integral constant expression}} \
9292
// ref-note {{outside the range of representable values}} \
9393
94+
namespace PrimitiveEmptyInitList {
95+
constexpr int a = {};
96+
static_assert(a == 0, "");
97+
constexpr bool b = {};
98+
static_assert(!b, "");
99+
constexpr double d = {};
100+
static_assert(d == 0.0, "");
101+
}
102+
94103

95104
enum E {};
96105
constexpr E e = static_cast<E>(0);

0 commit comments

Comments
 (0)