Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 34 additions & 26 deletions compiler/rustc_trait_selection/src/traits/wf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
//! well formed is performed elsewhere (e.g. during type checking or item well formedness
//! checking).

use std::iter;

use rustc_hir as hir;
use rustc_hir::lang_items::LangItem;
use rustc_infer::traits::{ObligationCauseCode, PredicateObligations};
Expand All @@ -16,6 +14,7 @@ use rustc_middle::ty::{
use rustc_session::errors::feature_err;
use rustc_span::def_id::{DefId, LocalDefId};
use rustc_span::{Span, sym};
use smallvec::SmallVec;
use tracing::{debug, instrument, trace};

use crate::infer::InferCtxt;
Expand Down Expand Up @@ -579,32 +578,41 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
return Default::default();
}

let predicates = self.tcx().predicates_of(def_id);
let mut origins = vec![def_id; predicates.predicates.len()];
let mut head = predicates;
while let Some(parent) = head.parent {
head = self.tcx().predicates_of(parent);
origins.extend(iter::repeat(parent).take(head.predicates.len()));
let tcx = self.tcx();
let mut chain: SmallVec<[_; 8]> = SmallVec::new();
let mut count = 0;
let mut origin = def_id;
let mut head = tcx.predicates_of(def_id);
loop {
count += head.predicates.len();
chain.push((origin, head));
let Some(parent) = head.parent else { break };
origin = parent;
head = tcx.predicates_of(parent);
}

let predicates = predicates.instantiate(self.tcx(), args);
trace!("{:#?}", predicates);
debug_assert_eq!(predicates.predicates.len(), origins.len());

iter::zip(predicates, origins.into_iter().rev())
.map(|((pred, span), origin_def_id)| {
let code = ObligationCauseCode::WhereClause(origin_def_id, span);
let cause = self.cause(code);
traits::Obligation::with_depth(
self.tcx(),
cause,
self.recursion_depth,
self.param_env,
pred.skip_norm_wip(),
)
})
.filter(|pred| !pred.has_escaping_bound_vars())
.collect()
// Emit ancestors' predicates before the item's own; diagnostics rely on this order.
let mut obligations = PredicateObligations::with_capacity(count);
for &(origin_def_id, preds) in chain.iter().rev() {
obligations.extend(
preds
.instantiate_own(tcx, args)
.map(|(clause, span)| {
let code = ObligationCauseCode::WhereClause(origin_def_id, span);
let cause = self.cause(code);
traits::Obligation::with_depth(
tcx,
cause,
self.recursion_depth,
self.param_env,
clause.skip_norm_wip(),
)
})
.filter(|pred| !pred.has_escaping_bound_vars()),
);
}
trace!(?obligations);
obligations
}

fn add_wf_preds_for_dyn_ty(
Expand Down
Loading