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