-
Notifications
You must be signed in to change notification settings - Fork 12.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Clang][C23] constexpr initialized with compound literal incorrectly allows non-null pointers #121694
Comments
@llvm/issue-subscribers-clang-frontend Author: Jan Hendrik Farr (Cydox)
@Fznamznon
Compiler Explorer: https://godbolt.org/z/oTvq738fa typedef struct {
int *a;
} foo;
int b = 42;
constexpr foo bar1 = (foo){.a = &b}; // compiles without warning (even with -Wpedantic)
constexpr foo bar2 = {.a = &b}; // error: constexpr pointer initializer is not null Initializing with a compound literal is a GNU extension [1]: But it should be treated like the variable was initialized with an initializer list. So the same constraints should be enforced in the
The behavior of the generated code appears to be correct and what you'd expect though (in the limited testing I've done).
The problem appears to be that initialization with compound literal bypasses this check: llvm-project/clang/lib/Sema/SemaInit.cpp Lines 8245 to 8249 in 2adcec7
and only hits this one: llvm-project/clang/lib/Sema/SemaDecl.cpp Lines 14507 to 14519 in 2adcec7
[1] https://gcc.gnu.org/onlinedocs/gcc/Compound-Literals.html |
I see the point, I'm not quite sure though what is the best output of clang should be. Applying [1] to [2] probably requires to emit a hard error, however I'm not sure that emitting an error for gcc extension whereas gcc itself doesn't emit an error is a good idea. |
GCC accepts this code as a GNU extension, with a pedantic warning:
and rejects this code as being a constraint violation:
which feels a bit inconsistent to me, at least at first glance. @pinskia do you happen to know if these are intentionally handled differently in GCC? My inclination would be that both should work as a GNU extension but maybe I'm missing something? |
Yes, there seems be to an order issue inside GCC, the check for non-null ptr for constexpr is handled before the "unwrapping": of the compound literal. Let me file a bug. I think GCC should reject |
@Fznamznon
Compiler Explorer: https://godbolt.org/z/oTvq738fa
Initializing with a compound literal is a GNU extension [1]:
But it should be treated like the variable was initialized with an initializer list. So the same constraints should be enforced in the
constexpr
case. Meaning no [2]:The behavior of the generated code appears to be correct and what you'd expect though (in the limited testing I've done).
gcc
behaves very similarly, however it does provide a warning with-Wpedantic
for thebar1
initialization (bar2
correctly errors).The problem appears to be that initialization with compound literal bypasses this check:
llvm-project/clang/lib/Sema/SemaInit.cpp
Lines 8245 to 8249 in 2adcec7
and only hits this one:
llvm-project/clang/lib/Sema/SemaDecl.cpp
Lines 14507 to 14519 in 2adcec7
[1] https://gcc.gnu.org/onlinedocs/gcc/Compound-Literals.html
[2] https://en.cppreference.com/w/c/language/constexpr
The text was updated successfully, but these errors were encountered: