aboutsummaryrefslogtreecommitdiff
path: root/gcc/cp/constraint.cc
diff options
context:
space:
mode:
authorPatrick Palka <ppalka@redhat.com>2020-03-28 08:43:20 -0400
committerPatrick Palka <ppalka@redhat.com>2020-03-28 08:43:20 -0400
commit75defde9fb56157d5f0279720d48866925b71b19 (patch)
tree9aacf4a114baa2bc27601c688bd699e3d5ffd9b5 /gcc/cp/constraint.cc
parentc6a562de88c44a555e1688c212869b20b02151bc (diff)
downloadgcc-75defde9fb56157d5f0279720d48866925b71b19.zip
gcc-75defde9fb56157d5f0279720d48866925b71b19.tar.gz
gcc-75defde9fb56157d5f0279720d48866925b71b19.tar.bz2
c++: Replay errors during diagnosis of constraint satisfaction failures
This patch adds a new flag -fconcepts-diagnostics-depth to the C++ frontend which controls how deeply we replay errors when diagnosing a constraint satisfaction failure. The default is -fconcepts-diagnostics-depth=1 which diagnoses only the topmost constraint satisfaction failure and is consistent with our behavior before this patch. By increasing this flag's value, the user can control how deeply they want the compiler to explain a constraint satisfaction error. For example, if the unsatisfied constraint is a disjunction, then the default behavior is to just say "no branch in the disjunction is satisfied", but with -fconcepts-diagnostics-depth=2 we will additionally replay and diagnose the error in each branch of the disjunction. And if the unsatisfied constraint is a requires expression, then we will replay the error in the requires expression, etc. This proceeds recursively until there is nothing more to replay or we exceeded the maximum depth specified by the flag. Implementation wise, this patch essentially just uncomments the existing commented-out code that performs the error-replaying, and along the way adds logic to keep track of and limit the current replay depth. Besides that, there is a new routine collect_operands_of_disjunction which flattens a disjunction and collects all of its operands into a vector. The extra diagnostics enabled by this flag are at times longer than they need to be (e.g. "the operand is_array_v<...> is unsatisfied because \n the expression is_array_v<...> [with ...] evaluated to false") and not immediately easy to follow (especially when there are nested disjunctions), but the transparency provided by these optional diagnostics seems to be pretty helpful in practice. gcc/c-family/ChangeLog: * c.opt: Add -fconcepts-diagnostics-depth. gcc/cp/ChangeLog: * constraint.cc (finish_constraint_binary_op): Set the location of EXPR as well as its range, because build_x_binary_op doesn't always do so. (current_constraint_diagnosis_depth): New. (concepts_diagnostics_max_depth_exceeded_p): New. (collect_operands_of_disjunction): New. (satisfy_disjunction): When diagnosing a satisfaction failure, maybe replay each branch of the disjunction, subject to the current diagnosis depth. (diagnose_valid_expression): When diagnosing a satisfaction failure, maybe replay the substitution error, subject to the current diagnosis recursion. (diagnose_valid_type): Likewise. (diagnose_nested_requiremnet): Likewise. (diagnosing_failed_constraint::diagnosing_failed_constraint): Increment current_constraint_diagnosis_depth when diagnosing. (diagnosing_failed_constraint::~diagnosing_failed_constraint): Decrement current_constraint_diagnosis_depth when diagnosing. (diagnosing_failed_constraint::replay_errors_p): New static member function. (diagnose_constraints): Don't diagnose if concepts_diagnostics_max_depth is 0. Emit a one-off note to increase -fconcepts-diagnostics-depth if the limit was exceeded. * cp-tree.h (diagnosing_failed_constraint::replay_errors_p): Declare. gcc/testsuite/ChangeLog: * g++.dg/concepts/diagnostic2.C: Expect "no operand" instead of "neither operand". * g++.dg/concepts/diagnostic5.C: New test.
Diffstat (limited to 'gcc/cp/constraint.cc')
-rw-r--r--gcc/cp/constraint.cc150
1 files changed, 130 insertions, 20 deletions
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index a86bcdf..76c5203 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -162,6 +162,7 @@ finish_constraint_binary_op (location_t loc,
/* When either operand is dependent, the overload set may be non-empty. */
if (expr == error_mark_node)
return error_mark_node;
+ expr.set_location (loc);
expr.set_range (lhs.get_start (), rhs.get_finish ());
return expr;
}
@@ -2395,6 +2396,49 @@ satisfy_conjunction (tree t, tree args, subst_info info)
return satisfy_constraint_r (TREE_OPERAND (t, 1), args, info);
}
+/* The current depth at which we're replaying an error during recursive
+ diagnosis of a constraint satisfaction failure. */
+
+static int current_constraint_diagnosis_depth;
+
+/* Whether CURRENT_CONSTRAINT_DIAGNOSIS_DEPTH has ever exceeded
+ CONCEPTS_DIAGNOSTICS_MAX_DEPTH during recursive diagnosis of a constraint
+ satisfaction error. */
+
+static bool concepts_diagnostics_max_depth_exceeded_p;
+
+/* Recursive subroutine of collect_operands_of_disjunction. T is a normalized
+ subexpression of a constraint (composed of CONJ_CONSTRs and DISJ_CONSTRs)
+ and E is the corresponding unnormalized subexpression (composed of
+ TRUTH_ANDIF_EXPRs and TRUTH_ORIF_EXPRs). */
+
+static void
+collect_operands_of_disjunction_r (tree t, tree e,
+ auto_vec<tree_pair> *operands)
+{
+ if (TREE_CODE (e) == TRUTH_ORIF_EXPR)
+ {
+ collect_operands_of_disjunction_r (TREE_OPERAND (t, 0),
+ TREE_OPERAND (e, 0), operands);
+ collect_operands_of_disjunction_r (TREE_OPERAND (t, 1),
+ TREE_OPERAND (e, 1), operands);
+ }
+ else
+ {
+ tree_pair p = std::make_pair (t, e);
+ operands->safe_push (p);
+ }
+}
+
+/* Recursively collect the normalized and unnormalized operands of the
+ disjunction T and append them to OPERANDS in order. */
+
+static void
+collect_operands_of_disjunction (tree t, auto_vec<tree_pair> *operands)
+{
+ collect_operands_of_disjunction_r (t, CONSTR_EXPR (t), operands);
+}
+
/* Compute the satisfaction of a disjunction. */
static tree
@@ -2412,11 +2456,25 @@ satisfy_disjunction (tree t, tree args, subst_info info)
tree rhs = satisfy_constraint_r (TREE_OPERAND (t, 1), args, quiet);
if (rhs != boolean_true_node && info.noisy ())
{
- location_t loc = cp_expr_location (CONSTR_EXPR (t));
- inform (loc, "neither operand of the disjunction is satisfied");
- /* TODO: Replay the LHS and RHS to find failures in both branches. */
- // satisfy_constraint_r (TREE_OPERAND (t, 0), args, info);
- // satisfy_constraint_r (TREE_OPERAND (t, 1), args, info);
+ cp_expr disj_expr = CONSTR_EXPR (t);
+ inform (disj_expr.get_location (),
+ "no operand of the disjunction is satisfied");
+ if (diagnosing_failed_constraint::replay_errors_p ())
+ {
+ /* Replay the error in each branch of the disjunction. */
+ auto_vec<tree_pair> operands;
+ collect_operands_of_disjunction (t, &operands);
+ for (unsigned i = 0; i < operands.length (); i++)
+ {
+ tree norm_op = operands[i].first;
+ tree op = operands[i].second;
+ location_t loc = make_location (cp_expr_location (op),
+ disj_expr.get_start (),
+ disj_expr.get_finish ());
+ inform (loc, "the operand %qE is unsatisfied because", op);
+ satisfy_constraint_r (norm_op, args, info);
+ }
+ }
}
return rhs;
}
@@ -3182,10 +3240,14 @@ diagnose_valid_expression (tree expr, tree args, tree in_decl)
return result;
location_t loc = cp_expr_loc_or_input_loc (expr);
- inform (loc, "the required expression %qE is invalid", expr);
-
- /* TODO: Replay the substitution to diagnose the error? */
- // tsubst_expr (expr, args, tf_error, in_decl, false);
+ if (diagnosing_failed_constraint::replay_errors_p ())
+ {
+ /* Replay the substitution error. */
+ inform (loc, "the required expression %qE is invalid, because", expr);
+ tsubst_expr (expr, args, tf_error, in_decl, false);
+ }
+ else
+ inform (loc, "the required expression %qE is invalid", expr);
return error_mark_node;
}
@@ -3198,10 +3260,14 @@ diagnose_valid_type (tree type, tree args, tree in_decl)
return result;
location_t loc = cp_expr_loc_or_input_loc (type);
- inform (loc, "the required type %qT is invalid", type);
-
- /* TODO: Replay the substitution to diagnose the error? */
- // tsubst (type, args, tf_error, in_decl);
+ if (diagnosing_failed_constraint::replay_errors_p ())
+ {
+ /* Replay the substitution error. */
+ inform (loc, "the required type %qT is invalid, because", type);
+ tsubst (type, args, tf_error, in_decl);
+ }
+ else
+ inform (loc, "the required type %qT is invalid", type);
return error_mark_node;
}
@@ -3280,11 +3346,16 @@ diagnose_nested_requirement (tree req, tree args)
tree expr = TREE_OPERAND (req, 0);
location_t loc = cp_expr_location (expr);
- inform (loc, "nested requirement %qE is not satisfied", expr);
+ if (diagnosing_failed_constraint::replay_errors_p ())
+ {
+ /* Replay the substitution error. */
+ inform (loc, "nested requirement %qE is not satisfied, because", expr);
+ subst_info noisy (tf_warning_or_error, NULL_TREE);
+ satisfy_constraint_expression (expr, args, noisy);
+ }
+ else
+ inform (loc, "nested requirement %qE is not satisfied", expr);
- /* TODO: Replay the substitution to diagnose the error? */
- // subst_info noisy (tf_warning_or_error, NULL_TREE);
- // satisfy_constraint (norm, args, info);
}
static void
@@ -3385,14 +3456,38 @@ diagnosing_failed_constraint (tree t, tree args, bool diag)
: diagnosing_error (diag)
{
if (diagnosing_error)
- current_failed_constraint = tree_cons (args, t, current_failed_constraint);
+ {
+ current_failed_constraint
+ = tree_cons (args, t, current_failed_constraint);
+ ++current_constraint_diagnosis_depth;
+ }
}
diagnosing_failed_constraint::
~diagnosing_failed_constraint ()
{
- if (diagnosing_error && current_failed_constraint)
- current_failed_constraint = TREE_CHAIN (current_failed_constraint);
+ if (diagnosing_error)
+ {
+ --current_constraint_diagnosis_depth;
+ if (current_failed_constraint)
+ current_failed_constraint = TREE_CHAIN (current_failed_constraint);
+ }
+
+}
+
+/* Whether we are allowed to replay an error that underlies a constraint failure
+ at the current diagnosis depth. */
+
+bool
+diagnosing_failed_constraint::replay_errors_p ()
+{
+ if (current_constraint_diagnosis_depth >= concepts_diagnostics_max_depth)
+ {
+ concepts_diagnostics_max_depth_exceeded_p = true;
+ return false;
+ }
+ else
+ return true;
}
/* Emit diagnostics detailing the failure ARGS to satisfy the constraints
@@ -3403,11 +3498,26 @@ diagnose_constraints (location_t loc, tree t, tree args)
{
inform (loc, "constraints not satisfied");
+ if (concepts_diagnostics_max_depth == 0)
+ return;
+
/* Replay satisfaction, but diagnose errors. */
if (!args)
constraint_satisfaction_value (t, tf_warning_or_error);
else
constraint_satisfaction_value (t, args, tf_warning_or_error);
+
+ static bool suggested_p;
+ if (concepts_diagnostics_max_depth_exceeded_p
+ && current_constraint_diagnosis_depth == 0
+ && !suggested_p)
+ {
+ inform (UNKNOWN_LOCATION,
+ "set %qs to at least %d for more detail",
+ "-fconcepts-diagnostics-depth=",
+ concepts_diagnostics_max_depth + 1);
+ suggested_p = true;
+ }
}
#include "gt-cp-constraint.h"