]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: bit_cast and consteval-only types [PR124096]
authorMarek Polacek <polacek@redhat.com>
Fri, 13 Feb 2026 18:46:58 +0000 (13:46 -0500)
committerMarek Polacek <polacek@redhat.com>
Wed, 18 Feb 2026 14:15:32 +0000 (09:15 -0500)
[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>
gcc/cp/semantics.cc
gcc/testsuite/g++.dg/reflect/bit_cast.C [new file with mode: 0644]

index 7f2ea93837039c1fafe170e79e6195a75a229f9d..81f54f56ddaa164380fe901493f6d61e5654c4f1 100644 (file)
@@ -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 (file)
index 0000000..bab6271
--- /dev/null
@@ -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 }