[clang] [clang] Reject initializing constexpr pointer variables with non-null pointers (PR #228776)
Abdulmalek Almkainzi via cfe-commits
cfe-commits at lists.llvm.org
Sat Oct 3 15:04:04 PDT 2026
aalmkainzi wrote:
Something to note is, when initializing with initializer list, the error message is different:
```
error: constexpr variable 'bar1' must be initialized by a constant expression
7 | constexpr foo bar1 = (foo){.a = &b};
| ^ ~~~~~~~~~~~~~~
error: constexpr pointer initializer is not null
8 | constexpr foo bar2 = {.a = &b};
| ^
2 errors generated.
```
This is because it's happening in here:
https://github.com/llvm/llvm-project/blob/a49dfca4e6f10c7044e03975f1f3f462864b74ca/clang/lib/Sema/SemaInit.cpp#L8582-L8588
This is reached only if the initializer is an initializer list.
My first attempt was to edit that place to also trigger for any assignment, not just initializer list, but that was more complicated than I expected.
The check I added instead is a bit hacky, but works.
The check in `SemaInit.cpp` can actually be deleted now, and the error messages become the same:
```
error: constexpr variable 'bar1' must be initialized by a constant expression
7 | constexpr foo bar1 = (foo){.a = &b};
| ^ ~~~~~~~~~~~~~~
error: constexpr variable 'bar2' must be initialized by a constant expression
8 | constexpr foo bar2 = {.a = &b};
| ^ ~~~~~~~~~
2 errors generated.
```
But I didn't do that to not change existing error messages.
https://github.com/llvm/llvm-project/pull/228776
More information about the cfe-commits
mailing list