]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_trait_selection/src/traits/fulfill.rs
Auto merge of #87284 - Aaron1011:remove-paren-special, r=petrochenkov
[rust.git] / compiler / rustc_trait_selection / src / traits / fulfill.rs
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;
14
15 use super::const_evaluatable;
16 use super::project;
17 use super::select::SelectionContext;
18 use super::wf;
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};
25
26 use crate::traits::error_reporting::InferCtxtExt as _;
27 use crate::traits::project::PolyProjectionObligation;
28 use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
29
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
33     /// or fails.
34     type CacheKey = ty::ParamEnvAnd<'tcx, ty::Predicate<'tcx>>;
35
36     fn as_cache_key(&self) -> Self::CacheKey {
37         self.obligation.param_env.and(self.obligation.predicate)
38     }
39 }
40
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.
60     //
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
69     // an infcx snapshot?
70     //
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,
78 }
79
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.
85     //
86     // For whatever reason using a boxed slice is slower than using a `Vec` here.
87     pub stalled_on: Vec<TyOrConstInferVar<'tcx>>,
88 }
89
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);
93
94 impl<'a, 'tcx> FulfillmentContext<'tcx> {
95     /// Creates a new fulfillment context.
96     pub fn new() -> FulfillmentContext<'tcx> {
97         FulfillmentContext {
98             predicates: ObligationForest::new(),
99             register_region_obligations: true,
100             usable_in_snapshot: false,
101         }
102     }
103
104     pub fn new_in_snapshot() -> FulfillmentContext<'tcx> {
105         FulfillmentContext {
106             predicates: ObligationForest::new(),
107             register_region_obligations: true,
108             usable_in_snapshot: true,
109         }
110     }
111
112     pub fn new_ignoring_regions() -> FulfillmentContext<'tcx> {
113         FulfillmentContext {
114             predicates: ObligationForest::new(),
115             register_region_obligations: false,
116             usable_in_snapshot: false,
117         }
118     }
119
120     /// Attempts to select obligations using `selcx`.
121     fn select(
122         &mut self,
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();
127
128         let mut errors = Vec::new();
129
130         loop {
131             debug!("select: starting another iteration");
132
133             // Process pending obligations.
134             let outcome: Outcome<_, _> =
135                 self.predicates.process_obligations(&mut FulfillProcessor {
136                     selcx,
137                     register_region_obligations: self.register_region_obligations,
138                 });
139             debug!("select: outcome={:#?}", outcome);
140
141             // FIXME: if we kept the original cache key, we could mark projection
142             // obligations as complete for the projection cache here.
143
144             errors.extend(outcome.errors.into_iter().map(to_fulfillment_error));
145
146             // If nothing new was added, no need to keep looping.
147             if outcome.stalled {
148                 break;
149             }
150         }
151
152         debug!(
153             "select({} predicates remaining, {} errors) done",
154             self.predicates.len(),
155             errors.len()
156         );
157
158         if errors.is_empty() { Ok(()) } else { Err(errors) }
159     }
160 }
161
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(
172         &mut self,
173         infcx: &InferCtxt<'_, 'tcx>,
174         param_env: ty::ParamEnv<'tcx>,
175         projection_ty: ty::ProjectionTy<'tcx>,
176         cause: ObligationCause<'tcx>,
177     ) -> Ty<'tcx> {
178         debug_assert!(!projection_ty.has_escaping_bound_vars());
179
180         // FIXME(#20304) -- cache
181
182         let mut selcx = SelectionContext::new(infcx);
183         let mut obligations = vec![];
184         let normalized_ty = project::normalize_projection_type(
185             &mut selcx,
186             param_env,
187             projection_ty,
188             cause,
189             0,
190             &mut obligations,
191         );
192         self.register_predicate_obligations(infcx, obligations);
193
194         debug!(?normalized_ty);
195
196         normalized_ty
197     }
198
199     fn register_predicate_obligation(
200         &mut self,
201         infcx: &InferCtxt<'_, 'tcx>,
202         obligation: PredicateObligation<'tcx>,
203     ) {
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);
207
208         debug!(?obligation, "register_predicate_obligation");
209
210         assert!(!infcx.is_in_snapshot() || self.usable_in_snapshot);
211
212         self.predicates
213             .register_obligation(PendingPredicateObligation { obligation, stalled_on: vec![] });
214     }
215
216     fn select_all_or_error(
217         &mut self,
218         infcx: &InferCtxt<'_, 'tcx>,
219     ) -> Result<(), Vec<FulfillmentError<'tcx>>> {
220         self.select_where_possible(infcx)?;
221
222         let errors: Vec<_> = self
223             .predicates
224             .to_errors(CodeAmbiguity)
225             .into_iter()
226             .map(to_fulfillment_error)
227             .collect();
228         if errors.is_empty() { Ok(()) } else { Err(errors) }
229     }
230
231     fn select_where_possible(
232         &mut self,
233         infcx: &InferCtxt<'_, 'tcx>,
234     ) -> Result<(), Vec<FulfillmentError<'tcx>>> {
235         let mut selcx = SelectionContext::new(infcx);
236         self.select(&mut selcx)
237     }
238
239     fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>> {
240         self.predicates.map_pending_obligations(|o| o.obligation.clone())
241     }
242 }
243
244 struct FulfillProcessor<'a, 'b, 'tcx> {
245     selcx: &'a mut SelectionContext<'b, 'tcx>,
246     register_region_obligations: bool,
247 }
248
249 fn mk_pending(os: Vec<PredicateObligation<'tcx>>) -> Vec<PendingPredicateObligation<'tcx>> {
250     os.into_iter()
251         .map(|o| PendingPredicateObligation { obligation: o, stalled_on: vec![] })
252         .collect()
253 }
254
255 impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
256     type Obligation = PendingPredicateObligation<'tcx>;
257     type Error = FulfillmentErrorCode<'tcx>;
258
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
263     ///
264     /// This is always inlined, despite its size, because it has a single
265     /// callsite and it is called *very* frequently.
266     #[inline(always)]
267     fn process_obligation(
268         &mut self,
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
273         // yet.
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.
277             1 => {
278                 let infer_var = pending_obligation.stalled_on[0];
279                 self.selcx.infcx().ty_or_const_infer_var_changed(infer_var)
280             }
281             0 => {
282                 // In this case we haven't changed, but wish to make a change.
283                 true
284             }
285             _ => {
286                 // This `for` loop was once a call to `all()`, but this lower-level
287                 // form was a perf win. See #64545 for details.
288                 (|| {
289                     for &infer_var in &pending_obligation.stalled_on {
290                         if self.selcx.infcx().ty_or_const_infer_var_changed(infer_var) {
291                             return true;
292                         }
293                     }
294                     false
295                 })()
296             }
297         };
298
299         if !change {
300             debug!(
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
304             );
305             return ProcessResult::Unchanged;
306         }
307
308         self.progress_changed_obligations(pending_obligation)
309     }
310
311     fn process_backedge<'c, I>(
312         &mut self,
313         cycle: I,
314         _marker: PhantomData<&'c PendingPredicateObligation<'tcx>>,
315     ) where
316         I: Clone + Iterator<Item = &'c PendingPredicateObligation<'tcx>>,
317     {
318         if self.selcx.coinductive_match(cycle.clone().map(|s| s.obligation.predicate)) {
319             debug!("process_child_obligations: coinductive match");
320         } else {
321             let cycle: Vec<_> = cycle.map(|c| c.obligation.clone()).collect();
322             self.selcx.infcx().report_overflow_error_cycle(&cycle);
323         }
324     }
325 }
326
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
330     // out of that loop.
331     #[inline(never)]
332     fn progress_changed_obligations(
333         &mut self,
334         pending_obligation: &mut PendingPredicateObligation<'tcx>,
335     ) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
336         pending_obligation.stalled_on.truncate(0);
337
338         let obligation = &mut pending_obligation.obligation;
339
340         if obligation.predicate.has_infer_types_or_consts() {
341             obligation.predicate =
342                 self.selcx.infcx().resolve_vars_if_possible(obligation.predicate);
343         }
344
345         debug!(?obligation, ?obligation.cause, "process_obligation");
346
347         let infcx = self.selcx.infcx();
348
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
354                 // predicate.
355                 ty::PredicateKind::Trait(trait_ref, _constness) => {
356                     let trait_obligation = obligation.with(binder.rebind(trait_ref));
357
358                     self.process_trait_obligation(
359                         obligation,
360                         trait_obligation,
361                         &mut pending_obligation.stalled_on,
362                     )
363                 }
364                 ty::PredicateKind::Projection(data) => {
365                     let project_obligation = obligation.with(binder.rebind(data));
366
367                     self.process_projection_obligation(
368                         project_obligation,
369                         &mut pending_obligation.stalled_on,
370                     )
371                 }
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())),
383                     ]))
384                 }
385                 ty::PredicateKind::TypeWellFormedFromEnv(..) => {
386                     bug!("TypeWellFormedFromEnv is only used for Chalk")
387                 }
388             },
389             Some(pred) => match pred {
390                 ty::PredicateKind::Trait(data, _) => {
391                     let trait_obligation = obligation.with(Binder::dummy(data));
392
393                     self.process_trait_obligation(
394                         obligation,
395                         trait_obligation,
396                         &mut pending_obligation.stalled_on,
397                     )
398                 }
399
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)),
404                     }
405                 }
406
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(
410                             t_a,
411                             r_b,
412                             &obligation.cause,
413                         );
414                     }
415                     ProcessResult::Changed(vec![])
416                 }
417
418                 ty::PredicateKind::Projection(ref data) => {
419                     let project_obligation = obligation.with(Binder::dummy(*data));
420
421                     self.process_projection_obligation(
422                         project_obligation,
423                         &mut pending_obligation.stalled_on,
424                     )
425                 }
426
427                 ty::PredicateKind::ObjectSafe(trait_def_id) => {
428                     if !self.selcx.tcx().is_object_safe(trait_def_id) {
429                         ProcessResult::Error(CodeSelectionError(Unimplemented))
430                     } else {
431                         ProcessResult::Changed(vec![])
432                     }
433                 }
434
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![])
440                             } else {
441                                 ProcessResult::Error(CodeSelectionError(Unimplemented))
442                             }
443                         }
444                         None => ProcessResult::Unchanged,
445                     }
446                 }
447
448                 ty::PredicateKind::WellFormed(arg) => {
449                     match wf::obligations(
450                         self.selcx.infcx(),
451                         obligation.param_env,
452                         obligation.cause.body_id,
453                         obligation.recursion_depth + 1,
454                         arg,
455                         obligation.cause.span,
456                     ) {
457                         None => {
458                             pending_obligation.stalled_on =
459                                 vec![TyOrConstInferVar::maybe_from_generic_arg(arg).unwrap()];
460                             ProcessResult::Unchanged
461                         }
462                         Some(os) => ProcessResult::Changed(mk_pending(os)),
463                     }
464                 }
465
466                 ty::PredicateKind::Subtype(subtype) => {
467                     match self.selcx.infcx().subtype_predicate(
468                         &obligation.cause,
469                         obligation.param_env,
470                         Binder::dummy(subtype),
471                     ) {
472                         None => {
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(),
477                             ];
478                             ProcessResult::Unchanged
479                         }
480                         Some(Ok(ok)) => ProcessResult::Changed(mk_pending(ok.obligations)),
481                         Some(Err(err)) => {
482                             let expected_found =
483                                 ExpectedFound::new(subtype.a_is_expected, subtype.a, subtype.b);
484                             ProcessResult::Error(FulfillmentErrorCode::CodeSubtypeError(
485                                 expected_found,
486                                 err,
487                             ))
488                         }
489                     }
490                 }
491
492                 ty::PredicateKind::ConstEvaluatable(def_id, substs) => {
493                     match const_evaluatable::is_const_evaluatable(
494                         self.selcx.infcx(),
495                         def_id,
496                         substs,
497                         obligation.param_env,
498                         obligation.cause.span,
499                     ) {
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),
505                             );
506                             ProcessResult::Unchanged
507                         }
508                         Err(
509                             e @ NotConstEvaluatable::MentionsParam
510                             | e @ NotConstEvaluatable::Error(_),
511                         ) => ProcessResult::Error(CodeSelectionError(
512                             SelectionError::NotConstEvaluatable(e),
513                         )),
514                     }
515                 }
516
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.
522                         //
523                         // Let's just see where this breaks :shrug:
524                         if let (ty::ConstKind::Unevaluated(a), ty::ConstKind::Unevaluated(b)) =
525                             (c1.val, c2.val)
526                         {
527                             if self
528                                 .selcx
529                                 .tcx()
530                                 .try_unify_abstract_consts(((a.def, a.substs), (b.def, b.substs)))
531                             {
532                                 return ProcessResult::Changed(vec![]);
533                             }
534                         }
535                     }
536
537                     let stalled_on = &mut pending_obligation.stalled_on;
538
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,
543                                 unevaluated,
544                                 Some(obligation.cause.span),
545                             ) {
546                                 Ok(val) => Ok(Const::from_value(self.selcx.tcx(), val, c.ty)),
547                                 Err(ErrorHandled::TooGeneric) => {
548                                     stalled_on.extend(
549                                         unevaluated
550                                             .substs
551                                             .iter()
552                                             .filter_map(TyOrConstInferVar::maybe_from_generic_arg),
553                                     );
554                                     Err(ErrorHandled::TooGeneric)
555                                 }
556                                 Err(err) => Err(err),
557                             }
558                         } else {
559                             Ok(c)
560                         }
561                     };
562
563                     match (evaluate(c1), evaluate(c2)) {
564                         (Ok(c1), Ok(c2)) => {
565                             match self
566                                 .selcx
567                                 .infcx()
568                                 .at(&obligation.cause, obligation.param_env)
569                                 .eq(c1, c2)
570                             {
571                                 Ok(_) => ProcessResult::Changed(vec![]),
572                                 Err(err) => ProcessResult::Error(
573                                     FulfillmentErrorCode::CodeConstEquateError(
574                                         ExpectedFound::new(true, c1, c2),
575                                         err,
576                                     ),
577                                 ),
578                             }
579                         }
580                         (Err(ErrorHandled::Reported(ErrorReported)), _)
581                         | (_, Err(ErrorHandled::Reported(ErrorReported))) => ProcessResult::Error(
582                             CodeSelectionError(SelectionError::NotConstEvaluatable(
583                                 NotConstEvaluatable::Error(ErrorReported),
584                             )),
585                         ),
586                         (Err(ErrorHandled::Linted), _) | (_, Err(ErrorHandled::Linted)) => {
587                             span_bug!(
588                                 obligation.cause.span(self.selcx.tcx()),
589                                 "ConstEquate: const_eval_resolve returned an unexpected error"
590                             )
591                         }
592                         (Err(ErrorHandled::TooGeneric), _) | (_, Err(ErrorHandled::TooGeneric)) => {
593                             if c1.has_infer_types_or_consts() || c2.has_infer_types_or_consts() {
594                                 ProcessResult::Unchanged
595                             } else {
596                                 // Two different constants using generic parameters ~> error.
597                                 let expected_found = ExpectedFound::new(true, c1, c2);
598                                 ProcessResult::Error(FulfillmentErrorCode::CodeConstEquateError(
599                                     expected_found,
600                                     TypeError::ConstMismatch(expected_found),
601                                 ))
602                             }
603                         }
604                     }
605                 }
606                 ty::PredicateKind::TypeWellFormedFromEnv(..) => {
607                     bug!("TypeWellFormedFromEnv is only used for Chalk")
608                 }
609             },
610         }
611     }
612
613     #[instrument(level = "debug", skip(self, obligation, stalled_on))]
614     fn process_trait_obligation(
615         &mut self,
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) {
625                 debug!(
626                     "selecting trait at depth {} evaluated to holds",
627                     obligation.recursion_depth
628                 );
629                 return ProcessResult::Changed(vec![]);
630             }
631         }
632
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()))
637             }
638             Ok(None) => {
639                 debug!("selecting trait at depth {} yielded Ok(None)", obligation.recursion_depth);
640
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.
645                 stalled_on.clear();
646                 stalled_on.extend(substs_infer_vars(
647                     self.selcx,
648                     trait_obligation.predicate.map_bound(|pred| pred.trait_ref.substs),
649                 ));
650
651                 debug!(
652                     "process_predicate: pending obligation {:?} now stalled on {:?}",
653                     infcx.resolve_vars_if_possible(obligation.clone()),
654                     stalled_on
655                 );
656
657                 ProcessResult::Unchanged
658             }
659             Err(selection_err) => {
660                 debug!("selecting trait at depth {} yielded Err", obligation.recursion_depth);
661
662                 ProcessResult::Error(CodeSelectionError(selection_err))
663             }
664         }
665     }
666
667     fn process_projection_obligation(
668         &mut self,
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)),
675             Ok(Ok(None)) => {
676                 stalled_on.clear();
677                 stalled_on.extend(substs_infer_vars(
678                     self.selcx,
679                     project_obligation.predicate.map_bound(|pred| pred.projection_ty.substs),
680                 ));
681                 ProcessResult::Unchanged
682             }
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)),
686             ])),
687             Err(e) => ProcessResult::Error(CodeProjectionError(e)),
688         }
689     }
690 }
691
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>> {
697     selcx
698         .infcx()
699         .resolve_vars_if_possible(substs)
700         .skip_binder() // ok because this check doesn't care about regions
701         .iter()
702         .filter(|arg| arg.has_infer_types_or_consts())
703         .flat_map(|arg| {
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();
709                 }
710             }
711             walker.visited.into_iter()
712         })
713         .filter_map(TyOrConstInferVar::maybe_from_generic_arg)
714 }
715
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)
725 }