public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-6809] c++: explain failing static_assert
@ 2022-01-21 20:18 Jason Merrill
  0 siblings, 0 replies; only message in thread
From: Jason Merrill @ 2022-01-21 20:18 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:2e01fde49eab8dfb7ba48b22c2da596b16571525

commit r12-6809-g2e01fde49eab8dfb7ba48b22c2da596b16571525
Author: Jason Merrill <jason@redhat.com>
Date:   Fri Jan 21 11:03:26 2022 -0500

    c++: explain failing static_assert
    
    While looking at another bug I wanted the compiler to tell me what the two
    unequal values were.
    
    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.

Diff:
---
 gcc/cp/semantics.cc                            | 33 ++++++++++++++++----------
 gcc/testsuite/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" }


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-01-21 20:18 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-21 20:18 [gcc r12-6809] c++: explain failing static_assert Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).