Refactor FulfillmentError
to track less data
Move the information about pointing at the call argument expression in an unmet obligation span from the `FulfillmentError` to a new `ObligationCauseCode`.
This commit is contained in:
parent
284a8a9ce7
commit
8a3f712518
11 changed files with 86 additions and 57 deletions
|
@ -57,7 +57,6 @@ impl TraitEngine<'tcx> for FulfillmentContext<'tcx> {
|
|||
.map(|obligation| FulfillmentError {
|
||||
obligation: obligation.clone(),
|
||||
code: FulfillmentErrorCode::CodeAmbiguity,
|
||||
points_at_arg_span: false,
|
||||
// FIXME - does Chalk have a notation of 'root obligation'?
|
||||
// This is just for diagnostics, so it's okay if this is wrong
|
||||
root_obligation: obligation.clone(),
|
||||
|
@ -112,7 +111,6 @@ impl TraitEngine<'tcx> for FulfillmentContext<'tcx> {
|
|||
code: FulfillmentErrorCode::CodeSelectionError(
|
||||
SelectionError::Unimplemented,
|
||||
),
|
||||
points_at_arg_span: false,
|
||||
// FIXME - does Chalk have a notation of 'root obligation'?
|
||||
// This is just for diagnostics, so it's okay if this is wrong
|
||||
root_obligation: obligation,
|
||||
|
@ -129,7 +127,6 @@ impl TraitEngine<'tcx> for FulfillmentContext<'tcx> {
|
|||
code: FulfillmentErrorCode::CodeSelectionError(
|
||||
SelectionError::Unimplemented,
|
||||
),
|
||||
points_at_arg_span: false,
|
||||
// FIXME - does Chalk have a notation of 'root obligation'?
|
||||
// This is just for diagnostics, so it's okay if this is wrong
|
||||
root_obligation: obligation,
|
||||
|
|
|
@ -66,7 +66,6 @@ pub trait InferCtxtExt<'tcx> {
|
|||
root_obligation: &PredicateObligation<'tcx>,
|
||||
error: &SelectionError<'tcx>,
|
||||
fallback_has_occurred: bool,
|
||||
points_at_arg: bool,
|
||||
);
|
||||
|
||||
/// Given some node representing a fn-like thing in the HIR map,
|
||||
|
@ -237,7 +236,6 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
|||
root_obligation: &PredicateObligation<'tcx>,
|
||||
error: &SelectionError<'tcx>,
|
||||
fallback_has_occurred: bool,
|
||||
points_at_arg: bool,
|
||||
) {
|
||||
let tcx = self.tcx;
|
||||
let mut span = obligation.cause.span;
|
||||
|
@ -387,7 +385,6 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
|||
&obligation,
|
||||
&mut err,
|
||||
&trait_ref,
|
||||
points_at_arg,
|
||||
have_alt_message,
|
||||
) {
|
||||
self.note_obligation_cause(&mut err, &obligation);
|
||||
|
@ -430,8 +427,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
|||
err.span_label(enclosing_scope_span, s.as_str());
|
||||
}
|
||||
|
||||
self.suggest_dereferences(&obligation, &mut err, trait_ref, points_at_arg);
|
||||
self.suggest_fn_call(&obligation, &mut err, trait_ref, points_at_arg);
|
||||
self.suggest_dereferences(&obligation, &mut err, trait_ref);
|
||||
self.suggest_fn_call(&obligation, &mut err, trait_ref);
|
||||
self.suggest_remove_reference(&obligation, &mut err, trait_ref);
|
||||
self.suggest_semicolon_removal(&obligation, &mut err, span, trait_ref);
|
||||
self.note_version_mismatch(&mut err, &trait_ref);
|
||||
|
@ -500,12 +497,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
|||
// Changing mutability doesn't make a difference to whether we have
|
||||
// an `Unsize` impl (Fixes ICE in #71036)
|
||||
if !is_unsize {
|
||||
self.suggest_change_mut(
|
||||
&obligation,
|
||||
&mut err,
|
||||
trait_ref,
|
||||
points_at_arg,
|
||||
);
|
||||
self.suggest_change_mut(&obligation, &mut err, trait_ref);
|
||||
}
|
||||
|
||||
// If this error is due to `!: Trait` not implemented but `(): Trait` is
|
||||
|
@ -1214,7 +1206,6 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
|
|||
&error.root_obligation,
|
||||
selection_error,
|
||||
fallback_has_occurred,
|
||||
error.points_at_arg_span,
|
||||
);
|
||||
}
|
||||
FulfillmentErrorCode::CodeProjectionError(ref e) => {
|
||||
|
|
|
@ -54,7 +54,6 @@ pub trait InferCtxtExt<'tcx> {
|
|||
obligation: &PredicateObligation<'tcx>,
|
||||
err: &mut DiagnosticBuilder<'tcx>,
|
||||
trait_ref: ty::PolyTraitRef<'tcx>,
|
||||
points_at_arg: bool,
|
||||
);
|
||||
|
||||
fn get_closure_name(
|
||||
|
@ -69,7 +68,6 @@ pub trait InferCtxtExt<'tcx> {
|
|||
obligation: &PredicateObligation<'tcx>,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
trait_ref: ty::Binder<'tcx, ty::TraitRef<'tcx>>,
|
||||
points_at_arg: bool,
|
||||
);
|
||||
|
||||
fn suggest_add_reference_to_arg(
|
||||
|
@ -77,7 +75,6 @@ pub trait InferCtxtExt<'tcx> {
|
|||
obligation: &PredicateObligation<'tcx>,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
trait_ref: &ty::Binder<'tcx, ty::TraitRef<'tcx>>,
|
||||
points_at_arg: bool,
|
||||
has_custom_message: bool,
|
||||
) -> bool;
|
||||
|
||||
|
@ -93,7 +90,6 @@ pub trait InferCtxtExt<'tcx> {
|
|||
obligation: &PredicateObligation<'tcx>,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
trait_ref: ty::Binder<'tcx, ty::TraitRef<'tcx>>,
|
||||
points_at_arg: bool,
|
||||
);
|
||||
|
||||
fn suggest_semicolon_removal(
|
||||
|
@ -490,16 +486,19 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
|||
obligation: &PredicateObligation<'tcx>,
|
||||
err: &mut DiagnosticBuilder<'tcx>,
|
||||
trait_ref: ty::PolyTraitRef<'tcx>,
|
||||
points_at_arg: bool,
|
||||
) {
|
||||
// It only make sense when suggesting dereferences for arguments
|
||||
if !points_at_arg {
|
||||
let code = if let ObligationCauseCode::FunctionArgumentObligation { parent_code, .. } =
|
||||
&obligation.cause.code
|
||||
{
|
||||
std::rc::Rc::clone(parent_code)
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
};
|
||||
let param_env = obligation.param_env;
|
||||
let body_id = obligation.cause.body_id;
|
||||
let span = obligation.cause.span;
|
||||
let real_trait_ref = match &obligation.cause.code {
|
||||
let real_trait_ref = match &*code {
|
||||
ObligationCauseCode::ImplDerivedObligation(cause)
|
||||
| ObligationCauseCode::DerivedObligation(cause)
|
||||
| ObligationCauseCode::BuiltinDerivedObligation(cause) => cause.parent_trait_ref,
|
||||
|
@ -584,7 +583,6 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
|||
obligation: &PredicateObligation<'tcx>,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
trait_ref: ty::Binder<'tcx, ty::TraitRef<'tcx>>,
|
||||
points_at_arg: bool,
|
||||
) {
|
||||
let self_ty = match trait_ref.self_ty().no_bound_vars() {
|
||||
None => return,
|
||||
|
@ -656,11 +654,11 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
|||
}
|
||||
_ => return,
|
||||
};
|
||||
if points_at_arg {
|
||||
if matches!(obligation.cause.code, ObligationCauseCode::FunctionArgumentObligation { .. }) {
|
||||
// When the obligation error has been ensured to have been caused by
|
||||
// an argument, the `obligation.cause.span` points at the expression
|
||||
// of the argument, so we can provide a suggestion. This is signaled
|
||||
// by `points_at_arg`. Otherwise, we give a more general note.
|
||||
// of the argument, so we can provide a suggestion. Otherwise, we give
|
||||
// a more general note.
|
||||
err.span_suggestion_verbose(
|
||||
obligation.cause.span.shrink_to_hi(),
|
||||
&msg,
|
||||
|
@ -677,7 +675,6 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
|||
obligation: &PredicateObligation<'tcx>,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
trait_ref: &ty::Binder<'tcx, ty::TraitRef<'tcx>>,
|
||||
points_at_arg: bool,
|
||||
has_custom_message: bool,
|
||||
) -> bool {
|
||||
let span = obligation.cause.span;
|
||||
|
@ -686,9 +683,14 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
|||
ExpnKind::Desugaring(DesugaringKind::ForLoop(ForLoopLoc::IntoIter))
|
||||
);
|
||||
|
||||
if !points_at_arg && !points_at_for_iter {
|
||||
return false;
|
||||
}
|
||||
let code =
|
||||
if let (ObligationCauseCode::FunctionArgumentObligation { parent_code, .. }, false) =
|
||||
(&obligation.cause.code, points_at_for_iter)
|
||||
{
|
||||
std::rc::Rc::clone(parent_code)
|
||||
} else {
|
||||
return false;
|
||||
};
|
||||
|
||||
// List of traits for which it would be nonsensical to suggest borrowing.
|
||||
// For instance, immutable references are always Copy, so suggesting to
|
||||
|
@ -787,7 +789,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
|||
return false;
|
||||
};
|
||||
|
||||
if let ObligationCauseCode::ImplDerivedObligation(obligation) = &obligation.cause.code {
|
||||
if let ObligationCauseCode::ImplDerivedObligation(obligation) = &*code {
|
||||
let expected_trait_ref = obligation.parent_trait_ref.skip_binder();
|
||||
let new_imm_trait_ref =
|
||||
ty::TraitRef::new(obligation.parent_trait_ref.def_id(), imm_substs);
|
||||
|
@ -799,7 +801,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
|||
return try_borrowing(new_mut_trait_ref, expected_trait_ref, true, &[]);
|
||||
}
|
||||
} else if let ObligationCauseCode::BindingObligation(_, _)
|
||||
| ObligationCauseCode::ItemObligation(_) = &obligation.cause.code
|
||||
| ObligationCauseCode::ItemObligation(_) = &*code
|
||||
{
|
||||
if try_borrowing(
|
||||
ty::TraitRef::new(trait_ref.def_id, imm_substs),
|
||||
|
@ -891,8 +893,12 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
|||
obligation: &PredicateObligation<'tcx>,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
trait_ref: ty::Binder<'tcx, ty::TraitRef<'tcx>>,
|
||||
points_at_arg: bool,
|
||||
) {
|
||||
let points_at_arg = matches!(
|
||||
obligation.cause.code,
|
||||
ObligationCauseCode::FunctionArgumentObligation { .. },
|
||||
);
|
||||
|
||||
let span = obligation.cause.span;
|
||||
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
|
||||
let refs_number =
|
||||
|
@ -2289,6 +2295,21 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
|||
)
|
||||
});
|
||||
}
|
||||
ObligationCauseCode::FunctionArgumentObligation {
|
||||
arg_hir_id: _,
|
||||
call_hir_id: _,
|
||||
ref parent_code,
|
||||
} => {
|
||||
ensure_sufficient_stack(|| {
|
||||
self.note_obligation_cause_code(
|
||||
err,
|
||||
predicate,
|
||||
&parent_code,
|
||||
obligated_types,
|
||||
seen_requirements,
|
||||
)
|
||||
});
|
||||
}
|
||||
ObligationCauseCode::CompareImplMethodObligation {
|
||||
item_name,
|
||||
trait_item_def_id,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue