rustc_trait_selection/traits/
fulfill.rs

1use std::marker::PhantomData;
2
3use rustc_data_structures::obligation_forest::{
4    Error, ForestObligation, ObligationForest, ObligationProcessor, Outcome, ProcessResult,
5};
6use rustc_infer::infer::DefineOpaqueTypes;
7use rustc_infer::traits::{
8    FromSolverError, PolyTraitObligation, PredicateObligations, ProjectionCacheKey, SelectionError,
9    TraitEngine,
10};
11use rustc_middle::bug;
12use rustc_middle::ty::abstract_const::NotConstEvaluatable;
13use rustc_middle::ty::error::{ExpectedFound, TypeError};
14use rustc_middle::ty::{self, Binder, Const, GenericArgsRef, TypeVisitableExt, TypingMode};
15use thin_vec::ThinVec;
16use tracing::{debug, debug_span, instrument};
17
18use super::effects::{self, HostEffectObligation};
19use super::project::{self, ProjectAndUnifyResult};
20use super::select::SelectionContext;
21use super::{
22    EvaluationResult, FulfillmentError, FulfillmentErrorCode, PredicateObligation,
23    ScrubbedTraitError, Unimplemented, const_evaluatable, wf,
24};
25use crate::error_reporting::InferCtxtErrorExt;
26use crate::infer::{InferCtxt, TyOrConstInferVar};
27use crate::traits::normalize::normalize_with_depth_to;
28use crate::traits::project::{PolyProjectionObligation, ProjectionCacheKeyExt as _};
29use crate::traits::query::evaluate_obligation::InferCtxtExt;
30use crate::traits::{EvaluateConstErr, sizedness_fast_path};
31
32pub(crate) type PendingPredicateObligations<'tcx> = ThinVec<PendingPredicateObligation<'tcx>>;
33
34impl<'tcx> ForestObligation for PendingPredicateObligation<'tcx> {
35    /// Note that we include both the `ParamEnv` and the `Predicate`,
36    /// as the `ParamEnv` can influence whether fulfillment succeeds
37    /// or fails.
38    type CacheKey = ty::ParamEnvAnd<'tcx, ty::Predicate<'tcx>>;
39
40    fn as_cache_key(&self) -> Self::CacheKey {
41        self.obligation.param_env.and(self.obligation.predicate)
42    }
43}
44
45/// The fulfillment context is used to drive trait resolution. It
46/// consists of a list of obligations that must be (eventually)
47/// satisfied. The job is to track which are satisfied, which yielded
48/// errors, and which are still pending. At any point, users can call
49/// `select_where_possible`, and the fulfillment context will try to do
50/// selection, retaining only those obligations that remain
51/// ambiguous. This may be helpful in pushing type inference
52/// along. Once all type inference constraints have been generated, the
53/// method `select_all_or_error` can be used to report any remaining
54/// ambiguous cases as errors.
55pub struct FulfillmentContext<'tcx, E: 'tcx> {
56    /// A list of all obligations that have been registered with this
57    /// fulfillment context.
58    predicates: ObligationForest<PendingPredicateObligation<'tcx>>,
59
60    /// The snapshot in which this context was created. Using the context
61    /// outside of this snapshot leads to subtle bugs if the snapshot
62    /// gets rolled back. Because of this we explicitly check that we only
63    /// use the context in exactly this snapshot.
64    usable_in_snapshot: usize,
65
66    _errors: PhantomData<E>,
67}
68
69#[derive(Clone, Debug)]
70pub struct PendingPredicateObligation<'tcx> {
71    pub obligation: PredicateObligation<'tcx>,
72    // This is far more often read than modified, meaning that we
73    // should mostly optimize for reading speed, while modifying is not as relevant.
74    //
75    // For whatever reason using a boxed slice is slower than using a `Vec` here.
76    pub stalled_on: Vec<TyOrConstInferVar>,
77}
78
79// `PendingPredicateObligation` is used a lot. Make sure it doesn't unintentionally get bigger.
80#[cfg(target_pointer_width = "64")]
81rustc_data_structures::static_assert_size!(PendingPredicateObligation<'_>, 72);
82
83impl<'tcx, E> FulfillmentContext<'tcx, E>
84where
85    E: FromSolverError<'tcx, OldSolverError<'tcx>>,
86{
87    /// Creates a new fulfillment context.
88    pub(super) fn new(infcx: &InferCtxt<'tcx>) -> FulfillmentContext<'tcx, E> {
89        assert!(
90            !infcx.next_trait_solver(),
91            "old trait solver fulfillment context created when \
92            infcx is set up for new trait solver"
93        );
94        FulfillmentContext {
95            predicates: ObligationForest::new(),
96            usable_in_snapshot: infcx.num_open_snapshots(),
97            _errors: PhantomData,
98        }
99    }
100
101    /// Attempts to select obligations using `selcx`.
102    fn select(&mut self, selcx: SelectionContext<'_, 'tcx>) -> Vec<E> {
103        let span = debug_span!("select", obligation_forest_size = ?self.predicates.len());
104        let _enter = span.enter();
105        let infcx = selcx.infcx;
106
107        // Process pending obligations.
108        let outcome: Outcome<_, _> =
109            self.predicates.process_obligations(&mut FulfillProcessor { selcx });
110
111        // FIXME: if we kept the original cache key, we could mark projection
112        // obligations as complete for the projection cache here.
113
114        let errors: Vec<E> = outcome
115            .errors
116            .into_iter()
117            .map(|err| E::from_solver_error(infcx, OldSolverError(err)))
118            .collect();
119
120        debug!(
121            "select({} predicates remaining, {} errors) done",
122            self.predicates.len(),
123            errors.len()
124        );
125
126        errors
127    }
128}
129
130impl<'tcx, E> TraitEngine<'tcx, E> for FulfillmentContext<'tcx, E>
131where
132    E: FromSolverError<'tcx, OldSolverError<'tcx>>,
133{
134    #[inline]
135    fn register_predicate_obligation(
136        &mut self,
137        infcx: &InferCtxt<'tcx>,
138        mut obligation: PredicateObligation<'tcx>,
139    ) {
140        assert_eq!(self.usable_in_snapshot, infcx.num_open_snapshots());
141        // this helps to reduce duplicate errors, as well as making
142        // debug output much nicer to read and so on.
143        debug_assert!(!obligation.param_env.has_non_region_infer());
144        obligation.predicate = infcx.resolve_vars_if_possible(obligation.predicate);
145
146        debug!(?obligation, "register_predicate_obligation");
147
148        self.predicates
149            .register_obligation(PendingPredicateObligation { obligation, stalled_on: vec![] });
150    }
151
152    fn collect_remaining_errors(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<E> {
153        self.predicates
154            .to_errors(FulfillmentErrorCode::Ambiguity { overflow: None })
155            .into_iter()
156            .map(|err| E::from_solver_error(infcx, OldSolverError(err)))
157            .collect()
158    }
159
160    fn select_where_possible(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<E> {
161        let selcx = SelectionContext::new(infcx);
162        self.select(selcx)
163    }
164
165    fn drain_unstalled_obligations(
166        &mut self,
167        infcx: &InferCtxt<'tcx>,
168    ) -> PredicateObligations<'tcx> {
169        let mut processor =
170            DrainProcessor { removed_predicates: PredicateObligations::new(), infcx };
171        let outcome: Outcome<_, _> = self.predicates.process_obligations(&mut processor);
172        assert!(outcome.errors.is_empty());
173        return processor.removed_predicates;
174
175        struct DrainProcessor<'a, 'tcx> {
176            infcx: &'a InferCtxt<'tcx>,
177            removed_predicates: PredicateObligations<'tcx>,
178        }
179
180        impl<'tcx> ObligationProcessor for DrainProcessor<'_, 'tcx> {
181            type Obligation = PendingPredicateObligation<'tcx>;
182            type Error = !;
183            type OUT = Outcome<Self::Obligation, Self::Error>;
184
185            fn needs_process_obligation(&self, pending_obligation: &Self::Obligation) -> bool {
186                pending_obligation
187                    .stalled_on
188                    .iter()
189                    .any(|&var| self.infcx.ty_or_const_infer_var_changed(var))
190            }
191
192            fn process_obligation(
193                &mut self,
194                pending_obligation: &mut PendingPredicateObligation<'tcx>,
195            ) -> ProcessResult<PendingPredicateObligation<'tcx>, !> {
196                assert!(self.needs_process_obligation(pending_obligation));
197                self.removed_predicates.push(pending_obligation.obligation.clone());
198                ProcessResult::Changed(Default::default())
199            }
200
201            fn process_backedge<'c, I>(
202                &mut self,
203                cycle: I,
204                _marker: PhantomData<&'c PendingPredicateObligation<'tcx>>,
205            ) -> Result<(), !>
206            where
207                I: Clone + Iterator<Item = &'c PendingPredicateObligation<'tcx>>,
208            {
209                self.removed_predicates.extend(cycle.map(|c| c.obligation.clone()));
210                Ok(())
211            }
212        }
213    }
214
215    fn has_pending_obligations(&self) -> bool {
216        self.predicates.has_pending_obligations()
217    }
218
219    fn pending_obligations(&self) -> PredicateObligations<'tcx> {
220        self.predicates.map_pending_obligations(|o| o.obligation.clone())
221    }
222}
223
224struct FulfillProcessor<'a, 'tcx> {
225    selcx: SelectionContext<'a, 'tcx>,
226}
227
228fn mk_pending<'tcx>(
229    parent: &PredicateObligation<'tcx>,
230    os: PredicateObligations<'tcx>,
231) -> PendingPredicateObligations<'tcx> {
232    os.into_iter()
233        .map(|mut o| {
234            o.set_depth_from_parent(parent.recursion_depth);
235            PendingPredicateObligation { obligation: o, stalled_on: vec![] }
236        })
237        .collect()
238}
239
240impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
241    type Obligation = PendingPredicateObligation<'tcx>;
242    type Error = FulfillmentErrorCode<'tcx>;
243    type OUT = Outcome<Self::Obligation, Self::Error>;
244
245    /// Compared to `needs_process_obligation` this and its callees
246    /// contain some optimizations that come at the price of false negatives.
247    ///
248    /// They
249    /// - reduce branching by covering only the most common case
250    /// - take a read-only view of the unification tables which allows skipping undo_log
251    ///   construction.
252    /// - bail out on value-cache misses in ena to avoid pointer chasing
253    /// - hoist RefCell locking out of the loop
254    #[inline]
255    fn skippable_obligations<'b>(
256        &'b self,
257        it: impl Iterator<Item = &'b Self::Obligation>,
258    ) -> usize {
259        let is_unchanged = self.selcx.infcx.is_ty_infer_var_definitely_unchanged();
260
261        it.take_while(|o| match o.stalled_on.as_slice() {
262            [o] => is_unchanged(*o),
263            _ => false,
264        })
265        .count()
266    }
267
268    /// Identifies whether a predicate obligation needs processing.
269    ///
270    /// This is always inlined because it has a single callsite and it is
271    /// called *very* frequently. Be careful modifying this code! Several
272    /// compile-time benchmarks are very sensitive to even small changes.
273    #[inline(always)]
274    fn needs_process_obligation(&self, pending_obligation: &Self::Obligation) -> bool {
275        // If we were stalled on some unresolved variables, first check whether
276        // any of them have been resolved; if not, don't bother doing more work
277        // yet.
278        let stalled_on = &pending_obligation.stalled_on;
279        match stalled_on.len() {
280            // This case is the hottest most of the time, being hit up to 99%
281            // of the time. `keccak` and `cranelift-codegen-0.82.1` are
282            // benchmarks that particularly stress this path.
283            1 => self.selcx.infcx.ty_or_const_infer_var_changed(stalled_on[0]),
284
285            // In this case we haven't changed, but wish to make a change. Note
286            // that this is a special case, and is not equivalent to the `_`
287            // case below, which would return `false` for an empty `stalled_on`
288            // vector.
289            //
290            // This case is usually hit only 1% of the time or less, though it
291            // reaches 20% in `wasmparser-0.101.0`.
292            0 => true,
293
294            // This case is usually hit only 1% of the time or less, though it
295            // reaches 95% in `mime-0.3.16`, 64% in `wast-54.0.0`, and 12% in
296            // `inflate-0.4.5`.
297            //
298            // The obvious way of writing this, with a call to `any()` and no
299            // closure, is currently slower than this version.
300            _ => (|| {
301                for &infer_var in stalled_on {
302                    if self.selcx.infcx.ty_or_const_infer_var_changed(infer_var) {
303                        return true;
304                    }
305                }
306                false
307            })(),
308        }
309    }
310
311    /// Processes a predicate obligation and returns either:
312    /// - `Changed(v)` if the predicate is true, presuming that `v` are also true
313    /// - `Unchanged` if we don't have enough info to be sure
314    /// - `Error(e)` if the predicate does not hold
315    ///
316    /// This is called much less often than `needs_process_obligation`, so we
317    /// never inline it.
318    #[inline(never)]
319    #[instrument(level = "debug", skip(self, pending_obligation))]
320    fn process_obligation(
321        &mut self,
322        pending_obligation: &mut PendingPredicateObligation<'tcx>,
323    ) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
324        pending_obligation.stalled_on.truncate(0);
325
326        let obligation = &mut pending_obligation.obligation;
327
328        debug!(?obligation, "pre-resolve");
329
330        if obligation.predicate.has_non_region_infer() {
331            obligation.predicate = self.selcx.infcx.resolve_vars_if_possible(obligation.predicate);
332        }
333
334        let obligation = &pending_obligation.obligation;
335
336        let infcx = self.selcx.infcx;
337
338        if sizedness_fast_path(infcx.tcx, obligation.predicate) {
339            return ProcessResult::Changed(thin_vec::thin_vec![]);
340        }
341
342        if obligation.predicate.has_aliases() {
343            let mut obligations = PredicateObligations::new();
344            let predicate = normalize_with_depth_to(
345                &mut self.selcx,
346                obligation.param_env,
347                obligation.cause.clone(),
348                obligation.recursion_depth + 1,
349                obligation.predicate,
350                &mut obligations,
351            );
352            if predicate != obligation.predicate {
353                obligations.push(obligation.with(infcx.tcx, predicate));
354                return ProcessResult::Changed(mk_pending(obligation, obligations));
355            }
356        }
357        let binder = obligation.predicate.kind();
358        match binder.no_bound_vars() {
359            None => match binder.skip_binder() {
360                // Evaluation will discard candidates using the leak check.
361                // This means we need to pass it the bound version of our
362                // predicate.
363                ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_ref)) => {
364                    let trait_obligation = obligation.with(infcx.tcx, binder.rebind(trait_ref));
365
366                    self.process_trait_obligation(
367                        obligation,
368                        trait_obligation,
369                        &mut pending_obligation.stalled_on,
370                    )
371                }
372                ty::PredicateKind::Clause(ty::ClauseKind::Projection(data)) => {
373                    let project_obligation = obligation.with(infcx.tcx, binder.rebind(data));
374
375                    self.process_projection_obligation(
376                        obligation,
377                        project_obligation,
378                        &mut pending_obligation.stalled_on,
379                    )
380                }
381                ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(_))
382                | ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(_))
383                | ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(..))
384                | ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(_))
385                | ty::PredicateKind::DynCompatible(_)
386                | ty::PredicateKind::Subtype(_)
387                | ty::PredicateKind::Coerce(_)
388                | ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(..))
389                | ty::PredicateKind::ConstEquate(..)
390                // FIXME(const_trait_impl): We may need to do this using the higher-ranked
391                // pred instead of just instantiating it with placeholders b/c of
392                // higher-ranked implied bound issues in the old solver.
393                | ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(..)) => {
394                    let pred = ty::Binder::dummy(infcx.enter_forall_and_leak_universe(binder));
395                    let mut obligations = PredicateObligations::with_capacity(1);
396                    obligations.push(obligation.with(infcx.tcx, pred));
397
398                    ProcessResult::Changed(mk_pending(obligation, obligations))
399                }
400                ty::PredicateKind::Ambiguous => ProcessResult::Unchanged,
401                ty::PredicateKind::NormalizesTo(..) => {
402                    bug!("NormalizesTo is only used by the new solver")
403                }
404                ty::PredicateKind::AliasRelate(..) => {
405                    bug!("AliasRelate is only used by the new solver")
406                }
407            },
408            Some(pred) => match pred {
409                ty::PredicateKind::Clause(ty::ClauseKind::Trait(data)) => {
410                    let trait_obligation = obligation.with(infcx.tcx, Binder::dummy(data));
411
412                    self.process_trait_obligation(
413                        obligation,
414                        trait_obligation,
415                        &mut pending_obligation.stalled_on,
416                    )
417                }
418
419                ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(data)) => {
420                    let host_obligation = obligation.with(infcx.tcx, data);
421
422                    self.process_host_obligation(
423                        obligation,
424                        host_obligation,
425                        &mut pending_obligation.stalled_on,
426                    )
427                }
428
429                ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(data)) => {
430                    if infcx.considering_regions {
431                        infcx.region_outlives_predicate(&obligation.cause, Binder::dummy(data));
432                    }
433
434                    ProcessResult::Changed(Default::default())
435                }
436
437                ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(
438                    t_a,
439                    r_b,
440                ))) => {
441                    if infcx.considering_regions {
442                        infcx.register_region_obligation_with_cause(t_a, r_b, &obligation.cause);
443                    }
444                    ProcessResult::Changed(Default::default())
445                }
446
447                ty::PredicateKind::Clause(ty::ClauseKind::Projection(ref data)) => {
448                    let project_obligation = obligation.with(infcx.tcx, Binder::dummy(*data));
449
450                    self.process_projection_obligation(
451                        obligation,
452                        project_obligation,
453                        &mut pending_obligation.stalled_on,
454                    )
455                }
456
457                ty::PredicateKind::DynCompatible(trait_def_id) => {
458                    if !self.selcx.tcx().is_dyn_compatible(trait_def_id) {
459                        ProcessResult::Error(FulfillmentErrorCode::Select(Unimplemented))
460                    } else {
461                        ProcessResult::Changed(Default::default())
462                    }
463                }
464
465                ty::PredicateKind::Ambiguous => ProcessResult::Unchanged,
466                ty::PredicateKind::NormalizesTo(..) => {
467                    bug!("NormalizesTo is only used by the new solver")
468                }
469                ty::PredicateKind::AliasRelate(..) => {
470                    bug!("AliasRelate is only used by the new solver")
471                }
472                // Compute `ConstArgHasType` above the overflow check below.
473                // This is because this is not ever a useful obligation to report
474                // as the cause of an overflow.
475                ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, ty)) => {
476                    let ct = infcx.shallow_resolve_const(ct);
477                    let ct_ty = match ct.kind() {
478                        ty::ConstKind::Infer(var) => {
479                            let var = match var {
480                                ty::InferConst::Var(vid) => TyOrConstInferVar::Const(vid),
481                                ty::InferConst::Fresh(_) => {
482                                    bug!("encountered fresh const in fulfill")
483                                }
484                            };
485                            pending_obligation.stalled_on.clear();
486                            pending_obligation.stalled_on.extend([var]);
487                            return ProcessResult::Unchanged;
488                        }
489                        ty::ConstKind::Error(_) => {
490                            return ProcessResult::Changed(PendingPredicateObligations::new());
491                        }
492                        ty::ConstKind::Value(cv) => cv.ty,
493                        ty::ConstKind::Unevaluated(uv) => {
494                            infcx.tcx.type_of(uv.def).instantiate(infcx.tcx, uv.args)
495                        }
496                        // FIXME(generic_const_exprs): we should construct an alias like
497                        // `<lhs_ty as Add<rhs_ty>>::Output` when this is an `Expr` representing
498                        // `lhs + rhs`.
499                        ty::ConstKind::Expr(_) => {
500                            return ProcessResult::Changed(mk_pending(
501                                obligation,
502                                PredicateObligations::new(),
503                            ));
504                        }
505                        ty::ConstKind::Placeholder(_) => {
506                            bug!("placeholder const {:?} in old solver", ct)
507                        }
508                        ty::ConstKind::Bound(_, _) => bug!("escaping bound vars in {:?}", ct),
509                        ty::ConstKind::Param(param_ct) => {
510                            param_ct.find_ty_from_env(obligation.param_env)
511                        }
512                    };
513
514                    match infcx.at(&obligation.cause, obligation.param_env).eq(
515                        // Only really exercised by generic_const_exprs
516                        DefineOpaqueTypes::Yes,
517                        ct_ty,
518                        ty,
519                    ) {
520                        Ok(inf_ok) => ProcessResult::Changed(mk_pending(
521                            obligation,
522                            inf_ok.into_obligations(),
523                        )),
524                        Err(_) => ProcessResult::Error(FulfillmentErrorCode::Select(
525                            SelectionError::ConstArgHasWrongType { ct, ct_ty, expected_ty: ty },
526                        )),
527                    }
528                }
529
530                // General case overflow check. Allow `process_trait_obligation`
531                // and `process_projection_obligation` to handle checking for
532                // the recursion limit themselves. Also don't check some
533                // predicate kinds that don't give further obligations.
534                _ if !self
535                    .selcx
536                    .tcx()
537                    .recursion_limit()
538                    .value_within_limit(obligation.recursion_depth) =>
539                {
540                    self.selcx.infcx.err_ctxt().report_overflow_obligation(&obligation, false);
541                }
542
543                ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(arg)) => {
544                    match wf::obligations(
545                        self.selcx.infcx,
546                        obligation.param_env,
547                        obligation.cause.body_id,
548                        obligation.recursion_depth + 1,
549                        arg,
550                        obligation.cause.span,
551                    ) {
552                        None => {
553                            pending_obligation.stalled_on =
554                                vec![TyOrConstInferVar::maybe_from_generic_arg(arg).unwrap()];
555                            ProcessResult::Unchanged
556                        }
557                        Some(os) => ProcessResult::Changed(mk_pending(obligation, os)),
558                    }
559                }
560
561                ty::PredicateKind::Subtype(subtype) => {
562                    match self.selcx.infcx.subtype_predicate(
563                        &obligation.cause,
564                        obligation.param_env,
565                        Binder::dummy(subtype),
566                    ) {
567                        Err((a, b)) => {
568                            // None means that both are unresolved.
569                            pending_obligation.stalled_on =
570                                vec![TyOrConstInferVar::Ty(a), TyOrConstInferVar::Ty(b)];
571                            ProcessResult::Unchanged
572                        }
573                        Ok(Ok(ok)) => {
574                            ProcessResult::Changed(mk_pending(obligation, ok.obligations))
575                        }
576                        Ok(Err(err)) => {
577                            let expected_found = if subtype.a_is_expected {
578                                ExpectedFound::new(subtype.a, subtype.b)
579                            } else {
580                                ExpectedFound::new(subtype.b, subtype.a)
581                            };
582                            ProcessResult::Error(FulfillmentErrorCode::Subtype(expected_found, err))
583                        }
584                    }
585                }
586
587                ty::PredicateKind::Coerce(coerce) => {
588                    match self.selcx.infcx.coerce_predicate(
589                        &obligation.cause,
590                        obligation.param_env,
591                        Binder::dummy(coerce),
592                    ) {
593                        Err((a, b)) => {
594                            // None means that both are unresolved.
595                            pending_obligation.stalled_on =
596                                vec![TyOrConstInferVar::Ty(a), TyOrConstInferVar::Ty(b)];
597                            ProcessResult::Unchanged
598                        }
599                        Ok(Ok(ok)) => {
600                            ProcessResult::Changed(mk_pending(obligation, ok.obligations))
601                        }
602                        Ok(Err(err)) => {
603                            let expected_found = ExpectedFound::new(coerce.b, coerce.a);
604                            ProcessResult::Error(FulfillmentErrorCode::Subtype(expected_found, err))
605                        }
606                    }
607                }
608
609                ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(uv)) => {
610                    match const_evaluatable::is_const_evaluatable(
611                        self.selcx.infcx,
612                        uv,
613                        obligation.param_env,
614                        obligation.cause.span,
615                    ) {
616                        Ok(()) => ProcessResult::Changed(Default::default()),
617                        Err(NotConstEvaluatable::MentionsInfer) => {
618                            pending_obligation.stalled_on.clear();
619                            pending_obligation.stalled_on.extend(
620                                uv.walk().filter_map(TyOrConstInferVar::maybe_from_generic_arg),
621                            );
622                            ProcessResult::Unchanged
623                        }
624                        Err(
625                            e @ NotConstEvaluatable::MentionsParam
626                            | e @ NotConstEvaluatable::Error(_),
627                        ) => ProcessResult::Error(FulfillmentErrorCode::Select(
628                            SelectionError::NotConstEvaluatable(e),
629                        )),
630                    }
631                }
632
633                ty::PredicateKind::ConstEquate(c1, c2) => {
634                    let tcx = self.selcx.tcx();
635                    assert!(
636                        tcx.features().generic_const_exprs(),
637                        "`ConstEquate` without a feature gate: {c1:?} {c2:?}",
638                    );
639                    // FIXME: we probably should only try to unify abstract constants
640                    // if the constants depend on generic parameters.
641                    //
642                    // Let's just see where this breaks :shrug:
643                    {
644                        let c1 = tcx.expand_abstract_consts(c1);
645                        let c2 = tcx.expand_abstract_consts(c2);
646                        debug!("equating consts:\nc1= {:?}\nc2= {:?}", c1, c2);
647
648                        use rustc_hir::def::DefKind;
649                        match (c1.kind(), c2.kind()) {
650                            (ty::ConstKind::Unevaluated(a), ty::ConstKind::Unevaluated(b))
651                                if a.def == b.def && tcx.def_kind(a.def) == DefKind::AssocConst =>
652                            {
653                                if let Ok(new_obligations) = infcx
654                                    .at(&obligation.cause, obligation.param_env)
655                                    // Can define opaque types as this is only reachable with
656                                    // `generic_const_exprs`
657                                    .eq(
658                                        DefineOpaqueTypes::Yes,
659                                        ty::AliasTerm::from(a),
660                                        ty::AliasTerm::from(b),
661                                    )
662                                {
663                                    return ProcessResult::Changed(mk_pending(
664                                        obligation,
665                                        new_obligations.into_obligations(),
666                                    ));
667                                }
668                            }
669                            (_, ty::ConstKind::Unevaluated(_))
670                            | (ty::ConstKind::Unevaluated(_), _) => (),
671                            (_, _) => {
672                                if let Ok(new_obligations) = infcx
673                                    .at(&obligation.cause, obligation.param_env)
674                                    // Can define opaque types as this is only reachable with
675                                    // `generic_const_exprs`
676                                    .eq(DefineOpaqueTypes::Yes, c1, c2)
677                                {
678                                    return ProcessResult::Changed(mk_pending(
679                                        obligation,
680                                        new_obligations.into_obligations(),
681                                    ));
682                                }
683                            }
684                        }
685                    }
686
687                    let stalled_on = &mut pending_obligation.stalled_on;
688
689                    let mut evaluate = |c: Const<'tcx>| {
690                        if let ty::ConstKind::Unevaluated(unevaluated) = c.kind() {
691                            match super::try_evaluate_const(
692                                self.selcx.infcx,
693                                c,
694                                obligation.param_env,
695                            ) {
696                                Ok(val) => Ok(val),
697                                e @ Err(EvaluateConstErr::HasGenericsOrInfers) => {
698                                    stalled_on.extend(
699                                        unevaluated
700                                            .args
701                                            .iter()
702                                            .filter_map(TyOrConstInferVar::maybe_from_generic_arg),
703                                    );
704                                    e
705                                }
706                                e @ Err(
707                                    EvaluateConstErr::EvaluationFailure(_)
708                                    | EvaluateConstErr::InvalidConstParamTy(_),
709                                ) => e,
710                            }
711                        } else {
712                            Ok(c)
713                        }
714                    };
715
716                    match (evaluate(c1), evaluate(c2)) {
717                        (Ok(c1), Ok(c2)) => {
718                            match self.selcx.infcx.at(&obligation.cause, obligation.param_env).eq(
719                                // Can define opaque types as this is only reachable with
720                                // `generic_const_exprs`
721                                DefineOpaqueTypes::Yes,
722                                c1,
723                                c2,
724                            ) {
725                                Ok(inf_ok) => ProcessResult::Changed(mk_pending(
726                                    obligation,
727                                    inf_ok.into_obligations(),
728                                )),
729                                Err(err) => {
730                                    ProcessResult::Error(FulfillmentErrorCode::ConstEquate(
731                                        ExpectedFound::new(c1, c2),
732                                        err,
733                                    ))
734                                }
735                            }
736                        }
737                        (Err(EvaluateConstErr::InvalidConstParamTy(e)), _)
738                        | (_, Err(EvaluateConstErr::InvalidConstParamTy(e))) => {
739                            ProcessResult::Error(FulfillmentErrorCode::Select(
740                                SelectionError::NotConstEvaluatable(NotConstEvaluatable::Error(e)),
741                            ))
742                        }
743                        (Err(EvaluateConstErr::EvaluationFailure(e)), _)
744                        | (_, Err(EvaluateConstErr::EvaluationFailure(e))) => {
745                            ProcessResult::Error(FulfillmentErrorCode::Select(
746                                SelectionError::NotConstEvaluatable(NotConstEvaluatable::Error(e)),
747                            ))
748                        }
749                        (Err(EvaluateConstErr::HasGenericsOrInfers), _)
750                        | (_, Err(EvaluateConstErr::HasGenericsOrInfers)) => {
751                            if c1.has_non_region_infer() || c2.has_non_region_infer() {
752                                ProcessResult::Unchanged
753                            } else {
754                                // Two different constants using generic parameters ~> error.
755                                let expected_found = ExpectedFound::new(c1, c2);
756                                ProcessResult::Error(FulfillmentErrorCode::ConstEquate(
757                                    expected_found,
758                                    TypeError::ConstMismatch(expected_found),
759                                ))
760                            }
761                        }
762                    }
763                }
764            },
765        }
766    }
767
768    #[inline(never)]
769    fn process_backedge<'c, I>(
770        &mut self,
771        cycle: I,
772        _marker: PhantomData<&'c PendingPredicateObligation<'tcx>>,
773    ) -> Result<(), FulfillmentErrorCode<'tcx>>
774    where
775        I: Clone + Iterator<Item = &'c PendingPredicateObligation<'tcx>>,
776    {
777        if self.selcx.coinductive_match(cycle.clone().map(|s| s.obligation.predicate)) {
778            debug!("process_child_obligations: coinductive match");
779            Ok(())
780        } else {
781            let cycle = cycle.map(|c| c.obligation.clone()).collect();
782            Err(FulfillmentErrorCode::Cycle(cycle))
783        }
784    }
785}
786
787impl<'a, 'tcx> FulfillProcessor<'a, 'tcx> {
788    #[instrument(level = "debug", skip(self, obligation, stalled_on))]
789    fn process_trait_obligation(
790        &mut self,
791        obligation: &PredicateObligation<'tcx>,
792        trait_obligation: PolyTraitObligation<'tcx>,
793        stalled_on: &mut Vec<TyOrConstInferVar>,
794    ) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
795        let infcx = self.selcx.infcx;
796        if obligation.predicate.is_global() && !matches!(infcx.typing_mode(), TypingMode::Coherence)
797        {
798            // no type variables present, can use evaluation for better caching.
799            // FIXME: consider caching errors too.
800            if infcx.predicate_must_hold_considering_regions(obligation) {
801                debug!(
802                    "selecting trait at depth {} evaluated to holds",
803                    obligation.recursion_depth
804                );
805                return ProcessResult::Changed(Default::default());
806            }
807        }
808
809        match self.selcx.poly_select(&trait_obligation) {
810            Ok(Some(impl_source)) => {
811                debug!("selecting trait at depth {} yielded Ok(Some)", obligation.recursion_depth);
812                ProcessResult::Changed(mk_pending(obligation, impl_source.nested_obligations()))
813            }
814            Ok(None) => {
815                debug!("selecting trait at depth {} yielded Ok(None)", obligation.recursion_depth);
816
817                // This is a bit subtle: for the most part, the
818                // only reason we can fail to make progress on
819                // trait selection is because we don't have enough
820                // information about the types in the trait.
821                stalled_on.clear();
822                stalled_on.extend(args_infer_vars(
823                    &self.selcx,
824                    trait_obligation.predicate.map_bound(|pred| pred.trait_ref.args),
825                ));
826
827                debug!(
828                    "process_predicate: pending obligation {:?} now stalled on {:?}",
829                    infcx.resolve_vars_if_possible(obligation.clone()),
830                    stalled_on
831                );
832
833                ProcessResult::Unchanged
834            }
835            Err(selection_err) => {
836                debug!("selecting trait at depth {} yielded Err", obligation.recursion_depth);
837
838                ProcessResult::Error(FulfillmentErrorCode::Select(selection_err))
839            }
840        }
841    }
842
843    fn process_projection_obligation(
844        &mut self,
845        obligation: &PredicateObligation<'tcx>,
846        project_obligation: PolyProjectionObligation<'tcx>,
847        stalled_on: &mut Vec<TyOrConstInferVar>,
848    ) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
849        let tcx = self.selcx.tcx();
850        let infcx = self.selcx.infcx;
851        if obligation.predicate.is_global() && !matches!(infcx.typing_mode(), TypingMode::Coherence)
852        {
853            // no type variables present, can use evaluation for better caching.
854            // FIXME: consider caching errors too.
855            if infcx.predicate_must_hold_considering_regions(obligation) {
856                if let Some(key) = ProjectionCacheKey::from_poly_projection_obligation(
857                    &mut self.selcx,
858                    &project_obligation,
859                ) {
860                    // If `predicate_must_hold_considering_regions` succeeds, then we've
861                    // evaluated all sub-obligations. We can therefore mark the 'root'
862                    // obligation as complete, and skip evaluating sub-obligations.
863                    infcx
864                        .inner
865                        .borrow_mut()
866                        .projection_cache()
867                        .complete(key, EvaluationResult::EvaluatedToOk);
868                }
869                return ProcessResult::Changed(Default::default());
870            } else {
871                debug!("Does NOT hold: {:?}", obligation);
872            }
873        }
874
875        match project::poly_project_and_unify_term(&mut self.selcx, &project_obligation) {
876            ProjectAndUnifyResult::Holds(os) => ProcessResult::Changed(mk_pending(obligation, os)),
877            ProjectAndUnifyResult::FailedNormalization => {
878                stalled_on.clear();
879                stalled_on.extend(args_infer_vars(
880                    &self.selcx,
881                    project_obligation.predicate.map_bound(|pred| pred.projection_term.args),
882                ));
883                ProcessResult::Unchanged
884            }
885            // Let the caller handle the recursion
886            ProjectAndUnifyResult::Recursive => {
887                let mut obligations = PredicateObligations::with_capacity(1);
888                obligations.push(project_obligation.with(tcx, project_obligation.predicate));
889
890                ProcessResult::Changed(mk_pending(obligation, obligations))
891            }
892            ProjectAndUnifyResult::MismatchedProjectionTypes(e) => {
893                ProcessResult::Error(FulfillmentErrorCode::Project(e))
894            }
895        }
896    }
897
898    fn process_host_obligation(
899        &mut self,
900        obligation: &PredicateObligation<'tcx>,
901        host_obligation: HostEffectObligation<'tcx>,
902        stalled_on: &mut Vec<TyOrConstInferVar>,
903    ) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
904        match effects::evaluate_host_effect_obligation(&mut self.selcx, &host_obligation) {
905            Ok(nested) => ProcessResult::Changed(mk_pending(obligation, nested)),
906            Err(effects::EvaluationFailure::Ambiguous) => {
907                stalled_on.clear();
908                stalled_on.extend(args_infer_vars(
909                    &self.selcx,
910                    ty::Binder::dummy(host_obligation.predicate.trait_ref.args),
911                ));
912                ProcessResult::Unchanged
913            }
914            Err(effects::EvaluationFailure::NoSolution) => {
915                ProcessResult::Error(FulfillmentErrorCode::Select(SelectionError::Unimplemented))
916            }
917        }
918    }
919}
920
921/// Returns the set of inference variables contained in `args`.
922fn args_infer_vars<'tcx>(
923    selcx: &SelectionContext<'_, 'tcx>,
924    args: ty::Binder<'tcx, GenericArgsRef<'tcx>>,
925) -> impl Iterator<Item = TyOrConstInferVar> {
926    selcx
927        .infcx
928        .resolve_vars_if_possible(args)
929        .skip_binder() // ok because this check doesn't care about regions
930        .iter()
931        .filter(|arg| arg.has_non_region_infer())
932        .flat_map(|arg| {
933            let mut walker = arg.walk();
934            while let Some(c) = walker.next() {
935                if !c.has_non_region_infer() {
936                    walker.visited.remove(&c);
937                    walker.skip_current_subtree();
938                }
939            }
940            walker.visited.into_iter()
941        })
942        .filter_map(TyOrConstInferVar::maybe_from_generic_arg)
943}
944
945#[derive(Debug)]
946pub struct OldSolverError<'tcx>(
947    Error<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>>,
948);
949
950impl<'tcx> FromSolverError<'tcx, OldSolverError<'tcx>> for FulfillmentError<'tcx> {
951    fn from_solver_error(_infcx: &InferCtxt<'tcx>, error: OldSolverError<'tcx>) -> Self {
952        let mut iter = error.0.backtrace.into_iter();
953        let obligation = iter.next().unwrap().obligation;
954        // The root obligation is the last item in the backtrace - if there's only
955        // one item, then it's the same as the main obligation
956        let root_obligation = iter.next_back().map_or_else(|| obligation.clone(), |e| e.obligation);
957        FulfillmentError::new(obligation, error.0.error, root_obligation)
958    }
959}
960
961impl<'tcx> FromSolverError<'tcx, OldSolverError<'tcx>> for ScrubbedTraitError<'tcx> {
962    fn from_solver_error(_infcx: &InferCtxt<'tcx>, error: OldSolverError<'tcx>) -> Self {
963        match error.0.error {
964            FulfillmentErrorCode::Select(_)
965            | FulfillmentErrorCode::Project(_)
966            | FulfillmentErrorCode::Subtype(_, _)
967            | FulfillmentErrorCode::ConstEquate(_, _) => ScrubbedTraitError::TrueError,
968            FulfillmentErrorCode::Ambiguity { overflow: _ } => ScrubbedTraitError::Ambiguity,
969            FulfillmentErrorCode::Cycle(cycle) => ScrubbedTraitError::Cycle(cycle),
970        }
971    }
972}