-
Notifications
You must be signed in to change notification settings - Fork 12.1k
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
__builtin_bit_cast does not allow bitcasting from nullptr_t #117166
Comments
@llvm/issue-subscribers-clang-frontend Author: Timm Baeder (tbaederr)
`nullptr_t` is not a pointer type, see the `static_assert` in https://godbolt.org/z/vjq1Tse7W
In this example, the second bitcast works, but the first one does not: typedef __UINTPTR_TYPE__ uintptr_t;
constexpr uintptr_t A = __builtin_bit_cast(uintptr_t, nullptr);
typedef decltype(nullptr) nullptr_t;
constexpr decltype(nullptr) N = __builtin_bit_cast(nullptr_t, (uintptr_t)12);
static_assert(N == nullptr); The output suggests some bogus reason though: ./array.cpp:26:21: error: constexpr variable 'A' must be initialized by a constant expression
26 | constexpr uintptr_t A = __builtin_bit_cast(uintptr_t, nullptr);
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./array.cpp:26:25: note: indeterminate value can only initialize an object of type 'unsigned char' or 'std::byte'; 'unsigned long' is invalid
26 | constexpr uintptr_t A = __builtin_bit_cast(uintptr_t, nullptr);
| ^ GCC accepts the first one, but not the second. I opened https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117727 for that. |
I guess this is on purpose actually: llvm-project/clang/lib/AST/ExprConstant.cpp Lines 7216 to 7218 in a12e79a
This line is from eee944e, the first commit introducing |
Paging @frederick-vs-ja for spec knowledge, since this isn't as clear-cut as I thought, according to the gcc bug linked above. |
In Clang, we treat struct X {
X() {}
decltype(nullptr) p;
};
X f() { return X(); } ... does not store zero bits to the Because |
nullptr_t
is not a pointer type, see thestatic_assert
in https://godbolt.org/z/vjq1Tse7WIn this example, the second bitcast works, but the first one does not:
The output suggests some bogus reason though:
GCC accepts the first one, but not the second. I opened https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117727 for that.
The text was updated successfully, but these errors were encountered: