1 use crate::infer::{InferCtxt, TyOrConstInferVar};
2 use rustc_data_structures::obligation_forest::ProcessResult;
3 use rustc_data_structures::obligation_forest::{Error, ForestObligation, Outcome};
4 use rustc_data_structures::obligation_forest::{ObligationForest, ObligationProcessor};
5 use rustc_infer::traits::ProjectionCacheKey;
6 use rustc_infer::traits::{SelectionError, TraitEngine, TraitObligation};
7 use rustc_middle::mir::interpret::ErrorHandled;
8 use rustc_middle::ty::abstract_const::NotConstEvaluatable;
9 use rustc_middle::ty::error::{ExpectedFound, TypeError};
10 use rustc_middle::ty::subst::SubstsRef;
11 use rustc_middle::ty::{self, Binder, Const, TypeVisitable};
12 use std::marker::PhantomData;
14 use super::const_evaluatable;
15 use super::project::{self, ProjectAndUnifyResult};
16 use super::select::SelectionContext;
18 use super::CodeAmbiguity;
19 use super::CodeProjectionError;
20 use super::CodeSelectionError;
21 use super::EvaluationResult;
22 use super::PredicateObligation;
23 use super::Unimplemented;
24 use super::{FulfillmentError, FulfillmentErrorCode};
26 use crate::traits::project::PolyProjectionObligation;
27 use crate::traits::project::ProjectionCacheKeyExt as _;
28 use crate::traits::query::evaluate_obligation::InferCtxtExt;
30 impl<'tcx> ForestObligation for PendingPredicateObligation<'tcx> {
31 /// Note that we include both the `ParamEnv` and the `Predicate`,
32 /// as the `ParamEnv` can influence whether fulfillment succeeds
34 type CacheKey = ty::ParamEnvAnd<'tcx, ty::Predicate<'tcx>>;
36 fn as_cache_key(&self) -> Self::CacheKey {
37 self.obligation.param_env.and(self.obligation.predicate)
41 /// The fulfillment context is used to drive trait resolution. It
42 /// consists of a list of obligations that must be (eventually)
43 /// satisfied. The job is to track which are satisfied, which yielded
44 /// errors, and which are still pending. At any point, users can call
45 /// `select_where_possible`, and the fulfillment context will try to do
46 /// selection, retaining only those obligations that remain
47 /// ambiguous. This may be helpful in pushing type inference
48 /// along. Once all type inference constraints have been generated, the
49 /// method `select_all_or_error` can be used to report any remaining
50 /// ambiguous cases as errors.
51 pub struct FulfillmentContext<'tcx> {
52 // A list of all obligations that have been registered with this
53 // fulfillment context.
54 predicates: ObligationForest<PendingPredicateObligation<'tcx>>,
56 // Is it OK to register obligations into this infcx inside
59 // The "primary fulfillment" in many cases in typeck lives
60 // outside of any snapshot, so any use of it inside a snapshot
61 // will lead to trouble and therefore is checked against, but
62 // other fulfillment contexts sometimes do live inside of
63 // a snapshot (they don't *straddle* a snapshot, so there
64 // is no trouble there).
65 usable_in_snapshot: bool,
68 #[derive(Clone, Debug)]
69 pub struct PendingPredicateObligation<'tcx> {
70 pub obligation: PredicateObligation<'tcx>,
71 // This is far more often read than modified, meaning that we
72 // should mostly optimize for reading speed, while modifying is not as relevant.
74 // For whatever reason using a boxed slice is slower than using a `Vec` here.
75 pub stalled_on: Vec<TyOrConstInferVar<'tcx>>,
78 // `PendingPredicateObligation` is used a lot. Make sure it doesn't unintentionally get bigger.
79 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
80 static_assert_size!(PendingPredicateObligation<'_>, 72);
82 impl<'a, 'tcx> FulfillmentContext<'tcx> {
83 /// Creates a new fulfillment context.
84 pub(super) fn new() -> FulfillmentContext<'tcx> {
85 FulfillmentContext { predicates: ObligationForest::new(), usable_in_snapshot: false }
88 pub(super) fn new_in_snapshot() -> FulfillmentContext<'tcx> {
89 FulfillmentContext { predicates: ObligationForest::new(), usable_in_snapshot: true }
92 /// Attempts to select obligations using `selcx`.
93 fn select(&mut self, selcx: SelectionContext<'a, 'tcx>) -> Vec<FulfillmentError<'tcx>> {
94 let span = debug_span!("select", obligation_forest_size = ?self.predicates.len());
95 let _enter = span.enter();
97 // Process pending obligations.
98 let outcome: Outcome<_, _> =
99 self.predicates.process_obligations(&mut FulfillProcessor { selcx });
101 // FIXME: if we kept the original cache key, we could mark projection
102 // obligations as complete for the projection cache here.
104 let errors: Vec<FulfillmentError<'tcx>> =
105 outcome.errors.into_iter().map(to_fulfillment_error).collect();
108 "select({} predicates remaining, {} errors) done",
109 self.predicates.len(),
117 impl<'tcx> TraitEngine<'tcx> for FulfillmentContext<'tcx> {
118 fn register_predicate_obligation(
120 infcx: &InferCtxt<'tcx>,
121 obligation: PredicateObligation<'tcx>,
123 // this helps to reduce duplicate errors, as well as making
124 // debug output much nicer to read and so on.
125 let obligation = infcx.resolve_vars_if_possible(obligation);
127 debug!(?obligation, "register_predicate_obligation");
129 assert!(!infcx.is_in_snapshot() || self.usable_in_snapshot);
132 .register_obligation(PendingPredicateObligation { obligation, stalled_on: vec![] });
135 fn collect_remaining_errors(&mut self) -> Vec<FulfillmentError<'tcx>> {
136 self.predicates.to_errors(CodeAmbiguity).into_iter().map(to_fulfillment_error).collect()
139 fn select_where_possible(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<FulfillmentError<'tcx>> {
140 let selcx = SelectionContext::new(infcx);
144 fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>> {
145 self.predicates.map_pending_obligations(|o| o.obligation.clone())
149 struct FulfillProcessor<'a, 'tcx> {
150 selcx: SelectionContext<'a, 'tcx>,
153 fn mk_pending(os: Vec<PredicateObligation<'_>>) -> Vec<PendingPredicateObligation<'_>> {
155 .map(|o| PendingPredicateObligation { obligation: o, stalled_on: vec![] })
159 impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
160 type Obligation = PendingPredicateObligation<'tcx>;
161 type Error = FulfillmentErrorCode<'tcx>;
162 type OUT = Outcome<Self::Obligation, Self::Error>;
164 /// Identifies whether a predicate obligation needs processing.
166 /// This is always inlined, despite its size, because it has a single
167 /// callsite and it is called *very* frequently.
169 fn needs_process_obligation(&self, pending_obligation: &Self::Obligation) -> bool {
170 // If we were stalled on some unresolved variables, first check whether
171 // any of them have been resolved; if not, don't bother doing more work
173 match pending_obligation.stalled_on.len() {
174 // Match arms are in order of frequency, which matters because this
175 // code is so hot. 1 and 0 dominate; 2+ is fairly rare.
177 let infer_var = pending_obligation.stalled_on[0];
178 self.selcx.infcx.ty_or_const_infer_var_changed(infer_var)
181 // In this case we haven't changed, but wish to make a change.
185 // This `for` loop was once a call to `all()`, but this lower-level
186 // form was a perf win. See #64545 for details.
188 for &infer_var in &pending_obligation.stalled_on {
189 if self.selcx.infcx.ty_or_const_infer_var_changed(infer_var) {
199 /// Processes a predicate obligation and returns either:
200 /// - `Changed(v)` if the predicate is true, presuming that `v` are also true
201 /// - `Unchanged` if we don't have enough info to be sure
202 /// - `Error(e)` if the predicate does not hold
204 /// This is called much less often than `needs_process_obligation`, so we
207 #[instrument(level = "debug", skip(self, pending_obligation))]
208 fn process_obligation(
210 pending_obligation: &mut PendingPredicateObligation<'tcx>,
211 ) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
212 pending_obligation.stalled_on.truncate(0);
214 let obligation = &mut pending_obligation.obligation;
216 debug!(?obligation, "pre-resolve");
218 if obligation.predicate.has_non_region_infer() {
219 obligation.predicate = self.selcx.infcx.resolve_vars_if_possible(obligation.predicate);
222 let obligation = &pending_obligation.obligation;
224 let infcx = self.selcx.infcx;
226 if obligation.predicate.has_projections() {
227 let mut obligations = Vec::new();
228 let predicate = crate::traits::project::try_normalize_with_depth_to(
230 obligation.param_env,
231 obligation.cause.clone(),
232 obligation.recursion_depth + 1,
233 obligation.predicate,
236 if predicate != obligation.predicate {
237 obligations.push(obligation.with(infcx.tcx, predicate));
238 return ProcessResult::Changed(mk_pending(obligations));
241 let binder = obligation.predicate.kind();
242 match binder.no_bound_vars() {
243 None => match binder.skip_binder() {
244 // Evaluation will discard candidates using the leak check.
245 // This means we need to pass it the bound version of our
247 ty::PredicateKind::Clause(ty::Clause::Trait(trait_ref)) => {
248 let trait_obligation = obligation.with(infcx.tcx, binder.rebind(trait_ref));
250 self.process_trait_obligation(
253 &mut pending_obligation.stalled_on,
256 ty::PredicateKind::Clause(ty::Clause::Projection(data)) => {
257 let project_obligation = obligation.with(infcx.tcx, binder.rebind(data));
259 self.process_projection_obligation(
262 &mut pending_obligation.stalled_on,
265 ty::PredicateKind::Clause(ty::Clause::RegionOutlives(_))
266 | ty::PredicateKind::Clause(ty::Clause::TypeOutlives(_))
267 | ty::PredicateKind::WellFormed(_)
268 | ty::PredicateKind::ObjectSafe(_)
269 | ty::PredicateKind::ClosureKind(..)
270 | ty::PredicateKind::Subtype(_)
271 | ty::PredicateKind::Coerce(_)
272 | ty::PredicateKind::ConstEvaluatable(..)
273 | ty::PredicateKind::ConstEquate(..) => {
275 ty::Binder::dummy(infcx.replace_bound_vars_with_placeholders(binder));
276 ProcessResult::Changed(mk_pending(vec![obligation.with(infcx.tcx, pred)]))
278 ty::PredicateKind::Ambiguous => ProcessResult::Unchanged,
279 ty::PredicateKind::TypeWellFormedFromEnv(..) => {
280 bug!("TypeWellFormedFromEnv is only used for Chalk")
283 Some(pred) => match pred {
284 ty::PredicateKind::Clause(ty::Clause::Trait(data)) => {
285 let trait_obligation = obligation.with(infcx.tcx, Binder::dummy(data));
287 self.process_trait_obligation(
290 &mut pending_obligation.stalled_on,
294 ty::PredicateKind::Clause(ty::Clause::RegionOutlives(data)) => {
295 if infcx.considering_regions {
296 infcx.region_outlives_predicate(&obligation.cause, Binder::dummy(data));
299 ProcessResult::Changed(vec![])
302 ty::PredicateKind::Clause(ty::Clause::TypeOutlives(ty::OutlivesPredicate(
306 if infcx.considering_regions {
307 infcx.register_region_obligation_with_cause(t_a, r_b, &obligation.cause);
309 ProcessResult::Changed(vec![])
312 ty::PredicateKind::Clause(ty::Clause::Projection(ref data)) => {
313 let project_obligation = obligation.with(infcx.tcx, Binder::dummy(*data));
315 self.process_projection_obligation(
318 &mut pending_obligation.stalled_on,
322 ty::PredicateKind::ObjectSafe(trait_def_id) => {
323 if !self.selcx.tcx().is_object_safe(trait_def_id) {
324 ProcessResult::Error(CodeSelectionError(Unimplemented))
326 ProcessResult::Changed(vec![])
330 ty::PredicateKind::ClosureKind(_, closure_substs, kind) => {
331 match self.selcx.infcx.closure_kind(closure_substs) {
332 Some(closure_kind) => {
333 if closure_kind.extends(kind) {
334 ProcessResult::Changed(vec![])
336 ProcessResult::Error(CodeSelectionError(Unimplemented))
339 None => ProcessResult::Unchanged,
343 ty::PredicateKind::WellFormed(arg) => {
344 match wf::obligations(
346 obligation.param_env,
347 obligation.cause.body_id,
348 obligation.recursion_depth + 1,
350 obligation.cause.span,
353 pending_obligation.stalled_on =
354 vec![TyOrConstInferVar::maybe_from_generic_arg(arg).unwrap()];
355 ProcessResult::Unchanged
357 Some(os) => ProcessResult::Changed(mk_pending(os)),
361 ty::PredicateKind::Subtype(subtype) => {
362 match self.selcx.infcx.subtype_predicate(
364 obligation.param_env,
365 Binder::dummy(subtype),
368 // None means that both are unresolved.
369 pending_obligation.stalled_on =
370 vec![TyOrConstInferVar::Ty(a), TyOrConstInferVar::Ty(b)];
371 ProcessResult::Unchanged
373 Ok(Ok(ok)) => ProcessResult::Changed(mk_pending(ok.obligations)),
376 ExpectedFound::new(subtype.a_is_expected, subtype.a, subtype.b);
377 ProcessResult::Error(FulfillmentErrorCode::CodeSubtypeError(
385 ty::PredicateKind::Coerce(coerce) => {
386 match self.selcx.infcx.coerce_predicate(
388 obligation.param_env,
389 Binder::dummy(coerce),
392 // None means that both are unresolved.
393 pending_obligation.stalled_on =
394 vec![TyOrConstInferVar::Ty(a), TyOrConstInferVar::Ty(b)];
395 ProcessResult::Unchanged
397 Ok(Ok(ok)) => ProcessResult::Changed(mk_pending(ok.obligations)),
399 let expected_found = ExpectedFound::new(false, coerce.a, coerce.b);
400 ProcessResult::Error(FulfillmentErrorCode::CodeSubtypeError(
408 ty::PredicateKind::ConstEvaluatable(uv) => {
409 match const_evaluatable::is_const_evaluatable(
412 obligation.param_env,
413 obligation.cause.span,
415 Ok(()) => ProcessResult::Changed(vec![]),
416 Err(NotConstEvaluatable::MentionsInfer) => {
417 pending_obligation.stalled_on.clear();
418 pending_obligation.stalled_on.extend(
419 uv.walk().filter_map(TyOrConstInferVar::maybe_from_generic_arg),
421 ProcessResult::Unchanged
424 e @ NotConstEvaluatable::MentionsParam
425 | e @ NotConstEvaluatable::Error(_),
426 ) => ProcessResult::Error(CodeSelectionError(
427 SelectionError::NotConstEvaluatable(e),
432 ty::PredicateKind::ConstEquate(c1, c2) => {
433 let tcx = self.selcx.tcx();
435 tcx.features().generic_const_exprs,
436 "`ConstEquate` without a feature gate: {c1:?} {c2:?}",
438 // FIXME: we probably should only try to unify abstract constants
439 // if the constants depend on generic parameters.
441 // Let's just see where this breaks :shrug:
443 let c1 = tcx.expand_abstract_consts(c1);
444 let c2 = tcx.expand_abstract_consts(c2);
445 debug!("equating consts:\nc1= {:?}\nc2= {:?}", c1, c2);
447 use rustc_hir::def::DefKind;
448 use ty::ConstKind::Unevaluated;
449 match (c1.kind(), c2.kind()) {
450 (Unevaluated(a), Unevaluated(b))
451 if a.def.did == b.def.did
452 && tcx.def_kind(a.def.did) == DefKind::AssocConst =>
454 if let Ok(new_obligations) = infcx
455 .at(&obligation.cause, obligation.param_env)
457 .eq(a.substs, b.substs)
459 return ProcessResult::Changed(mk_pending(
460 new_obligations.into_obligations(),
464 (_, Unevaluated(_)) | (Unevaluated(_), _) => (),
466 if let Ok(new_obligations) =
467 infcx.at(&obligation.cause, obligation.param_env).eq(c1, c2)
469 return ProcessResult::Changed(mk_pending(
470 new_obligations.into_obligations(),
477 let stalled_on = &mut pending_obligation.stalled_on;
479 let mut evaluate = |c: Const<'tcx>| {
480 if let ty::ConstKind::Unevaluated(unevaluated) = c.kind() {
481 match self.selcx.infcx.try_const_eval_resolve(
482 obligation.param_env,
485 Some(obligation.cause.span),
489 ErrorHandled::TooGeneric => {
491 unevaluated.substs.iter().filter_map(
492 TyOrConstInferVar::maybe_from_generic_arg,
495 Err(ErrorHandled::TooGeneric)
505 match (evaluate(c1), evaluate(c2)) {
506 (Ok(c1), Ok(c2)) => {
510 .at(&obligation.cause, obligation.param_env)
514 ProcessResult::Changed(mk_pending(inf_ok.into_obligations()))
516 Err(err) => ProcessResult::Error(
517 FulfillmentErrorCode::CodeConstEquateError(
518 ExpectedFound::new(true, c1, c2),
524 (Err(ErrorHandled::Reported(reported)), _)
525 | (_, Err(ErrorHandled::Reported(reported))) => ProcessResult::Error(
526 CodeSelectionError(SelectionError::NotConstEvaluatable(
527 NotConstEvaluatable::Error(reported),
530 (Err(ErrorHandled::TooGeneric), _) | (_, Err(ErrorHandled::TooGeneric)) => {
531 if c1.has_non_region_infer() || c2.has_non_region_infer() {
532 ProcessResult::Unchanged
534 // Two different constants using generic parameters ~> error.
535 let expected_found = ExpectedFound::new(true, c1, c2);
536 ProcessResult::Error(FulfillmentErrorCode::CodeConstEquateError(
538 TypeError::ConstMismatch(expected_found),
544 ty::PredicateKind::Ambiguous => ProcessResult::Unchanged,
545 ty::PredicateKind::TypeWellFormedFromEnv(..) => {
546 bug!("TypeWellFormedFromEnv is only used for Chalk")
553 fn process_backedge<'c, I>(
556 _marker: PhantomData<&'c PendingPredicateObligation<'tcx>>,
557 ) -> Result<(), FulfillmentErrorCode<'tcx>>
559 I: Clone + Iterator<Item = &'c PendingPredicateObligation<'tcx>>,
561 if self.selcx.coinductive_match(cycle.clone().map(|s| s.obligation.predicate)) {
562 debug!("process_child_obligations: coinductive match");
565 let cycle: Vec<_> = cycle.map(|c| c.obligation.clone()).collect();
566 Err(FulfillmentErrorCode::CodeCycle(cycle))
571 impl<'a, 'tcx> FulfillProcessor<'a, 'tcx> {
572 #[instrument(level = "debug", skip(self, obligation, stalled_on))]
573 fn process_trait_obligation(
575 obligation: &PredicateObligation<'tcx>,
576 trait_obligation: TraitObligation<'tcx>,
577 stalled_on: &mut Vec<TyOrConstInferVar<'tcx>>,
578 ) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
579 let infcx = self.selcx.infcx;
580 if obligation.predicate.is_global() {
581 // no type variables present, can use evaluation for better caching.
582 // FIXME: consider caching errors too.
583 if infcx.predicate_must_hold_considering_regions(obligation) {
585 "selecting trait at depth {} evaluated to holds",
586 obligation.recursion_depth
588 return ProcessResult::Changed(vec![]);
592 match self.selcx.select(&trait_obligation) {
593 Ok(Some(impl_source)) => {
594 debug!("selecting trait at depth {} yielded Ok(Some)", obligation.recursion_depth);
595 ProcessResult::Changed(mk_pending(impl_source.nested_obligations()))
598 debug!("selecting trait at depth {} yielded Ok(None)", obligation.recursion_depth);
600 // This is a bit subtle: for the most part, the
601 // only reason we can fail to make progress on
602 // trait selection is because we don't have enough
603 // information about the types in the trait.
605 stalled_on.extend(substs_infer_vars(
607 trait_obligation.predicate.map_bound(|pred| pred.trait_ref.substs),
611 "process_predicate: pending obligation {:?} now stalled on {:?}",
612 infcx.resolve_vars_if_possible(obligation.clone()),
616 ProcessResult::Unchanged
618 Err(selection_err) => {
619 debug!("selecting trait at depth {} yielded Err", obligation.recursion_depth);
621 ProcessResult::Error(CodeSelectionError(selection_err))
626 fn process_projection_obligation(
628 obligation: &PredicateObligation<'tcx>,
629 project_obligation: PolyProjectionObligation<'tcx>,
630 stalled_on: &mut Vec<TyOrConstInferVar<'tcx>>,
631 ) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
632 let tcx = self.selcx.tcx();
634 if obligation.predicate.is_global() {
635 // no type variables present, can use evaluation for better caching.
636 // FIXME: consider caching errors too.
637 if self.selcx.infcx.predicate_must_hold_considering_regions(obligation) {
638 if let Some(key) = ProjectionCacheKey::from_poly_projection_predicate(
640 project_obligation.predicate,
642 // If `predicate_must_hold_considering_regions` succeeds, then we've
643 // evaluated all sub-obligations. We can therefore mark the 'root'
644 // obligation as complete, and skip evaluating sub-obligations.
650 .complete(key, EvaluationResult::EvaluatedToOk);
652 return ProcessResult::Changed(vec![]);
654 debug!("Does NOT hold: {:?}", obligation);
658 match project::poly_project_and_unify_type(&mut self.selcx, &project_obligation) {
659 ProjectAndUnifyResult::Holds(os) => ProcessResult::Changed(mk_pending(os)),
660 ProjectAndUnifyResult::FailedNormalization => {
662 stalled_on.extend(substs_infer_vars(
664 project_obligation.predicate.map_bound(|pred| pred.projection_ty.substs),
666 ProcessResult::Unchanged
668 // Let the caller handle the recursion
669 ProjectAndUnifyResult::Recursive => ProcessResult::Changed(mk_pending(vec![
670 project_obligation.with(tcx, project_obligation.predicate),
672 ProjectAndUnifyResult::MismatchedProjectionTypes(e) => {
673 ProcessResult::Error(CodeProjectionError(e))
679 /// Returns the set of inference variables contained in `substs`.
680 fn substs_infer_vars<'a, 'tcx>(
681 selcx: &SelectionContext<'a, 'tcx>,
682 substs: ty::Binder<'tcx, SubstsRef<'tcx>>,
683 ) -> impl Iterator<Item = TyOrConstInferVar<'tcx>> {
686 .resolve_vars_if_possible(substs)
687 .skip_binder() // ok because this check doesn't care about regions
689 .filter(|arg| arg.has_non_region_infer())
691 let mut walker = arg.walk();
692 while let Some(c) = walker.next() {
693 if !c.has_non_region_infer() {
694 walker.visited.remove(&c);
695 walker.skip_current_subtree();
698 walker.visited.into_iter()
700 .filter_map(TyOrConstInferVar::maybe_from_generic_arg)
703 fn to_fulfillment_error<'tcx>(
704 error: Error<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>>,
705 ) -> FulfillmentError<'tcx> {
706 let mut iter = error.backtrace.into_iter();
707 let obligation = iter.next().unwrap().obligation;
708 // The root obligation is the last item in the backtrace - if there's only
709 // one item, then it's the same as the main obligation
710 let root_obligation = iter.next_back().map_or_else(|| obligation.clone(), |e| e.obligation);
711 FulfillmentError::new(obligation, error.error, root_obligation)