tbaeder created this revision.
tbaeder added reviewers: aaron.ballman, erichkeane.
Herald added a project: All.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This is about the problem described in 
https://discourse.llvm.org/t/apvalue-lifetime-problem-when-evaluating-constant-expressions/64002/5

The (old) condition in this if statement is meant to handle initializers that 
depend on (other) array elements. In this case, the array needs to be expanded 
before the array filler is evaluated:

  if (NumEltsToInit != NumElts && MaybeElementDependentArrayFiller(FillerExpr))
    NumEltsToInit = NumElts;

in the test case described on Discourse (which is attached in the patch), 
`MaybeElementDependentArrayFiller()` returns `false` for the outer array but 
true for the inner array. Since every `InitListExpr` can have an array filler, 
I believe the function should take that into account.

This fixes https://github.com/llvm/llvm-project/issues/56016


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D131155

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


Index: clang/test/SemaCXX/constexpr-array-init.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/constexpr-array-init.cpp
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -std=c++20 -verify %s
+
+struct Foo {
+  int a;
+  constexpr Foo()
+      : a(get_int()) {
+  }
+
+  constexpr int get_int() const {
+    return 5;
+  }
+};
+
+static constexpr Foo bar[2][1] = {
+    {{}},
+};
+static_assert(bar[0][0].a == 5);
+static_assert(bar[1][0].a == 5);
+
Index: clang/lib/AST/ExprConstant.cpp
===================================================================
--- clang/lib/AST/ExprConstant.cpp
+++ clang/lib/AST/ExprConstant.cpp
@@ -10695,6 +10695,11 @@
       if (MaybeElementDependentArrayFiller(ILE->getInit(I)))
         return true;
     }
+
+    if (ILE->hasArrayFiller() &&
+        MaybeElementDependentArrayFiller(ILE->getArrayFiller()))
+      return true;
+
     return false;
   }
   return true;


Index: clang/test/SemaCXX/constexpr-array-init.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/constexpr-array-init.cpp
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -std=c++20 -verify %s
+
+struct Foo {
+  int a;
+  constexpr Foo()
+      : a(get_int()) {
+  }
+
+  constexpr int get_int() const {
+    return 5;
+  }
+};
+
+static constexpr Foo bar[2][1] = {
+    {{}},
+};
+static_assert(bar[0][0].a == 5);
+static_assert(bar[1][0].a == 5);
+
Index: clang/lib/AST/ExprConstant.cpp
===================================================================
--- clang/lib/AST/ExprConstant.cpp
+++ clang/lib/AST/ExprConstant.cpp
@@ -10695,6 +10695,11 @@
       if (MaybeElementDependentArrayFiller(ILE->getInit(I)))
         return true;
     }
+
+    if (ILE->hasArrayFiller() &&
+        MaybeElementDependentArrayFiller(ILE->getArrayFiller()))
+      return true;
+
     return false;
   }
   return true;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to