https://gcc.gnu.org/g:2206271eadd60f14372f87f3afc94632dc0463b1
commit r16-7567-g2206271eadd60f14372f87f3afc94632dc0463b1 Author: Marek Polacek <[email protected]> Date: Fri Feb 13 13:46:58 2026 -0500 c++: bit_cast and consteval-only types [PR124096] [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 <[email protected]> Diff: --- gcc/cp/semantics.cc | 6 ++++++ gcc/testsuite/g++.dg/reflect/bit_cast.C | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 7f2ea9383703..81f54f56ddaa 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -14836,6 +14836,12 @@ cp_build_bit_cast (location_t loc, tree type, tree arg, "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)) diff --git a/gcc/testsuite/g++.dg/reflect/bit_cast.C b/gcc/testsuite/g++.dg/reflect/bit_cast.C new file mode 100644 index 000000000000..bab62717f7ee --- /dev/null +++ b/gcc/testsuite/g++.dg/reflect/bit_cast.C @@ -0,0 +1,19 @@ +// 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 }
