1 use crate::infer::{InferCtxt, TyOrConstInferVar};
2 use rustc_data_structures::fx::FxHashMap;
3 use rustc_data_structures::obligation_forest::ProcessResult;
4 use rustc_data_structures::obligation_forest::{Error, ForestObligation, Outcome};
5 use rustc_data_structures::obligation_forest::{ObligationForest, ObligationProcessor};
6 use rustc_infer::traits::ProjectionCacheKey;
7 use rustc_infer::traits::{SelectionError, TraitEngine, TraitObligation};
8 use rustc_middle::mir::interpret::ErrorHandled;
9 use rustc_middle::ty::abstract_const::NotConstEvaluatable;
10 use rustc_middle::ty::error::{ExpectedFound, TypeError};
11 use rustc_middle::ty::subst::SubstsRef;
12 use rustc_middle::ty::{self, Binder, Const, TypeVisitable};
13 use std::marker::PhantomData;
15 use super::const_evaluatable;
16 use super::project::{self, ProjectAndUnifyResult};
17 use super::select::SelectionContext;
19 use super::CodeAmbiguity;
20 use super::CodeProjectionError;
21 use super::CodeSelectionError;
22 use super::EvaluationResult;
23 use super::PredicateObligation;
24 use super::Unimplemented;
25 use super::{FulfillmentError, FulfillmentErrorCode};
27 use crate::traits::project::PolyProjectionObligation;
28 use crate::traits::project::ProjectionCacheKeyExt as _;
29 use crate::traits::query::evaluate_obligation::InferCtxtExt;
31 impl<'tcx> ForestObligation for PendingPredicateObligation<'tcx> {
32 /// Note that we include both the `ParamEnv` and the `Predicate`,
33 /// as the `ParamEnv` can influence whether fulfillment succeeds
35 type CacheKey = ty::ParamEnvAnd<'tcx, ty::Predicate<'tcx>>;
37 fn as_cache_key(&self) -> Self::CacheKey {
38 self.obligation.param_env.and(self.obligation.predicate)
42 /// The fulfillment context is used to drive trait resolution. It
43 /// consists of a list of obligations that must be (eventually)
44 /// satisfied. The job is to track which are satisfied, which yielded
45 /// errors, and which are still pending. At any point, users can call
46 /// `select_where_possible`, and the fulfillment context will try to do
47 /// selection, retaining only those obligations that remain
48 /// ambiguous. This may be helpful in pushing type inference
49 /// along. Once all type inference constraints have been generated, the
50 /// method `select_all_or_error` can be used to report any remaining
51 /// ambiguous cases as errors.
52 pub struct FulfillmentContext<'tcx> {
53 // A list of all obligations that have been registered with this
54 // fulfillment context.
55 predicates: ObligationForest<PendingPredicateObligation<'tcx>>,
57 relationships: FxHashMap<ty::TyVid, ty::FoundRelationships>,
59 // Is it OK to register obligations into this infcx inside
62 // The "primary fulfillment" in many cases in typeck lives
63 // outside of any snapshot, so any use of it inside a snapshot
64 // will lead to trouble and therefore is checked against, but
65 // other fulfillment contexts sometimes do live inside of
66 // a snapshot (they don't *straddle* a snapshot, so there
67 // is no trouble there).
68 usable_in_snapshot: bool,
71 #[derive(Clone, Debug)]
72 pub struct PendingPredicateObligation<'tcx> {
73 pub obligation: PredicateObligation<'tcx>,
74 // This is far more often read than modified, meaning that we
75 // should mostly optimize for reading speed, while modifying is not as relevant.
77 // For whatever reason using a boxed slice is slower than using a `Vec` here.
78 pub stalled_on: Vec<TyOrConstInferVar<'tcx>>,
81 // `PendingPredicateObligation` is used a lot. Make sure it doesn't unintentionally get bigger.
82 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
83 static_assert_size!(PendingPredicateObligation<'_>, 72);
85 impl<'a, 'tcx> FulfillmentContext<'tcx> {
86 /// Creates a new fulfillment context.
87 pub(super) fn new() -> FulfillmentContext<'tcx> {
89 predicates: ObligationForest::new(),
90 relationships: FxHashMap::default(),
91 usable_in_snapshot: false,
95 pub(super) fn new_in_snapshot() -> FulfillmentContext<'tcx> {
97 predicates: ObligationForest::new(),
98 relationships: FxHashMap::default(),
99 usable_in_snapshot: true,
103 /// Attempts to select obligations using `selcx`.
104 fn select(&mut self, selcx: SelectionContext<'a, 'tcx>) -> Vec<FulfillmentError<'tcx>> {
105 let span = debug_span!("select", obligation_forest_size = ?self.predicates.len());
106 let _enter = span.enter();
108 // Process pending obligations.
109 let outcome: Outcome<_, _> =
110 self.predicates.process_obligations(&mut FulfillProcessor { selcx });
112 // FIXME: if we kept the original cache key, we could mark projection
113 // obligations as complete for the projection cache here.
115 let errors: Vec<FulfillmentError<'tcx>> =
116 outcome.errors.into_iter().map(to_fulfillment_error).collect();
119 "select({} predicates remaining, {} errors) done",
120 self.predicates.len(),
128 impl<'tcx> TraitEngine<'tcx> for FulfillmentContext<'tcx> {
129 fn register_predicate_obligation(
131 infcx: &InferCtxt<'tcx>,
132 obligation: PredicateObligation<'tcx>,
134 // this helps to reduce duplicate errors, as well as making
135 // debug output much nicer to read and so on.
136 let obligation = infcx.resolve_vars_if_possible(obligation);
138 debug!(?obligation, "register_predicate_obligation");
140 assert!(!infcx.is_in_snapshot() || self.usable_in_snapshot);
142 super::relationships::update(self, infcx, &obligation);
145 .register_obligation(PendingPredicateObligation { obligation, stalled_on: vec![] });
148 fn select_all_or_error(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<FulfillmentError<'tcx>> {
150 let errors = self.select_where_possible(infcx);
151 if !errors.is_empty() {
156 self.predicates.to_errors(CodeAmbiguity).into_iter().map(to_fulfillment_error).collect()
159 fn select_where_possible(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<FulfillmentError<'tcx>> {
160 let selcx = SelectionContext::new(infcx);
164 fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>> {
165 self.predicates.map_pending_obligations(|o| o.obligation.clone())
168 fn relationships(&mut self) -> &mut FxHashMap<ty::TyVid, ty::FoundRelationships> {
169 &mut self.relationships
173 struct FulfillProcessor<'a, 'tcx> {
174 selcx: SelectionContext<'a, 'tcx>,
177 fn mk_pending(os: Vec<PredicateObligation<'_>>) -> Vec<PendingPredicateObligation<'_>> {
179 .map(|o| PendingPredicateObligation { obligation: o, stalled_on: vec![] })
183 impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
184 type Obligation = PendingPredicateObligation<'tcx>;
185 type Error = FulfillmentErrorCode<'tcx>;
186 type OUT = Outcome<Self::Obligation, Self::Error>;
188 /// Identifies whether a predicate obligation needs processing.
190 /// This is always inlined, despite its size, because it has a single
191 /// callsite and it is called *very* frequently.
193 fn needs_process_obligation(&self, pending_obligation: &Self::Obligation) -> bool {
194 // If we were stalled on some unresolved variables, first check whether
195 // any of them have been resolved; if not, don't bother doing more work
197 match pending_obligation.stalled_on.len() {
198 // Match arms are in order of frequency, which matters because this
199 // code is so hot. 1 and 0 dominate; 2+ is fairly rare.
201 let infer_var = pending_obligation.stalled_on[0];
202 self.selcx.infcx.ty_or_const_infer_var_changed(infer_var)
205 // In this case we haven't changed, but wish to make a change.
209 // This `for` loop was once a call to `all()`, but this lower-level
210 // form was a perf win. See #64545 for details.
212 for &infer_var in &pending_obligation.stalled_on {
213 if self.selcx.infcx.ty_or_const_infer_var_changed(infer_var) {
223 /// Processes a predicate obligation and returns either:
224 /// - `Changed(v)` if the predicate is true, presuming that `v` are also true
225 /// - `Unchanged` if we don't have enough info to be sure
226 /// - `Error(e)` if the predicate does not hold
228 /// This is called much less often than `needs_process_obligation`, so we
231 #[instrument(level = "debug", skip(self, pending_obligation))]
232 fn process_obligation(
234 pending_obligation: &mut PendingPredicateObligation<'tcx>,
235 ) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
236 pending_obligation.stalled_on.truncate(0);
238 let obligation = &mut pending_obligation.obligation;
240 debug!(?obligation, "pre-resolve");
242 if obligation.predicate.has_non_region_infer() {
243 obligation.predicate = self.selcx.infcx.resolve_vars_if_possible(obligation.predicate);
246 let obligation = &pending_obligation.obligation;
248 let infcx = self.selcx.infcx;
250 if obligation.predicate.has_projections() {
251 let mut obligations = Vec::new();
252 let predicate = crate::traits::project::try_normalize_with_depth_to(
254 obligation.param_env,
255 obligation.cause.clone(),
256 obligation.recursion_depth + 1,
257 obligation.predicate,
260 if predicate != obligation.predicate {
261 obligations.push(obligation.with(infcx.tcx, predicate));
262 return ProcessResult::Changed(mk_pending(obligations));
265 let binder = obligation.predicate.kind();
266 match binder.no_bound_vars() {
267 None => match binder.skip_binder() {
268 // Evaluation will discard candidates using the leak check.
269 // This means we need to pass it the bound version of our
271 ty::PredicateKind::Clause(ty::Clause::Trait(trait_ref)) => {
272 let trait_obligation = obligation.with(infcx.tcx, binder.rebind(trait_ref));
274 self.process_trait_obligation(
277 &mut pending_obligation.stalled_on,
280 ty::PredicateKind::Clause(ty::Clause::Projection(data)) => {
281 let project_obligation = obligation.with(infcx.tcx, binder.rebind(data));
283 self.process_projection_obligation(
286 &mut pending_obligation.stalled_on,
289 ty::PredicateKind::Clause(ty::Clause::RegionOutlives(_))
290 | ty::PredicateKind::Clause(ty::Clause::TypeOutlives(_))
291 | ty::PredicateKind::WellFormed(_)
292 | ty::PredicateKind::ObjectSafe(_)
293 | ty::PredicateKind::ClosureKind(..)
294 | ty::PredicateKind::Subtype(_)
295 | ty::PredicateKind::Coerce(_)
296 | ty::PredicateKind::ConstEvaluatable(..)
297 | ty::PredicateKind::ConstEquate(..) => {
299 ty::Binder::dummy(infcx.replace_bound_vars_with_placeholders(binder));
300 ProcessResult::Changed(mk_pending(vec![obligation.with(infcx.tcx, pred)]))
302 ty::PredicateKind::Ambiguous => ProcessResult::Unchanged,
303 ty::PredicateKind::TypeWellFormedFromEnv(..) => {
304 bug!("TypeWellFormedFromEnv is only used for Chalk")
307 Some(pred) => match pred {
308 ty::PredicateKind::Clause(ty::Clause::Trait(data)) => {
309 let trait_obligation = obligation.with(infcx.tcx, Binder::dummy(data));
311 self.process_trait_obligation(
314 &mut pending_obligation.stalled_on,
318 ty::PredicateKind::Clause(ty::Clause::RegionOutlives(data)) => {
319 if infcx.considering_regions {
320 infcx.region_outlives_predicate(&obligation.cause, Binder::dummy(data));
323 ProcessResult::Changed(vec![])
326 ty::PredicateKind::Clause(ty::Clause::TypeOutlives(ty::OutlivesPredicate(
330 if infcx.considering_regions {
331 infcx.register_region_obligation_with_cause(t_a, r_b, &obligation.cause);
333 ProcessResult::Changed(vec![])
336 ty::PredicateKind::Clause(ty::Clause::Projection(ref data)) => {
337 let project_obligation = obligation.with(infcx.tcx, Binder::dummy(*data));
339 self.process_projection_obligation(
342 &mut pending_obligation.stalled_on,
346 ty::PredicateKind::ObjectSafe(trait_def_id) => {
347 if !self.selcx.tcx().is_object_safe(trait_def_id) {
348 ProcessResult::Error(CodeSelectionError(Unimplemented))
350 ProcessResult::Changed(vec![])
354 ty::PredicateKind::ClosureKind(_, closure_substs, kind) => {
355 match self.selcx.infcx.closure_kind(closure_substs) {
356 Some(closure_kind) => {
357 if closure_kind.extends(kind) {
358 ProcessResult::Changed(vec![])
360 ProcessResult::Error(CodeSelectionError(Unimplemented))
363 None => ProcessResult::Unchanged,
367 ty::PredicateKind::WellFormed(arg) => {
368 match wf::obligations(
370 obligation.param_env,
371 obligation.cause.body_id,
372 obligation.recursion_depth + 1,
374 obligation.cause.span,
377 pending_obligation.stalled_on =
378 vec![TyOrConstInferVar::maybe_from_generic_arg(arg).unwrap()];
379 ProcessResult::Unchanged
381 Some(os) => ProcessResult::Changed(mk_pending(os)),
385 ty::PredicateKind::Subtype(subtype) => {
386 match self.selcx.infcx.subtype_predicate(
388 obligation.param_env,
389 Binder::dummy(subtype),
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)),
400 ExpectedFound::new(subtype.a_is_expected, subtype.a, subtype.b);
401 ProcessResult::Error(FulfillmentErrorCode::CodeSubtypeError(
409 ty::PredicateKind::Coerce(coerce) => {
410 match self.selcx.infcx.coerce_predicate(
412 obligation.param_env,
413 Binder::dummy(coerce),
416 // None means that both are unresolved.
417 pending_obligation.stalled_on =
418 vec![TyOrConstInferVar::Ty(a), TyOrConstInferVar::Ty(b)];
419 ProcessResult::Unchanged
421 Ok(Ok(ok)) => ProcessResult::Changed(mk_pending(ok.obligations)),
423 let expected_found = ExpectedFound::new(false, coerce.a, coerce.b);
424 ProcessResult::Error(FulfillmentErrorCode::CodeSubtypeError(
432 ty::PredicateKind::ConstEvaluatable(uv) => {
433 match const_evaluatable::is_const_evaluatable(
436 obligation.param_env,
437 obligation.cause.span,
439 Ok(()) => ProcessResult::Changed(vec![]),
440 Err(NotConstEvaluatable::MentionsInfer) => {
441 pending_obligation.stalled_on.clear();
442 pending_obligation.stalled_on.extend(
443 uv.walk().filter_map(TyOrConstInferVar::maybe_from_generic_arg),
445 ProcessResult::Unchanged
448 e @ NotConstEvaluatable::MentionsParam
449 | e @ NotConstEvaluatable::Error(_),
450 ) => ProcessResult::Error(CodeSelectionError(
451 SelectionError::NotConstEvaluatable(e),
456 ty::PredicateKind::ConstEquate(c1, c2) => {
458 self.selcx.tcx().features().generic_const_exprs,
459 "`ConstEquate` without a feature gate: {c1:?} {c2:?}",
461 debug!(?c1, ?c2, "equating consts");
462 // FIXME: we probably should only try to unify abstract constants
463 // if the constants depend on generic parameters.
465 // Let's just see where this breaks :shrug:
466 if let (ty::ConstKind::Unevaluated(a), ty::ConstKind::Unevaluated(b)) =
467 (c1.kind(), c2.kind())
469 if infcx.try_unify_abstract_consts(a, b, obligation.param_env) {
470 return ProcessResult::Changed(vec![]);
474 let stalled_on = &mut pending_obligation.stalled_on;
476 let mut evaluate = |c: Const<'tcx>| {
477 if let ty::ConstKind::Unevaluated(unevaluated) = c.kind() {
478 match self.selcx.infcx.try_const_eval_resolve(
479 obligation.param_env,
482 Some(obligation.cause.span),
486 ErrorHandled::TooGeneric => {
488 unevaluated.substs.iter().filter_map(
489 TyOrConstInferVar::maybe_from_generic_arg,
492 Err(ErrorHandled::TooGeneric)
502 match (evaluate(c1), evaluate(c2)) {
503 (Ok(c1), Ok(c2)) => {
507 .at(&obligation.cause, obligation.param_env)
510 Ok(_) => ProcessResult::Changed(vec![]),
511 Err(err) => ProcessResult::Error(
512 FulfillmentErrorCode::CodeConstEquateError(
513 ExpectedFound::new(true, c1, c2),
519 (Err(ErrorHandled::Reported(reported)), _)
520 | (_, Err(ErrorHandled::Reported(reported))) => ProcessResult::Error(
521 CodeSelectionError(SelectionError::NotConstEvaluatable(
522 NotConstEvaluatable::Error(reported),
525 (Err(ErrorHandled::TooGeneric), _) | (_, Err(ErrorHandled::TooGeneric)) => {
526 if c1.has_non_region_infer() || c2.has_non_region_infer() {
527 ProcessResult::Unchanged
529 // Two different constants using generic parameters ~> error.
530 let expected_found = ExpectedFound::new(true, c1, c2);
531 ProcessResult::Error(FulfillmentErrorCode::CodeConstEquateError(
533 TypeError::ConstMismatch(expected_found),
539 ty::PredicateKind::Ambiguous => ProcessResult::Unchanged,
540 ty::PredicateKind::TypeWellFormedFromEnv(..) => {
541 bug!("TypeWellFormedFromEnv is only used for Chalk")
548 fn process_backedge<'c, I>(
551 _marker: PhantomData<&'c PendingPredicateObligation<'tcx>>,
552 ) -> Result<(), FulfillmentErrorCode<'tcx>>
554 I: Clone + Iterator<Item = &'c PendingPredicateObligation<'tcx>>,
556 if self.selcx.coinductive_match(cycle.clone().map(|s| s.obligation.predicate)) {
557 debug!("process_child_obligations: coinductive match");
560 let cycle: Vec<_> = cycle.map(|c| c.obligation.clone()).collect();
561 Err(FulfillmentErrorCode::CodeCycle(cycle))
566 impl<'a, 'tcx> FulfillProcessor<'a, 'tcx> {
567 #[instrument(level = "debug", skip(self, obligation, stalled_on))]
568 fn process_trait_obligation(
570 obligation: &PredicateObligation<'tcx>,
571 trait_obligation: TraitObligation<'tcx>,
572 stalled_on: &mut Vec<TyOrConstInferVar<'tcx>>,
573 ) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
574 let infcx = self.selcx.infcx;
575 if obligation.predicate.is_global() {
576 // no type variables present, can use evaluation for better caching.
577 // FIXME: consider caching errors too.
578 if infcx.predicate_must_hold_considering_regions(obligation) {
580 "selecting trait at depth {} evaluated to holds",
581 obligation.recursion_depth
583 return ProcessResult::Changed(vec![]);
587 match self.selcx.select(&trait_obligation) {
588 Ok(Some(impl_source)) => {
589 debug!("selecting trait at depth {} yielded Ok(Some)", obligation.recursion_depth);
590 ProcessResult::Changed(mk_pending(impl_source.nested_obligations()))
593 debug!("selecting trait at depth {} yielded Ok(None)", obligation.recursion_depth);
595 // This is a bit subtle: for the most part, the
596 // only reason we can fail to make progress on
597 // trait selection is because we don't have enough
598 // information about the types in the trait.
600 stalled_on.extend(substs_infer_vars(
602 trait_obligation.predicate.map_bound(|pred| pred.trait_ref.substs),
606 "process_predicate: pending obligation {:?} now stalled on {:?}",
607 infcx.resolve_vars_if_possible(obligation.clone()),
611 ProcessResult::Unchanged
613 Err(selection_err) => {
614 debug!("selecting trait at depth {} yielded Err", obligation.recursion_depth);
616 ProcessResult::Error(CodeSelectionError(selection_err))
621 fn process_projection_obligation(
623 obligation: &PredicateObligation<'tcx>,
624 project_obligation: PolyProjectionObligation<'tcx>,
625 stalled_on: &mut Vec<TyOrConstInferVar<'tcx>>,
626 ) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
627 let tcx = self.selcx.tcx();
629 if obligation.predicate.is_global() {
630 // no type variables present, can use evaluation for better caching.
631 // FIXME: consider caching errors too.
632 if self.selcx.infcx.predicate_must_hold_considering_regions(obligation) {
633 if let Some(key) = ProjectionCacheKey::from_poly_projection_predicate(
635 project_obligation.predicate,
637 // If `predicate_must_hold_considering_regions` succeeds, then we've
638 // evaluated all sub-obligations. We can therefore mark the 'root'
639 // obligation as complete, and skip evaluating sub-obligations.
645 .complete(key, EvaluationResult::EvaluatedToOk);
647 return ProcessResult::Changed(vec![]);
649 debug!("Does NOT hold: {:?}", obligation);
653 match project::poly_project_and_unify_type(&mut self.selcx, &project_obligation) {
654 ProjectAndUnifyResult::Holds(os) => ProcessResult::Changed(mk_pending(os)),
655 ProjectAndUnifyResult::FailedNormalization => {
657 stalled_on.extend(substs_infer_vars(
659 project_obligation.predicate.map_bound(|pred| pred.projection_ty.substs),
661 ProcessResult::Unchanged
663 // Let the caller handle the recursion
664 ProjectAndUnifyResult::Recursive => ProcessResult::Changed(mk_pending(vec![
665 project_obligation.with(tcx, project_obligation.predicate),
667 ProjectAndUnifyResult::MismatchedProjectionTypes(e) => {
668 ProcessResult::Error(CodeProjectionError(e))
674 /// Returns the set of inference variables contained in `substs`.
675 fn substs_infer_vars<'a, 'tcx>(
676 selcx: &SelectionContext<'a, 'tcx>,
677 substs: ty::Binder<'tcx, SubstsRef<'tcx>>,
678 ) -> impl Iterator<Item = TyOrConstInferVar<'tcx>> {
681 .resolve_vars_if_possible(substs)
682 .skip_binder() // ok because this check doesn't care about regions
684 .filter(|arg| arg.has_non_region_infer())
686 let mut walker = arg.walk();
687 while let Some(c) = walker.next() {
688 if !c.has_non_region_infer() {
689 walker.visited.remove(&c);
690 walker.skip_current_subtree();
693 walker.visited.into_iter()
695 .filter_map(TyOrConstInferVar::maybe_from_generic_arg)
698 fn to_fulfillment_error<'tcx>(
699 error: Error<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>>,
700 ) -> FulfillmentError<'tcx> {
701 let mut iter = error.backtrace.into_iter();
702 let obligation = iter.next().unwrap().obligation;
703 // The root obligation is the last item in the backtrace - if there's only
704 // one item, then it's the same as the main obligation
705 let root_obligation = iter.next_back().map_or_else(|| obligation.clone(), |e| e.obligation);
706 FulfillmentError::new(obligation, error.error, root_obligation)