[bit.cast]/2: Mandates: Neither To nor From are consteval-only types
but we are not checking this, so the attached test compiled. I'm
adding the check into cp_build_bit_cast rather than <bit> so that
we detect this even for __builtin_bit_cast.
PR c++/124096
gcc/cp/ChangeLog:
* semantics.cc (cp_build_bit_cast): Check that neither argument is
consteval-only.
gcc/testsuite/ChangeLog:
* g++.dg/reflect/bit_cast.C: New test.
Reviewed-by: Jakub Jelinek <jakub@redhat.com>
"is not trivially copyable", type);
return error_mark_node;
}
+ if (consteval_only_p (type) || consteval_only_p (arg))
+ {
+ error_at (loc, "%<__builtin_bit_cast%> cannot be used with "
+ "consteval-only types");
+ return error_mark_node;
+ }
}
if (error_operand_p (arg))
--- /dev/null
+// PR c++/124096
+// { dg-do compile { target c++26 } }
+// { dg-additional-options "-freflection" }
+
+#include <bit>
+
+consteval void
+f ()
+{
+ auto a = ^^int;
+ auto *b = &a;
+ void *c = nullptr;
+ (void) std::bit_cast<void *>(b); // { dg-message "from here" }
+ (void) std::bit_cast<decltype(^^int) *>(c); // { dg-message "from here" }
+ __builtin_bit_cast (void *, b); // { dg-error ".__builtin_bit_cast. cannot be used with consteval-only types" }
+ __builtin_bit_cast (decltype(^^int) *, c); // { dg-error ".__builtin_bit_cast. cannot be used with consteval-only types" }
+}
+
+// { dg-error ".__builtin_bit_cast. cannot be used with consteval-only types" "" { target *-*-* } 0 }