[PATCH] D145486: [clang] Fix single-element array initialization in constexpr

Mariya Podchishchaeva via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 7 04:11:41 PST 2023


Fznamznon created this revision.
Herald added a project: All.
Fznamznon requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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 https://github.com/llvm/llvm-project/issues/60803


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D145486

Files:
  clang/lib/AST/ExprConstant.cpp
  clang/test/SemaCXX/constexpr-single-element-array.cpp


Index: clang/test/SemaCXX/constexpr-single-element-array.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/constexpr-single-element-array.cpp
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -std=c++20 -verify %s
+
+/// This test makes sure that single element array in constexpr doesn't produce
+/// spurious errors.
+
+/// expected-no-diagnostics
+struct Sub { int x; };
+
+struct S {
+  constexpr S() { Arr[0] = Sub{}; }
+  Sub Arr[1];
+};
+
+constexpr bool test() {
+  S s;
+  return true;
+}
+
+static_assert(test());
Index: clang/lib/AST/ExprConstant.cpp
===================================================================
--- clang/lib/AST/ExprConstant.cpp
+++ clang/lib/AST/ExprConstant.cpp
@@ -10917,7 +10917,7 @@
         for (unsigned I = OldElts; I < N; ++I)
           Value->getArrayInitializedElt(I) = Filler;
 
-      if (HasTrivialConstructor && N == FinalSize) {
+      if (HasTrivialConstructor && N == FinalSize && N != 1) {
         // If we have a trivial constructor, only evaluate it once and copy
         // the result into all the array elements.
         APValue &FirstResult = Value->getArrayInitializedElt(0);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D145486.502982.patch
Type: text/x-patch
Size: 1187 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230307/9389edb0/attachment-0001.bin>


More information about the cfe-commits mailing list