Auto merge of #91030 - estebank:trait-bounds-are-tricky-2, r=oli-obk

Properly track `ImplObligations`

Instead of probing for all possible `impl`s that could have caused an
`ImplObligation`, keep track of its `DefId` and obligation spans for
accurate error reporting.

Follow to #89580. Addresses #89418.
This commit is contained in:
bors 2022-03-24 10:24:54 +00:00
commit d2df372bca
24 changed files with 426 additions and 218 deletions

View file

@ -13,15 +13,11 @@ use super::project::ProjectionTyObligation;
use super::util;
use super::util::{closure_trait_ref_and_return_type, predicate_for_trait_def};
use super::wf;
use super::DerivedObligationCause;
use super::Normalized;
use super::Obligation;
use super::ObligationCauseCode;
use super::Selection;
use super::SelectionResult;
use super::TraitQueryMode;
use super::{ErrorReporting, Overflow, SelectionError};
use super::{ObligationCause, PredicateObligation, TraitObligation};
use super::{
DerivedObligationCause, ErrorReporting, ImplDerivedObligation, ImplDerivedObligationCause,
Normalized, Obligation, ObligationCause, ObligationCauseCode, Overflow, PredicateObligation,
Selection, SelectionError, SelectionResult, TraitObligation, TraitQueryMode,
};
use crate::infer::{InferCtxt, InferOk, TypeFreshener};
use crate::traits::error_reporting::InferCtxtExt;
@ -2333,11 +2329,12 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
#[tracing::instrument(level = "debug", skip(self, cause, param_env))]
fn impl_or_trait_obligations(
&mut self,
cause: ObligationCause<'tcx>,
cause: &ObligationCause<'tcx>,
recursion_depth: usize,
param_env: ty::ParamEnv<'tcx>,
def_id: DefId, // of impl or trait
substs: SubstsRef<'tcx>, // for impl or trait
parent_trait_pred: ty::Binder<'tcx, ty::TraitPredicate<'tcx>>,
) -> Vec<PredicateObligation<'tcx>> {
let tcx = self.tcx();
@ -2359,8 +2356,17 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
debug!(?predicates);
assert_eq!(predicates.parent, None);
let mut obligations = Vec::with_capacity(predicates.predicates.len());
for (predicate, _) in predicates.predicates {
debug!(?predicate);
let parent_code = cause.clone_code();
for (predicate, span) in predicates.predicates {
let span = *span;
let derived =
DerivedObligationCause { parent_trait_pred, parent_code: parent_code.clone() };
let code = ImplDerivedObligation(Box::new(ImplDerivedObligationCause {
derived,
impl_def_id: def_id,
span,
}));
let cause = ObligationCause::new(cause.span, cause.body_id, code);
let predicate = normalize_with_depth_to(
self,
param_env,
@ -2369,12 +2375,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
predicate.subst(tcx, substs),
&mut obligations,
);
obligations.push(Obligation {
cause: cause.clone(),
recursion_depth,
param_env,
predicate,
});
obligations.push(Obligation { cause, recursion_depth, param_env, predicate });
}
obligations