While looking at another bug I wanted the compiler to tell me what the two
unequal values were.

Tested x86_64-pc-linux-gnu, applying to trunk.

gcc/cp/ChangeLog:

        * semantics.cc (find_failing_clause): Return expr if not
        decomposable.
        (finish_static_assert): Show constant values in failing
        comparison.

gcc/testsuite/ChangeLog:

        * g++.dg/template/explicit-args6.C: Add expected message.
---
 gcc/cp/semantics.cc                           | 33 +++++++++++--------
 .../g++.dg/template/explicit-args6.C          |  2 ++
 2 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 8e22944c2dc..640b2d75471 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -11062,9 +11062,10 @@ find_failing_clause_r (tree expr)
 static tree
 find_failing_clause (tree expr)
 {
-  if (TREE_CODE (expr) != TRUTH_ANDIF_EXPR)
-    return NULL_TREE;
-  return find_failing_clause_r (expr);
+  if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
+    if (tree e = find_failing_clause_r (expr))
+      expr = e;
+  return expr;
 }
 
 /* Build a STATIC_ASSERT for a static assertion with the condition
@@ -11134,9 +11135,9 @@ finish_static_assert (tree condition, tree message, 
location_t location,
          tree bad = find_failing_clause (orig_condition);
          /* If not, or its location is unusable, fall back to the previous
             location.  */
-         location_t cloc = location;
-         if (cp_expr_location (bad) != UNKNOWN_LOCATION)
-           cloc = cp_expr_location (bad);
+         location_t cloc = cp_expr_loc_or_loc (bad, location);
+         /* Nobody wants to see the artificial (bool) cast.  */
+         bad = tree_strip_nop_conversions (bad);
 
           /* Report the error. */
          if (len == 0)
@@ -11144,16 +11145,22 @@ finish_static_assert (tree condition, tree message, 
location_t location,
          else
            error_at (cloc, "static assertion failed: %s",
                      TREE_STRING_POINTER (message));
-         if (show_expr_p)
-           inform (cloc, "%qE evaluates to false",
-                   /* Nobody wants to see the artificial (bool) cast.  */
-                   (bad ? tree_strip_nop_conversions (bad) : orig_condition));
 
          /* Actually explain the failure if this is a concept check or a
             requires-expression.  */
-         if (concept_check_p (orig_condition)
-             || TREE_CODE (orig_condition) == REQUIRES_EXPR)
-           diagnose_constraints (location, orig_condition, NULL_TREE);
+         if (concept_check_p (bad)
+             || TREE_CODE (bad) == REQUIRES_EXPR)
+           diagnose_constraints (location, bad, NULL_TREE);
+         else if (COMPARISON_CLASS_P (bad)
+                  && ARITHMETIC_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0))))
+           {
+             tree op0 = fold_non_dependent_expr (TREE_OPERAND (bad, 0));
+             tree op1 = fold_non_dependent_expr (TREE_OPERAND (bad, 1));
+             tree cond = build2 (TREE_CODE (bad), boolean_type_node, op0, op1);
+             inform (cloc, "the comparison reduces to %qE", cond);
+           }
+         else if (show_expr_p)
+           inform (cloc, "%qE evaluates to false", bad);
        }
       else if (condition && condition != error_mark_node)
        {
diff --git a/gcc/testsuite/g++.dg/template/explicit-args6.C 
b/gcc/testsuite/g++.dg/template/explicit-args6.C
index d853564e3be..311a7678923 100644
--- a/gcc/testsuite/g++.dg/template/explicit-args6.C
+++ b/gcc/testsuite/g++.dg/template/explicit-args6.C
@@ -20,6 +20,8 @@ constexpr unsigned
 frob()
 {
   static_assert(N == 1, "user-friendly diagnostic"); // { dg-error 
"user-friendly" }
+  // dg-message { "-1 == 1" "" { target *-*-* } .-1 }
+
   // narrowing check, reject negative values
   return unsigned{N};          // { dg-prune-output "narrowing" }
 } // { dg-prune-output "flows off the end" }

base-commit: c163647ffbc9a20c8feb6e079dbecccfe016c82e
prerequisite-patch-id: 6e305598074684612bebec8a36ecf37de96519f8
-- 
2.27.0

Reply via email to