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