libstdc++: Use __builtin_constexpr_diag instead of __asm__("") in <meta> if possible

Your asm -> __asm__ patch made me think that these are perfect
candidates for __builtin_constexpr_diag, the asms were there just
to make it provably non-constant expression (guess *(char*)nullptr = 0;
would do as well).  But with __builtin_constexpr_diag we can tell
the user the details.

Additionally, when testing it I've found that the
https://eel.is/c++draft/meta.reflection#access.context-13
Throws: meta::exception unless cls is either the null reflection
or a reflection of a complete class type.
part of via's description was implemented wierdly, it did throw
an exception, but not by telling user that via has been called with
something that is not a null reflection nor reflection of a complete
class type, but that is_class_type has been called on a reflection
of something other than a type.

2026-03-11  Jakub Jelinek  <jakub@redhat.com>

	* include/std/meta (std::meta::exception::what()): Use
	__builtin_constexpr_diag instead of __asm__("") if supported.
	(std::meta::access_context::via(info)): Don't call is_class_type
	if __cls is not a type.  Use __builtin_constexpr_diag instead of
	__asm__("") if supported for -fno-exceptions.

	* g++.dg/reflect/eh8.C: Expect different diagnostics.
	* g++.dg/reflect/no-exceptions2.C: New test.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
This commit is contained in:
Jakub Jelinek
2026-03-11 07:55:58 +01:00
committed by Jakub Jelinek
parent f233f787c2
commit 5b09326f7b
3 changed files with 32 additions and 2 deletions

View File

@@ -24,7 +24,7 @@ bar ()
{
exception a (u8"\N{GRINNING FACE}\N{GRINNING FACE WITH SMILING EYES}\N{LEFT SPEECH BUBBLE}", ^^foo);
const char *b = a.what (); // { dg-message "in 'constexpr' expansion of 'a.std::meta::exception::what\\\(\\\)" }
return true; // { dg-error "inline assembly is not a constant expression" "" { target *-*-* } 0 }
return true; // { dg-error "constexpr message: std::meta::exception message could not be successfully transcoded from UTF-8 to ordinary literal encoding" "" { target *-*-* } 0 }
}
static_assert (foo ());

View File

@@ -0,0 +1,15 @@
// { dg-do compile { target c++26 } }
// { dg-additional-options "-freflection -fno-exceptions" }
#include <meta>
consteval bool
foo ()
{
auto ctx = std::meta::access_context::unchecked ();
auto ctx2 = ctx.via (^^::); // { dg-message "in 'constexpr' expansion of 'ctx.std::meta::access_context::via\\\(\\\^\\\^::\\\)'" }
return true;
}
auto b = foo (); // { dg-message "in 'constexpr' expansion of 'foo\\\(\\\)'" }
// { dg-error "constexpr message: std::meta::access_context::via argument other than null or complete class type reflection" "" { target *-*-* } 0 }

View File

@@ -120,7 +120,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// from UTF-8 to ordinary literal encoding failed.
// In that case what() should be non-constant.
if (_M_what.size() == 0 && _M_u8what.size() != 0)
#if __has_builtin(__builtin_constexpr_diag)
__builtin_constexpr_diag (2, "",
"std::meta::exception message could not "
"be successfully transcoded from UTF-8 to "
"ordinary literal encoding");
#else
__asm__("");
#endif
return _M_what.c_str();
}
consteval u8string_view u8what() const noexcept { return _M_u8what; }
@@ -591,15 +598,23 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
access_context::via(info __cls) const
{
if (__cls != info {}
&& (!std::meta::is_class_type(__cls)
&& (!std::meta::is_type(__cls)
|| !std::meta::is_class_type(__cls)
|| !std::meta::is_complete_type(__cls)))
{
#if __cpp_exceptions
throw std::meta::exception(u8"via argument other than null "
"or complete class type reflection",
^^access_context::via);
#else
#if __has_builtin(__builtin_constexpr_diag)
__builtin_constexpr_diag (2, "",
"std::meta::access_context::via argument "
"other than null or complete class type "
"reflection");
#else
__asm__("");
#endif
return *this;
#endif
}