Skip to content

Commit a146bdc

Browse files
Fznamznontstellar
authored andcommitted
[clang] Fix single-element array initialization in constexpr
https://reviews.llvm.org/D130791 added an improvement that in case array element has a trivial constructor, it is evaluated once and the result is re-used for remaining elements. Make sure the constructor is evaluated for single-elements arrays too. Fixes llvm#60803 Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D145486 (cherry picked from commit af682f0)
1 parent ae37edf commit a146bdc

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

clang/lib/AST/ExprConstant.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10913,7 +10913,7 @@ bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E,
1091310913
for (unsigned I = OldElts; I < N; ++I)
1091410914
Value->getArrayInitializedElt(I) = Filler;
1091510915

10916-
if (HasTrivialConstructor && N == FinalSize) {
10916+
if (HasTrivialConstructor && N == FinalSize && FinalSize != 1) {
1091710917
// If we have a trivial constructor, only evaluate it once and copy
1091810918
// the result into all the array elements.
1091910919
APValue &FirstResult = Value->getArrayInitializedElt(0);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %clang_cc1 -std=c++20 -verify %s
2+
3+
// This test makes sure that a single element array doesn't produce
4+
// spurious errors during constexpr evaluation.
5+
6+
// expected-no-diagnostics
7+
struct Sub { int x; };
8+
9+
struct S {
10+
constexpr S() { Arr[0] = Sub{}; }
11+
Sub Arr[1];
12+
};
13+
14+
constexpr bool test() {
15+
S s;
16+
return true;
17+
}
18+
19+
static_assert(test());

0 commit comments

Comments
 (0)