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_errors::ErrorReported;
6 use rustc_infer::traits::{SelectionError, TraitEngine, TraitEngineExt as _, TraitObligation};
7 use rustc_middle::mir::abstract_const::NotConstEvaluatable;
8 use rustc_middle::mir::interpret::ErrorHandled;
9 use rustc_middle::ty::error::{ExpectedFound, TypeError};
10 use rustc_middle::ty::subst::SubstsRef;
11 use rustc_middle::ty::ToPredicate;
12 use rustc_middle::ty::{self, Binder, Const, Ty, TypeFoldable};
13 use std::marker::PhantomData;
15 use super::const_evaluatable;
17 use super::select::SelectionContext;
19 use super::CodeAmbiguity;
20 use super::CodeProjectionError;
21 use super::CodeSelectionError;
22 use super::Unimplemented;
23 use super::{FulfillmentError, FulfillmentErrorCode};
24 use super::{ObligationCause, PredicateObligation};
26 use crate::traits::error_reporting::InferCtxtExt as _;
27 use crate::traits::project::PolyProjectionObligation;
28 use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
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>>,
55 // Should this fulfillment context register type-lives-for-region
56 // obligations on its parent infcx? In some cases, region
57 // obligations are either already known to hold (normalization) or
58 // hopefully verifed elsewhere (type-impls-bound), and therefore
59 // should not be checked.
61 // Note that if we are normalizing a type that we already
62 // know is well-formed, there should be no harm setting this
63 // to true - all the region variables should be determinable
64 // using the RFC 447 rules, which don't depend on
65 // type-lives-for-region constraints, and because the type
66 // is well-formed, the constraints should hold.
67 register_region_obligations: bool,
68 // Is it OK to register obligations into this infcx inside
71 // The "primary fulfillment" in many cases in typeck lives
72 // outside of any snapshot, so any use of it inside a snapshot
73 // will lead to trouble and therefore is checked against, but
74 // other fulfillment contexts sometimes do live inside of
75 // a snapshot (they don't *straddle* a snapshot, so there
76 // is no trouble there).
77 usable_in_snapshot: bool,
80 #[derive(Clone, Debug)]
81 pub struct PendingPredicateObligation<'tcx> {
82 pub obligation: PredicateObligation<'tcx>,
83 // This is far more often read than modified, meaning that we
84 // should mostly optimize for reading speed, while modifying is not as relevant.
86 // For whatever reason using a boxed slice is slower than using a `Vec` here.
87 pub stalled_on: Vec<TyOrConstInferVar<'tcx>>,
90 // `PendingPredicateObligation` is used a lot. Make sure it doesn't unintentionally get bigger.
91 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
92 static_assert_size!(PendingPredicateObligation<'_>, 56);
94 impl<'a, 'tcx> FulfillmentContext<'tcx> {
95 /// Creates a new fulfillment context.
96 pub fn new() -> FulfillmentContext<'tcx> {
98 predicates: ObligationForest::new(),
99 register_region_obligations: true,
100 usable_in_snapshot: false,
104 pub fn new_in_snapshot() -> FulfillmentContext<'tcx> {
106 predicates: ObligationForest::new(),
107 register_region_obligations: true,
108 usable_in_snapshot: true,
112 pub fn new_ignoring_regions() -> FulfillmentContext<'tcx> {
114 predicates: ObligationForest::new(),
115 register_region_obligations: false,
116 usable_in_snapshot: false,
120 /// Attempts to select obligations using `selcx`.
123 selcx: &mut SelectionContext<'a, 'tcx>,
124 ) -> Result<(), Vec<FulfillmentError<'tcx>>> {
125 let span = debug_span!("select", obligation_forest_size = ?self.predicates.len());
126 let _enter = span.enter();
128 let mut errors = Vec::new();
131 debug!("select: starting another iteration");
133 // Process pending obligations.
134 let outcome: Outcome<_, _> =
135 self.predicates.process_obligations(&mut FulfillProcessor {
137 register_region_obligations: self.register_region_obligations,
139 debug!("select: outcome={:#?}", outcome);
141 // FIXME: if we kept the original cache key, we could mark projection
142 // obligations as complete for the projection cache here.
144 errors.extend(outcome.errors.into_iter().map(to_fulfillment_error));
146 // If nothing new was added, no need to keep looping.
153 "select({} predicates remaining, {} errors) done",
154 self.predicates.len(),
158 if errors.is_empty() { Ok(()) } else { Err(errors) }
162 impl<'tcx> TraitEngine<'tcx> for FulfillmentContext<'tcx> {
163 /// "Normalize" a projection type `<SomeType as SomeTrait>::X` by
164 /// creating a fresh type variable `$0` as well as a projection
165 /// predicate `<SomeType as SomeTrait>::X == $0`. When the
166 /// inference engine runs, it will attempt to find an impl of
167 /// `SomeTrait` or a where-clause that lets us unify `$0` with
168 /// something concrete. If this fails, we'll unify `$0` with
169 /// `projection_ty` again.
170 #[tracing::instrument(level = "debug", skip(self, infcx, param_env, cause))]
171 fn normalize_projection_type(
173 infcx: &InferCtxt<'_, 'tcx>,
174 param_env: ty::ParamEnv<'tcx>,
175 projection_ty: ty::ProjectionTy<'tcx>,
176 cause: ObligationCause<'tcx>,
178 debug_assert!(!projection_ty.has_escaping_bound_vars());
180 // FIXME(#20304) -- cache
182 let mut selcx = SelectionContext::new(infcx);
183 let mut obligations = vec![];
184 let normalized_ty = project::normalize_projection_type(
192 self.register_predicate_obligations(infcx, obligations);
194 debug!(?normalized_ty);
199 fn register_predicate_obligation(
201 infcx: &InferCtxt<'_, 'tcx>,
202 obligation: PredicateObligation<'tcx>,
204 // this helps to reduce duplicate errors, as well as making
205 // debug output much nicer to read and so on.
206 let obligation = infcx.resolve_vars_if_possible(obligation);
208 debug!(?obligation, "register_predicate_obligation");
210 assert!(!infcx.is_in_snapshot() || self.usable_in_snapshot);
213 .register_obligation(PendingPredicateObligation { obligation, stalled_on: vec![] });
216 fn select_all_or_error(
218 infcx: &InferCtxt<'_, 'tcx>,
219 ) -> Result<(), Vec<FulfillmentError<'tcx>>> {
220 self.select_where_possible(infcx)?;
222 let errors: Vec<_> = self
224 .to_errors(CodeAmbiguity)
226 .map(to_fulfillment_error)
228 if errors.is_empty() { Ok(()) } else { Err(errors) }
231 fn select_where_possible(
233 infcx: &InferCtxt<'_, 'tcx>,
234 ) -> Result<(), Vec<FulfillmentError<'tcx>>> {
235 let mut selcx = SelectionContext::new(infcx);
236 self.select(&mut selcx)
239 fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>> {
240 self.predicates.map_pending_obligations(|o| o.obligation.clone())
244 struct FulfillProcessor<'a, 'b, 'tcx> {
245 selcx: &'a mut SelectionContext<'b, 'tcx>,
246 register_region_obligations: bool,
249 fn mk_pending(os: Vec<PredicateObligation<'tcx>>) -> Vec<PendingPredicateObligation<'tcx>> {
251 .map(|o| PendingPredicateObligation { obligation: o, stalled_on: vec![] })
255 impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
256 type Obligation = PendingPredicateObligation<'tcx>;
257 type Error = FulfillmentErrorCode<'tcx>;
259 /// Processes a predicate obligation and returns either:
260 /// - `Changed(v)` if the predicate is true, presuming that `v` are also true
261 /// - `Unchanged` if we don't have enough info to be sure
262 /// - `Error(e)` if the predicate does not hold
264 /// This is always inlined, despite its size, because it has a single
265 /// callsite and it is called *very* frequently.
267 fn process_obligation(
269 pending_obligation: &mut Self::Obligation,
270 ) -> ProcessResult<Self::Obligation, Self::Error> {
271 // If we were stalled on some unresolved variables, first check whether
272 // any of them have been resolved; if not, don't bother doing more work
274 let change = match pending_obligation.stalled_on.len() {
275 // Match arms are in order of frequency, which matters because this
276 // code is so hot. 1 and 0 dominate; 2+ is fairly rare.
278 let infer_var = pending_obligation.stalled_on[0];
279 self.selcx.infcx().ty_or_const_infer_var_changed(infer_var)
282 // In this case we haven't changed, but wish to make a change.
286 // This `for` loop was once a call to `all()`, but this lower-level
287 // form was a perf win. See #64545 for details.
289 for &infer_var in &pending_obligation.stalled_on {
290 if self.selcx.infcx().ty_or_const_infer_var_changed(infer_var) {
301 "process_predicate: pending obligation {:?} still stalled on {:?}",
302 self.selcx.infcx().resolve_vars_if_possible(pending_obligation.obligation.clone()),
303 pending_obligation.stalled_on
305 return ProcessResult::Unchanged;
308 self.progress_changed_obligations(pending_obligation)
311 fn process_backedge<'c, I>(
314 _marker: PhantomData<&'c PendingPredicateObligation<'tcx>>,
316 I: Clone + Iterator<Item = &'c PendingPredicateObligation<'tcx>>,
318 if self.selcx.coinductive_match(cycle.clone().map(|s| s.obligation.predicate)) {
319 debug!("process_child_obligations: coinductive match");
321 let cycle: Vec<_> = cycle.map(|c| c.obligation.clone()).collect();
322 self.selcx.infcx().report_overflow_error_cycle(&cycle);
327 impl<'a, 'b, 'tcx> FulfillProcessor<'a, 'b, 'tcx> {
328 // The code calling this method is extremely hot and only rarely
329 // actually uses this, so move this part of the code
332 fn progress_changed_obligations(
334 pending_obligation: &mut PendingPredicateObligation<'tcx>,
335 ) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
336 pending_obligation.stalled_on.truncate(0);
338 let obligation = &mut pending_obligation.obligation;
340 if obligation.predicate.has_infer_types_or_consts() {
341 obligation.predicate =
342 self.selcx.infcx().resolve_vars_if_possible(obligation.predicate);
345 debug!(?obligation, ?obligation.cause, "process_obligation");
347 let infcx = self.selcx.infcx();
349 let binder = obligation.predicate.kind();
350 match binder.no_bound_vars() {
351 None => match binder.skip_binder() {
352 // Evaluation will discard candidates using the leak check.
353 // This means we need to pass it the bound version of our
355 ty::PredicateKind::Trait(trait_ref, _constness) => {
356 let trait_obligation = obligation.with(binder.rebind(trait_ref));
358 self.process_trait_obligation(
361 &mut pending_obligation.stalled_on,
364 ty::PredicateKind::Projection(data) => {
365 let project_obligation = obligation.with(binder.rebind(data));
367 self.process_projection_obligation(
369 &mut pending_obligation.stalled_on,
372 ty::PredicateKind::RegionOutlives(_)
373 | ty::PredicateKind::TypeOutlives(_)
374 | ty::PredicateKind::WellFormed(_)
375 | ty::PredicateKind::ObjectSafe(_)
376 | ty::PredicateKind::ClosureKind(..)
377 | ty::PredicateKind::Subtype(_)
378 | ty::PredicateKind::ConstEvaluatable(..)
379 | ty::PredicateKind::ConstEquate(..) => {
380 let pred = infcx.replace_bound_vars_with_placeholders(binder);
381 ProcessResult::Changed(mk_pending(vec![
382 obligation.with(pred.to_predicate(self.selcx.tcx())),
385 ty::PredicateKind::TypeWellFormedFromEnv(..) => {
386 bug!("TypeWellFormedFromEnv is only used for Chalk")
389 Some(pred) => match pred {
390 ty::PredicateKind::Trait(data, _) => {
391 let trait_obligation = obligation.with(Binder::dummy(data));
393 self.process_trait_obligation(
396 &mut pending_obligation.stalled_on,
400 ty::PredicateKind::RegionOutlives(data) => {
401 match infcx.region_outlives_predicate(&obligation.cause, Binder::dummy(data)) {
402 Ok(()) => ProcessResult::Changed(vec![]),
403 Err(_) => ProcessResult::Error(CodeSelectionError(Unimplemented)),
407 ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(t_a, r_b)) => {
408 if self.register_region_obligations {
409 self.selcx.infcx().register_region_obligation_with_cause(
415 ProcessResult::Changed(vec![])
418 ty::PredicateKind::Projection(ref data) => {
419 let project_obligation = obligation.with(Binder::dummy(*data));
421 self.process_projection_obligation(
423 &mut pending_obligation.stalled_on,
427 ty::PredicateKind::ObjectSafe(trait_def_id) => {
428 if !self.selcx.tcx().is_object_safe(trait_def_id) {
429 ProcessResult::Error(CodeSelectionError(Unimplemented))
431 ProcessResult::Changed(vec![])
435 ty::PredicateKind::ClosureKind(_, closure_substs, kind) => {
436 match self.selcx.infcx().closure_kind(closure_substs) {
437 Some(closure_kind) => {
438 if closure_kind.extends(kind) {
439 ProcessResult::Changed(vec![])
441 ProcessResult::Error(CodeSelectionError(Unimplemented))
444 None => ProcessResult::Unchanged,
448 ty::PredicateKind::WellFormed(arg) => {
449 match wf::obligations(
451 obligation.param_env,
452 obligation.cause.body_id,
453 obligation.recursion_depth + 1,
455 obligation.cause.span,
458 pending_obligation.stalled_on =
459 vec![TyOrConstInferVar::maybe_from_generic_arg(arg).unwrap()];
460 ProcessResult::Unchanged
462 Some(os) => ProcessResult::Changed(mk_pending(os)),
466 ty::PredicateKind::Subtype(subtype) => {
467 match self.selcx.infcx().subtype_predicate(
469 obligation.param_env,
470 Binder::dummy(subtype),
473 // None means that both are unresolved.
474 pending_obligation.stalled_on = vec![
475 TyOrConstInferVar::maybe_from_ty(subtype.a).unwrap(),
476 TyOrConstInferVar::maybe_from_ty(subtype.b).unwrap(),
478 ProcessResult::Unchanged
480 Some(Ok(ok)) => ProcessResult::Changed(mk_pending(ok.obligations)),
483 ExpectedFound::new(subtype.a_is_expected, subtype.a, subtype.b);
484 ProcessResult::Error(FulfillmentErrorCode::CodeSubtypeError(
492 ty::PredicateKind::ConstEvaluatable(def_id, substs) => {
493 match const_evaluatable::is_const_evaluatable(
497 obligation.param_env,
498 obligation.cause.span,
500 Ok(()) => ProcessResult::Changed(vec![]),
501 Err(NotConstEvaluatable::MentionsInfer) => {
502 pending_obligation.stalled_on.clear();
503 pending_obligation.stalled_on.extend(
504 substs.iter().filter_map(TyOrConstInferVar::maybe_from_generic_arg),
506 ProcessResult::Unchanged
509 e @ NotConstEvaluatable::MentionsParam
510 | e @ NotConstEvaluatable::Error(_),
511 ) => ProcessResult::Error(CodeSelectionError(
512 SelectionError::NotConstEvaluatable(e),
517 ty::PredicateKind::ConstEquate(c1, c2) => {
518 debug!(?c1, ?c2, "equating consts");
519 if self.selcx.tcx().features().const_evaluatable_checked {
520 // FIXME: we probably should only try to unify abstract constants
521 // if the constants depend on generic parameters.
523 // Let's just see where this breaks :shrug:
524 if let (ty::ConstKind::Unevaluated(a), ty::ConstKind::Unevaluated(b)) =
530 .try_unify_abstract_consts(((a.def, a.substs), (b.def, b.substs)))
532 return ProcessResult::Changed(vec![]);
537 let stalled_on = &mut pending_obligation.stalled_on;
539 let mut evaluate = |c: &'tcx Const<'tcx>| {
540 if let ty::ConstKind::Unevaluated(unevaluated) = c.val {
541 match self.selcx.infcx().const_eval_resolve(
542 obligation.param_env,
544 Some(obligation.cause.span),
546 Ok(val) => Ok(Const::from_value(self.selcx.tcx(), val, c.ty)),
547 Err(ErrorHandled::TooGeneric) => {
552 .filter_map(TyOrConstInferVar::maybe_from_generic_arg),
554 Err(ErrorHandled::TooGeneric)
556 Err(err) => Err(err),
563 match (evaluate(c1), evaluate(c2)) {
564 (Ok(c1), Ok(c2)) => {
568 .at(&obligation.cause, obligation.param_env)
571 Ok(_) => ProcessResult::Changed(vec![]),
572 Err(err) => ProcessResult::Error(
573 FulfillmentErrorCode::CodeConstEquateError(
574 ExpectedFound::new(true, c1, c2),
580 (Err(ErrorHandled::Reported(ErrorReported)), _)
581 | (_, Err(ErrorHandled::Reported(ErrorReported))) => ProcessResult::Error(
582 CodeSelectionError(SelectionError::NotConstEvaluatable(
583 NotConstEvaluatable::Error(ErrorReported),
586 (Err(ErrorHandled::Linted), _) | (_, Err(ErrorHandled::Linted)) => {
588 obligation.cause.span(self.selcx.tcx()),
589 "ConstEquate: const_eval_resolve returned an unexpected error"
592 (Err(ErrorHandled::TooGeneric), _) | (_, Err(ErrorHandled::TooGeneric)) => {
593 if c1.has_infer_types_or_consts() || c2.has_infer_types_or_consts() {
594 ProcessResult::Unchanged
596 // Two different constants using generic parameters ~> error.
597 let expected_found = ExpectedFound::new(true, c1, c2);
598 ProcessResult::Error(FulfillmentErrorCode::CodeConstEquateError(
600 TypeError::ConstMismatch(expected_found),
606 ty::PredicateKind::TypeWellFormedFromEnv(..) => {
607 bug!("TypeWellFormedFromEnv is only used for Chalk")
613 #[instrument(level = "debug", skip(self, obligation, stalled_on))]
614 fn process_trait_obligation(
616 obligation: &PredicateObligation<'tcx>,
617 trait_obligation: TraitObligation<'tcx>,
618 stalled_on: &mut Vec<TyOrConstInferVar<'tcx>>,
619 ) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
620 let infcx = self.selcx.infcx();
621 if obligation.predicate.is_global() {
622 // no type variables present, can use evaluation for better caching.
623 // FIXME: consider caching errors too.
624 if infcx.predicate_must_hold_considering_regions(obligation) {
626 "selecting trait at depth {} evaluated to holds",
627 obligation.recursion_depth
629 return ProcessResult::Changed(vec![]);
633 match self.selcx.select(&trait_obligation) {
634 Ok(Some(impl_source)) => {
635 debug!("selecting trait at depth {} yielded Ok(Some)", obligation.recursion_depth);
636 ProcessResult::Changed(mk_pending(impl_source.nested_obligations()))
639 debug!("selecting trait at depth {} yielded Ok(None)", obligation.recursion_depth);
641 // This is a bit subtle: for the most part, the
642 // only reason we can fail to make progress on
643 // trait selection is because we don't have enough
644 // information about the types in the trait.
646 stalled_on.extend(substs_infer_vars(
648 trait_obligation.predicate.map_bound(|pred| pred.trait_ref.substs),
652 "process_predicate: pending obligation {:?} now stalled on {:?}",
653 infcx.resolve_vars_if_possible(obligation.clone()),
657 ProcessResult::Unchanged
659 Err(selection_err) => {
660 debug!("selecting trait at depth {} yielded Err", obligation.recursion_depth);
662 ProcessResult::Error(CodeSelectionError(selection_err))
667 fn process_projection_obligation(
669 project_obligation: PolyProjectionObligation<'tcx>,
670 stalled_on: &mut Vec<TyOrConstInferVar<'tcx>>,
671 ) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
672 let tcx = self.selcx.tcx();
673 match project::poly_project_and_unify_type(self.selcx, &project_obligation) {
674 Ok(Ok(Some(os))) => ProcessResult::Changed(mk_pending(os)),
677 stalled_on.extend(substs_infer_vars(
679 project_obligation.predicate.map_bound(|pred| pred.projection_ty.substs),
681 ProcessResult::Unchanged
683 // Let the caller handle the recursion
684 Ok(Err(project::InProgress)) => ProcessResult::Changed(mk_pending(vec![
685 project_obligation.with(project_obligation.predicate.to_predicate(tcx)),
687 Err(e) => ProcessResult::Error(CodeProjectionError(e)),
692 /// Returns the set of inference variables contained in `substs`.
693 fn substs_infer_vars<'a, 'tcx>(
694 selcx: &mut SelectionContext<'a, 'tcx>,
695 substs: ty::Binder<'tcx, SubstsRef<'tcx>>,
696 ) -> impl Iterator<Item = TyOrConstInferVar<'tcx>> {
699 .resolve_vars_if_possible(substs)
700 .skip_binder() // ok because this check doesn't care about regions
702 .filter(|arg| arg.has_infer_types_or_consts())
704 let mut walker = arg.walk();
705 while let Some(c) = walker.next() {
706 if !c.has_infer_types_or_consts() {
707 walker.visited.remove(&c);
708 walker.skip_current_subtree();
711 walker.visited.into_iter()
713 .filter_map(TyOrConstInferVar::maybe_from_generic_arg)
716 fn to_fulfillment_error<'tcx>(
717 error: Error<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>>,
718 ) -> FulfillmentError<'tcx> {
719 let mut iter = error.backtrace.into_iter();
720 let obligation = iter.next().unwrap().obligation;
721 // The root obligation is the last item in the backtrace - if there's only
722 // one item, then it's the same as the main obligation
723 let root_obligation = iter.next_back().map_or_else(|| obligation.clone(), |e| e.obligation);
724 FulfillmentError::new(obligation, error.error, root_obligation)