On Fri, Feb 13, 2026 at 08:49:49PM +0000, Jonathan Wakely wrote:
> On Fri, 13 Feb 2026 at 20:18, Marek Polacek <[email protected]> wrote:
> >
> > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
> >
> > -- >8 --
> > [bit.cast]/2: Mandates: Neither To nor From are consteval-only types
> >
> > but we are not checking this, so the attached test compiled.
> >
> >         PR libstdc++/124096
> >
> > libstdc++-v3/ChangeLog:
> >
> >         * include/std/bit: Check that neither To nor From are consteval-only
> >         types.
> >         * testsuite/26_numerics/bit/bit.cast/124096.cc: New test.
> > ---
> >  libstdc++-v3/include/std/bit                        |  4 ++++
> >  .../testsuite/26_numerics/bit/bit.cast/124096.cc    | 13 +++++++++++++
> >  2 files changed, 17 insertions(+)
> >  create mode 100644 
> > libstdc++-v3/testsuite/26_numerics/bit/bit.cast/124096.cc
> >
> > diff --git a/libstdc++-v3/include/std/bit b/libstdc++-v3/include/std/bit
> > index 6ea0f6eef83..f33acb999cf 100644
> > --- a/libstdc++-v3/include/std/bit
> > +++ b/libstdc++-v3/include/std/bit
> > @@ -93,6 +93,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> >        && is_trivially_copyable_v<_To> && is_trivially_copyable_v<_From>
> >  #endif
> >      {
> > +#if __cpp_impl_reflection >= 202506L
> > +      static_assert(!is_consteval_only_v<_To> && 
> > !is_consteval_only_v<_From>,
> > +                   "neither To nor From can be consteval-only types");
> > +#endif
> >        return __builtin_bit_cast(_To, __from);
> 
> Should it be checked in the front-end as well (or instead)?
> 
> Otherwise __builtin_bit_cast(std::meta::info, x) will still work.

Good point, I guess the built-in should be checked too.  Let's check
it in the front end then (if I kept the static_assert, we'd complain
twice).  Thanks.

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

-- >8 --
[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.
---
 gcc/cp/semantics.cc                     |  6 ++++++
 gcc/testsuite/g++.dg/reflect/bit_cast.C | 19 +++++++++++++++++++
 2 files changed, 25 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/reflect/bit_cast.C

diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 7f2ea938370..81f54f56dda 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 00000000000..bab62717f7e
--- /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 }

base-commit: 06221fe413302f16bbbc22324f98173f45ea9088
-- 
2.53.0

Reply via email to