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 <nathanieloshead@gmail.com>
Reviewed-by: Marek Polacek <polacek@redhat.com>
Reviewed-by: Jason Merrill <jason@redhat.com>
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);
--- /dev/null
+// { 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 }