Skip to content

__builtin_bit_cast does not allow bitcasting from nullptr_t #117166

@tbaederr

Description

@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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"constexprAnything related to constant evaluationinvalidResolved as invalid, i.e. not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions