diff --git a/clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp b/clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp index 03c556cd70bb7..6ee3826fb3eea 100644 --- a/clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp +++ b/clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp @@ -254,9 +254,13 @@ static bool readPointerToBuffer(const Context &Ctx, const Pointer &FromPtr, } assert(P.isInitialized()); - // nullptr_t is a PT_Ptr for us, but it's still not std::is_pointer_v. - if (T == PT_Ptr) - assert(false && "Implement casting to pointer types"); + if (T == PT_Ptr) { + assert(P.getType()->isNullPtrType()); + // Clang treats nullptr_t has having NO bits in its value + // representation. So, we accept it here and leave its bits + // uninitialized. + return true; + } auto Buff = std::make_unique(ObjectReprChars.getQuantity()); diff --git a/clang/test/AST/ByteCode/builtin-bit-cast.cpp b/clang/test/AST/ByteCode/builtin-bit-cast.cpp index d3935b4f921b3..f89eb3584bbcf 100644 --- a/clang/test/AST/ByteCode/builtin-bit-cast.cpp +++ b/clang/test/AST/ByteCode/builtin-bit-cast.cpp @@ -133,8 +133,14 @@ namespace simple { /// This works in GCC and in the bytecode interpreter, but the current interpreter /// diagnoses it. + /// FIXME: Should also be rejected in the bytecode interpreter. static_assert(__builtin_bit_cast(intptr_t, nullptr) == 0); // ref-error {{not an integral constant expression}} \ // ref-note {{indeterminate value can only initialize an object}} + + constexpr int test_from_nullptr_pass = (__builtin_bit_cast(unsigned char[sizeof(nullptr)], nullptr), 0); + constexpr unsigned char NPData[sizeof(nullptr)] = {1,2,3,4}; + constexpr nullptr_t NP = __builtin_bit_cast(nullptr_t, NPData); + static_assert(NP == nullptr); } namespace Fail {