[clang] Consider aggregate bases when checking if an InitListExpr is constant (PR #80519)

Reid Kleckner via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 5 15:20:44 PST 2024


================
@@ -108,3 +109,22 @@ int computed_with_lambda = [] {
   return result;
 }();
 #endif
+
+#if __cplusplus >= 201703L
+namespace DynamicFileScopeLiteral {
+// This covers the case where we have a file-scope compound literal with a
+// non-constant initializer in C++. Previously, we had a bug where Clang forgot
+// to consider initializer list elements for bases.
+struct Empty {};
+struct Foo : Empty {
+  int x;
+  int y;
+};
+int f();
+Foo o = (Foo){
+  {},
+  1,
+  f() // expected-error {{initializer element is not a compile-time constant}}
----------------
rnk wrote:

That is correct and I did discover that in my testing, but I think fixing that goes beyond the scope of this patch. Clang currently rejects this case if you remove the empty base: https://godbolt.org/z/1x8hshM75 Adding support for non-constant compound literals at file scope requires codegen changes, and this is fixing a small bug to make more `isConstantInitializer` correct.

The real issue is that I couldn't find a better way to test `isConstantInitializer` that will be resilient to that future bug fix, and I'm not sure what to do about that.

https://github.com/llvm/llvm-project/pull/80519


More information about the cfe-commits mailing list