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