-
Notifications
You must be signed in to change notification settings - Fork 11.3k
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
Permitting static constexpr variables in consteval functions #82994
Comments
BTW, if I make the functions |
@llvm/issue-subscribers-clang-frontend Author: Mateusz Pusz (mpusz)
It seems that Clang does not support static constexpr variables in constexpr functions properly:
https://godbolt.org/z/fx1jTModK The value of a static constexpr variable set in a constexpr function should be available at runtime. |
Confirmed, it would be nice to have a reduction, it is not obvious what is going on here but maybe @cor3ntin might have an idea. |
@llvm/issue-subscribers-c-20 Author: Mateusz Pusz (mpusz)
It seems that Clang does not support static constexpr variables in constexpr functions properly:
https://godbolt.org/z/fx1jTModK The value of a static constexpr variable set in a constexpr function should be available at runtime. |
Reduced example (Godbolt link): #include <cstddef>
#include <cstdio>
template<class T, std::size_t N>
struct my_array {
T elems_[N];
};
consteval my_array<char, 2> get_array()
{
my_array<char, 2> result;
result.elems_[0] = 'a';
result.elems_[1] = '\0';
return result;
}
#ifdef CONSTEVAL_DISABLED
constexpr
#else
consteval
#endif
const char* get_buffer_ptr()
{
static constexpr auto arr = get_array();
return arr.elems_;
}
int main()
{
constexpr auto p = get_buffer_ptr();
static_assert(p[0] == 'a'); // passes with Clang
static_assert(p[1] == '\0'); // passes with Clang
// if get_buffer_ptr is consteval, an empty NTBS is printed with Clang,
// even though static_assert's pass
std::puts(p);
} It seems that Clang mistakenly skipped the constant-initialization of static constexpr variables (or mistakenly used zero-initialization only) in consteval functions. @shafik I think this should be labeled "c++23" as the examples are only well-formed since P2647R1. |
@cor3ntin @AaronBallman this feels vaguely familiar but I can't the problem it remind me of |
consteval const int* a() {
static constexpr int b = 3;
return &b;
}
int c() { return *a(); } here is an even more minimal reduction (https://godbolt.org/z/5dEr7sdbs) |
@llvm/issue-subscribers-clang-codegen Author: Mateusz Pusz (mpusz)
It seems that Clang does not support static constexpr variables in constexpr functions properly:
https://godbolt.org/z/fx1jTModK The value of a static constexpr variable set in a constexpr function should be available at runtime. |
Because the function Maybe we need to have a visitor to extract static constexpr vars from immediate functions (and emit them)? |
An extra visitor would be a little expensive. Perhaps local static variables in consteval functions should be handed to the ASTConsumer immediately by Sema, rather than assuming that the consumer will find them indirectly. |
Hmm. I suppose it's not technically as simple as looking for the declarations referenced by the constant value of the call, since you could leak out a reference to a static local via either a template argument or a local / lambda class. I don't love the idea of forcing IRGen to eagerly track every static local in every |
CC @Fznamznon |
Note clang does not behave the same as edg/MSVC here: https://godbolt.org/z/Gzjjj7dP4 |
It seems that Clang does not support static constexpr variables in constexpr functions properly:
https://godbolt.org/z/fx1jTModK
The value of a static constexpr variable set in a constexpr function should be available at runtime.
The text was updated successfully, but these errors were encountered: