aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Merrill <jason@redhat.com>2022-01-21 11:03:26 -0500
committerJason Merrill <jason@redhat.com>2022-01-21 14:41:00 -0500
commit2e01fde49eab8dfb7ba48b22c2da596b16571525 (patch)
treef84355c2ba7ca6da9fdc1da1456ad9de358fbaa1
parent847a8301add0a316767878342c1367948835c181 (diff)
downloadgcc-2e01fde49eab8dfb7ba48b22c2da596b16571525.zip
gcc-2e01fde49eab8dfb7ba48b22c2da596b16571525.tar.gz
gcc-2e01fde49eab8dfb7ba48b22c2da596b16571525.tar.bz2
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.
-rw-r--r--gcc/cp/semantics.cc33
-rw-r--r--gcc/testsuite/g++.dg/template/explicit-args6.C2
2 files changed, 22 insertions, 13 deletions
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 8e22944..640b2d7 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 d853564..311a767 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" }