public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: gcc-patches@gcc.gnu.org
Subject: [pushed] c++: explain failing static_assert
Date: Fri, 21 Jan 2022 15:18:05 -0500	[thread overview]
Message-ID: <20220121201805.563013-1-jason@redhat.com> (raw)

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	other threads:[~2022-01-21 20:18 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220121201805.563013-1-jason@redhat.com \
    --to=jason@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).