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::{TraitEngine, TraitEngineExt as _, TraitObligation};
7 use rustc_middle::mir::interpret::ErrorHandled;
8 use rustc_middle::ty::error::ExpectedFound;
9 use rustc_middle::ty::ToPredicate;
10 use rustc_middle::ty::{self, Binder, Const, Ty, TypeFoldable};
11 use std::marker::PhantomData;
13 use super::const_evaluatable;
15 use super::select::SelectionContext;
17 use super::CodeAmbiguity;
18 use super::CodeProjectionError;
19 use super::CodeSelectionError;
20 use super::{ConstEvalFailure, Unimplemented};
21 use super::{FulfillmentError, FulfillmentErrorCode};
22 use super::{ObligationCause, PredicateObligation};
24 use crate::traits::error_reporting::InferCtxtExt as _;
25 use crate::traits::project::PolyProjectionObligation;
26 use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
28 impl<'tcx> ForestObligation for PendingPredicateObligation<'tcx> {
29 /// Note that we include both the `ParamEnv` and the `Predicate`,
30 /// as the `ParamEnv` can influence whether fulfillment succeeds
32 type CacheKey = ty::ParamEnvAnd<'tcx, ty::Predicate<'tcx>>;
34 fn as_cache_key(&self) -> Self::CacheKey {
35 self.obligation.param_env.and(self.obligation.predicate)
39 /// The fulfillment context is used to drive trait resolution. It
40 /// consists of a list of obligations that must be (eventually)
41 /// satisfied. The job is to track which are satisfied, which yielded
42 /// errors, and which are still pending. At any point, users can call
43 /// `select_where_possible`, and the fulfillment context will try to do
44 /// selection, retaining only those obligations that remain
45 /// ambiguous. This may be helpful in pushing type inference
46 /// along. Once all type inference constraints have been generated, the
47 /// method `select_all_or_error` can be used to report any remaining
48 /// ambiguous cases as errors.
49 pub struct FulfillmentContext<'tcx> {
50 // A list of all obligations that have been registered with this
51 // fulfillment context.
52 predicates: ObligationForest<PendingPredicateObligation<'tcx>>,
53 // Should this fulfillment context register type-lives-for-region
54 // obligations on its parent infcx? In some cases, region
55 // obligations are either already known to hold (normalization) or
56 // hopefully verifed elsewhere (type-impls-bound), and therefore
57 // should not be checked.
59 // Note that if we are normalizing a type that we already
60 // know is well-formed, there should be no harm setting this
61 // to true - all the region variables should be determinable
62 // using the RFC 447 rules, which don't depend on
63 // type-lives-for-region constraints, and because the type
64 // is well-formed, the constraints should hold.
65 register_region_obligations: bool,
66 // Is it OK to register obligations into this infcx inside
69 // The "primary fulfillment" in many cases in typeck lives
70 // outside of any snapshot, so any use of it inside a snapshot
71 // will lead to trouble and therefore is checked against, but
72 // other fulfillment contexts sometimes do live inside of
73 // a snapshot (they don't *straddle* a snapshot, so there
74 // is no trouble there).
75 usable_in_snapshot: bool,
78 #[derive(Clone, Debug)]
79 pub struct PendingPredicateObligation<'tcx> {
80 pub obligation: PredicateObligation<'tcx>,
81 // This is far more often read than modified, meaning that we
82 // should mostly optimize for reading speed, while modifying is not as relevant.
84 // For whatever reason using a boxed slice is slower than using a `Vec` here.
85 pub stalled_on: Vec<TyOrConstInferVar<'tcx>>,
88 // `PendingPredicateObligation` is used a lot. Make sure it doesn't unintentionally get bigger.
89 #[cfg(target_arch = "x86_64")]
90 static_assert_size!(PendingPredicateObligation<'_>, 56);
92 impl<'a, 'tcx> FulfillmentContext<'tcx> {
93 /// Creates a new fulfillment context.
94 pub fn new() -> FulfillmentContext<'tcx> {
96 predicates: ObligationForest::new(),
97 register_region_obligations: true,
98 usable_in_snapshot: false,
102 pub fn new_in_snapshot() -> FulfillmentContext<'tcx> {
104 predicates: ObligationForest::new(),
105 register_region_obligations: true,
106 usable_in_snapshot: true,
110 pub fn new_ignoring_regions() -> FulfillmentContext<'tcx> {
112 predicates: ObligationForest::new(),
113 register_region_obligations: false,
114 usable_in_snapshot: false,
118 /// Attempts to select obligations using `selcx`.
121 selcx: &mut SelectionContext<'a, 'tcx>,
122 ) -> Result<(), Vec<FulfillmentError<'tcx>>> {
123 let span = debug_span!("select", obligation_forest_size = ?self.predicates.len());
124 let _enter = span.enter();
126 let mut errors = Vec::new();
129 debug!("select: starting another iteration");
131 // Process pending obligations.
132 let outcome: Outcome<_, _> =
133 self.predicates.process_obligations(&mut FulfillProcessor {
135 register_region_obligations: self.register_region_obligations,
137 debug!("select: outcome={:#?}", outcome);
139 // FIXME: if we kept the original cache key, we could mark projection
140 // obligations as complete for the projection cache here.
142 errors.extend(outcome.errors.into_iter().map(to_fulfillment_error));
144 // If nothing new was added, no need to keep looping.
151 "select({} predicates remaining, {} errors) done",
152 self.predicates.len(),
156 if errors.is_empty() { Ok(()) } else { Err(errors) }
160 impl<'tcx> TraitEngine<'tcx> for FulfillmentContext<'tcx> {
161 /// "Normalize" a projection type `<SomeType as SomeTrait>::X` by
162 /// creating a fresh type variable `$0` as well as a projection
163 /// predicate `<SomeType as SomeTrait>::X == $0`. When the
164 /// inference engine runs, it will attempt to find an impl of
165 /// `SomeTrait` or a where-clause that lets us unify `$0` with
166 /// something concrete. If this fails, we'll unify `$0` with
167 /// `projection_ty` again.
168 fn normalize_projection_type(
170 infcx: &InferCtxt<'_, 'tcx>,
171 param_env: ty::ParamEnv<'tcx>,
172 projection_ty: ty::ProjectionTy<'tcx>,
173 cause: ObligationCause<'tcx>,
175 debug!(?projection_ty, "normalize_projection_type");
177 debug_assert!(!projection_ty.has_escaping_bound_vars());
179 // FIXME(#20304) -- cache
181 let mut selcx = SelectionContext::new(infcx);
182 let mut obligations = vec![];
183 let normalized_ty = project::normalize_projection_type(
191 self.register_predicate_obligations(infcx, obligations);
193 debug!(?normalized_ty);
198 fn register_predicate_obligation(
200 infcx: &InferCtxt<'_, 'tcx>,
201 obligation: PredicateObligation<'tcx>,
203 // this helps to reduce duplicate errors, as well as making
204 // debug output much nicer to read and so on.
205 let obligation = infcx.resolve_vars_if_possible(obligation);
207 debug!(?obligation, "register_predicate_obligation");
209 assert!(!infcx.is_in_snapshot() || self.usable_in_snapshot);
212 .register_obligation(PendingPredicateObligation { obligation, stalled_on: vec![] });
215 fn select_all_or_error(
217 infcx: &InferCtxt<'_, 'tcx>,
218 ) -> Result<(), Vec<FulfillmentError<'tcx>>> {
219 self.select_where_possible(infcx)?;
221 let errors: Vec<_> = self
223 .to_errors(CodeAmbiguity)
225 .map(to_fulfillment_error)
227 if errors.is_empty() { Ok(()) } else { Err(errors) }
230 fn select_where_possible(
232 infcx: &InferCtxt<'_, 'tcx>,
233 ) -> Result<(), Vec<FulfillmentError<'tcx>>> {
234 let mut selcx = SelectionContext::new(infcx);
235 self.select(&mut selcx)
238 fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>> {
239 self.predicates.map_pending_obligations(|o| o.obligation.clone())
243 struct FulfillProcessor<'a, 'b, 'tcx> {
244 selcx: &'a mut SelectionContext<'b, 'tcx>,
245 register_region_obligations: bool,
248 fn mk_pending(os: Vec<PredicateObligation<'tcx>>) -> Vec<PendingPredicateObligation<'tcx>> {
250 .map(|o| PendingPredicateObligation { obligation: o, stalled_on: vec![] })
254 impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
255 type Obligation = PendingPredicateObligation<'tcx>;
256 type Error = FulfillmentErrorCode<'tcx>;
258 /// Processes a predicate obligation and returns either:
259 /// - `Changed(v)` if the predicate is true, presuming that `v` are also true
260 /// - `Unchanged` if we don't have enough info to be sure
261 /// - `Error(e)` if the predicate does not hold
263 /// This is always inlined, despite its size, because it has a single
264 /// callsite and it is called *very* frequently.
266 fn process_obligation(
268 pending_obligation: &mut Self::Obligation,
269 ) -> ProcessResult<Self::Obligation, Self::Error> {
270 // If we were stalled on some unresolved variables, first check whether
271 // any of them have been resolved; if not, don't bother doing more work
273 let change = match pending_obligation.stalled_on.len() {
274 // Match arms are in order of frequency, which matters because this
275 // code is so hot. 1 and 0 dominate; 2+ is fairly rare.
277 let infer_var = pending_obligation.stalled_on[0];
278 self.selcx.infcx().ty_or_const_infer_var_changed(infer_var)
281 // In this case we haven't changed, but wish to make a change.
285 // This `for` loop was once a call to `all()`, but this lower-level
286 // form was a perf win. See #64545 for details.
288 for &infer_var in &pending_obligation.stalled_on {
289 if self.selcx.infcx().ty_or_const_infer_var_changed(infer_var) {
300 "process_predicate: pending obligation {:?} still stalled on {:?}",
301 self.selcx.infcx().resolve_vars_if_possible(pending_obligation.obligation.clone()),
302 pending_obligation.stalled_on
304 return ProcessResult::Unchanged;
307 self.progress_changed_obligations(pending_obligation)
310 fn process_backedge<'c, I>(
313 _marker: PhantomData<&'c PendingPredicateObligation<'tcx>>,
315 I: Clone + Iterator<Item = &'c PendingPredicateObligation<'tcx>>,
317 if self.selcx.coinductive_match(cycle.clone().map(|s| s.obligation.predicate)) {
318 debug!("process_child_obligations: coinductive match");
320 let cycle: Vec<_> = cycle.map(|c| c.obligation.clone()).collect();
321 self.selcx.infcx().report_overflow_error_cycle(&cycle);
326 impl<'a, 'b, 'tcx> FulfillProcessor<'a, 'b, 'tcx> {
327 // The code calling this method is extremely hot and only rarely
328 // actually uses this, so move this part of the code
331 fn progress_changed_obligations(
333 pending_obligation: &mut PendingPredicateObligation<'tcx>,
334 ) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
335 pending_obligation.stalled_on.truncate(0);
337 let obligation = &mut pending_obligation.obligation;
339 if obligation.predicate.has_infer_types_or_consts() {
340 obligation.predicate =
341 self.selcx.infcx().resolve_vars_if_possible(obligation.predicate);
344 debug!(?obligation, ?obligation.cause, "process_obligation");
346 let infcx = self.selcx.infcx();
348 let binder = obligation.predicate.kind();
349 if binder.skip_binder().has_escaping_bound_vars() {
350 match binder.skip_binder() {
351 // Evaluation will discard candidates using the leak check.
352 // This means we need to pass it the bound version of our
354 ty::PredicateAtom::Trait(trait_ref, _constness) => {
355 let trait_obligation = obligation.with(binder.rebind(trait_ref));
357 self.process_trait_obligation(
360 &mut pending_obligation.stalled_on,
363 ty::PredicateAtom::Projection(data) => {
364 let project_obligation = obligation.with(binder.rebind(data));
366 self.process_projection_obligation(
368 &mut pending_obligation.stalled_on,
371 ty::PredicateAtom::RegionOutlives(_)
372 | ty::PredicateAtom::TypeOutlives(_)
373 | ty::PredicateAtom::WellFormed(_)
374 | ty::PredicateAtom::ObjectSafe(_)
375 | ty::PredicateAtom::ClosureKind(..)
376 | ty::PredicateAtom::Subtype(_)
377 | ty::PredicateAtom::ConstEvaluatable(..)
378 | ty::PredicateAtom::ConstEquate(..) => {
379 let pred = infcx.replace_bound_vars_with_placeholders(binder);
380 ProcessResult::Changed(mk_pending(vec![
381 obligation.with(pred.to_predicate(self.selcx.tcx())),
384 ty::PredicateAtom::TypeWellFormedFromEnv(..) => {
385 bug!("TypeWellFormedFromEnv is only used for Chalk")
389 match binder.skip_binder() {
390 ty::PredicateAtom::Trait(data, _) => {
391 let trait_obligation = obligation.with(Binder::dummy(data));
393 self.process_trait_obligation(
396 &mut pending_obligation.stalled_on,
400 ty::PredicateAtom::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::PredicateAtom::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::PredicateAtom::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::PredicateAtom::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::PredicateAtom::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::PredicateAtom::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::PredicateAtom::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::PredicateAtom::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(ErrorHandled::TooGeneric) => {
502 pending_obligation.stalled_on = substs
504 .filter_map(TyOrConstInferVar::maybe_from_generic_arg)
506 ProcessResult::Unchanged
508 Err(e) => ProcessResult::Error(CodeSelectionError(ConstEvalFailure(e))),
512 ty::PredicateAtom::ConstEquate(c1, c2) => {
513 debug!(?c1, ?c2, "equating consts");
514 if self.selcx.tcx().features().const_evaluatable_checked {
515 // FIXME: we probably should only try to unify abstract constants
516 // if the constants depend on generic parameters.
518 // Let's just see where this breaks :shrug:
520 ty::ConstKind::Unevaluated(a_def, a_substs, None),
521 ty::ConstKind::Unevaluated(b_def, b_substs, None),
527 .try_unify_abstract_consts(((a_def, a_substs), (b_def, b_substs)))
529 return ProcessResult::Changed(vec![]);
534 let stalled_on = &mut pending_obligation.stalled_on;
536 let mut evaluate = |c: &'tcx Const<'tcx>| {
537 if let ty::ConstKind::Unevaluated(def, substs, promoted) = c.val {
538 match self.selcx.infcx().const_eval_resolve(
539 obligation.param_env,
543 Some(obligation.cause.span),
545 Ok(val) => Ok(Const::from_value(self.selcx.tcx(), val, c.ty)),
546 Err(ErrorHandled::TooGeneric) => {
551 TyOrConstInferVar::maybe_from_generic_arg(arg)
555 Err(ErrorHandled::TooGeneric)
557 Err(err) => Err(err),
564 match (evaluate(c1), evaluate(c2)) {
565 (Ok(c1), Ok(c2)) => {
569 .at(&obligation.cause, obligation.param_env)
572 Ok(_) => ProcessResult::Changed(vec![]),
573 Err(err) => ProcessResult::Error(
574 FulfillmentErrorCode::CodeConstEquateError(
575 ExpectedFound::new(true, c1, c2),
581 (Err(ErrorHandled::Reported(ErrorReported)), _)
582 | (_, Err(ErrorHandled::Reported(ErrorReported))) => {
583 ProcessResult::Error(CodeSelectionError(ConstEvalFailure(
584 ErrorHandled::Reported(ErrorReported),
587 (Err(ErrorHandled::Linted), _) | (_, Err(ErrorHandled::Linted)) => {
589 obligation.cause.span(self.selcx.tcx()),
590 "ConstEquate: const_eval_resolve returned an unexpected error"
593 (Err(ErrorHandled::TooGeneric), _) | (_, Err(ErrorHandled::TooGeneric)) => {
594 ProcessResult::Unchanged
598 ty::PredicateAtom::TypeWellFormedFromEnv(..) => {
599 bug!("TypeWellFormedFromEnv is only used for Chalk")
605 #[instrument(level = "debug", skip(self, obligation, stalled_on))]
606 fn process_trait_obligation(
608 obligation: &PredicateObligation<'tcx>,
609 trait_obligation: TraitObligation<'tcx>,
610 stalled_on: &mut Vec<TyOrConstInferVar<'tcx>>,
611 ) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
612 let infcx = self.selcx.infcx();
613 if obligation.predicate.is_global() {
614 // no type variables present, can use evaluation for better caching.
615 // FIXME: consider caching errors too.
616 if infcx.predicate_must_hold_considering_regions(obligation) {
618 "selecting trait at depth {} evaluated to holds",
619 obligation.recursion_depth
621 return ProcessResult::Changed(vec![]);
625 match self.selcx.select(&trait_obligation) {
626 Ok(Some(impl_source)) => {
627 debug!("selecting trait at depth {} yielded Ok(Some)", obligation.recursion_depth);
628 ProcessResult::Changed(mk_pending(impl_source.nested_obligations()))
631 debug!("selecting trait at depth {} yielded Ok(None)", obligation.recursion_depth);
633 // This is a bit subtle: for the most part, the
634 // only reason we can fail to make progress on
635 // trait selection is because we don't have enough
636 // information about the types in the trait.
637 *stalled_on = trait_ref_infer_vars(
639 trait_obligation.predicate.map_bound(|pred| pred.trait_ref),
643 "process_predicate: pending obligation {:?} now stalled on {:?}",
644 infcx.resolve_vars_if_possible(obligation.clone()),
648 ProcessResult::Unchanged
650 Err(selection_err) => {
651 info!("selecting trait at depth {} yielded Err", obligation.recursion_depth);
653 ProcessResult::Error(CodeSelectionError(selection_err))
658 fn process_projection_obligation(
660 project_obligation: PolyProjectionObligation<'tcx>,
661 stalled_on: &mut Vec<TyOrConstInferVar<'tcx>>,
662 ) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
663 let tcx = self.selcx.tcx();
664 match project::poly_project_and_unify_type(self.selcx, &project_obligation) {
665 Ok(Ok(Some(os))) => ProcessResult::Changed(mk_pending(os)),
667 *stalled_on = trait_ref_infer_vars(
669 project_obligation.predicate.to_poly_trait_ref(tcx),
671 ProcessResult::Unchanged
673 // Let the caller handle the recursion
674 Ok(Err(project::InProgress)) => ProcessResult::Changed(mk_pending(vec![
675 project_obligation.with(project_obligation.predicate.to_predicate(tcx)),
677 Err(e) => ProcessResult::Error(CodeProjectionError(e)),
682 /// Returns the set of inference variables contained in a trait ref.
683 fn trait_ref_infer_vars<'a, 'tcx>(
684 selcx: &mut SelectionContext<'a, 'tcx>,
685 trait_ref: ty::PolyTraitRef<'tcx>,
686 ) -> Vec<TyOrConstInferVar<'tcx>> {
689 .resolve_vars_if_possible(trait_ref)
693 // FIXME(eddyb) try using `skip_current_subtree` to skip everything that
694 // doesn't contain inference variables, not just the outermost level.
695 .filter(|arg| arg.has_infer_types_or_consts())
696 .flat_map(|arg| arg.walk())
697 .filter_map(TyOrConstInferVar::maybe_from_generic_arg)
701 fn to_fulfillment_error<'tcx>(
702 error: Error<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>>,
703 ) -> FulfillmentError<'tcx> {
704 let obligation = error.backtrace.into_iter().next().unwrap().obligation;
705 FulfillmentError::new(obligation, error.error)