]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_trait_selection/src/traits/fulfill.rs
Rollup merge of #104753 - compiler-errors:drop-tracking-var-ice, r=oli-obk
[rust.git] / compiler / rustc_trait_selection / src / traits / fulfill.rs
1 use crate::infer::{InferCtxt, TyOrConstInferVar};
2 use rustc_data_structures::fx::FxHashMap;
3 use rustc_data_structures::obligation_forest::ProcessResult;
4 use rustc_data_structures::obligation_forest::{Error, ForestObligation, Outcome};
5 use rustc_data_structures::obligation_forest::{ObligationForest, ObligationProcessor};
6 use rustc_infer::traits::ProjectionCacheKey;
7 use rustc_infer::traits::{SelectionError, TraitEngine, TraitEngineExt as _, TraitObligation};
8 use rustc_middle::mir::interpret::ErrorHandled;
9 use rustc_middle::ty::abstract_const::NotConstEvaluatable;
10 use rustc_middle::ty::error::{ExpectedFound, TypeError};
11 use rustc_middle::ty::subst::SubstsRef;
12 use rustc_middle::ty::{self, Binder, Const, Ty, TypeVisitable};
13 use std::marker::PhantomData;
14
15 use super::const_evaluatable;
16 use super::project::{self, ProjectAndUnifyResult};
17 use super::select::SelectionContext;
18 use super::wf;
19 use super::CodeAmbiguity;
20 use super::CodeProjectionError;
21 use super::CodeSelectionError;
22 use super::EvaluationResult;
23 use super::Unimplemented;
24 use super::{FulfillmentError, FulfillmentErrorCode};
25 use super::{ObligationCause, PredicateObligation};
26
27 use crate::traits::project::PolyProjectionObligation;
28 use crate::traits::project::ProjectionCacheKeyExt as _;
29 use crate::traits::query::evaluate_obligation::InferCtxtExt;
30
31 impl<'tcx> ForestObligation for PendingPredicateObligation<'tcx> {
32     /// Note that we include both the `ParamEnv` and the `Predicate`,
33     /// as the `ParamEnv` can influence whether fulfillment succeeds
34     /// or fails.
35     type CacheKey = ty::ParamEnvAnd<'tcx, ty::Predicate<'tcx>>;
36
37     fn as_cache_key(&self) -> Self::CacheKey {
38         self.obligation.param_env.and(self.obligation.predicate)
39     }
40 }
41
42 /// The fulfillment context is used to drive trait resolution. It
43 /// consists of a list of obligations that must be (eventually)
44 /// satisfied. The job is to track which are satisfied, which yielded
45 /// errors, and which are still pending. At any point, users can call
46 /// `select_where_possible`, and the fulfillment context will try to do
47 /// selection, retaining only those obligations that remain
48 /// ambiguous. This may be helpful in pushing type inference
49 /// along. Once all type inference constraints have been generated, the
50 /// method `select_all_or_error` can be used to report any remaining
51 /// ambiguous cases as errors.
52 pub struct FulfillmentContext<'tcx> {
53     // A list of all obligations that have been registered with this
54     // fulfillment context.
55     predicates: ObligationForest<PendingPredicateObligation<'tcx>>,
56
57     relationships: FxHashMap<ty::TyVid, ty::FoundRelationships>,
58
59     // Is it OK to register obligations into this infcx inside
60     // an infcx snapshot?
61     //
62     // The "primary fulfillment" in many cases in typeck lives
63     // outside of any snapshot, so any use of it inside a snapshot
64     // will lead to trouble and therefore is checked against, but
65     // other fulfillment contexts sometimes do live inside of
66     // a snapshot (they don't *straddle* a snapshot, so there
67     // is no trouble there).
68     usable_in_snapshot: bool,
69 }
70
71 #[derive(Clone, Debug)]
72 pub struct PendingPredicateObligation<'tcx> {
73     pub obligation: PredicateObligation<'tcx>,
74     // This is far more often read than modified, meaning that we
75     // should mostly optimize for reading speed, while modifying is not as relevant.
76     //
77     // For whatever reason using a boxed slice is slower than using a `Vec` here.
78     pub stalled_on: Vec<TyOrConstInferVar<'tcx>>,
79 }
80
81 // `PendingPredicateObligation` is used a lot. Make sure it doesn't unintentionally get bigger.
82 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
83 static_assert_size!(PendingPredicateObligation<'_>, 72);
84
85 impl<'a, 'tcx> FulfillmentContext<'tcx> {
86     /// Creates a new fulfillment context.
87     pub(super) fn new() -> FulfillmentContext<'tcx> {
88         FulfillmentContext {
89             predicates: ObligationForest::new(),
90             relationships: FxHashMap::default(),
91             usable_in_snapshot: false,
92         }
93     }
94
95     pub(super) fn new_in_snapshot() -> FulfillmentContext<'tcx> {
96         FulfillmentContext {
97             predicates: ObligationForest::new(),
98             relationships: FxHashMap::default(),
99             usable_in_snapshot: true,
100         }
101     }
102
103     /// Attempts to select obligations using `selcx`.
104     fn select(&mut self, selcx: SelectionContext<'a, 'tcx>) -> Vec<FulfillmentError<'tcx>> {
105         let span = debug_span!("select", obligation_forest_size = ?self.predicates.len());
106         let _enter = span.enter();
107
108         // Process pending obligations.
109         let outcome: Outcome<_, _> =
110             self.predicates.process_obligations(&mut FulfillProcessor { selcx });
111
112         // FIXME: if we kept the original cache key, we could mark projection
113         // obligations as complete for the projection cache here.
114
115         let errors: Vec<FulfillmentError<'tcx>> =
116             outcome.errors.into_iter().map(to_fulfillment_error).collect();
117
118         debug!(
119             "select({} predicates remaining, {} errors) done",
120             self.predicates.len(),
121             errors.len()
122         );
123
124         errors
125     }
126 }
127
128 impl<'tcx> TraitEngine<'tcx> for FulfillmentContext<'tcx> {
129     /// "Normalize" a projection type `<SomeType as SomeTrait>::X` by
130     /// creating a fresh type variable `$0` as well as a projection
131     /// predicate `<SomeType as SomeTrait>::X == $0`. When the
132     /// inference engine runs, it will attempt to find an impl of
133     /// `SomeTrait` or a where-clause that lets us unify `$0` with
134     /// something concrete. If this fails, we'll unify `$0` with
135     /// `projection_ty` again.
136     #[instrument(level = "debug", skip(self, infcx, param_env, cause))]
137     fn normalize_projection_type(
138         &mut self,
139         infcx: &InferCtxt<'tcx>,
140         param_env: ty::ParamEnv<'tcx>,
141         projection_ty: ty::ProjectionTy<'tcx>,
142         cause: ObligationCause<'tcx>,
143     ) -> Ty<'tcx> {
144         debug_assert!(!projection_ty.has_escaping_bound_vars());
145
146         // FIXME(#20304) -- cache
147
148         let mut selcx = SelectionContext::new(infcx);
149         let mut obligations = vec![];
150         let normalized_ty = project::normalize_projection_type(
151             &mut selcx,
152             param_env,
153             projection_ty,
154             cause,
155             0,
156             &mut obligations,
157         );
158         self.register_predicate_obligations(infcx, obligations);
159
160         debug!(?normalized_ty);
161
162         normalized_ty.ty().unwrap()
163     }
164
165     fn register_predicate_obligation(
166         &mut self,
167         infcx: &InferCtxt<'tcx>,
168         obligation: PredicateObligation<'tcx>,
169     ) {
170         // this helps to reduce duplicate errors, as well as making
171         // debug output much nicer to read and so on.
172         let obligation = infcx.resolve_vars_if_possible(obligation);
173
174         debug!(?obligation, "register_predicate_obligation");
175
176         assert!(!infcx.is_in_snapshot() || self.usable_in_snapshot);
177
178         super::relationships::update(self, infcx, &obligation);
179
180         self.predicates
181             .register_obligation(PendingPredicateObligation { obligation, stalled_on: vec![] });
182     }
183
184     fn select_all_or_error(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<FulfillmentError<'tcx>> {
185         {
186             let errors = self.select_where_possible(infcx);
187             if !errors.is_empty() {
188                 return errors;
189             }
190         }
191
192         self.predicates.to_errors(CodeAmbiguity).into_iter().map(to_fulfillment_error).collect()
193     }
194
195     fn select_where_possible(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<FulfillmentError<'tcx>> {
196         let selcx = SelectionContext::new(infcx);
197         self.select(selcx)
198     }
199
200     fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>> {
201         self.predicates.map_pending_obligations(|o| o.obligation.clone())
202     }
203
204     fn relationships(&mut self) -> &mut FxHashMap<ty::TyVid, ty::FoundRelationships> {
205         &mut self.relationships
206     }
207 }
208
209 struct FulfillProcessor<'a, 'tcx> {
210     selcx: SelectionContext<'a, 'tcx>,
211 }
212
213 fn mk_pending(os: Vec<PredicateObligation<'_>>) -> Vec<PendingPredicateObligation<'_>> {
214     os.into_iter()
215         .map(|o| PendingPredicateObligation { obligation: o, stalled_on: vec![] })
216         .collect()
217 }
218
219 impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
220     type Obligation = PendingPredicateObligation<'tcx>;
221     type Error = FulfillmentErrorCode<'tcx>;
222     type OUT = Outcome<Self::Obligation, Self::Error>;
223
224     /// Identifies whether a predicate obligation needs processing.
225     ///
226     /// This is always inlined, despite its size, because it has a single
227     /// callsite and it is called *very* frequently.
228     #[inline(always)]
229     fn needs_process_obligation(&self, pending_obligation: &Self::Obligation) -> bool {
230         // If we were stalled on some unresolved variables, first check whether
231         // any of them have been resolved; if not, don't bother doing more work
232         // yet.
233         match pending_obligation.stalled_on.len() {
234             // Match arms are in order of frequency, which matters because this
235             // code is so hot. 1 and 0 dominate; 2+ is fairly rare.
236             1 => {
237                 let infer_var = pending_obligation.stalled_on[0];
238                 self.selcx.infcx().ty_or_const_infer_var_changed(infer_var)
239             }
240             0 => {
241                 // In this case we haven't changed, but wish to make a change.
242                 true
243             }
244             _ => {
245                 // This `for` loop was once a call to `all()`, but this lower-level
246                 // form was a perf win. See #64545 for details.
247                 (|| {
248                     for &infer_var in &pending_obligation.stalled_on {
249                         if self.selcx.infcx().ty_or_const_infer_var_changed(infer_var) {
250                             return true;
251                         }
252                     }
253                     false
254                 })()
255             }
256         }
257     }
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 called much less often than `needs_process_obligation`, so we
265     /// never inline it.
266     #[inline(never)]
267     #[instrument(level = "debug", skip(self, pending_obligation))]
268     fn process_obligation(
269         &mut self,
270         pending_obligation: &mut PendingPredicateObligation<'tcx>,
271     ) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
272         pending_obligation.stalled_on.truncate(0);
273
274         let obligation = &mut pending_obligation.obligation;
275
276         debug!(?obligation, "pre-resolve");
277
278         if obligation.predicate.has_non_region_infer() {
279             obligation.predicate =
280                 self.selcx.infcx().resolve_vars_if_possible(obligation.predicate);
281         }
282
283         let obligation = &pending_obligation.obligation;
284
285         let infcx = self.selcx.infcx();
286
287         if obligation.predicate.has_projections() {
288             let mut obligations = Vec::new();
289             let predicate = crate::traits::project::try_normalize_with_depth_to(
290                 &mut self.selcx,
291                 obligation.param_env,
292                 obligation.cause.clone(),
293                 obligation.recursion_depth + 1,
294                 obligation.predicate,
295                 &mut obligations,
296             );
297             if predicate != obligation.predicate {
298                 obligations.push(obligation.with(infcx.tcx, predicate));
299                 return ProcessResult::Changed(mk_pending(obligations));
300             }
301         }
302         let binder = obligation.predicate.kind();
303         match binder.no_bound_vars() {
304             None => match binder.skip_binder() {
305                 // Evaluation will discard candidates using the leak check.
306                 // This means we need to pass it the bound version of our
307                 // predicate.
308                 ty::PredicateKind::Trait(trait_ref) => {
309                     let trait_obligation = obligation.with(infcx.tcx, binder.rebind(trait_ref));
310
311                     self.process_trait_obligation(
312                         obligation,
313                         trait_obligation,
314                         &mut pending_obligation.stalled_on,
315                     )
316                 }
317                 ty::PredicateKind::Projection(data) => {
318                     let project_obligation = obligation.with(infcx.tcx, binder.rebind(data));
319
320                     self.process_projection_obligation(
321                         obligation,
322                         project_obligation,
323                         &mut pending_obligation.stalled_on,
324                     )
325                 }
326                 ty::PredicateKind::RegionOutlives(_)
327                 | ty::PredicateKind::TypeOutlives(_)
328                 | ty::PredicateKind::WellFormed(_)
329                 | ty::PredicateKind::ObjectSafe(_)
330                 | ty::PredicateKind::ClosureKind(..)
331                 | ty::PredicateKind::Subtype(_)
332                 | ty::PredicateKind::Coerce(_)
333                 | ty::PredicateKind::ConstEvaluatable(..)
334                 | ty::PredicateKind::ConstEquate(..) => {
335                     let pred =
336                         ty::Binder::dummy(infcx.replace_bound_vars_with_placeholders(binder));
337                     ProcessResult::Changed(mk_pending(vec![obligation.with(infcx.tcx, pred)]))
338                 }
339                 ty::PredicateKind::Ambiguous => ProcessResult::Unchanged,
340                 ty::PredicateKind::TypeWellFormedFromEnv(..) => {
341                     bug!("TypeWellFormedFromEnv is only used for Chalk")
342                 }
343             },
344             Some(pred) => match pred {
345                 ty::PredicateKind::Trait(data) => {
346                     let trait_obligation = obligation.with(infcx.tcx, Binder::dummy(data));
347
348                     self.process_trait_obligation(
349                         obligation,
350                         trait_obligation,
351                         &mut pending_obligation.stalled_on,
352                     )
353                 }
354
355                 ty::PredicateKind::RegionOutlives(data) => {
356                     if infcx.considering_regions {
357                         infcx.region_outlives_predicate(&obligation.cause, Binder::dummy(data));
358                     }
359
360                     ProcessResult::Changed(vec![])
361                 }
362
363                 ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(t_a, r_b)) => {
364                     if infcx.considering_regions {
365                         infcx.register_region_obligation_with_cause(t_a, r_b, &obligation.cause);
366                     }
367                     ProcessResult::Changed(vec![])
368                 }
369
370                 ty::PredicateKind::Projection(ref data) => {
371                     let project_obligation = obligation.with(infcx.tcx, Binder::dummy(*data));
372
373                     self.process_projection_obligation(
374                         obligation,
375                         project_obligation,
376                         &mut pending_obligation.stalled_on,
377                     )
378                 }
379
380                 ty::PredicateKind::ObjectSafe(trait_def_id) => {
381                     if !self.selcx.tcx().is_object_safe(trait_def_id) {
382                         ProcessResult::Error(CodeSelectionError(Unimplemented))
383                     } else {
384                         ProcessResult::Changed(vec![])
385                     }
386                 }
387
388                 ty::PredicateKind::ClosureKind(_, closure_substs, kind) => {
389                     match self.selcx.infcx().closure_kind(closure_substs) {
390                         Some(closure_kind) => {
391                             if closure_kind.extends(kind) {
392                                 ProcessResult::Changed(vec![])
393                             } else {
394                                 ProcessResult::Error(CodeSelectionError(Unimplemented))
395                             }
396                         }
397                         None => ProcessResult::Unchanged,
398                     }
399                 }
400
401                 ty::PredicateKind::WellFormed(arg) => {
402                     match wf::obligations(
403                         self.selcx.infcx(),
404                         obligation.param_env,
405                         obligation.cause.body_id,
406                         obligation.recursion_depth + 1,
407                         arg,
408                         obligation.cause.span,
409                     ) {
410                         None => {
411                             pending_obligation.stalled_on =
412                                 vec![TyOrConstInferVar::maybe_from_generic_arg(arg).unwrap()];
413                             ProcessResult::Unchanged
414                         }
415                         Some(os) => ProcessResult::Changed(mk_pending(os)),
416                     }
417                 }
418
419                 ty::PredicateKind::Subtype(subtype) => {
420                     match self.selcx.infcx().subtype_predicate(
421                         &obligation.cause,
422                         obligation.param_env,
423                         Binder::dummy(subtype),
424                     ) {
425                         Err((a, b)) => {
426                             // None means that both are unresolved.
427                             pending_obligation.stalled_on =
428                                 vec![TyOrConstInferVar::Ty(a), TyOrConstInferVar::Ty(b)];
429                             ProcessResult::Unchanged
430                         }
431                         Ok(Ok(ok)) => ProcessResult::Changed(mk_pending(ok.obligations)),
432                         Ok(Err(err)) => {
433                             let expected_found =
434                                 ExpectedFound::new(subtype.a_is_expected, subtype.a, subtype.b);
435                             ProcessResult::Error(FulfillmentErrorCode::CodeSubtypeError(
436                                 expected_found,
437                                 err,
438                             ))
439                         }
440                     }
441                 }
442
443                 ty::PredicateKind::Coerce(coerce) => {
444                     match self.selcx.infcx().coerce_predicate(
445                         &obligation.cause,
446                         obligation.param_env,
447                         Binder::dummy(coerce),
448                     ) {
449                         Err((a, b)) => {
450                             // None means that both are unresolved.
451                             pending_obligation.stalled_on =
452                                 vec![TyOrConstInferVar::Ty(a), TyOrConstInferVar::Ty(b)];
453                             ProcessResult::Unchanged
454                         }
455                         Ok(Ok(ok)) => ProcessResult::Changed(mk_pending(ok.obligations)),
456                         Ok(Err(err)) => {
457                             let expected_found = ExpectedFound::new(false, coerce.a, coerce.b);
458                             ProcessResult::Error(FulfillmentErrorCode::CodeSubtypeError(
459                                 expected_found,
460                                 err,
461                             ))
462                         }
463                     }
464                 }
465
466                 ty::PredicateKind::ConstEvaluatable(uv) => {
467                     match const_evaluatable::is_const_evaluatable(
468                         self.selcx.infcx(),
469                         uv,
470                         obligation.param_env,
471                         obligation.cause.span,
472                     ) {
473                         Ok(()) => ProcessResult::Changed(vec![]),
474                         Err(NotConstEvaluatable::MentionsInfer) => {
475                             pending_obligation.stalled_on.clear();
476                             pending_obligation.stalled_on.extend(
477                                 uv.walk().filter_map(TyOrConstInferVar::maybe_from_generic_arg),
478                             );
479                             ProcessResult::Unchanged
480                         }
481                         Err(
482                             e @ NotConstEvaluatable::MentionsParam
483                             | e @ NotConstEvaluatable::Error(_),
484                         ) => ProcessResult::Error(CodeSelectionError(
485                             SelectionError::NotConstEvaluatable(e),
486                         )),
487                     }
488                 }
489
490                 ty::PredicateKind::ConstEquate(c1, c2) => {
491                     assert!(
492                         self.selcx.tcx().features().generic_const_exprs,
493                         "`ConstEquate` without a feature gate: {c1:?} {c2:?}",
494                     );
495                     debug!(?c1, ?c2, "equating consts");
496                     // FIXME: we probably should only try to unify abstract constants
497                     // if the constants depend on generic parameters.
498                     //
499                     // Let's just see where this breaks :shrug:
500                     if let (ty::ConstKind::Unevaluated(a), ty::ConstKind::Unevaluated(b)) =
501                         (c1.kind(), c2.kind())
502                     {
503                         if infcx.try_unify_abstract_consts(a, b, obligation.param_env) {
504                             return ProcessResult::Changed(vec![]);
505                         }
506                     }
507
508                     let stalled_on = &mut pending_obligation.stalled_on;
509
510                     let mut evaluate = |c: Const<'tcx>| {
511                         if let ty::ConstKind::Unevaluated(unevaluated) = c.kind() {
512                             match self.selcx.infcx().try_const_eval_resolve(
513                                 obligation.param_env,
514                                 unevaluated,
515                                 c.ty(),
516                                 Some(obligation.cause.span),
517                             ) {
518                                 Ok(val) => Ok(val),
519                                 Err(e) => match e {
520                                     ErrorHandled::TooGeneric => {
521                                         stalled_on.extend(
522                                             unevaluated.substs.iter().filter_map(
523                                                 TyOrConstInferVar::maybe_from_generic_arg,
524                                             ),
525                                         );
526                                         Err(ErrorHandled::TooGeneric)
527                                     }
528                                     _ => Err(e),
529                                 },
530                             }
531                         } else {
532                             Ok(c)
533                         }
534                     };
535
536                     match (evaluate(c1), evaluate(c2)) {
537                         (Ok(c1), Ok(c2)) => {
538                             match self
539                                 .selcx
540                                 .infcx()
541                                 .at(&obligation.cause, obligation.param_env)
542                                 .eq(c1, c2)
543                             {
544                                 Ok(_) => ProcessResult::Changed(vec![]),
545                                 Err(err) => ProcessResult::Error(
546                                     FulfillmentErrorCode::CodeConstEquateError(
547                                         ExpectedFound::new(true, c1, c2),
548                                         err,
549                                     ),
550                                 ),
551                             }
552                         }
553                         (Err(ErrorHandled::Reported(reported)), _)
554                         | (_, Err(ErrorHandled::Reported(reported))) => ProcessResult::Error(
555                             CodeSelectionError(SelectionError::NotConstEvaluatable(
556                                 NotConstEvaluatable::Error(reported),
557                             )),
558                         ),
559                         (Err(ErrorHandled::TooGeneric), _) | (_, Err(ErrorHandled::TooGeneric)) => {
560                             if c1.has_non_region_infer() || c2.has_non_region_infer() {
561                                 ProcessResult::Unchanged
562                             } else {
563                                 // Two different constants using generic parameters ~> error.
564                                 let expected_found = ExpectedFound::new(true, c1, c2);
565                                 ProcessResult::Error(FulfillmentErrorCode::CodeConstEquateError(
566                                     expected_found,
567                                     TypeError::ConstMismatch(expected_found),
568                                 ))
569                             }
570                         }
571                     }
572                 }
573                 ty::PredicateKind::Ambiguous => ProcessResult::Unchanged,
574                 ty::PredicateKind::TypeWellFormedFromEnv(..) => {
575                     bug!("TypeWellFormedFromEnv is only used for Chalk")
576                 }
577             },
578         }
579     }
580
581     #[inline(never)]
582     fn process_backedge<'c, I>(
583         &mut self,
584         cycle: I,
585         _marker: PhantomData<&'c PendingPredicateObligation<'tcx>>,
586     ) -> Result<(), FulfillmentErrorCode<'tcx>>
587     where
588         I: Clone + Iterator<Item = &'c PendingPredicateObligation<'tcx>>,
589     {
590         if self.selcx.coinductive_match(cycle.clone().map(|s| s.obligation.predicate)) {
591             debug!("process_child_obligations: coinductive match");
592             Ok(())
593         } else {
594             let cycle: Vec<_> = cycle.map(|c| c.obligation.clone()).collect();
595             Err(FulfillmentErrorCode::CodeCycle(cycle))
596         }
597     }
598 }
599
600 impl<'a, 'tcx> FulfillProcessor<'a, 'tcx> {
601     #[instrument(level = "debug", skip(self, obligation, stalled_on))]
602     fn process_trait_obligation(
603         &mut self,
604         obligation: &PredicateObligation<'tcx>,
605         trait_obligation: TraitObligation<'tcx>,
606         stalled_on: &mut Vec<TyOrConstInferVar<'tcx>>,
607     ) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
608         let infcx = self.selcx.infcx();
609         if obligation.predicate.is_global() {
610             // no type variables present, can use evaluation for better caching.
611             // FIXME: consider caching errors too.
612             if infcx.predicate_must_hold_considering_regions(obligation) {
613                 debug!(
614                     "selecting trait at depth {} evaluated to holds",
615                     obligation.recursion_depth
616                 );
617                 return ProcessResult::Changed(vec![]);
618             }
619         }
620
621         match self.selcx.select(&trait_obligation) {
622             Ok(Some(impl_source)) => {
623                 debug!("selecting trait at depth {} yielded Ok(Some)", obligation.recursion_depth);
624                 ProcessResult::Changed(mk_pending(impl_source.nested_obligations()))
625             }
626             Ok(None) => {
627                 debug!("selecting trait at depth {} yielded Ok(None)", obligation.recursion_depth);
628
629                 // This is a bit subtle: for the most part, the
630                 // only reason we can fail to make progress on
631                 // trait selection is because we don't have enough
632                 // information about the types in the trait.
633                 stalled_on.clear();
634                 stalled_on.extend(substs_infer_vars(
635                     &self.selcx,
636                     trait_obligation.predicate.map_bound(|pred| pred.trait_ref.substs),
637                 ));
638
639                 debug!(
640                     "process_predicate: pending obligation {:?} now stalled on {:?}",
641                     infcx.resolve_vars_if_possible(obligation.clone()),
642                     stalled_on
643                 );
644
645                 ProcessResult::Unchanged
646             }
647             Err(selection_err) => {
648                 debug!("selecting trait at depth {} yielded Err", obligation.recursion_depth);
649
650                 ProcessResult::Error(CodeSelectionError(selection_err))
651             }
652         }
653     }
654
655     fn process_projection_obligation(
656         &mut self,
657         obligation: &PredicateObligation<'tcx>,
658         project_obligation: PolyProjectionObligation<'tcx>,
659         stalled_on: &mut Vec<TyOrConstInferVar<'tcx>>,
660     ) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
661         let tcx = self.selcx.tcx();
662
663         if obligation.predicate.is_global() {
664             // no type variables present, can use evaluation for better caching.
665             // FIXME: consider caching errors too.
666             if self.selcx.infcx().predicate_must_hold_considering_regions(obligation) {
667                 if let Some(key) = ProjectionCacheKey::from_poly_projection_predicate(
668                     &mut self.selcx,
669                     project_obligation.predicate,
670                 ) {
671                     // If `predicate_must_hold_considering_regions` succeeds, then we've
672                     // evaluated all sub-obligations. We can therefore mark the 'root'
673                     // obligation as complete, and skip evaluating sub-obligations.
674                     self.selcx
675                         .infcx()
676                         .inner
677                         .borrow_mut()
678                         .projection_cache()
679                         .complete(key, EvaluationResult::EvaluatedToOk);
680                 }
681                 return ProcessResult::Changed(vec![]);
682             } else {
683                 debug!("Does NOT hold: {:?}", obligation);
684             }
685         }
686
687         match project::poly_project_and_unify_type(&mut self.selcx, &project_obligation) {
688             ProjectAndUnifyResult::Holds(os) => ProcessResult::Changed(mk_pending(os)),
689             ProjectAndUnifyResult::FailedNormalization => {
690                 stalled_on.clear();
691                 stalled_on.extend(substs_infer_vars(
692                     &self.selcx,
693                     project_obligation.predicate.map_bound(|pred| pred.projection_ty.substs),
694                 ));
695                 ProcessResult::Unchanged
696             }
697             // Let the caller handle the recursion
698             ProjectAndUnifyResult::Recursive => ProcessResult::Changed(mk_pending(vec![
699                 project_obligation.with(tcx, project_obligation.predicate),
700             ])),
701             ProjectAndUnifyResult::MismatchedProjectionTypes(e) => {
702                 ProcessResult::Error(CodeProjectionError(e))
703             }
704         }
705     }
706 }
707
708 /// Returns the set of inference variables contained in `substs`.
709 fn substs_infer_vars<'a, 'tcx>(
710     selcx: &SelectionContext<'a, 'tcx>,
711     substs: ty::Binder<'tcx, SubstsRef<'tcx>>,
712 ) -> impl Iterator<Item = TyOrConstInferVar<'tcx>> {
713     selcx
714         .infcx()
715         .resolve_vars_if_possible(substs)
716         .skip_binder() // ok because this check doesn't care about regions
717         .iter()
718         .filter(|arg| arg.has_non_region_infer())
719         .flat_map(|arg| {
720             let mut walker = arg.walk();
721             while let Some(c) = walker.next() {
722                 if !c.has_non_region_infer() {
723                     walker.visited.remove(&c);
724                     walker.skip_current_subtree();
725                 }
726             }
727             walker.visited.into_iter()
728         })
729         .filter_map(TyOrConstInferVar::maybe_from_generic_arg)
730 }
731
732 fn to_fulfillment_error<'tcx>(
733     error: Error<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>>,
734 ) -> FulfillmentError<'tcx> {
735     let mut iter = error.backtrace.into_iter();
736     let obligation = iter.next().unwrap().obligation;
737     // The root obligation is the last item in the backtrace - if there's only
738     // one item, then it's the same as the main obligation
739     let root_obligation = iter.next_back().map_or_else(|| obligation.clone(), |e| e.obligation);
740     FulfillmentError::new(obligation, error.error, root_obligation)
741 }