]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++/reflection: Improve static_assert diagnostics when comparing reflections
authorNathaniel Shead <nathanieloshead@gmail.com>
Wed, 25 Feb 2026 12:19:32 +0000 (23:19 +1100)
committerNathaniel Shead <nathanieloshead@gmail.com>
Sun, 1 Mar 2026 08:16:47 +0000 (19:16 +1100)
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>
gcc/cp/constexpr.cc
gcc/testsuite/g++.dg/reflect/diag5.C [new file with mode: 0644]

index 44e6b352d9a9a9c4994ae7688fd954b9b961a1fe..afd8d5731a1168532b2f4872b196c9007c084441 100644 (file)
@@ -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 (file)
index 0000000..1f80836
--- /dev/null
@@ -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 }