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