1 use crate::infer::{InferCtxt, TyOrConstInferVar};
2 use rustc_data_structures::obligation_forest::ProcessResult;
3 use rustc_data_structures::obligation_forest::{DoCompleted, Error, ForestObligation};
4 use rustc_data_structures::obligation_forest::{ObligationForest, ObligationProcessor};
5 use rustc_errors::ErrorReported;
6 use rustc_infer::traits::{TraitEngine, TraitEngineExt as _};
7 use rustc_middle::mir::interpret::ErrorHandled;
8 use rustc_middle::ty::error::ExpectedFound;
9 use rustc_middle::ty::{self, Const, ToPolyTraitRef, Ty, TypeFoldable};
10 use std::marker::PhantomData;
13 use super::select::SelectionContext;
15 use super::CodeAmbiguity;
16 use super::CodeProjectionError;
17 use super::CodeSelectionError;
18 use super::{ConstEvalFailure, Unimplemented};
19 use super::{FulfillmentError, FulfillmentErrorCode};
20 use super::{ObligationCause, PredicateObligation};
22 use crate::traits::error_reporting::InferCtxtExt as _;
23 use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
25 impl<'tcx> ForestObligation for PendingPredicateObligation<'tcx> {
26 /// Note that we include both the `ParamEnv` and the `Predicate`,
27 /// as the `ParamEnv` can influence whether fulfillment succeeds
29 type CacheKey = ty::ParamEnvAnd<'tcx, ty::Predicate<'tcx>>;
31 fn as_cache_key(&self) -> Self::CacheKey {
32 self.obligation.param_env.and(self.obligation.predicate)
36 /// The fulfillment context is used to drive trait resolution. It
37 /// consists of a list of obligations that must be (eventually)
38 /// satisfied. The job is to track which are satisfied, which yielded
39 /// errors, and which are still pending. At any point, users can call
40 /// `select_where_possible`, and the fulfillment context will try to do
41 /// selection, retaining only those obligations that remain
42 /// ambiguous. This may be helpful in pushing type inference
43 /// along. Once all type inference constraints have been generated, the
44 /// method `select_all_or_error` can be used to report any remaining
45 /// ambiguous cases as errors.
46 pub struct FulfillmentContext<'tcx> {
47 // A list of all obligations that have been registered with this
48 // fulfillment context.
49 predicates: ObligationForest<PendingPredicateObligation<'tcx>>,
50 // Should this fulfillment context register type-lives-for-region
51 // obligations on its parent infcx? In some cases, region
52 // obligations are either already known to hold (normalization) or
53 // hopefully verifed elsewhere (type-impls-bound), and therefore
54 // should not be checked.
56 // Note that if we are normalizing a type that we already
57 // know is well-formed, there should be no harm setting this
58 // to true - all the region variables should be determinable
59 // using the RFC 447 rules, which don't depend on
60 // type-lives-for-region constraints, and because the type
61 // is well-formed, the constraints should hold.
62 register_region_obligations: bool,
63 // Is it OK to register obligations into this infcx inside
66 // The "primary fulfillment" in many cases in typeck lives
67 // outside of any snapshot, so any use of it inside a snapshot
68 // will lead to trouble and therefore is checked against, but
69 // other fulfillment contexts sometimes do live inside of
70 // a snapshot (they don't *straddle* a snapshot, so there
71 // is no trouble there).
72 usable_in_snapshot: bool,
75 #[derive(Clone, Debug)]
76 pub struct PendingPredicateObligation<'tcx> {
77 pub obligation: PredicateObligation<'tcx>,
78 // This is far more often read than modified, meaning that we
79 // should mostly optimize for reading speed, while modifying is not as relevant.
81 // For whatever reason using a boxed slice is slower than using a `Vec` here.
82 pub stalled_on: Vec<TyOrConstInferVar<'tcx>>,
85 // `PendingPredicateObligation` is used a lot. Make sure it doesn't unintentionally get bigger.
86 #[cfg(target_arch = "x86_64")]
87 static_assert_size!(PendingPredicateObligation<'_>, 64);
89 impl<'a, 'tcx> FulfillmentContext<'tcx> {
90 /// Creates a new fulfillment context.
91 pub fn new() -> FulfillmentContext<'tcx> {
93 predicates: ObligationForest::new(),
94 register_region_obligations: true,
95 usable_in_snapshot: false,
99 pub fn new_in_snapshot() -> FulfillmentContext<'tcx> {
101 predicates: ObligationForest::new(),
102 register_region_obligations: true,
103 usable_in_snapshot: true,
107 pub fn new_ignoring_regions() -> FulfillmentContext<'tcx> {
109 predicates: ObligationForest::new(),
110 register_region_obligations: false,
111 usable_in_snapshot: false,
115 /// Attempts to select obligations using `selcx`.
118 selcx: &mut SelectionContext<'a, 'tcx>,
119 ) -> Result<(), Vec<FulfillmentError<'tcx>>> {
120 debug!("select(obligation-forest-size={})", self.predicates.len());
122 let mut errors = Vec::new();
125 debug!("select: starting another iteration");
127 // Process pending obligations.
128 let outcome = self.predicates.process_obligations(
129 &mut FulfillProcessor {
131 register_region_obligations: self.register_region_obligations,
135 debug!("select: outcome={:#?}", outcome);
137 // FIXME: if we kept the original cache key, we could mark projection
138 // obligations as complete for the projection cache here.
140 errors.extend(outcome.errors.into_iter().map(to_fulfillment_error));
142 // If nothing new was added, no need to keep looping.
149 "select({} predicates remaining, {} errors) done",
150 self.predicates.len(),
154 if errors.is_empty() { Ok(()) } else { Err(errors) }
158 impl<'tcx> TraitEngine<'tcx> for FulfillmentContext<'tcx> {
159 /// "Normalize" a projection type `<SomeType as SomeTrait>::X` by
160 /// creating a fresh type variable `$0` as well as a projection
161 /// predicate `<SomeType as SomeTrait>::X == $0`. When the
162 /// inference engine runs, it will attempt to find an impl of
163 /// `SomeTrait` or a where-clause that lets us unify `$0` with
164 /// something concrete. If this fails, we'll unify `$0` with
165 /// `projection_ty` again.
166 fn normalize_projection_type(
168 infcx: &InferCtxt<'_, 'tcx>,
169 param_env: ty::ParamEnv<'tcx>,
170 projection_ty: ty::ProjectionTy<'tcx>,
171 cause: ObligationCause<'tcx>,
173 debug!("normalize_projection_type(projection_ty={:?})", projection_ty);
175 debug_assert!(!projection_ty.has_escaping_bound_vars());
177 // FIXME(#20304) -- cache
179 let mut selcx = SelectionContext::new(infcx);
180 let mut obligations = vec![];
181 let normalized_ty = project::normalize_projection_type(
189 self.register_predicate_obligations(infcx, obligations);
191 debug!("normalize_projection_type: result={:?}", normalized_ty);
196 fn register_predicate_obligation(
198 infcx: &InferCtxt<'_, 'tcx>,
199 obligation: PredicateObligation<'tcx>,
201 // this helps to reduce duplicate errors, as well as making
202 // debug output much nicer to read and so on.
203 let obligation = infcx.resolve_vars_if_possible(&obligation);
205 debug!("register_predicate_obligation(obligation={:?})", obligation);
207 assert!(!infcx.is_in_snapshot() || self.usable_in_snapshot);
210 .register_obligation(PendingPredicateObligation { obligation, stalled_on: vec![] });
213 fn select_all_or_error(
215 infcx: &InferCtxt<'_, 'tcx>,
216 ) -> Result<(), Vec<FulfillmentError<'tcx>>> {
217 self.select_where_possible(infcx)?;
219 let errors: Vec<_> = self
221 .to_errors(CodeAmbiguity)
223 .map(to_fulfillment_error)
225 if errors.is_empty() { Ok(()) } else { Err(errors) }
228 fn select_where_possible(
230 infcx: &InferCtxt<'_, 'tcx>,
231 ) -> Result<(), Vec<FulfillmentError<'tcx>>> {
232 let mut selcx = SelectionContext::new(infcx);
233 self.select(&mut selcx)
236 fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>> {
237 self.predicates.map_pending_obligations(|o| o.obligation.clone())
241 struct FulfillProcessor<'a, 'b, 'tcx> {
242 selcx: &'a mut SelectionContext<'b, 'tcx>,
243 register_region_obligations: bool,
246 fn mk_pending(os: Vec<PredicateObligation<'tcx>>) -> Vec<PendingPredicateObligation<'tcx>> {
248 .map(|o| PendingPredicateObligation { obligation: o, stalled_on: vec![] })
252 impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
253 type Obligation = PendingPredicateObligation<'tcx>;
254 type Error = FulfillmentErrorCode<'tcx>;
256 /// Processes a predicate obligation and returns either:
257 /// - `Changed(v)` if the predicate is true, presuming that `v` are also true
258 /// - `Unchanged` if we don't have enough info to be sure
259 /// - `Error(e)` if the predicate does not hold
261 /// This is always inlined, despite its size, because it has a single
262 /// callsite and it is called *very* frequently.
264 fn process_obligation(
266 pending_obligation: &mut Self::Obligation,
267 ) -> ProcessResult<Self::Obligation, Self::Error> {
268 // If we were stalled on some unresolved variables, first check whether
269 // any of them have been resolved; if not, don't bother doing more work
271 let change = match pending_obligation.stalled_on.len() {
272 // Match arms are in order of frequency, which matters because this
273 // code is so hot. 1 and 0 dominate; 2+ is fairly rare.
275 let infer_var = pending_obligation.stalled_on[0];
276 self.selcx.infcx().ty_or_const_infer_var_changed(infer_var)
279 // In this case we haven't changed, but wish to make a change.
283 // This `for` loop was once a call to `all()`, but this lower-level
284 // form was a perf win. See #64545 for details.
286 for &infer_var in &pending_obligation.stalled_on {
287 if self.selcx.infcx().ty_or_const_infer_var_changed(infer_var) {
298 "process_predicate: pending obligation {:?} still stalled on {:?}",
299 self.selcx.infcx().resolve_vars_if_possible(&pending_obligation.obligation),
300 pending_obligation.stalled_on
302 return ProcessResult::Unchanged;
305 // This part of the code is much colder.
307 pending_obligation.stalled_on.truncate(0);
309 let obligation = &mut pending_obligation.obligation;
311 if obligation.predicate.has_infer_types_or_consts() {
312 obligation.predicate =
313 self.selcx.infcx().resolve_vars_if_possible(&obligation.predicate);
316 debug!("process_obligation: obligation = {:?} cause = {:?}", obligation, obligation.cause);
318 let infcx = self.selcx.infcx();
320 match obligation.predicate.kind() {
321 ty::PredicateKind::Trait(ref data, _) => {
322 let trait_obligation = obligation.with(*data);
324 if obligation.predicate.is_global() {
325 // no type variables present, can use evaluation for better caching.
326 // FIXME: consider caching errors too.
327 if infcx.predicate_must_hold_considering_regions(&obligation) {
329 "selecting trait `{:?}` at depth {} evaluated to holds",
330 data, obligation.recursion_depth
332 return ProcessResult::Changed(vec![]);
336 match self.selcx.select(&trait_obligation) {
337 Ok(Some(impl_source)) => {
339 "selecting trait `{:?}` at depth {} yielded Ok(Some)",
340 data, obligation.recursion_depth
342 ProcessResult::Changed(mk_pending(impl_source.nested_obligations()))
346 "selecting trait `{:?}` at depth {} yielded Ok(None)",
347 data, obligation.recursion_depth
350 // This is a bit subtle: for the most part, the
351 // only reason we can fail to make progress on
352 // trait selection is because we don't have enough
353 // information about the types in the trait.
354 pending_obligation.stalled_on =
355 trait_ref_infer_vars(self.selcx, data.to_poly_trait_ref());
358 "process_predicate: pending obligation {:?} now stalled on {:?}",
359 infcx.resolve_vars_if_possible(obligation),
360 pending_obligation.stalled_on
363 ProcessResult::Unchanged
365 Err(selection_err) => {
367 "selecting trait `{:?}` at depth {} yielded Err",
368 data, obligation.recursion_depth
371 ProcessResult::Error(CodeSelectionError(selection_err))
376 &ty::PredicateKind::RegionOutlives(binder) => {
377 match infcx.region_outlives_predicate(&obligation.cause, binder) {
378 Ok(()) => ProcessResult::Changed(vec![]),
379 Err(_) => ProcessResult::Error(CodeSelectionError(Unimplemented)),
383 ty::PredicateKind::TypeOutlives(ref binder) => {
384 // Check if there are higher-ranked vars.
385 match binder.no_bound_vars() {
386 // If there are, inspect the underlying type further.
388 // Convert from `Binder<OutlivesPredicate<Ty, Region>>` to `Binder<Ty>`.
389 let binder = binder.map_bound_ref(|pred| pred.0);
391 // Check if the type has any bound vars.
392 match binder.no_bound_vars() {
393 // If so, this obligation is an error (for now). Eventually we should be
394 // able to support additional cases here, like `for<'a> &'a str: 'a`.
395 // NOTE: this is duplicate-implemented between here and fulfillment.
396 None => ProcessResult::Error(CodeSelectionError(Unimplemented)),
397 // Otherwise, we have something of the form
398 // `for<'a> T: 'a where 'a not in T`, which we can treat as
401 let r_static = self.selcx.tcx().lifetimes.re_static;
402 if self.register_region_obligations {
403 self.selcx.infcx().register_region_obligation_with_cause(
409 ProcessResult::Changed(vec![])
413 // If there aren't, register the obligation.
414 Some(ty::OutlivesPredicate(t_a, r_b)) => {
415 if self.register_region_obligations {
416 self.selcx.infcx().register_region_obligation_with_cause(
422 ProcessResult::Changed(vec![])
427 ty::PredicateKind::Projection(ref data) => {
428 let project_obligation = obligation.with(*data);
429 match project::poly_project_and_unify_type(self.selcx, &project_obligation) {
431 let tcx = self.selcx.tcx();
432 pending_obligation.stalled_on =
433 trait_ref_infer_vars(self.selcx, data.to_poly_trait_ref(tcx));
434 ProcessResult::Unchanged
436 Ok(Some(os)) => ProcessResult::Changed(mk_pending(os)),
437 Err(e) => ProcessResult::Error(CodeProjectionError(e)),
441 &ty::PredicateKind::ObjectSafe(trait_def_id) => {
442 if !self.selcx.tcx().is_object_safe(trait_def_id) {
443 ProcessResult::Error(CodeSelectionError(Unimplemented))
445 ProcessResult::Changed(vec![])
449 &ty::PredicateKind::ClosureKind(_, closure_substs, kind) => {
450 match self.selcx.infcx().closure_kind(closure_substs) {
451 Some(closure_kind) => {
452 if closure_kind.extends(kind) {
453 ProcessResult::Changed(vec![])
455 ProcessResult::Error(CodeSelectionError(Unimplemented))
458 None => ProcessResult::Unchanged,
462 &ty::PredicateKind::WellFormed(arg) => {
463 match wf::obligations(
465 obligation.param_env,
466 obligation.cause.body_id,
468 obligation.cause.span,
471 pending_obligation.stalled_on =
472 vec![TyOrConstInferVar::maybe_from_generic_arg(arg).unwrap()];
473 ProcessResult::Unchanged
475 Some(os) => ProcessResult::Changed(mk_pending(os)),
479 &ty::PredicateKind::Subtype(subtype) => {
480 match self.selcx.infcx().subtype_predicate(
482 obligation.param_env,
486 // None means that both are unresolved.
487 pending_obligation.stalled_on = vec![
488 TyOrConstInferVar::maybe_from_ty(subtype.skip_binder().a).unwrap(),
489 TyOrConstInferVar::maybe_from_ty(subtype.skip_binder().b).unwrap(),
491 ProcessResult::Unchanged
493 Some(Ok(ok)) => ProcessResult::Changed(mk_pending(ok.obligations)),
495 let expected_found = ExpectedFound::new(
496 subtype.skip_binder().a_is_expected,
497 subtype.skip_binder().a,
498 subtype.skip_binder().b,
500 ProcessResult::Error(FulfillmentErrorCode::CodeSubtypeError(
508 &ty::PredicateKind::ConstEvaluatable(def_id, substs) => {
509 match self.selcx.infcx().const_eval_resolve(
510 obligation.param_env,
514 Some(obligation.cause.span),
516 Ok(_) => ProcessResult::Changed(vec![]),
517 Err(err) => ProcessResult::Error(CodeSelectionError(ConstEvalFailure(err))),
521 ty::PredicateKind::ConstEquate(c1, c2) => {
522 debug!("equating consts: c1={:?} c2={:?}", c1, c2);
524 let stalled_on = &mut pending_obligation.stalled_on;
526 let mut evaluate = |c: &'tcx Const<'tcx>| {
527 if let ty::ConstKind::Unevaluated(def, substs, promoted) = c.val {
528 match self.selcx.infcx().const_eval_resolve(
529 obligation.param_env,
533 Some(obligation.cause.span),
535 Ok(val) => Ok(Const::from_value(self.selcx.tcx(), val, c.ty)),
536 Err(ErrorHandled::TooGeneric) => {
540 .filter_map(|ty| TyOrConstInferVar::maybe_from_ty(ty))
543 Err(ErrorHandled::TooGeneric)
545 Err(err) => Err(err),
552 match (evaluate(c1), evaluate(c2)) {
553 (Ok(c1), Ok(c2)) => {
557 .at(&obligation.cause, obligation.param_env)
560 Ok(_) => ProcessResult::Changed(vec![]),
562 ProcessResult::Error(FulfillmentErrorCode::CodeConstEquateError(
563 ExpectedFound::new(true, c1, c2),
569 (Err(ErrorHandled::Reported(ErrorReported)), _)
570 | (_, Err(ErrorHandled::Reported(ErrorReported))) => ProcessResult::Error(
571 CodeSelectionError(ConstEvalFailure(ErrorHandled::Reported(ErrorReported))),
573 (Err(ErrorHandled::Linted), _) | (_, Err(ErrorHandled::Linted)) => span_bug!(
574 obligation.cause.span(self.selcx.tcx()),
575 "ConstEquate: const_eval_resolve returned an unexpected error"
577 (Err(ErrorHandled::TooGeneric), _) | (_, Err(ErrorHandled::TooGeneric)) => {
578 ProcessResult::Unchanged
585 fn process_backedge<'c, I>(
588 _marker: PhantomData<&'c PendingPredicateObligation<'tcx>>,
590 I: Clone + Iterator<Item = &'c PendingPredicateObligation<'tcx>>,
592 if self.selcx.coinductive_match(cycle.clone().map(|s| s.obligation.predicate)) {
593 debug!("process_child_obligations: coinductive match");
595 let cycle: Vec<_> = cycle.map(|c| c.obligation.clone()).collect();
596 self.selcx.infcx().report_overflow_error_cycle(&cycle);
601 /// Returns the set of inference variables contained in a trait ref.
602 fn trait_ref_infer_vars<'a, 'tcx>(
603 selcx: &mut SelectionContext<'a, 'tcx>,
604 trait_ref: ty::PolyTraitRef<'tcx>,
605 ) -> Vec<TyOrConstInferVar<'tcx>> {
608 .resolve_vars_if_possible(&trait_ref)
609 .skip_binder() // ok b/c this check doesn't care about regions
612 // FIXME(eddyb) try using `skip_current_subtree` to skip everything that
613 // doesn't contain inference variables, not just the outermost level.
614 .filter(|arg| arg.has_infer_types_or_consts())
615 .flat_map(|arg| arg.walk())
616 .filter_map(TyOrConstInferVar::maybe_from_generic_arg)
620 fn to_fulfillment_error<'tcx>(
621 error: Error<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>>,
622 ) -> FulfillmentError<'tcx> {
623 let obligation = error.backtrace.into_iter().next().unwrap().obligation;
624 FulfillmentError::new(obligation, error.error)