rustc_infer/infer/
mod.rs

1use std::cell::{Cell, RefCell};
2use std::fmt;
3
4pub use at::DefineOpaqueTypes;
5use free_regions::RegionRelations;
6pub use freshen::TypeFreshener;
7use lexical_region_resolve::LexicalRegionResolutions;
8pub use lexical_region_resolve::RegionResolutionError;
9pub use opaque_types::{OpaqueTypeStorage, OpaqueTypeStorageEntries, OpaqueTypeTable};
10use region_constraints::{
11    GenericKind, RegionConstraintCollector, RegionConstraintStorage, VarInfos, VerifyBound,
12};
13pub use relate::StructurallyRelateAliases;
14pub use relate::combine::PredicateEmittingRelation;
15use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
16use rustc_data_structures::undo_log::{Rollback, UndoLogs};
17use rustc_data_structures::unify as ut;
18use rustc_errors::{DiagCtxtHandle, ErrorGuaranteed};
19use rustc_hir as hir;
20use rustc_hir::def_id::{DefId, LocalDefId};
21use rustc_macros::extension;
22pub use rustc_macros::{TypeFoldable, TypeVisitable};
23use rustc_middle::bug;
24use rustc_middle::infer::canonical::{CanonicalQueryInput, CanonicalVarValues};
25use rustc_middle::mir::ConstraintCategory;
26use rustc_middle::traits::select;
27use rustc_middle::traits::solve::Goal;
28use rustc_middle::ty::error::{ExpectedFound, TypeError};
29use rustc_middle::ty::{
30    self, BoundVarReplacerDelegate, ConstVid, FloatVid, GenericArg, GenericArgKind, GenericArgs,
31    GenericArgsRef, GenericParamDefKind, InferConst, IntVid, OpaqueHiddenType, OpaqueTypeKey,
32    PseudoCanonicalInput, Term, TermKind, Ty, TyCtxt, TyVid, TypeFoldable, TypeFolder,
33    TypeSuperFoldable, TypeVisitable, TypeVisitableExt, TypingEnv, TypingMode, fold_regions,
34};
35use rustc_span::{DUMMY_SP, Span, Symbol};
36use snapshot::undo_log::InferCtxtUndoLogs;
37use tracing::{debug, instrument};
38use type_variable::TypeVariableOrigin;
39
40use crate::infer::region_constraints::UndoLog;
41use crate::infer::unify_key::{ConstVariableOrigin, ConstVariableValue, ConstVidKey};
42use crate::traits::{
43    self, ObligationCause, ObligationInspector, PredicateObligations, TraitEngine,
44};
45
46pub mod at;
47pub mod canonical;
48mod context;
49mod free_regions;
50mod freshen;
51mod lexical_region_resolve;
52mod opaque_types;
53pub mod outlives;
54mod projection;
55pub mod region_constraints;
56pub mod relate;
57pub mod resolve;
58pub(crate) mod snapshot;
59mod type_variable;
60mod unify_key;
61
62/// `InferOk<'tcx, ()>` is used a lot. It may seem like a useless wrapper
63/// around `PredicateObligations<'tcx>`, but it has one important property:
64/// because `InferOk` is marked with `#[must_use]`, if you have a method
65/// `InferCtxt::f` that returns `InferResult<'tcx, ()>` and you call it with
66/// `infcx.f()?;` you'll get a warning about the obligations being discarded
67/// without use, which is probably unintentional and has been a source of bugs
68/// in the past.
69#[must_use]
70#[derive(Debug)]
71pub struct InferOk<'tcx, T> {
72    pub value: T,
73    pub obligations: PredicateObligations<'tcx>,
74}
75pub type InferResult<'tcx, T> = Result<InferOk<'tcx, T>, TypeError<'tcx>>;
76
77pub(crate) type FixupResult<T> = Result<T, FixupError>; // "fixup result"
78
79pub(crate) type UnificationTable<'a, 'tcx, T> = ut::UnificationTable<
80    ut::InPlace<T, &'a mut ut::UnificationStorage<T>, &'a mut InferCtxtUndoLogs<'tcx>>,
81>;
82
83/// This type contains all the things within `InferCtxt` that sit within a
84/// `RefCell` and are involved with taking/rolling back snapshots. Snapshot
85/// operations are hot enough that we want only one call to `borrow_mut` per
86/// call to `start_snapshot` and `rollback_to`.
87#[derive(Clone)]
88pub struct InferCtxtInner<'tcx> {
89    undo_log: InferCtxtUndoLogs<'tcx>,
90
91    /// Cache for projections.
92    ///
93    /// This cache is snapshotted along with the infcx.
94    projection_cache: traits::ProjectionCacheStorage<'tcx>,
95
96    /// We instantiate `UnificationTable` with `bounds<Ty>` because the types
97    /// that might instantiate a general type variable have an order,
98    /// represented by its upper and lower bounds.
99    type_variable_storage: type_variable::TypeVariableStorage<'tcx>,
100
101    /// Map from const parameter variable to the kind of const it represents.
102    const_unification_storage: ut::UnificationTableStorage<ConstVidKey<'tcx>>,
103
104    /// Map from integral variable to the kind of integer it represents.
105    int_unification_storage: ut::UnificationTableStorage<ty::IntVid>,
106
107    /// Map from floating variable to the kind of float it represents.
108    float_unification_storage: ut::UnificationTableStorage<ty::FloatVid>,
109
110    /// Tracks the set of region variables and the constraints between them.
111    ///
112    /// This is initially `Some(_)` but when
113    /// `resolve_regions_and_report_errors` is invoked, this gets set to `None`
114    /// -- further attempts to perform unification, etc., may fail if new
115    /// region constraints would've been added.
116    region_constraint_storage: Option<RegionConstraintStorage<'tcx>>,
117
118    /// A set of constraints that regionck must validate.
119    ///
120    /// Each constraint has the form `T:'a`, meaning "some type `T` must
121    /// outlive the lifetime 'a". These constraints derive from
122    /// instantiated type parameters. So if you had a struct defined
123    /// like the following:
124    /// ```ignore (illustrative)
125    /// struct Foo<T: 'static> { ... }
126    /// ```
127    /// In some expression `let x = Foo { ... }`, it will
128    /// instantiate the type parameter `T` with a fresh type `$0`. At
129    /// the same time, it will record a region obligation of
130    /// `$0: 'static`. This will get checked later by regionck. (We
131    /// can't generally check these things right away because we have
132    /// to wait until types are resolved.)
133    ///
134    /// These are stored in a map keyed to the id of the innermost
135    /// enclosing fn body / static initializer expression. This is
136    /// because the location where the obligation was incurred can be
137    /// relevant with respect to which sublifetime assumptions are in
138    /// place. The reason that we store under the fn-id, and not
139    /// something more fine-grained, is so that it is easier for
140    /// regionck to be sure that it has found *all* the region
141    /// obligations (otherwise, it's easy to fail to walk to a
142    /// particular node-id).
143    ///
144    /// Before running `resolve_regions_and_report_errors`, the creator
145    /// of the inference context is expected to invoke
146    /// [`InferCtxt::process_registered_region_obligations`]
147    /// for each body-id in this map, which will process the
148    /// obligations within. This is expected to be done 'late enough'
149    /// that all type inference variables have been bound and so forth.
150    region_obligations: Vec<TypeOutlivesConstraint<'tcx>>,
151
152    /// Caches for opaque type inference.
153    opaque_type_storage: OpaqueTypeStorage<'tcx>,
154}
155
156impl<'tcx> InferCtxtInner<'tcx> {
157    fn new() -> InferCtxtInner<'tcx> {
158        InferCtxtInner {
159            undo_log: InferCtxtUndoLogs::default(),
160
161            projection_cache: Default::default(),
162            type_variable_storage: Default::default(),
163            const_unification_storage: Default::default(),
164            int_unification_storage: Default::default(),
165            float_unification_storage: Default::default(),
166            region_constraint_storage: Some(Default::default()),
167            region_obligations: vec![],
168            opaque_type_storage: Default::default(),
169        }
170    }
171
172    #[inline]
173    pub fn region_obligations(&self) -> &[TypeOutlivesConstraint<'tcx>] {
174        &self.region_obligations
175    }
176
177    #[inline]
178    pub fn projection_cache(&mut self) -> traits::ProjectionCache<'_, 'tcx> {
179        self.projection_cache.with_log(&mut self.undo_log)
180    }
181
182    #[inline]
183    fn try_type_variables_probe_ref(
184        &self,
185        vid: ty::TyVid,
186    ) -> Option<&type_variable::TypeVariableValue<'tcx>> {
187        // Uses a read-only view of the unification table, this way we don't
188        // need an undo log.
189        self.type_variable_storage.eq_relations_ref().try_probe_value(vid)
190    }
191
192    #[inline]
193    fn type_variables(&mut self) -> type_variable::TypeVariableTable<'_, 'tcx> {
194        self.type_variable_storage.with_log(&mut self.undo_log)
195    }
196
197    #[inline]
198    pub fn opaque_types(&mut self) -> opaque_types::OpaqueTypeTable<'_, 'tcx> {
199        self.opaque_type_storage.with_log(&mut self.undo_log)
200    }
201
202    #[inline]
203    fn int_unification_table(&mut self) -> UnificationTable<'_, 'tcx, ty::IntVid> {
204        self.int_unification_storage.with_log(&mut self.undo_log)
205    }
206
207    #[inline]
208    fn float_unification_table(&mut self) -> UnificationTable<'_, 'tcx, ty::FloatVid> {
209        self.float_unification_storage.with_log(&mut self.undo_log)
210    }
211
212    #[inline]
213    fn const_unification_table(&mut self) -> UnificationTable<'_, 'tcx, ConstVidKey<'tcx>> {
214        self.const_unification_storage.with_log(&mut self.undo_log)
215    }
216
217    #[inline]
218    pub fn unwrap_region_constraints(&mut self) -> RegionConstraintCollector<'_, 'tcx> {
219        self.region_constraint_storage
220            .as_mut()
221            .expect("region constraints already solved")
222            .with_log(&mut self.undo_log)
223    }
224}
225
226pub struct InferCtxt<'tcx> {
227    pub tcx: TyCtxt<'tcx>,
228
229    /// The mode of this inference context, see the struct documentation
230    /// for more details.
231    typing_mode: TypingMode<'tcx>,
232
233    /// Whether this inference context should care about region obligations in
234    /// the root universe. Most notably, this is used during hir typeck as region
235    /// solving is left to borrowck instead.
236    pub considering_regions: bool,
237
238    /// If set, this flag causes us to skip the 'leak check' during
239    /// higher-ranked subtyping operations. This flag is a temporary one used
240    /// to manage the removal of the leak-check: for the time being, we still run the
241    /// leak-check, but we issue warnings.
242    skip_leak_check: bool,
243
244    pub inner: RefCell<InferCtxtInner<'tcx>>,
245
246    /// Once region inference is done, the values for each variable.
247    lexical_region_resolutions: RefCell<Option<LexicalRegionResolutions<'tcx>>>,
248
249    /// Caches the results of trait selection. This cache is used
250    /// for things that depends on inference variables or placeholders.
251    pub selection_cache: select::SelectionCache<'tcx, ty::ParamEnv<'tcx>>,
252
253    /// Caches the results of trait evaluation. This cache is used
254    /// for things that depends on inference variables or placeholders.
255    pub evaluation_cache: select::EvaluationCache<'tcx, ty::ParamEnv<'tcx>>,
256
257    /// The set of predicates on which errors have been reported, to
258    /// avoid reporting the same error twice.
259    pub reported_trait_errors:
260        RefCell<FxIndexMap<Span, (Vec<Goal<'tcx, ty::Predicate<'tcx>>>, ErrorGuaranteed)>>,
261
262    pub reported_signature_mismatch: RefCell<FxHashSet<(Span, Option<Span>)>>,
263
264    /// When an error occurs, we want to avoid reporting "derived"
265    /// errors that are due to this original failure. We have this
266    /// flag that one can set whenever one creates a type-error that
267    /// is due to an error in a prior pass.
268    ///
269    /// Don't read this flag directly, call `is_tainted_by_errors()`
270    /// and `set_tainted_by_errors()`.
271    tainted_by_errors: Cell<Option<ErrorGuaranteed>>,
272
273    /// What is the innermost universe we have created? Starts out as
274    /// `UniverseIndex::root()` but grows from there as we enter
275    /// universal quantifiers.
276    ///
277    /// N.B., at present, we exclude the universal quantifiers on the
278    /// item we are type-checking, and just consider those names as
279    /// part of the root universe. So this would only get incremented
280    /// when we enter into a higher-ranked (`for<..>`) type or trait
281    /// bound.
282    universe: Cell<ty::UniverseIndex>,
283
284    next_trait_solver: bool,
285
286    pub obligation_inspector: Cell<Option<ObligationInspector<'tcx>>>,
287}
288
289/// See the `error_reporting` module for more details.
290#[derive(Clone, Copy, Debug, PartialEq, Eq, TypeFoldable, TypeVisitable)]
291pub enum ValuePairs<'tcx> {
292    Regions(ExpectedFound<ty::Region<'tcx>>),
293    Terms(ExpectedFound<ty::Term<'tcx>>),
294    Aliases(ExpectedFound<ty::AliasTerm<'tcx>>),
295    TraitRefs(ExpectedFound<ty::TraitRef<'tcx>>),
296    PolySigs(ExpectedFound<ty::PolyFnSig<'tcx>>),
297    ExistentialTraitRef(ExpectedFound<ty::PolyExistentialTraitRef<'tcx>>),
298    ExistentialProjection(ExpectedFound<ty::PolyExistentialProjection<'tcx>>),
299}
300
301impl<'tcx> ValuePairs<'tcx> {
302    pub fn ty(&self) -> Option<(Ty<'tcx>, Ty<'tcx>)> {
303        if let ValuePairs::Terms(ExpectedFound { expected, found }) = self
304            && let Some(expected) = expected.as_type()
305            && let Some(found) = found.as_type()
306        {
307            Some((expected, found))
308        } else {
309            None
310        }
311    }
312}
313
314/// The trace designates the path through inference that we took to
315/// encounter an error or subtyping constraint.
316///
317/// See the `error_reporting` module for more details.
318#[derive(Clone, Debug)]
319pub struct TypeTrace<'tcx> {
320    pub cause: ObligationCause<'tcx>,
321    pub values: ValuePairs<'tcx>,
322}
323
324/// The origin of a `r1 <= r2` constraint.
325///
326/// See `error_reporting` module for more details
327#[derive(Clone, Debug)]
328pub enum SubregionOrigin<'tcx> {
329    /// Arose from a subtyping relation
330    Subtype(Box<TypeTrace<'tcx>>),
331
332    /// When casting `&'a T` to an `&'b Trait` object,
333    /// relating `'a` to `'b`.
334    RelateObjectBound(Span),
335
336    /// Some type parameter was instantiated with the given type,
337    /// and that type must outlive some region.
338    RelateParamBound(Span, Ty<'tcx>, Option<Span>),
339
340    /// The given region parameter was instantiated with a region
341    /// that must outlive some other region.
342    RelateRegionParamBound(Span, Option<Ty<'tcx>>),
343
344    /// Creating a pointer `b` to contents of another reference.
345    Reborrow(Span),
346
347    /// (&'a &'b T) where a >= b
348    ReferenceOutlivesReferent(Ty<'tcx>, Span),
349
350    /// Comparing the signature and requirements of an impl method against
351    /// the containing trait.
352    CompareImplItemObligation {
353        span: Span,
354        impl_item_def_id: LocalDefId,
355        trait_item_def_id: DefId,
356    },
357
358    /// Checking that the bounds of a trait's associated type hold for a given impl.
359    CheckAssociatedTypeBounds {
360        parent: Box<SubregionOrigin<'tcx>>,
361        impl_item_def_id: LocalDefId,
362        trait_item_def_id: DefId,
363    },
364
365    AscribeUserTypeProvePredicate(Span),
366}
367
368// `SubregionOrigin` is used a lot. Make sure it doesn't unintentionally get bigger.
369#[cfg(target_pointer_width = "64")]
370rustc_data_structures::static_assert_size!(SubregionOrigin<'_>, 32);
371
372impl<'tcx> SubregionOrigin<'tcx> {
373    pub fn to_constraint_category(&self) -> ConstraintCategory<'tcx> {
374        match self {
375            Self::Subtype(type_trace) => type_trace.cause.to_constraint_category(),
376            Self::AscribeUserTypeProvePredicate(span) => ConstraintCategory::Predicate(*span),
377            _ => ConstraintCategory::BoringNoLocation,
378        }
379    }
380}
381
382/// Times when we replace bound regions with existentials:
383#[derive(Clone, Copy, Debug)]
384pub enum BoundRegionConversionTime {
385    /// when a fn is called
386    FnCall,
387
388    /// when two higher-ranked types are compared
389    HigherRankedType,
390
391    /// when projecting an associated type
392    AssocTypeProjection(DefId),
393}
394
395/// Reasons to create a region inference variable.
396///
397/// See `error_reporting` module for more details.
398#[derive(Copy, Clone, Debug)]
399pub enum RegionVariableOrigin {
400    /// Region variables created for ill-categorized reasons.
401    ///
402    /// They mostly indicate places in need of refactoring.
403    Misc(Span),
404
405    /// Regions created by a `&P` or `[...]` pattern.
406    PatternRegion(Span),
407
408    /// Regions created by `&` operator.
409    BorrowRegion(Span),
410
411    /// Regions created as part of an autoref of a method receiver.
412    Autoref(Span),
413
414    /// Regions created as part of an automatic coercion.
415    Coercion(Span),
416
417    /// Region variables created as the values for early-bound regions.
418    ///
419    /// FIXME(@lcnr): This should also store a `DefId`, similar to
420    /// `TypeVariableOrigin`.
421    RegionParameterDefinition(Span, Symbol),
422
423    /// Region variables created when instantiating a binder with
424    /// existential variables, e.g. when calling a function or method.
425    BoundRegion(Span, ty::BoundRegionKind, BoundRegionConversionTime),
426
427    UpvarRegion(ty::UpvarId, Span),
428
429    /// This origin is used for the inference variables that we create
430    /// during NLL region processing.
431    Nll(NllRegionVariableOrigin),
432}
433
434#[derive(Copy, Clone, Debug)]
435pub enum NllRegionVariableOrigin {
436    /// During NLL region processing, we create variables for free
437    /// regions that we encounter in the function signature and
438    /// elsewhere. This origin indices we've got one of those.
439    FreeRegion,
440
441    /// "Universal" instantiation of a higher-ranked region (e.g.,
442    /// from a `for<'a> T` binder). Meant to represent "any region".
443    Placeholder(ty::PlaceholderRegion),
444
445    Existential {
446        /// If this is true, then this variable was created to represent a lifetime
447        /// bound in a `for` binder. For example, it might have been created to
448        /// represent the lifetime `'a` in a type like `for<'a> fn(&'a u32)`.
449        /// Such variables are created when we are trying to figure out if there
450        /// is any valid instantiation of `'a` that could fit into some scenario.
451        ///
452        /// This is used to inform error reporting: in the case that we are trying to
453        /// determine whether there is any valid instantiation of a `'a` variable that meets
454        /// some constraint C, we want to blame the "source" of that `for` type,
455        /// rather than blaming the source of the constraint C.
456        from_forall: bool,
457    },
458}
459
460#[derive(Copy, Clone, Debug)]
461pub struct FixupError {
462    unresolved: TyOrConstInferVar,
463}
464
465impl fmt::Display for FixupError {
466    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
467        match self.unresolved {
468            TyOrConstInferVar::TyInt(_) => write!(
469                f,
470                "cannot determine the type of this integer; \
471                 add a suffix to specify the type explicitly"
472            ),
473            TyOrConstInferVar::TyFloat(_) => write!(
474                f,
475                "cannot determine the type of this number; \
476                 add a suffix to specify the type explicitly"
477            ),
478            TyOrConstInferVar::Ty(_) => write!(f, "unconstrained type"),
479            TyOrConstInferVar::Const(_) => write!(f, "unconstrained const value"),
480        }
481    }
482}
483
484/// See the `region_obligations` field for more information.
485#[derive(Clone, Debug)]
486pub struct TypeOutlivesConstraint<'tcx> {
487    pub sub_region: ty::Region<'tcx>,
488    pub sup_type: Ty<'tcx>,
489    pub origin: SubregionOrigin<'tcx>,
490}
491
492/// Used to configure inference contexts before their creation.
493pub struct InferCtxtBuilder<'tcx> {
494    tcx: TyCtxt<'tcx>,
495    considering_regions: bool,
496    skip_leak_check: bool,
497    /// Whether we should use the new trait solver in the local inference context,
498    /// which affects things like which solver is used in `predicate_may_hold`.
499    next_trait_solver: bool,
500}
501
502#[extension(pub trait TyCtxtInferExt<'tcx>)]
503impl<'tcx> TyCtxt<'tcx> {
504    fn infer_ctxt(self) -> InferCtxtBuilder<'tcx> {
505        InferCtxtBuilder {
506            tcx: self,
507            considering_regions: true,
508            skip_leak_check: false,
509            next_trait_solver: self.next_trait_solver_globally(),
510        }
511    }
512}
513
514impl<'tcx> InferCtxtBuilder<'tcx> {
515    pub fn with_next_trait_solver(mut self, next_trait_solver: bool) -> Self {
516        self.next_trait_solver = next_trait_solver;
517        self
518    }
519
520    pub fn ignoring_regions(mut self) -> Self {
521        self.considering_regions = false;
522        self
523    }
524
525    pub fn skip_leak_check(mut self, skip_leak_check: bool) -> Self {
526        self.skip_leak_check = skip_leak_check;
527        self
528    }
529
530    /// Given a canonical value `C` as a starting point, create an
531    /// inference context that contains each of the bound values
532    /// within instantiated as a fresh variable. The `f` closure is
533    /// invoked with the new infcx, along with the instantiated value
534    /// `V` and a instantiation `S`. This instantiation `S` maps from
535    /// the bound values in `C` to their instantiated values in `V`
536    /// (in other words, `S(C) = V`).
537    pub fn build_with_canonical<T>(
538        mut self,
539        span: Span,
540        input: &CanonicalQueryInput<'tcx, T>,
541    ) -> (InferCtxt<'tcx>, T, CanonicalVarValues<'tcx>)
542    where
543        T: TypeFoldable<TyCtxt<'tcx>>,
544    {
545        let infcx = self.build(input.typing_mode);
546        let (value, args) = infcx.instantiate_canonical(span, &input.canonical);
547        (infcx, value, args)
548    }
549
550    pub fn build_with_typing_env(
551        mut self,
552        TypingEnv { typing_mode, param_env }: TypingEnv<'tcx>,
553    ) -> (InferCtxt<'tcx>, ty::ParamEnv<'tcx>) {
554        (self.build(typing_mode), param_env)
555    }
556
557    pub fn build(&mut self, typing_mode: TypingMode<'tcx>) -> InferCtxt<'tcx> {
558        let InferCtxtBuilder { tcx, considering_regions, skip_leak_check, next_trait_solver } =
559            *self;
560        InferCtxt {
561            tcx,
562            typing_mode,
563            considering_regions,
564            skip_leak_check,
565            inner: RefCell::new(InferCtxtInner::new()),
566            lexical_region_resolutions: RefCell::new(None),
567            selection_cache: Default::default(),
568            evaluation_cache: Default::default(),
569            reported_trait_errors: Default::default(),
570            reported_signature_mismatch: Default::default(),
571            tainted_by_errors: Cell::new(None),
572            universe: Cell::new(ty::UniverseIndex::ROOT),
573            next_trait_solver,
574            obligation_inspector: Cell::new(None),
575        }
576    }
577}
578
579impl<'tcx, T> InferOk<'tcx, T> {
580    /// Extracts `value`, registering any obligations into `fulfill_cx`.
581    pub fn into_value_registering_obligations<E: 'tcx>(
582        self,
583        infcx: &InferCtxt<'tcx>,
584        fulfill_cx: &mut dyn TraitEngine<'tcx, E>,
585    ) -> T {
586        let InferOk { value, obligations } = self;
587        fulfill_cx.register_predicate_obligations(infcx, obligations);
588        value
589    }
590}
591
592impl<'tcx> InferOk<'tcx, ()> {
593    pub fn into_obligations(self) -> PredicateObligations<'tcx> {
594        self.obligations
595    }
596}
597
598impl<'tcx> InferCtxt<'tcx> {
599    pub fn dcx(&self) -> DiagCtxtHandle<'_> {
600        self.tcx.dcx().taintable_handle(&self.tainted_by_errors)
601    }
602
603    pub fn next_trait_solver(&self) -> bool {
604        self.next_trait_solver
605    }
606
607    #[inline(always)]
608    pub fn typing_mode(&self) -> TypingMode<'tcx> {
609        self.typing_mode
610    }
611
612    pub fn freshen<T: TypeFoldable<TyCtxt<'tcx>>>(&self, t: T) -> T {
613        t.fold_with(&mut self.freshener())
614    }
615
616    /// Returns the origin of the type variable identified by `vid`.
617    ///
618    /// No attempt is made to resolve `vid` to its root variable.
619    pub fn type_var_origin(&self, vid: TyVid) -> TypeVariableOrigin {
620        self.inner.borrow_mut().type_variables().var_origin(vid)
621    }
622
623    /// Returns the origin of the const variable identified by `vid`
624    // FIXME: We should store origins separately from the unification table
625    // so this doesn't need to be optional.
626    pub fn const_var_origin(&self, vid: ConstVid) -> Option<ConstVariableOrigin> {
627        match self.inner.borrow_mut().const_unification_table().probe_value(vid) {
628            ConstVariableValue::Known { .. } => None,
629            ConstVariableValue::Unknown { origin, .. } => Some(origin),
630        }
631    }
632
633    pub fn freshener<'b>(&'b self) -> TypeFreshener<'b, 'tcx> {
634        freshen::TypeFreshener::new(self)
635    }
636
637    pub fn unresolved_variables(&self) -> Vec<Ty<'tcx>> {
638        let mut inner = self.inner.borrow_mut();
639        let mut vars: Vec<Ty<'_>> = inner
640            .type_variables()
641            .unresolved_variables()
642            .into_iter()
643            .map(|t| Ty::new_var(self.tcx, t))
644            .collect();
645        vars.extend(
646            (0..inner.int_unification_table().len())
647                .map(|i| ty::IntVid::from_usize(i))
648                .filter(|&vid| inner.int_unification_table().probe_value(vid).is_unknown())
649                .map(|v| Ty::new_int_var(self.tcx, v)),
650        );
651        vars.extend(
652            (0..inner.float_unification_table().len())
653                .map(|i| ty::FloatVid::from_usize(i))
654                .filter(|&vid| inner.float_unification_table().probe_value(vid).is_unknown())
655                .map(|v| Ty::new_float_var(self.tcx, v)),
656        );
657        vars
658    }
659
660    #[instrument(skip(self), level = "debug")]
661    pub fn sub_regions(
662        &self,
663        origin: SubregionOrigin<'tcx>,
664        a: ty::Region<'tcx>,
665        b: ty::Region<'tcx>,
666    ) {
667        self.inner.borrow_mut().unwrap_region_constraints().make_subregion(origin, a, b);
668    }
669
670    /// Processes a `Coerce` predicate from the fulfillment context.
671    /// This is NOT the preferred way to handle coercion, which is to
672    /// invoke `FnCtxt::coerce` or a similar method (see `coercion.rs`).
673    ///
674    /// This method here is actually a fallback that winds up being
675    /// invoked when `FnCtxt::coerce` encounters unresolved type variables
676    /// and records a coercion predicate. Presently, this method is equivalent
677    /// to `subtype_predicate` -- that is, "coercing" `a` to `b` winds up
678    /// actually requiring `a <: b`. This is of course a valid coercion,
679    /// but it's not as flexible as `FnCtxt::coerce` would be.
680    ///
681    /// (We may refactor this in the future, but there are a number of
682    /// practical obstacles. Among other things, `FnCtxt::coerce` presently
683    /// records adjustments that are required on the HIR in order to perform
684    /// the coercion, and we don't currently have a way to manage that.)
685    pub fn coerce_predicate(
686        &self,
687        cause: &ObligationCause<'tcx>,
688        param_env: ty::ParamEnv<'tcx>,
689        predicate: ty::PolyCoercePredicate<'tcx>,
690    ) -> Result<InferResult<'tcx, ()>, (TyVid, TyVid)> {
691        let subtype_predicate = predicate.map_bound(|p| ty::SubtypePredicate {
692            a_is_expected: false, // when coercing from `a` to `b`, `b` is expected
693            a: p.a,
694            b: p.b,
695        });
696        self.subtype_predicate(cause, param_env, subtype_predicate)
697    }
698
699    pub fn subtype_predicate(
700        &self,
701        cause: &ObligationCause<'tcx>,
702        param_env: ty::ParamEnv<'tcx>,
703        predicate: ty::PolySubtypePredicate<'tcx>,
704    ) -> Result<InferResult<'tcx, ()>, (TyVid, TyVid)> {
705        // Check for two unresolved inference variables, in which case we can
706        // make no progress. This is partly a micro-optimization, but it's
707        // also an opportunity to "sub-unify" the variables. This isn't
708        // *necessary* to prevent cycles, because they would eventually be sub-unified
709        // anyhow during generalization, but it helps with diagnostics (we can detect
710        // earlier that they are sub-unified).
711        //
712        // Note that we can just skip the binders here because
713        // type variables can't (at present, at
714        // least) capture any of the things bound by this binder.
715        //
716        // Note that this sub here is not just for diagnostics - it has semantic
717        // effects as well.
718        let r_a = self.shallow_resolve(predicate.skip_binder().a);
719        let r_b = self.shallow_resolve(predicate.skip_binder().b);
720        match (r_a.kind(), r_b.kind()) {
721            (&ty::Infer(ty::TyVar(a_vid)), &ty::Infer(ty::TyVar(b_vid))) => {
722                return Err((a_vid, b_vid));
723            }
724            _ => {}
725        }
726
727        self.enter_forall(predicate, |ty::SubtypePredicate { a_is_expected, a, b }| {
728            if a_is_expected {
729                Ok(self.at(cause, param_env).sub(DefineOpaqueTypes::Yes, a, b))
730            } else {
731                Ok(self.at(cause, param_env).sup(DefineOpaqueTypes::Yes, b, a))
732            }
733        })
734    }
735
736    /// Number of type variables created so far.
737    pub fn num_ty_vars(&self) -> usize {
738        self.inner.borrow_mut().type_variables().num_vars()
739    }
740
741    pub fn next_ty_var(&self, span: Span) -> Ty<'tcx> {
742        self.next_ty_var_with_origin(TypeVariableOrigin { span, param_def_id: None })
743    }
744
745    pub fn next_ty_var_with_origin(&self, origin: TypeVariableOrigin) -> Ty<'tcx> {
746        let vid = self.inner.borrow_mut().type_variables().new_var(self.universe(), origin);
747        Ty::new_var(self.tcx, vid)
748    }
749
750    pub fn next_ty_var_id_in_universe(&self, span: Span, universe: ty::UniverseIndex) -> TyVid {
751        let origin = TypeVariableOrigin { span, param_def_id: None };
752        self.inner.borrow_mut().type_variables().new_var(universe, origin)
753    }
754
755    pub fn next_ty_var_in_universe(&self, span: Span, universe: ty::UniverseIndex) -> Ty<'tcx> {
756        let vid = self.next_ty_var_id_in_universe(span, universe);
757        Ty::new_var(self.tcx, vid)
758    }
759
760    pub fn next_const_var(&self, span: Span) -> ty::Const<'tcx> {
761        self.next_const_var_with_origin(ConstVariableOrigin { span, param_def_id: None })
762    }
763
764    pub fn next_const_var_with_origin(&self, origin: ConstVariableOrigin) -> ty::Const<'tcx> {
765        let vid = self
766            .inner
767            .borrow_mut()
768            .const_unification_table()
769            .new_key(ConstVariableValue::Unknown { origin, universe: self.universe() })
770            .vid;
771        ty::Const::new_var(self.tcx, vid)
772    }
773
774    pub fn next_const_var_in_universe(
775        &self,
776        span: Span,
777        universe: ty::UniverseIndex,
778    ) -> ty::Const<'tcx> {
779        let origin = ConstVariableOrigin { span, param_def_id: None };
780        let vid = self
781            .inner
782            .borrow_mut()
783            .const_unification_table()
784            .new_key(ConstVariableValue::Unknown { origin, universe })
785            .vid;
786        ty::Const::new_var(self.tcx, vid)
787    }
788
789    pub fn next_int_var(&self) -> Ty<'tcx> {
790        let next_int_var_id =
791            self.inner.borrow_mut().int_unification_table().new_key(ty::IntVarValue::Unknown);
792        Ty::new_int_var(self.tcx, next_int_var_id)
793    }
794
795    pub fn next_float_var(&self) -> Ty<'tcx> {
796        let next_float_var_id =
797            self.inner.borrow_mut().float_unification_table().new_key(ty::FloatVarValue::Unknown);
798        Ty::new_float_var(self.tcx, next_float_var_id)
799    }
800
801    /// Creates a fresh region variable with the next available index.
802    /// The variable will be created in the maximum universe created
803    /// thus far, allowing it to name any region created thus far.
804    pub fn next_region_var(&self, origin: RegionVariableOrigin) -> ty::Region<'tcx> {
805        self.next_region_var_in_universe(origin, self.universe())
806    }
807
808    /// Creates a fresh region variable with the next available index
809    /// in the given universe; typically, you can use
810    /// `next_region_var` and just use the maximal universe.
811    pub fn next_region_var_in_universe(
812        &self,
813        origin: RegionVariableOrigin,
814        universe: ty::UniverseIndex,
815    ) -> ty::Region<'tcx> {
816        let region_var =
817            self.inner.borrow_mut().unwrap_region_constraints().new_region_var(universe, origin);
818        ty::Region::new_var(self.tcx, region_var)
819    }
820
821    pub fn next_term_var_of_kind(&self, term: ty::Term<'tcx>, span: Span) -> ty::Term<'tcx> {
822        match term.kind() {
823            ty::TermKind::Ty(_) => self.next_ty_var(span).into(),
824            ty::TermKind::Const(_) => self.next_const_var(span).into(),
825        }
826    }
827
828    /// Return the universe that the region `r` was created in. For
829    /// most regions (e.g., `'static`, named regions from the user,
830    /// etc) this is the root universe U0. For inference variables or
831    /// placeholders, however, it will return the universe which they
832    /// are associated.
833    pub fn universe_of_region(&self, r: ty::Region<'tcx>) -> ty::UniverseIndex {
834        self.inner.borrow_mut().unwrap_region_constraints().universe(r)
835    }
836
837    /// Number of region variables created so far.
838    pub fn num_region_vars(&self) -> usize {
839        self.inner.borrow_mut().unwrap_region_constraints().num_region_vars()
840    }
841
842    /// Just a convenient wrapper of `next_region_var` for using during NLL.
843    #[instrument(skip(self), level = "debug")]
844    pub fn next_nll_region_var(&self, origin: NllRegionVariableOrigin) -> ty::Region<'tcx> {
845        self.next_region_var(RegionVariableOrigin::Nll(origin))
846    }
847
848    /// Just a convenient wrapper of `next_region_var` for using during NLL.
849    #[instrument(skip(self), level = "debug")]
850    pub fn next_nll_region_var_in_universe(
851        &self,
852        origin: NllRegionVariableOrigin,
853        universe: ty::UniverseIndex,
854    ) -> ty::Region<'tcx> {
855        self.next_region_var_in_universe(RegionVariableOrigin::Nll(origin), universe)
856    }
857
858    pub fn var_for_def(&self, span: Span, param: &ty::GenericParamDef) -> GenericArg<'tcx> {
859        match param.kind {
860            GenericParamDefKind::Lifetime => {
861                // Create a region inference variable for the given
862                // region parameter definition.
863                self.next_region_var(RegionVariableOrigin::RegionParameterDefinition(
864                    span, param.name,
865                ))
866                .into()
867            }
868            GenericParamDefKind::Type { .. } => {
869                // Create a type inference variable for the given
870                // type parameter definition. The generic parameters are
871                // for actual parameters that may be referred to by
872                // the default of this type parameter, if it exists.
873                // e.g., `struct Foo<A, B, C = (A, B)>(...);` when
874                // used in a path such as `Foo::<T, U>::new()` will
875                // use an inference variable for `C` with `[T, U]`
876                // as the generic parameters for the default, `(T, U)`.
877                let ty_var_id = self.inner.borrow_mut().type_variables().new_var(
878                    self.universe(),
879                    TypeVariableOrigin { param_def_id: Some(param.def_id), span },
880                );
881
882                Ty::new_var(self.tcx, ty_var_id).into()
883            }
884            GenericParamDefKind::Const { .. } => {
885                let origin = ConstVariableOrigin { param_def_id: Some(param.def_id), span };
886                let const_var_id = self
887                    .inner
888                    .borrow_mut()
889                    .const_unification_table()
890                    .new_key(ConstVariableValue::Unknown { origin, universe: self.universe() })
891                    .vid;
892                ty::Const::new_var(self.tcx, const_var_id).into()
893            }
894        }
895    }
896
897    /// Given a set of generics defined on a type or impl, returns the generic parameters mapping
898    /// each type/region parameter to a fresh inference variable.
899    pub fn fresh_args_for_item(&self, span: Span, def_id: DefId) -> GenericArgsRef<'tcx> {
900        GenericArgs::for_item(self.tcx, def_id, |param, _| self.var_for_def(span, param))
901    }
902
903    /// Returns `true` if errors have been reported since this infcx was
904    /// created. This is sometimes used as a heuristic to skip
905    /// reporting errors that often occur as a result of earlier
906    /// errors, but where it's hard to be 100% sure (e.g., unresolved
907    /// inference variables, regionck errors).
908    #[must_use = "this method does not have any side effects"]
909    pub fn tainted_by_errors(&self) -> Option<ErrorGuaranteed> {
910        self.tainted_by_errors.get()
911    }
912
913    /// Set the "tainted by errors" flag to true. We call this when we
914    /// observe an error from a prior pass.
915    pub fn set_tainted_by_errors(&self, e: ErrorGuaranteed) {
916        debug!("set_tainted_by_errors(ErrorGuaranteed)");
917        self.tainted_by_errors.set(Some(e));
918    }
919
920    pub fn region_var_origin(&self, vid: ty::RegionVid) -> RegionVariableOrigin {
921        let mut inner = self.inner.borrow_mut();
922        let inner = &mut *inner;
923        inner.unwrap_region_constraints().var_origin(vid)
924    }
925
926    /// Clone the list of variable regions. This is used only during NLL processing
927    /// to put the set of region variables into the NLL region context.
928    pub fn get_region_var_infos(&self) -> VarInfos {
929        let inner = self.inner.borrow();
930        assert!(!UndoLogs::<UndoLog<'_>>::in_snapshot(&inner.undo_log));
931        let storage = inner.region_constraint_storage.as_ref().expect("regions already resolved");
932        assert!(storage.data.is_empty(), "{:#?}", storage.data);
933        // We clone instead of taking because borrowck still wants to use the
934        // inference context after calling this for diagnostics and the new
935        // trait solver.
936        storage.var_infos.clone()
937    }
938
939    #[instrument(level = "debug", skip(self), ret)]
940    pub fn take_opaque_types(&self) -> Vec<(OpaqueTypeKey<'tcx>, OpaqueHiddenType<'tcx>)> {
941        self.inner.borrow_mut().opaque_type_storage.take_opaque_types().collect()
942    }
943
944    #[instrument(level = "debug", skip(self), ret)]
945    pub fn clone_opaque_types(&self) -> Vec<(OpaqueTypeKey<'tcx>, OpaqueHiddenType<'tcx>)> {
946        self.inner.borrow_mut().opaque_type_storage.iter_opaque_types().collect()
947    }
948
949    #[inline(always)]
950    pub fn can_define_opaque_ty(&self, id: impl Into<DefId>) -> bool {
951        debug_assert!(!self.next_trait_solver());
952        match self.typing_mode() {
953            TypingMode::Analysis {
954                defining_opaque_types_and_generators: defining_opaque_types,
955            }
956            | TypingMode::Borrowck { defining_opaque_types } => {
957                id.into().as_local().is_some_and(|def_id| defining_opaque_types.contains(&def_id))
958            }
959            // FIXME(#132279): This function is quite weird in post-analysis
960            // and post-borrowck analysis mode. We may need to modify its uses
961            // to support PostBorrowckAnalysis in the old solver as well.
962            TypingMode::Coherence
963            | TypingMode::PostBorrowckAnalysis { .. }
964            | TypingMode::PostAnalysis => false,
965        }
966    }
967
968    pub fn ty_to_string(&self, t: Ty<'tcx>) -> String {
969        self.resolve_vars_if_possible(t).to_string()
970    }
971
972    /// If `TyVar(vid)` resolves to a type, return that type. Else, return the
973    /// universe index of `TyVar(vid)`.
974    pub fn probe_ty_var(&self, vid: TyVid) -> Result<Ty<'tcx>, ty::UniverseIndex> {
975        use self::type_variable::TypeVariableValue;
976
977        match self.inner.borrow_mut().type_variables().probe(vid) {
978            TypeVariableValue::Known { value } => Ok(value),
979            TypeVariableValue::Unknown { universe } => Err(universe),
980        }
981    }
982
983    pub fn shallow_resolve(&self, ty: Ty<'tcx>) -> Ty<'tcx> {
984        if let ty::Infer(v) = *ty.kind() {
985            match v {
986                ty::TyVar(v) => {
987                    // Not entirely obvious: if `typ` is a type variable,
988                    // it can be resolved to an int/float variable, which
989                    // can then be recursively resolved, hence the
990                    // recursion. Note though that we prevent type
991                    // variables from unifying to other type variables
992                    // directly (though they may be embedded
993                    // structurally), and we prevent cycles in any case,
994                    // so this recursion should always be of very limited
995                    // depth.
996                    //
997                    // Note: if these two lines are combined into one we get
998                    // dynamic borrow errors on `self.inner`.
999                    let known = self.inner.borrow_mut().type_variables().probe(v).known();
1000                    known.map_or(ty, |t| self.shallow_resolve(t))
1001                }
1002
1003                ty::IntVar(v) => {
1004                    match self.inner.borrow_mut().int_unification_table().probe_value(v) {
1005                        ty::IntVarValue::IntType(ty) => Ty::new_int(self.tcx, ty),
1006                        ty::IntVarValue::UintType(ty) => Ty::new_uint(self.tcx, ty),
1007                        ty::IntVarValue::Unknown => ty,
1008                    }
1009                }
1010
1011                ty::FloatVar(v) => {
1012                    match self.inner.borrow_mut().float_unification_table().probe_value(v) {
1013                        ty::FloatVarValue::Known(ty) => Ty::new_float(self.tcx, ty),
1014                        ty::FloatVarValue::Unknown => ty,
1015                    }
1016                }
1017
1018                ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => ty,
1019            }
1020        } else {
1021            ty
1022        }
1023    }
1024
1025    pub fn shallow_resolve_const(&self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
1026        match ct.kind() {
1027            ty::ConstKind::Infer(infer_ct) => match infer_ct {
1028                InferConst::Var(vid) => self
1029                    .inner
1030                    .borrow_mut()
1031                    .const_unification_table()
1032                    .probe_value(vid)
1033                    .known()
1034                    .unwrap_or(ct),
1035                InferConst::Fresh(_) => ct,
1036            },
1037            ty::ConstKind::Param(_)
1038            | ty::ConstKind::Bound(_, _)
1039            | ty::ConstKind::Placeholder(_)
1040            | ty::ConstKind::Unevaluated(_)
1041            | ty::ConstKind::Value(_)
1042            | ty::ConstKind::Error(_)
1043            | ty::ConstKind::Expr(_) => ct,
1044        }
1045    }
1046
1047    pub fn shallow_resolve_term(&self, term: ty::Term<'tcx>) -> ty::Term<'tcx> {
1048        match term.kind() {
1049            ty::TermKind::Ty(ty) => self.shallow_resolve(ty).into(),
1050            ty::TermKind::Const(ct) => self.shallow_resolve_const(ct).into(),
1051        }
1052    }
1053
1054    pub fn root_var(&self, var: ty::TyVid) -> ty::TyVid {
1055        self.inner.borrow_mut().type_variables().root_var(var)
1056    }
1057
1058    pub fn root_const_var(&self, var: ty::ConstVid) -> ty::ConstVid {
1059        self.inner.borrow_mut().const_unification_table().find(var).vid
1060    }
1061
1062    /// Resolves an int var to a rigid int type, if it was constrained to one,
1063    /// or else the root int var in the unification table.
1064    pub fn opportunistic_resolve_int_var(&self, vid: ty::IntVid) -> Ty<'tcx> {
1065        let mut inner = self.inner.borrow_mut();
1066        let value = inner.int_unification_table().probe_value(vid);
1067        match value {
1068            ty::IntVarValue::IntType(ty) => Ty::new_int(self.tcx, ty),
1069            ty::IntVarValue::UintType(ty) => Ty::new_uint(self.tcx, ty),
1070            ty::IntVarValue::Unknown => {
1071                Ty::new_int_var(self.tcx, inner.int_unification_table().find(vid))
1072            }
1073        }
1074    }
1075
1076    /// Resolves a float var to a rigid int type, if it was constrained to one,
1077    /// or else the root float var in the unification table.
1078    pub fn opportunistic_resolve_float_var(&self, vid: ty::FloatVid) -> Ty<'tcx> {
1079        let mut inner = self.inner.borrow_mut();
1080        let value = inner.float_unification_table().probe_value(vid);
1081        match value {
1082            ty::FloatVarValue::Known(ty) => Ty::new_float(self.tcx, ty),
1083            ty::FloatVarValue::Unknown => {
1084                Ty::new_float_var(self.tcx, inner.float_unification_table().find(vid))
1085            }
1086        }
1087    }
1088
1089    /// Where possible, replaces type/const variables in
1090    /// `value` with their final value. Note that region variables
1091    /// are unaffected. If a type/const variable has not been unified, it
1092    /// is left as is. This is an idempotent operation that does
1093    /// not affect inference state in any way and so you can do it
1094    /// at will.
1095    pub fn resolve_vars_if_possible<T>(&self, value: T) -> T
1096    where
1097        T: TypeFoldable<TyCtxt<'tcx>>,
1098    {
1099        if let Err(guar) = value.error_reported() {
1100            self.set_tainted_by_errors(guar);
1101        }
1102        if !value.has_non_region_infer() {
1103            return value;
1104        }
1105        let mut r = resolve::OpportunisticVarResolver::new(self);
1106        value.fold_with(&mut r)
1107    }
1108
1109    pub fn resolve_numeric_literals_with_default<T>(&self, value: T) -> T
1110    where
1111        T: TypeFoldable<TyCtxt<'tcx>>,
1112    {
1113        if !value.has_infer() {
1114            return value; // Avoid duplicated type-folding.
1115        }
1116        let mut r = InferenceLiteralEraser { tcx: self.tcx };
1117        value.fold_with(&mut r)
1118    }
1119
1120    pub fn probe_const_var(&self, vid: ty::ConstVid) -> Result<ty::Const<'tcx>, ty::UniverseIndex> {
1121        match self.inner.borrow_mut().const_unification_table().probe_value(vid) {
1122            ConstVariableValue::Known { value } => Ok(value),
1123            ConstVariableValue::Unknown { origin: _, universe } => Err(universe),
1124        }
1125    }
1126
1127    /// Attempts to resolve all type/region/const variables in
1128    /// `value`. Region inference must have been run already (e.g.,
1129    /// by calling `resolve_regions_and_report_errors`). If some
1130    /// variable was never unified, an `Err` results.
1131    ///
1132    /// This method is idempotent, but it not typically not invoked
1133    /// except during the writeback phase.
1134    pub fn fully_resolve<T: TypeFoldable<TyCtxt<'tcx>>>(&self, value: T) -> FixupResult<T> {
1135        match resolve::fully_resolve(self, value) {
1136            Ok(value) => {
1137                if value.has_non_region_infer() {
1138                    bug!("`{value:?}` is not fully resolved");
1139                }
1140                if value.has_infer_regions() {
1141                    let guar = self.dcx().delayed_bug(format!("`{value:?}` is not fully resolved"));
1142                    Ok(fold_regions(self.tcx, value, |re, _| {
1143                        if re.is_var() { ty::Region::new_error(self.tcx, guar) } else { re }
1144                    }))
1145                } else {
1146                    Ok(value)
1147                }
1148            }
1149            Err(e) => Err(e),
1150        }
1151    }
1152
1153    // Instantiates the bound variables in a given binder with fresh inference
1154    // variables in the current universe.
1155    //
1156    // Use this method if you'd like to find some generic parameters of the binder's
1157    // variables (e.g. during a method call). If there isn't a [`BoundRegionConversionTime`]
1158    // that corresponds to your use case, consider whether or not you should
1159    // use [`InferCtxt::enter_forall`] instead.
1160    pub fn instantiate_binder_with_fresh_vars<T>(
1161        &self,
1162        span: Span,
1163        lbrct: BoundRegionConversionTime,
1164        value: ty::Binder<'tcx, T>,
1165    ) -> T
1166    where
1167        T: TypeFoldable<TyCtxt<'tcx>> + Copy,
1168    {
1169        if let Some(inner) = value.no_bound_vars() {
1170            return inner;
1171        }
1172
1173        let bound_vars = value.bound_vars();
1174        let mut args = Vec::with_capacity(bound_vars.len());
1175
1176        for bound_var_kind in bound_vars {
1177            let arg: ty::GenericArg<'_> = match bound_var_kind {
1178                ty::BoundVariableKind::Ty(_) => self.next_ty_var(span).into(),
1179                ty::BoundVariableKind::Region(br) => {
1180                    self.next_region_var(RegionVariableOrigin::BoundRegion(span, br, lbrct)).into()
1181                }
1182                ty::BoundVariableKind::Const => self.next_const_var(span).into(),
1183            };
1184            args.push(arg);
1185        }
1186
1187        struct ToFreshVars<'tcx> {
1188            args: Vec<ty::GenericArg<'tcx>>,
1189        }
1190
1191        impl<'tcx> BoundVarReplacerDelegate<'tcx> for ToFreshVars<'tcx> {
1192            fn replace_region(&mut self, br: ty::BoundRegion) -> ty::Region<'tcx> {
1193                self.args[br.var.index()].expect_region()
1194            }
1195            fn replace_ty(&mut self, bt: ty::BoundTy) -> Ty<'tcx> {
1196                self.args[bt.var.index()].expect_ty()
1197            }
1198            fn replace_const(&mut self, bv: ty::BoundVar) -> ty::Const<'tcx> {
1199                self.args[bv.index()].expect_const()
1200            }
1201        }
1202        let delegate = ToFreshVars { args };
1203        self.tcx.replace_bound_vars_uncached(value, delegate)
1204    }
1205
1206    /// See the [`region_constraints::RegionConstraintCollector::verify_generic_bound`] method.
1207    pub(crate) fn verify_generic_bound(
1208        &self,
1209        origin: SubregionOrigin<'tcx>,
1210        kind: GenericKind<'tcx>,
1211        a: ty::Region<'tcx>,
1212        bound: VerifyBound<'tcx>,
1213    ) {
1214        debug!("verify_generic_bound({:?}, {:?} <: {:?})", kind, a, bound);
1215
1216        self.inner
1217            .borrow_mut()
1218            .unwrap_region_constraints()
1219            .verify_generic_bound(origin, kind, a, bound);
1220    }
1221
1222    /// Obtains the latest type of the given closure; this may be a
1223    /// closure in the current function, in which case its
1224    /// `ClosureKind` may not yet be known.
1225    pub fn closure_kind(&self, closure_ty: Ty<'tcx>) -> Option<ty::ClosureKind> {
1226        let unresolved_kind_ty = match *closure_ty.kind() {
1227            ty::Closure(_, args) => args.as_closure().kind_ty(),
1228            ty::CoroutineClosure(_, args) => args.as_coroutine_closure().kind_ty(),
1229            _ => bug!("unexpected type {closure_ty}"),
1230        };
1231        let closure_kind_ty = self.shallow_resolve(unresolved_kind_ty);
1232        closure_kind_ty.to_opt_closure_kind()
1233    }
1234
1235    pub fn universe(&self) -> ty::UniverseIndex {
1236        self.universe.get()
1237    }
1238
1239    /// Creates and return a fresh universe that extends all previous
1240    /// universes. Updates `self.universe` to that new universe.
1241    pub fn create_next_universe(&self) -> ty::UniverseIndex {
1242        let u = self.universe.get().next_universe();
1243        debug!("create_next_universe {u:?}");
1244        self.universe.set(u);
1245        u
1246    }
1247
1248    /// Extract [`ty::TypingMode`] of this inference context to get a `TypingEnv`
1249    /// which contains the necessary information to use the trait system without
1250    /// using canonicalization or carrying this inference context around.
1251    pub fn typing_env(&self, param_env: ty::ParamEnv<'tcx>) -> ty::TypingEnv<'tcx> {
1252        let typing_mode = match self.typing_mode() {
1253            // FIXME(#132279): This erases the `defining_opaque_types` as it isn't possible
1254            // to handle them without proper canonicalization. This means we may cause cycle
1255            // errors and fail to reveal opaques while inside of bodies. We should rename this
1256            // function and require explicit comments on all use-sites in the future.
1257            ty::TypingMode::Analysis { defining_opaque_types_and_generators: _ }
1258            | ty::TypingMode::Borrowck { defining_opaque_types: _ } => {
1259                TypingMode::non_body_analysis()
1260            }
1261            mode @ (ty::TypingMode::Coherence
1262            | ty::TypingMode::PostBorrowckAnalysis { .. }
1263            | ty::TypingMode::PostAnalysis) => mode,
1264        };
1265        ty::TypingEnv { typing_mode, param_env }
1266    }
1267
1268    /// Similar to [`Self::canonicalize_query`], except that it returns
1269    /// a [`PseudoCanonicalInput`] and requires both the `value` and the
1270    /// `param_env` to not contain any inference variables or placeholders.
1271    pub fn pseudo_canonicalize_query<V>(
1272        &self,
1273        param_env: ty::ParamEnv<'tcx>,
1274        value: V,
1275    ) -> PseudoCanonicalInput<'tcx, V>
1276    where
1277        V: TypeVisitable<TyCtxt<'tcx>>,
1278    {
1279        debug_assert!(!value.has_infer());
1280        debug_assert!(!value.has_placeholders());
1281        debug_assert!(!param_env.has_infer());
1282        debug_assert!(!param_env.has_placeholders());
1283        self.typing_env(param_env).as_query_input(value)
1284    }
1285
1286    /// The returned function is used in a fast path. If it returns `true` the variable is
1287    /// unchanged, `false` indicates that the status is unknown.
1288    #[inline]
1289    pub fn is_ty_infer_var_definitely_unchanged(&self) -> impl Fn(TyOrConstInferVar) -> bool {
1290        // This hoists the borrow/release out of the loop body.
1291        let inner = self.inner.try_borrow();
1292
1293        move |infer_var: TyOrConstInferVar| match (infer_var, &inner) {
1294            (TyOrConstInferVar::Ty(ty_var), Ok(inner)) => {
1295                use self::type_variable::TypeVariableValue;
1296
1297                matches!(
1298                    inner.try_type_variables_probe_ref(ty_var),
1299                    Some(TypeVariableValue::Unknown { .. })
1300                )
1301            }
1302            _ => false,
1303        }
1304    }
1305
1306    /// `ty_or_const_infer_var_changed` is equivalent to one of these two:
1307    ///   * `shallow_resolve(ty) != ty` (where `ty.kind = ty::Infer(_)`)
1308    ///   * `shallow_resolve(ct) != ct` (where `ct.kind = ty::ConstKind::Infer(_)`)
1309    ///
1310    /// However, `ty_or_const_infer_var_changed` is more efficient. It's always
1311    /// inlined, despite being large, because it has only two call sites that
1312    /// are extremely hot (both in `traits::fulfill`'s checking of `stalled_on`
1313    /// inference variables), and it handles both `Ty` and `ty::Const` without
1314    /// having to resort to storing full `GenericArg`s in `stalled_on`.
1315    #[inline(always)]
1316    pub fn ty_or_const_infer_var_changed(&self, infer_var: TyOrConstInferVar) -> bool {
1317        match infer_var {
1318            TyOrConstInferVar::Ty(v) => {
1319                use self::type_variable::TypeVariableValue;
1320
1321                // If `inlined_probe` returns a `Known` value, it never equals
1322                // `ty::Infer(ty::TyVar(v))`.
1323                match self.inner.borrow_mut().type_variables().inlined_probe(v) {
1324                    TypeVariableValue::Unknown { .. } => false,
1325                    TypeVariableValue::Known { .. } => true,
1326                }
1327            }
1328
1329            TyOrConstInferVar::TyInt(v) => {
1330                // If `inlined_probe_value` returns a value it's always a
1331                // `ty::Int(_)` or `ty::UInt(_)`, which never matches a
1332                // `ty::Infer(_)`.
1333                self.inner.borrow_mut().int_unification_table().inlined_probe_value(v).is_known()
1334            }
1335
1336            TyOrConstInferVar::TyFloat(v) => {
1337                // If `probe_value` returns a value it's always a
1338                // `ty::Float(_)`, which never matches a `ty::Infer(_)`.
1339                //
1340                // Not `inlined_probe_value(v)` because this call site is colder.
1341                self.inner.borrow_mut().float_unification_table().probe_value(v).is_known()
1342            }
1343
1344            TyOrConstInferVar::Const(v) => {
1345                // If `probe_value` returns a `Known` value, it never equals
1346                // `ty::ConstKind::Infer(ty::InferConst::Var(v))`.
1347                //
1348                // Not `inlined_probe_value(v)` because this call site is colder.
1349                match self.inner.borrow_mut().const_unification_table().probe_value(v) {
1350                    ConstVariableValue::Unknown { .. } => false,
1351                    ConstVariableValue::Known { .. } => true,
1352                }
1353            }
1354        }
1355    }
1356
1357    /// Attach a callback to be invoked on each root obligation evaluated in the new trait solver.
1358    pub fn attach_obligation_inspector(&self, inspector: ObligationInspector<'tcx>) {
1359        debug_assert!(
1360            self.obligation_inspector.get().is_none(),
1361            "shouldn't override a set obligation inspector"
1362        );
1363        self.obligation_inspector.set(Some(inspector));
1364    }
1365}
1366
1367/// Helper for [InferCtxt::ty_or_const_infer_var_changed] (see comment on that), currently
1368/// used only for `traits::fulfill`'s list of `stalled_on` inference variables.
1369#[derive(Copy, Clone, Debug)]
1370pub enum TyOrConstInferVar {
1371    /// Equivalent to `ty::Infer(ty::TyVar(_))`.
1372    Ty(TyVid),
1373    /// Equivalent to `ty::Infer(ty::IntVar(_))`.
1374    TyInt(IntVid),
1375    /// Equivalent to `ty::Infer(ty::FloatVar(_))`.
1376    TyFloat(FloatVid),
1377
1378    /// Equivalent to `ty::ConstKind::Infer(ty::InferConst::Var(_))`.
1379    Const(ConstVid),
1380}
1381
1382impl<'tcx> TyOrConstInferVar {
1383    /// Tries to extract an inference variable from a type or a constant, returns `None`
1384    /// for types other than `ty::Infer(_)` (or `InferTy::Fresh*`) and
1385    /// for constants other than `ty::ConstKind::Infer(_)` (or `InferConst::Fresh`).
1386    pub fn maybe_from_generic_arg(arg: GenericArg<'tcx>) -> Option<Self> {
1387        match arg.kind() {
1388            GenericArgKind::Type(ty) => Self::maybe_from_ty(ty),
1389            GenericArgKind::Const(ct) => Self::maybe_from_const(ct),
1390            GenericArgKind::Lifetime(_) => None,
1391        }
1392    }
1393
1394    /// Tries to extract an inference variable from a type or a constant, returns `None`
1395    /// for types other than `ty::Infer(_)` (or `InferTy::Fresh*`) and
1396    /// for constants other than `ty::ConstKind::Infer(_)` (or `InferConst::Fresh`).
1397    pub fn maybe_from_term(term: Term<'tcx>) -> Option<Self> {
1398        match term.kind() {
1399            TermKind::Ty(ty) => Self::maybe_from_ty(ty),
1400            TermKind::Const(ct) => Self::maybe_from_const(ct),
1401        }
1402    }
1403
1404    /// Tries to extract an inference variable from a type, returns `None`
1405    /// for types other than `ty::Infer(_)` (or `InferTy::Fresh*`).
1406    fn maybe_from_ty(ty: Ty<'tcx>) -> Option<Self> {
1407        match *ty.kind() {
1408            ty::Infer(ty::TyVar(v)) => Some(TyOrConstInferVar::Ty(v)),
1409            ty::Infer(ty::IntVar(v)) => Some(TyOrConstInferVar::TyInt(v)),
1410            ty::Infer(ty::FloatVar(v)) => Some(TyOrConstInferVar::TyFloat(v)),
1411            _ => None,
1412        }
1413    }
1414
1415    /// Tries to extract an inference variable from a constant, returns `None`
1416    /// for constants other than `ty::ConstKind::Infer(_)` (or `InferConst::Fresh`).
1417    fn maybe_from_const(ct: ty::Const<'tcx>) -> Option<Self> {
1418        match ct.kind() {
1419            ty::ConstKind::Infer(InferConst::Var(v)) => Some(TyOrConstInferVar::Const(v)),
1420            _ => None,
1421        }
1422    }
1423}
1424
1425/// Replace `{integer}` with `i32` and `{float}` with `f64`.
1426/// Used only for diagnostics.
1427struct InferenceLiteralEraser<'tcx> {
1428    tcx: TyCtxt<'tcx>,
1429}
1430
1431impl<'tcx> TypeFolder<TyCtxt<'tcx>> for InferenceLiteralEraser<'tcx> {
1432    fn cx(&self) -> TyCtxt<'tcx> {
1433        self.tcx
1434    }
1435
1436    fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
1437        match ty.kind() {
1438            ty::Infer(ty::IntVar(_) | ty::FreshIntTy(_)) => self.tcx.types.i32,
1439            ty::Infer(ty::FloatVar(_) | ty::FreshFloatTy(_)) => self.tcx.types.f64,
1440            _ => ty.super_fold_with(self),
1441        }
1442    }
1443}
1444
1445impl<'tcx> TypeTrace<'tcx> {
1446    pub fn span(&self) -> Span {
1447        self.cause.span
1448    }
1449
1450    pub fn types(cause: &ObligationCause<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> TypeTrace<'tcx> {
1451        TypeTrace {
1452            cause: cause.clone(),
1453            values: ValuePairs::Terms(ExpectedFound::new(a.into(), b.into())),
1454        }
1455    }
1456
1457    pub fn trait_refs(
1458        cause: &ObligationCause<'tcx>,
1459        a: ty::TraitRef<'tcx>,
1460        b: ty::TraitRef<'tcx>,
1461    ) -> TypeTrace<'tcx> {
1462        TypeTrace { cause: cause.clone(), values: ValuePairs::TraitRefs(ExpectedFound::new(a, b)) }
1463    }
1464
1465    pub fn consts(
1466        cause: &ObligationCause<'tcx>,
1467        a: ty::Const<'tcx>,
1468        b: ty::Const<'tcx>,
1469    ) -> TypeTrace<'tcx> {
1470        TypeTrace {
1471            cause: cause.clone(),
1472            values: ValuePairs::Terms(ExpectedFound::new(a.into(), b.into())),
1473        }
1474    }
1475}
1476
1477impl<'tcx> SubregionOrigin<'tcx> {
1478    pub fn span(&self) -> Span {
1479        match *self {
1480            SubregionOrigin::Subtype(ref a) => a.span(),
1481            SubregionOrigin::RelateObjectBound(a) => a,
1482            SubregionOrigin::RelateParamBound(a, ..) => a,
1483            SubregionOrigin::RelateRegionParamBound(a, _) => a,
1484            SubregionOrigin::Reborrow(a) => a,
1485            SubregionOrigin::ReferenceOutlivesReferent(_, a) => a,
1486            SubregionOrigin::CompareImplItemObligation { span, .. } => span,
1487            SubregionOrigin::AscribeUserTypeProvePredicate(span) => span,
1488            SubregionOrigin::CheckAssociatedTypeBounds { ref parent, .. } => parent.span(),
1489        }
1490    }
1491
1492    pub fn from_obligation_cause<F>(cause: &traits::ObligationCause<'tcx>, default: F) -> Self
1493    where
1494        F: FnOnce() -> Self,
1495    {
1496        match *cause.code() {
1497            traits::ObligationCauseCode::ReferenceOutlivesReferent(ref_type) => {
1498                SubregionOrigin::ReferenceOutlivesReferent(ref_type, cause.span)
1499            }
1500
1501            traits::ObligationCauseCode::CompareImplItem {
1502                impl_item_def_id,
1503                trait_item_def_id,
1504                kind: _,
1505            } => SubregionOrigin::CompareImplItemObligation {
1506                span: cause.span,
1507                impl_item_def_id,
1508                trait_item_def_id,
1509            },
1510
1511            traits::ObligationCauseCode::CheckAssociatedTypeBounds {
1512                impl_item_def_id,
1513                trait_item_def_id,
1514            } => SubregionOrigin::CheckAssociatedTypeBounds {
1515                impl_item_def_id,
1516                trait_item_def_id,
1517                parent: Box::new(default()),
1518            },
1519
1520            traits::ObligationCauseCode::AscribeUserTypeProvePredicate(span) => {
1521                SubregionOrigin::AscribeUserTypeProvePredicate(span)
1522            }
1523
1524            traits::ObligationCauseCode::ObjectTypeBound(ty, _reg) => {
1525                SubregionOrigin::RelateRegionParamBound(cause.span, Some(ty))
1526            }
1527
1528            _ => default(),
1529        }
1530    }
1531}
1532
1533impl RegionVariableOrigin {
1534    pub fn span(&self) -> Span {
1535        match *self {
1536            RegionVariableOrigin::Misc(a)
1537            | RegionVariableOrigin::PatternRegion(a)
1538            | RegionVariableOrigin::BorrowRegion(a)
1539            | RegionVariableOrigin::Autoref(a)
1540            | RegionVariableOrigin::Coercion(a)
1541            | RegionVariableOrigin::RegionParameterDefinition(a, ..)
1542            | RegionVariableOrigin::BoundRegion(a, ..)
1543            | RegionVariableOrigin::UpvarRegion(_, a) => a,
1544            RegionVariableOrigin::Nll(..) => bug!("NLL variable used with `span`"),
1545        }
1546    }
1547}
1548
1549impl<'tcx> InferCtxt<'tcx> {
1550    /// Given a [`hir::Block`], get the span of its last expression or
1551    /// statement, peeling off any inner blocks.
1552    pub fn find_block_span(&self, block: &'tcx hir::Block<'tcx>) -> Span {
1553        let block = block.innermost_block();
1554        if let Some(expr) = &block.expr {
1555            expr.span
1556        } else if let Some(stmt) = block.stmts.last() {
1557            // possibly incorrect trailing `;` in the else arm
1558            stmt.span
1559        } else {
1560            // empty block; point at its entirety
1561            block.span
1562        }
1563    }
1564
1565    /// Given a [`hir::HirId`] for a block (or an expr of a block), get the span
1566    /// of its last expression or statement, peeling off any inner blocks.
1567    pub fn find_block_span_from_hir_id(&self, hir_id: hir::HirId) -> Span {
1568        match self.tcx.hir_node(hir_id) {
1569            hir::Node::Block(blk)
1570            | hir::Node::Expr(&hir::Expr { kind: hir::ExprKind::Block(blk, _), .. }) => {
1571                self.find_block_span(blk)
1572            }
1573            hir::Node::Expr(e) => e.span,
1574            _ => DUMMY_SP,
1575        }
1576    }
1577}