[clang] [clang] Reject constexpr-unknown values as constant expressions more consistently (PR #129952)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 5 21:43:42 PST 2025


================
@@ -154,3 +154,26 @@ int g() {
     static_assert(f(arr) == 5);
 }
 }
+
+namespace GH128409 {
+  int &ff();
+  int &x = ff(); // nointerpreter-note {{declared here}}
+  constinit int &z = x; // expected-error {{variable does not have a constant initializer}}
+                        // expected-note at -1 {{required by 'constinit' specifier here}}
+                        // nointerpreter-note at -2 {{initializer of 'x' is not a constant expression}}
----------------
tbaederr wrote:

I don't want to block on this, but the diagnostic here is a little off... Even _if_ the initializer of `x` was a constant expression, the declaration of `z` would still not be valid because `x` is not const (or constexpr).

In other words, if I saw this error in practice, I'd try to fix `x`'s initializer, just to be greeted by another diagnostic.

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


More information about the cfe-commits mailing list