https://gcc.gnu.org/g:46bd94dbdb3c0e1a3ffd7484a1ceda951c782033
commit r16-7830-g46bd94dbdb3c0e1a3ffd7484a1ceda951c782033 Author: Nathaniel Shead <[email protected]> Date: Wed Feb 25 23:19:32 2026 +1100 c++/reflection: Improve static_assert diagnostics when comparing reflections This adds another case to static_assert to print a helpful diagnostic when comparing reflections. This makes it easier to see what's gone wrong when doing 'static_assert(some_query(^^a) == some_query(^^b));' by decomposing the resulting resulting reflection. gcc/cp/ChangeLog: * constexpr.cc (diagnose_failing_condition): Also decompose comparisons of reflections. gcc/testsuite/ChangeLog: * g++.dg/reflect/diag5.C: New test. Signed-off-by: Nathaniel Shead <[email protected]> Reviewed-by: Marek Polacek <[email protected]> Reviewed-by: Jason Merrill <[email protected]> Diff: --- gcc/cp/constexpr.cc | 3 ++- gcc/testsuite/g++.dg/reflect/diag5.C | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 44e6b352d9a9..afd8d5731a11 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -3107,7 +3107,8 @@ diagnose_failing_condition (tree bad, location_t cloc, bool show_expr_p, else if (maybe_diagnose_standard_trait (cloc, bad)) ; else if (COMPARISON_CLASS_P (bad) - && ARITHMETIC_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0)))) + && (ARITHMETIC_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0))) + || REFLECTION_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0))))) { tree op0 = fold_operand (TREE_OPERAND (bad, 0), ctx); tree op1 = fold_operand (TREE_OPERAND (bad, 1), ctx); diff --git a/gcc/testsuite/g++.dg/reflect/diag5.C b/gcc/testsuite/g++.dg/reflect/diag5.C new file mode 100644 index 000000000000..1f808364ce87 --- /dev/null +++ b/gcc/testsuite/g++.dg/reflect/diag5.C @@ -0,0 +1,9 @@ +// { dg-do compile { target c++26 } } +// { dg-additional-options "-freflection" } +// Test that we decompose reflections in static_asserts, where applicable. + +constexpr auto a = ^^int; +consteval auto b() { return ^^double; } + +static_assert(a == b()); // { dg-error "static assertion failed" } +// { dg-message "the comparison reduces to '\\\(\\^\\^int == \\^\\^double\\\)'" "" { target *-*-* } .-1 }
