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.
This commit is contained in:
Jason Merrill 2022-01-21 11:03:26 -05:00
parent 847a8301ad
commit 2e01fde49e
2 changed files with 22 additions and 13 deletions

View File

@ -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)
{

View File

@ -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" }