[clang] [clang] Fixed inconsistent diagnostics when initializing a constexpr variable with a compound literal (PR #232825)

Abdulmalek Almkainzi via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 22 02:22:46 PDT 2026


aalmkainzi wrote:

Ok i'll try to do a recap.

file scope compound literals are expected to have constant initializer (not strictly constexpr, just the global/static const kind), but the `BuildCompoundLiteralExpr` function converts it to a `ConstantExpr`, so the evaluator doesn't error out even if it's not strictly standard `constexpr`. example:
```C
const int a = 1;
constexpr int b = (int){a}; // accepted because BuildCompoundLiteralExpr converts the expression `a` into ConstantExpr
constexpr int c = a; // rejected
```

Now there's the other problem of non-null pointers being allowed into constexpr if they are in compound literal, because currently the only check for non nulls is actually in `Perform` not the evaluator, and `Perform` doesn't recurse or visit subexpressions if the rhs is a compound literal.

The other PR I made https://github.com/llvm/llvm-project/pull/230477 makes `Perform` handle that by looping over all the members.

I also did another approach by making the evaluator fail if non null pointer initializer is encountered in C. its in this branch https://github.com/aalmkainzi/llvm-project/tree/disallow-non-null-constexpr-pointers-in-both-modes (not yet part of any PR).

In my opinion, it makes more sense to fail at the evaluator, not Perform, since that seems to be where all the other similar checks are.

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


More information about the cfe-commits mailing list