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.
> }
> #endif // __cpp_lib_bit_cast
> diff --git a/libstdc++-v3/testsuite/26_numerics/bit/bit.cast/124096.cc
> b/libstdc++-v3/testsuite/26_numerics/bit/bit.cast/124096.cc
> new file mode 100644
> index 00000000000..12c93f712df
> --- /dev/null
> +++ b/libstdc++-v3/testsuite/26_numerics/bit/bit.cast/124096.cc
> @@ -0,0 +1,13 @@
> +// PR libstdc++/124096
> +// { dg-do compile { target c++26 } }
> +// { dg-additional-options "-freflection" }
> +
> +#include <bit>
> +
> +consteval void
> +f ()
> +{
> + (void) std::bit_cast<long long>(^^int); // { dg-error "here" }
> +}
> +
> +// { dg-error "static assertion failed:" "" { target *-*-* } 0 }
>
> base-commit: 8a794643cc71301d96bb0e62c8d89494929fa0eb
> --
> 2.53.0
>