• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 pub use self::at::DefineOpaqueTypes;
2 pub use self::freshen::TypeFreshener;
3 pub use self::lexical_region_resolve::RegionResolutionError;
4 pub use self::LateBoundRegionConversionTime::*;
5 pub use self::RegionVariableOrigin::*;
6 pub use self::SubregionOrigin::*;
7 pub use self::ValuePairs::*;
8 pub use combine::ObligationEmittingRelation;
9 use rustc_data_structures::undo_log::UndoLogs;
10 
11 use self::opaque_types::OpaqueTypeStorage;
12 pub(crate) use self::undo_log::{InferCtxtUndoLogs, Snapshot, UndoLog};
13 
14 use crate::traits::{self, ObligationCause, PredicateObligations, TraitEngine, TraitEngineExt};
15 
16 use rustc_data_structures::fx::FxIndexMap;
17 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
18 use rustc_data_structures::sync::Lrc;
19 use rustc_data_structures::undo_log::Rollback;
20 use rustc_data_structures::unify as ut;
21 use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed};
22 use rustc_hir::def_id::{DefId, LocalDefId};
23 use rustc_middle::infer::canonical::{Canonical, CanonicalVarValues};
24 use rustc_middle::infer::unify_key::{ConstVarValue, ConstVariableValue};
25 use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind, ToType};
26 use rustc_middle::mir::interpret::{ErrorHandled, EvalToValTreeResult};
27 use rustc_middle::mir::ConstraintCategory;
28 use rustc_middle::traits::{select, DefiningAnchor};
29 use rustc_middle::ty::error::{ExpectedFound, TypeError};
30 use rustc_middle::ty::fold::BoundVarReplacerDelegate;
31 use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
32 use rustc_middle::ty::relate::RelateResult;
33 use rustc_middle::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, SubstsRef};
34 use rustc_middle::ty::visit::{TypeVisitable, TypeVisitableExt};
35 pub use rustc_middle::ty::IntVarValue;
36 use rustc_middle::ty::{self, GenericParamDefKind, InferConst, InferTy, Ty, TyCtxt};
37 use rustc_middle::ty::{ConstVid, FloatVid, IntVid, TyVid};
38 use rustc_span::symbol::Symbol;
39 use rustc_span::Span;
40 
41 use std::cell::{Cell, RefCell};
42 use std::fmt;
43 
44 use self::combine::CombineFields;
45 use self::error_reporting::TypeErrCtxt;
46 use self::free_regions::RegionRelations;
47 use self::lexical_region_resolve::LexicalRegionResolutions;
48 use self::region_constraints::{GenericKind, VarInfos, VerifyBound};
49 use self::region_constraints::{
50     RegionConstraintCollector, RegionConstraintStorage, RegionSnapshot,
51 };
52 use self::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
53 
54 pub mod at;
55 pub mod canonical;
56 mod combine;
57 mod equate;
58 pub mod error_reporting;
59 pub mod free_regions;
60 mod freshen;
61 mod fudge;
62 mod generalize;
63 mod glb;
64 mod higher_ranked;
65 pub mod lattice;
66 mod lexical_region_resolve;
67 mod lub;
68 pub mod nll_relate;
69 pub mod opaque_types;
70 pub mod outlives;
71 mod projection;
72 pub mod region_constraints;
73 pub mod resolve;
74 mod sub;
75 pub mod type_variable;
76 mod undo_log;
77 
78 #[must_use]
79 #[derive(Debug)]
80 pub struct InferOk<'tcx, T> {
81     pub value: T,
82     pub obligations: PredicateObligations<'tcx>,
83 }
84 pub type InferResult<'tcx, T> = Result<InferOk<'tcx, T>, TypeError<'tcx>>;
85 
86 pub type UnitResult<'tcx> = RelateResult<'tcx, ()>; // "unify result"
87 pub type FixupResult<'tcx, T> = Result<T, FixupError<'tcx>>; // "fixup result"
88 
89 pub(crate) type UnificationTable<'a, 'tcx, T> = ut::UnificationTable<
90     ut::InPlace<T, &'a mut ut::UnificationStorage<T>, &'a mut InferCtxtUndoLogs<'tcx>>,
91 >;
92 
93 /// This type contains all the things within `InferCtxt` that sit within a
94 /// `RefCell` and are involved with taking/rolling back snapshots. Snapshot
95 /// operations are hot enough that we want only one call to `borrow_mut` per
96 /// call to `start_snapshot` and `rollback_to`.
97 #[derive(Clone)]
98 pub struct InferCtxtInner<'tcx> {
99     /// Cache for projections.
100     ///
101     /// This cache is snapshotted along with the infcx.
102     projection_cache: traits::ProjectionCacheStorage<'tcx>,
103 
104     /// We instantiate `UnificationTable` with `bounds<Ty>` because the types
105     /// that might instantiate a general type variable have an order,
106     /// represented by its upper and lower bounds.
107     type_variable_storage: type_variable::TypeVariableStorage<'tcx>,
108 
109     /// Map from const parameter variable to the kind of const it represents.
110     const_unification_storage: ut::UnificationTableStorage<ty::ConstVid<'tcx>>,
111 
112     /// Map from integral variable to the kind of integer it represents.
113     int_unification_storage: ut::UnificationTableStorage<ty::IntVid>,
114 
115     /// Map from floating variable to the kind of float it represents.
116     float_unification_storage: ut::UnificationTableStorage<ty::FloatVid>,
117 
118     /// Tracks the set of region variables and the constraints between them.
119     ///
120     /// This is initially `Some(_)` but when
121     /// `resolve_regions_and_report_errors` is invoked, this gets set to `None`
122     /// -- further attempts to perform unification, etc., may fail if new
123     /// region constraints would've been added.
124     region_constraint_storage: Option<RegionConstraintStorage<'tcx>>,
125 
126     /// A set of constraints that regionck must validate.
127     ///
128     /// Each constraint has the form `T:'a`, meaning "some type `T` must
129     /// outlive the lifetime 'a". These constraints derive from
130     /// instantiated type parameters. So if you had a struct defined
131     /// like the following:
132     /// ```ignore (illustrative)
133     /// struct Foo<T: 'static> { ... }
134     /// ```
135     /// In some expression `let x = Foo { ... }`, it will
136     /// instantiate the type parameter `T` with a fresh type `$0`. At
137     /// the same time, it will record a region obligation of
138     /// `$0: 'static`. This will get checked later by regionck. (We
139     /// can't generally check these things right away because we have
140     /// to wait until types are resolved.)
141     ///
142     /// These are stored in a map keyed to the id of the innermost
143     /// enclosing fn body / static initializer expression. This is
144     /// because the location where the obligation was incurred can be
145     /// relevant with respect to which sublifetime assumptions are in
146     /// place. The reason that we store under the fn-id, and not
147     /// something more fine-grained, is so that it is easier for
148     /// regionck to be sure that it has found *all* the region
149     /// obligations (otherwise, it's easy to fail to walk to a
150     /// particular node-id).
151     ///
152     /// Before running `resolve_regions_and_report_errors`, the creator
153     /// of the inference context is expected to invoke
154     /// [`InferCtxt::process_registered_region_obligations`]
155     /// for each body-id in this map, which will process the
156     /// obligations within. This is expected to be done 'late enough'
157     /// that all type inference variables have been bound and so forth.
158     region_obligations: Vec<RegionObligation<'tcx>>,
159 
160     undo_log: InferCtxtUndoLogs<'tcx>,
161 
162     /// Caches for opaque type inference.
163     opaque_type_storage: OpaqueTypeStorage<'tcx>,
164 }
165 
166 impl<'tcx> InferCtxtInner<'tcx> {
new() -> InferCtxtInner<'tcx>167     fn new() -> InferCtxtInner<'tcx> {
168         InferCtxtInner {
169             projection_cache: Default::default(),
170             type_variable_storage: type_variable::TypeVariableStorage::new(),
171             undo_log: InferCtxtUndoLogs::default(),
172             const_unification_storage: ut::UnificationTableStorage::new(),
173             int_unification_storage: ut::UnificationTableStorage::new(),
174             float_unification_storage: ut::UnificationTableStorage::new(),
175             region_constraint_storage: Some(RegionConstraintStorage::new()),
176             region_obligations: vec![],
177             opaque_type_storage: Default::default(),
178         }
179     }
180 
181     #[inline]
region_obligations(&self) -> &[RegionObligation<'tcx>]182     pub fn region_obligations(&self) -> &[RegionObligation<'tcx>] {
183         &self.region_obligations
184     }
185 
186     #[inline]
projection_cache(&mut self) -> traits::ProjectionCache<'_, 'tcx>187     pub fn projection_cache(&mut self) -> traits::ProjectionCache<'_, 'tcx> {
188         self.projection_cache.with_log(&mut self.undo_log)
189     }
190 
191     #[inline]
try_type_variables_probe_ref( &self, vid: ty::TyVid, ) -> Option<&type_variable::TypeVariableValue<'tcx>>192     fn try_type_variables_probe_ref(
193         &self,
194         vid: ty::TyVid,
195     ) -> Option<&type_variable::TypeVariableValue<'tcx>> {
196         // Uses a read-only view of the unification table, this way we don't
197         // need an undo log.
198         self.type_variable_storage.eq_relations_ref().try_probe_value(vid)
199     }
200 
201     #[inline]
type_variables(&mut self) -> type_variable::TypeVariableTable<'_, 'tcx>202     fn type_variables(&mut self) -> type_variable::TypeVariableTable<'_, 'tcx> {
203         self.type_variable_storage.with_log(&mut self.undo_log)
204     }
205 
206     #[inline]
opaque_types(&mut self) -> opaque_types::OpaqueTypeTable<'_, 'tcx>207     pub fn opaque_types(&mut self) -> opaque_types::OpaqueTypeTable<'_, 'tcx> {
208         self.opaque_type_storage.with_log(&mut self.undo_log)
209     }
210 
211     #[inline]
int_unification_table(&mut self) -> UnificationTable<'_, 'tcx, ty::IntVid>212     fn int_unification_table(&mut self) -> UnificationTable<'_, 'tcx, ty::IntVid> {
213         self.int_unification_storage.with_log(&mut self.undo_log)
214     }
215 
216     #[inline]
float_unification_table(&mut self) -> UnificationTable<'_, 'tcx, ty::FloatVid>217     fn float_unification_table(&mut self) -> UnificationTable<'_, 'tcx, ty::FloatVid> {
218         self.float_unification_storage.with_log(&mut self.undo_log)
219     }
220 
221     #[inline]
const_unification_table(&mut self) -> UnificationTable<'_, 'tcx, ty::ConstVid<'tcx>>222     fn const_unification_table(&mut self) -> UnificationTable<'_, 'tcx, ty::ConstVid<'tcx>> {
223         self.const_unification_storage.with_log(&mut self.undo_log)
224     }
225 
226     #[inline]
unwrap_region_constraints(&mut self) -> RegionConstraintCollector<'_, 'tcx>227     pub fn unwrap_region_constraints(&mut self) -> RegionConstraintCollector<'_, 'tcx> {
228         self.region_constraint_storage
229             .as_mut()
230             .expect("region constraints already solved")
231             .with_log(&mut self.undo_log)
232     }
233 }
234 
235 pub struct InferCtxt<'tcx> {
236     pub tcx: TyCtxt<'tcx>,
237 
238     /// The `DefId` of the item in whose context we are performing inference or typeck.
239     /// It is used to check whether an opaque type use is a defining use.
240     ///
241     /// If it is `DefiningAnchor::Bubble`, we can't resolve opaque types here and need to bubble up
242     /// the obligation. This frequently happens for
243     /// short lived InferCtxt within queries. The opaque type obligations are forwarded
244     /// to the outside until the end up in an `InferCtxt` for typeck or borrowck.
245     ///
246     /// Its default value is `DefiningAnchor::Error`, this way it is easier to catch errors that
247     /// might come up during inference or typeck.
248     pub defining_use_anchor: DefiningAnchor,
249 
250     /// Whether this inference context should care about region obligations in
251     /// the root universe. Most notably, this is used during hir typeck as region
252     /// solving is left to borrowck instead.
253     pub considering_regions: bool,
254 
255     /// If set, this flag causes us to skip the 'leak check' during
256     /// higher-ranked subtyping operations. This flag is a temporary one used
257     /// to manage the removal of the leak-check: for the time being, we still run the
258     /// leak-check, but we issue warnings.
259     skip_leak_check: bool,
260 
261     pub inner: RefCell<InferCtxtInner<'tcx>>,
262 
263     /// Once region inference is done, the values for each variable.
264     lexical_region_resolutions: RefCell<Option<LexicalRegionResolutions<'tcx>>>,
265 
266     /// Caches the results of trait selection. This cache is used
267     /// for things that have to do with the parameters in scope.
268     pub selection_cache: select::SelectionCache<'tcx>,
269 
270     /// Caches the results of trait evaluation.
271     pub evaluation_cache: select::EvaluationCache<'tcx>,
272 
273     /// The set of predicates on which errors have been reported, to
274     /// avoid reporting the same error twice.
275     pub reported_trait_errors: RefCell<FxIndexMap<Span, Vec<ty::Predicate<'tcx>>>>,
276 
277     pub reported_closure_mismatch: RefCell<FxHashSet<(Span, Option<Span>)>>,
278 
279     /// When an error occurs, we want to avoid reporting "derived"
280     /// errors that are due to this original failure. Normally, we
281     /// handle this with the `err_count_on_creation` count, which
282     /// basically just tracks how many errors were reported when we
283     /// started type-checking a fn and checks to see if any new errors
284     /// have been reported since then. Not great, but it works.
285     ///
286     /// However, when errors originated in other passes -- notably
287     /// resolve -- this heuristic breaks down. Therefore, we have this
288     /// auxiliary flag that one can set whenever one creates a
289     /// type-error that is due to an error in a prior pass.
290     ///
291     /// Don't read this flag directly, call `is_tainted_by_errors()`
292     /// and `set_tainted_by_errors()`.
293     tainted_by_errors: Cell<Option<ErrorGuaranteed>>,
294 
295     /// Track how many errors were reported when this infcx is created.
296     /// If the number of errors increases, that's also a sign (like
297     /// `tainted_by_errors`) to avoid reporting certain kinds of errors.
298     // FIXME(matthewjasper) Merge into `tainted_by_errors`
299     err_count_on_creation: usize,
300 
301     /// What is the innermost universe we have created? Starts out as
302     /// `UniverseIndex::root()` but grows from there as we enter
303     /// universal quantifiers.
304     ///
305     /// N.B., at present, we exclude the universal quantifiers on the
306     /// item we are type-checking, and just consider those names as
307     /// part of the root universe. So this would only get incremented
308     /// when we enter into a higher-ranked (`for<..>`) type or trait
309     /// bound.
310     universe: Cell<ty::UniverseIndex>,
311 
312     /// During coherence we have to assume that other crates may add
313     /// additional impls which we currently don't know about.
314     ///
315     /// To deal with this evaluation, we should be conservative
316     /// and consider the possibility of impls from outside this crate.
317     /// This comes up primarily when resolving ambiguity. Imagine
318     /// there is some trait reference `$0: Bar` where `$0` is an
319     /// inference variable. If `intercrate` is true, then we can never
320     /// say for sure that this reference is not implemented, even if
321     /// there are *no impls at all for `Bar`*, because `$0` could be
322     /// bound to some type that in a downstream crate that implements
323     /// `Bar`.
324     ///
325     /// Outside of coherence, we set this to false because we are only
326     /// interested in types that the user could actually have written.
327     /// In other words, we consider `$0: Bar` to be unimplemented if
328     /// there is no type that the user could *actually name* that
329     /// would satisfy it. This avoids crippling inference, basically.
330     pub intercrate: bool,
331 
332     next_trait_solver: bool,
333 }
334 
335 /// See the `error_reporting` module for more details.
336 #[derive(Clone, Copy, Debug, PartialEq, Eq, TypeFoldable, TypeVisitable)]
337 pub enum ValuePairs<'tcx> {
338     Regions(ExpectedFound<ty::Region<'tcx>>),
339     Terms(ExpectedFound<ty::Term<'tcx>>),
340     Aliases(ExpectedFound<ty::AliasTy<'tcx>>),
341     TraitRefs(ExpectedFound<ty::TraitRef<'tcx>>),
342     PolyTraitRefs(ExpectedFound<ty::PolyTraitRef<'tcx>>),
343     Sigs(ExpectedFound<ty::FnSig<'tcx>>),
344 }
345 
346 impl<'tcx> ValuePairs<'tcx> {
ty(&self) -> Option<(Ty<'tcx>, Ty<'tcx>)>347     pub fn ty(&self) -> Option<(Ty<'tcx>, Ty<'tcx>)> {
348         if let ValuePairs::Terms(ExpectedFound { expected, found }) = self
349             && let Some(expected) = expected.ty()
350             && let Some(found) = found.ty()
351         {
352             Some((expected, found))
353         } else {
354             None
355         }
356     }
357 }
358 
359 /// The trace designates the path through inference that we took to
360 /// encounter an error or subtyping constraint.
361 ///
362 /// See the `error_reporting` module for more details.
363 #[derive(Clone, Debug)]
364 pub struct TypeTrace<'tcx> {
365     pub cause: ObligationCause<'tcx>,
366     pub values: ValuePairs<'tcx>,
367 }
368 
369 /// The origin of a `r1 <= r2` constraint.
370 ///
371 /// See `error_reporting` module for more details
372 #[derive(Clone, Debug)]
373 pub enum SubregionOrigin<'tcx> {
374     /// Arose from a subtyping relation
375     Subtype(Box<TypeTrace<'tcx>>),
376 
377     /// When casting `&'a T` to an `&'b Trait` object,
378     /// relating `'a` to `'b`.
379     RelateObjectBound(Span),
380 
381     /// Some type parameter was instantiated with the given type,
382     /// and that type must outlive some region.
383     RelateParamBound(Span, Ty<'tcx>, Option<Span>),
384 
385     /// The given region parameter was instantiated with a region
386     /// that must outlive some other region.
387     RelateRegionParamBound(Span),
388 
389     /// Creating a pointer `b` to contents of another reference.
390     Reborrow(Span),
391 
392     /// (&'a &'b T) where a >= b
393     ReferenceOutlivesReferent(Ty<'tcx>, Span),
394 
395     /// Comparing the signature and requirements of an impl method against
396     /// the containing trait.
397     CompareImplItemObligation {
398         span: Span,
399         impl_item_def_id: LocalDefId,
400         trait_item_def_id: DefId,
401     },
402 
403     /// Checking that the bounds of a trait's associated type hold for a given impl.
404     CheckAssociatedTypeBounds {
405         parent: Box<SubregionOrigin<'tcx>>,
406         impl_item_def_id: LocalDefId,
407         trait_item_def_id: DefId,
408     },
409 
410     AscribeUserTypeProvePredicate(Span),
411 }
412 
413 // `SubregionOrigin` is used a lot. Make sure it doesn't unintentionally get bigger.
414 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
415 static_assert_size!(SubregionOrigin<'_>, 32);
416 
417 impl<'tcx> SubregionOrigin<'tcx> {
to_constraint_category(&self) -> ConstraintCategory<'tcx>418     pub fn to_constraint_category(&self) -> ConstraintCategory<'tcx> {
419         match self {
420             Self::Subtype(type_trace) => type_trace.cause.to_constraint_category(),
421             Self::AscribeUserTypeProvePredicate(span) => ConstraintCategory::Predicate(*span),
422             _ => ConstraintCategory::BoringNoLocation,
423         }
424     }
425 }
426 
427 /// Times when we replace late-bound regions with variables:
428 #[derive(Clone, Copy, Debug)]
429 pub enum LateBoundRegionConversionTime {
430     /// when a fn is called
431     FnCall,
432 
433     /// when two higher-ranked types are compared
434     HigherRankedType,
435 
436     /// when projecting an associated type
437     AssocTypeProjection(DefId),
438 }
439 
440 /// Reasons to create a region inference variable.
441 ///
442 /// See `error_reporting` module for more details.
443 #[derive(Copy, Clone, Debug)]
444 pub enum RegionVariableOrigin {
445     /// Region variables created for ill-categorized reasons.
446     ///
447     /// They mostly indicate places in need of refactoring.
448     MiscVariable(Span),
449 
450     /// Regions created by a `&P` or `[...]` pattern.
451     PatternRegion(Span),
452 
453     /// Regions created by `&` operator.
454     ///
455     AddrOfRegion(Span),
456     /// Regions created as part of an autoref of a method receiver.
457     Autoref(Span),
458 
459     /// Regions created as part of an automatic coercion.
460     Coercion(Span),
461 
462     /// Region variables created as the values for early-bound regions.
463     EarlyBoundRegion(Span, Symbol),
464 
465     /// Region variables created for bound regions
466     /// in a function or method that is called.
467     LateBoundRegion(Span, ty::BoundRegionKind, LateBoundRegionConversionTime),
468 
469     UpvarRegion(ty::UpvarId, Span),
470 
471     /// This origin is used for the inference variables that we create
472     /// during NLL region processing.
473     Nll(NllRegionVariableOrigin),
474 }
475 
476 #[derive(Copy, Clone, Debug)]
477 pub enum NllRegionVariableOrigin {
478     /// During NLL region processing, we create variables for free
479     /// regions that we encounter in the function signature and
480     /// elsewhere. This origin indices we've got one of those.
481     FreeRegion,
482 
483     /// "Universal" instantiation of a higher-ranked region (e.g.,
484     /// from a `for<'a> T` binder). Meant to represent "any region".
485     Placeholder(ty::PlaceholderRegion),
486 
487     Existential {
488         /// If this is true, then this variable was created to represent a lifetime
489         /// bound in a `for` binder. For example, it might have been created to
490         /// represent the lifetime `'a` in a type like `for<'a> fn(&'a u32)`.
491         /// Such variables are created when we are trying to figure out if there
492         /// is any valid instantiation of `'a` that could fit into some scenario.
493         ///
494         /// This is used to inform error reporting: in the case that we are trying to
495         /// determine whether there is any valid instantiation of a `'a` variable that meets
496         /// some constraint C, we want to blame the "source" of that `for` type,
497         /// rather than blaming the source of the constraint C.
498         from_forall: bool,
499     },
500 }
501 
502 // FIXME(eddyb) investigate overlap between this and `TyOrConstInferVar`.
503 #[derive(Copy, Clone, Debug)]
504 pub enum FixupError<'tcx> {
505     UnresolvedIntTy(IntVid),
506     UnresolvedFloatTy(FloatVid),
507     UnresolvedTy(TyVid),
508     UnresolvedConst(ConstVid<'tcx>),
509 }
510 
511 /// See the `region_obligations` field for more information.
512 #[derive(Clone, Debug)]
513 pub struct RegionObligation<'tcx> {
514     pub sub_region: ty::Region<'tcx>,
515     pub sup_type: Ty<'tcx>,
516     pub origin: SubregionOrigin<'tcx>,
517 }
518 
519 impl<'tcx> fmt::Display for FixupError<'tcx> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result520     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
521         use self::FixupError::*;
522 
523         match *self {
524             UnresolvedIntTy(_) => write!(
525                 f,
526                 "cannot determine the type of this integer; \
527                  add a suffix to specify the type explicitly"
528             ),
529             UnresolvedFloatTy(_) => write!(
530                 f,
531                 "cannot determine the type of this number; \
532                  add a suffix to specify the type explicitly"
533             ),
534             UnresolvedTy(_) => write!(f, "unconstrained type"),
535             UnresolvedConst(_) => write!(f, "unconstrained const value"),
536         }
537     }
538 }
539 
540 /// Used to configure inference contexts before their creation.
541 pub struct InferCtxtBuilder<'tcx> {
542     tcx: TyCtxt<'tcx>,
543     defining_use_anchor: DefiningAnchor,
544     considering_regions: bool,
545     skip_leak_check: bool,
546     /// Whether we are in coherence mode.
547     intercrate: bool,
548     /// Whether we should use the new trait solver in the local inference context,
549     /// which affects things like which solver is used in `predicate_may_hold`.
550     next_trait_solver: bool,
551 }
552 
553 pub trait TyCtxtInferExt<'tcx> {
infer_ctxt(self) -> InferCtxtBuilder<'tcx>554     fn infer_ctxt(self) -> InferCtxtBuilder<'tcx>;
555 }
556 
557 impl<'tcx> TyCtxtInferExt<'tcx> for TyCtxt<'tcx> {
infer_ctxt(self) -> InferCtxtBuilder<'tcx>558     fn infer_ctxt(self) -> InferCtxtBuilder<'tcx> {
559         InferCtxtBuilder {
560             tcx: self,
561             defining_use_anchor: DefiningAnchor::Error,
562             considering_regions: true,
563             skip_leak_check: false,
564             intercrate: false,
565             next_trait_solver: self.next_trait_solver_globally(),
566         }
567     }
568 }
569 
570 impl<'tcx> InferCtxtBuilder<'tcx> {
571     /// Whenever the `InferCtxt` should be able to handle defining uses of opaque types,
572     /// you need to call this function. Otherwise the opaque type will be treated opaquely.
573     ///
574     /// It is only meant to be called in two places, for typeck
575     /// (via `Inherited::build`) and for the inference context used
576     /// in mir borrowck.
with_opaque_type_inference(mut self, defining_use_anchor: DefiningAnchor) -> Self577     pub fn with_opaque_type_inference(mut self, defining_use_anchor: DefiningAnchor) -> Self {
578         self.defining_use_anchor = defining_use_anchor;
579         self
580     }
581 
with_next_trait_solver(mut self, next_trait_solver: bool) -> Self582     pub fn with_next_trait_solver(mut self, next_trait_solver: bool) -> Self {
583         self.next_trait_solver = next_trait_solver;
584         self
585     }
586 
intercrate(mut self, intercrate: bool) -> Self587     pub fn intercrate(mut self, intercrate: bool) -> Self {
588         self.intercrate = intercrate;
589         self
590     }
591 
ignoring_regions(mut self) -> Self592     pub fn ignoring_regions(mut self) -> Self {
593         self.considering_regions = false;
594         self
595     }
596 
skip_leak_check(mut self, skip_leak_check: bool) -> Self597     pub fn skip_leak_check(mut self, skip_leak_check: bool) -> Self {
598         self.skip_leak_check = skip_leak_check;
599         self
600     }
601 
602     /// Given a canonical value `C` as a starting point, create an
603     /// inference context that contains each of the bound values
604     /// within instantiated as a fresh variable. The `f` closure is
605     /// invoked with the new infcx, along with the instantiated value
606     /// `V` and a substitution `S`. This substitution `S` maps from
607     /// the bound values in `C` to their instantiated values in `V`
608     /// (in other words, `S(C) = V`).
build_with_canonical<T>( &mut self, span: Span, canonical: &Canonical<'tcx, T>, ) -> (InferCtxt<'tcx>, T, CanonicalVarValues<'tcx>) where T: TypeFoldable<TyCtxt<'tcx>>,609     pub fn build_with_canonical<T>(
610         &mut self,
611         span: Span,
612         canonical: &Canonical<'tcx, T>,
613     ) -> (InferCtxt<'tcx>, T, CanonicalVarValues<'tcx>)
614     where
615         T: TypeFoldable<TyCtxt<'tcx>>,
616     {
617         let infcx = self.build();
618         let (value, subst) = infcx.instantiate_canonical_with_fresh_inference_vars(span, canonical);
619         (infcx, value, subst)
620     }
621 
build(&mut self) -> InferCtxt<'tcx>622     pub fn build(&mut self) -> InferCtxt<'tcx> {
623         let InferCtxtBuilder {
624             tcx,
625             defining_use_anchor,
626             considering_regions,
627             skip_leak_check,
628             intercrate,
629             next_trait_solver,
630         } = *self;
631         InferCtxt {
632             tcx,
633             defining_use_anchor,
634             considering_regions,
635             skip_leak_check,
636             inner: RefCell::new(InferCtxtInner::new()),
637             lexical_region_resolutions: RefCell::new(None),
638             selection_cache: Default::default(),
639             evaluation_cache: Default::default(),
640             reported_trait_errors: Default::default(),
641             reported_closure_mismatch: Default::default(),
642             tainted_by_errors: Cell::new(None),
643             err_count_on_creation: tcx.sess.err_count(),
644             universe: Cell::new(ty::UniverseIndex::ROOT),
645             intercrate,
646             next_trait_solver,
647         }
648     }
649 }
650 
651 impl<'tcx, T> InferOk<'tcx, T> {
unit(self) -> InferOk<'tcx, ()>652     pub fn unit(self) -> InferOk<'tcx, ()> {
653         InferOk { value: (), obligations: self.obligations }
654     }
655 
656     /// Extracts `value`, registering any obligations into `fulfill_cx`.
into_value_registering_obligations( self, infcx: &InferCtxt<'tcx>, fulfill_cx: &mut dyn TraitEngine<'tcx>, ) -> T657     pub fn into_value_registering_obligations(
658         self,
659         infcx: &InferCtxt<'tcx>,
660         fulfill_cx: &mut dyn TraitEngine<'tcx>,
661     ) -> T {
662         let InferOk { value, obligations } = self;
663         fulfill_cx.register_predicate_obligations(infcx, obligations);
664         value
665     }
666 }
667 
668 impl<'tcx> InferOk<'tcx, ()> {
into_obligations(self) -> PredicateObligations<'tcx>669     pub fn into_obligations(self) -> PredicateObligations<'tcx> {
670         self.obligations
671     }
672 }
673 
674 #[must_use = "once you start a snapshot, you should always consume it"]
675 pub struct CombinedSnapshot<'tcx> {
676     undo_snapshot: Snapshot<'tcx>,
677     region_constraints_snapshot: RegionSnapshot,
678     universe: ty::UniverseIndex,
679 }
680 
681 impl<'tcx> InferCtxt<'tcx> {
next_trait_solver(&self) -> bool682     pub fn next_trait_solver(&self) -> bool {
683         self.next_trait_solver
684     }
685 
686     /// Creates a `TypeErrCtxt` for emitting various inference errors.
687     /// During typeck, use `FnCtxt::err_ctxt` instead.
err_ctxt(&self) -> TypeErrCtxt<'_, 'tcx>688     pub fn err_ctxt(&self) -> TypeErrCtxt<'_, 'tcx> {
689         TypeErrCtxt {
690             infcx: self,
691             typeck_results: None,
692             fallback_has_occurred: false,
693             normalize_fn_sig: Box::new(|fn_sig| fn_sig),
694             autoderef_steps: Box::new(|ty| {
695                 debug_assert!(false, "shouldn't be using autoderef_steps outside of typeck");
696                 vec![(ty, vec![])]
697             }),
698         }
699     }
700 
freshen<T: TypeFoldable<TyCtxt<'tcx>>>(&self, t: T) -> T701     pub fn freshen<T: TypeFoldable<TyCtxt<'tcx>>>(&self, t: T) -> T {
702         t.fold_with(&mut self.freshener())
703     }
704 
705     /// Returns the origin of the type variable identified by `vid`, or `None`
706     /// if this is not a type variable.
707     ///
708     /// No attempt is made to resolve `ty`.
type_var_origin(&self, ty: Ty<'tcx>) -> Option<TypeVariableOrigin>709     pub fn type_var_origin(&self, ty: Ty<'tcx>) -> Option<TypeVariableOrigin> {
710         match *ty.kind() {
711             ty::Infer(ty::TyVar(vid)) => {
712                 Some(*self.inner.borrow_mut().type_variables().var_origin(vid))
713             }
714             _ => None,
715         }
716     }
717 
freshener<'b>(&'b self) -> TypeFreshener<'b, 'tcx>718     pub fn freshener<'b>(&'b self) -> TypeFreshener<'b, 'tcx> {
719         freshen::TypeFreshener::new(self)
720     }
721 
unsolved_variables(&self) -> Vec<Ty<'tcx>>722     pub fn unsolved_variables(&self) -> Vec<Ty<'tcx>> {
723         let mut inner = self.inner.borrow_mut();
724         let mut vars: Vec<Ty<'_>> = inner
725             .type_variables()
726             .unsolved_variables()
727             .into_iter()
728             .map(|t| Ty::new_var(self.tcx, t))
729             .collect();
730         vars.extend(
731             (0..inner.int_unification_table().len())
732                 .map(|i| ty::IntVid { index: i as u32 })
733                 .filter(|&vid| inner.int_unification_table().probe_value(vid).is_none())
734                 .map(|v| Ty::new_int_var(self.tcx, v)),
735         );
736         vars.extend(
737             (0..inner.float_unification_table().len())
738                 .map(|i| ty::FloatVid { index: i as u32 })
739                 .filter(|&vid| inner.float_unification_table().probe_value(vid).is_none())
740                 .map(|v| Ty::new_float_var(self.tcx, v)),
741         );
742         vars
743     }
744 
combine_fields<'a>( &'a self, trace: TypeTrace<'tcx>, param_env: ty::ParamEnv<'tcx>, define_opaque_types: DefineOpaqueTypes, ) -> CombineFields<'a, 'tcx>745     fn combine_fields<'a>(
746         &'a self,
747         trace: TypeTrace<'tcx>,
748         param_env: ty::ParamEnv<'tcx>,
749         define_opaque_types: DefineOpaqueTypes,
750     ) -> CombineFields<'a, 'tcx> {
751         CombineFields {
752             infcx: self,
753             trace,
754             cause: None,
755             param_env,
756             obligations: PredicateObligations::new(),
757             define_opaque_types,
758         }
759     }
760 
in_snapshot(&self) -> bool761     pub fn in_snapshot(&self) -> bool {
762         UndoLogs::<UndoLog<'tcx>>::in_snapshot(&self.inner.borrow_mut().undo_log)
763     }
764 
num_open_snapshots(&self) -> usize765     pub fn num_open_snapshots(&self) -> usize {
766         UndoLogs::<UndoLog<'tcx>>::num_open_snapshots(&self.inner.borrow_mut().undo_log)
767     }
768 
start_snapshot(&self) -> CombinedSnapshot<'tcx>769     fn start_snapshot(&self) -> CombinedSnapshot<'tcx> {
770         debug!("start_snapshot()");
771 
772         let mut inner = self.inner.borrow_mut();
773 
774         CombinedSnapshot {
775             undo_snapshot: inner.undo_log.start_snapshot(),
776             region_constraints_snapshot: inner.unwrap_region_constraints().start_snapshot(),
777             universe: self.universe(),
778         }
779     }
780 
781     #[instrument(skip(self, snapshot), level = "debug")]
rollback_to(&self, cause: &str, snapshot: CombinedSnapshot<'tcx>)782     fn rollback_to(&self, cause: &str, snapshot: CombinedSnapshot<'tcx>) {
783         let CombinedSnapshot { undo_snapshot, region_constraints_snapshot, universe } = snapshot;
784 
785         self.universe.set(universe);
786 
787         let mut inner = self.inner.borrow_mut();
788         inner.rollback_to(undo_snapshot);
789         inner.unwrap_region_constraints().rollback_to(region_constraints_snapshot);
790     }
791 
792     #[instrument(skip(self, snapshot), level = "debug")]
commit_from(&self, snapshot: CombinedSnapshot<'tcx>)793     fn commit_from(&self, snapshot: CombinedSnapshot<'tcx>) {
794         let CombinedSnapshot { undo_snapshot, region_constraints_snapshot: _, universe: _ } =
795             snapshot;
796 
797         self.inner.borrow_mut().commit(undo_snapshot);
798     }
799 
800     /// Execute `f` and commit the bindings if closure `f` returns `Ok(_)`.
801     #[instrument(skip(self, f), level = "debug")]
commit_if_ok<T, E, F>(&self, f: F) -> Result<T, E> where F: FnOnce(&CombinedSnapshot<'tcx>) -> Result<T, E>,802     pub fn commit_if_ok<T, E, F>(&self, f: F) -> Result<T, E>
803     where
804         F: FnOnce(&CombinedSnapshot<'tcx>) -> Result<T, E>,
805     {
806         let snapshot = self.start_snapshot();
807         let r = f(&snapshot);
808         debug!("commit_if_ok() -- r.is_ok() = {}", r.is_ok());
809         match r {
810             Ok(_) => {
811                 self.commit_from(snapshot);
812             }
813             Err(_) => {
814                 self.rollback_to("commit_if_ok -- error", snapshot);
815             }
816         }
817         r
818     }
819 
820     /// Execute `f` then unroll any bindings it creates.
821     #[instrument(skip(self, f), level = "debug")]
probe<R, F>(&self, f: F) -> R where F: FnOnce(&CombinedSnapshot<'tcx>) -> R,822     pub fn probe<R, F>(&self, f: F) -> R
823     where
824         F: FnOnce(&CombinedSnapshot<'tcx>) -> R,
825     {
826         let snapshot = self.start_snapshot();
827         let r = f(&snapshot);
828         self.rollback_to("probe", snapshot);
829         r
830     }
831 
832     /// Scan the constraints produced since `snapshot` and check whether
833     /// we added any region constraints.
region_constraints_added_in_snapshot(&self, snapshot: &CombinedSnapshot<'tcx>) -> bool834     pub fn region_constraints_added_in_snapshot(&self, snapshot: &CombinedSnapshot<'tcx>) -> bool {
835         self.inner
836             .borrow_mut()
837             .unwrap_region_constraints()
838             .region_constraints_added_in_snapshot(&snapshot.undo_snapshot)
839     }
840 
opaque_types_added_in_snapshot(&self, snapshot: &CombinedSnapshot<'tcx>) -> bool841     pub fn opaque_types_added_in_snapshot(&self, snapshot: &CombinedSnapshot<'tcx>) -> bool {
842         self.inner.borrow().undo_log.opaque_types_in_snapshot(&snapshot.undo_snapshot)
843     }
844 
can_sub<T>(&self, param_env: ty::ParamEnv<'tcx>, a: T, b: T) -> bool where T: at::ToTrace<'tcx>,845     pub fn can_sub<T>(&self, param_env: ty::ParamEnv<'tcx>, a: T, b: T) -> bool
846     where
847         T: at::ToTrace<'tcx>,
848     {
849         let origin = &ObligationCause::dummy();
850         self.probe(|_| self.at(origin, param_env).sub(DefineOpaqueTypes::No, a, b).is_ok())
851     }
852 
can_eq<T>(&self, param_env: ty::ParamEnv<'tcx>, a: T, b: T) -> bool where T: at::ToTrace<'tcx>,853     pub fn can_eq<T>(&self, param_env: ty::ParamEnv<'tcx>, a: T, b: T) -> bool
854     where
855         T: at::ToTrace<'tcx>,
856     {
857         let origin = &ObligationCause::dummy();
858         self.probe(|_| self.at(origin, param_env).eq(DefineOpaqueTypes::No, a, b).is_ok())
859     }
860 
861     #[instrument(skip(self), level = "debug")]
sub_regions( &self, origin: SubregionOrigin<'tcx>, a: ty::Region<'tcx>, b: ty::Region<'tcx>, )862     pub fn sub_regions(
863         &self,
864         origin: SubregionOrigin<'tcx>,
865         a: ty::Region<'tcx>,
866         b: ty::Region<'tcx>,
867     ) {
868         self.inner.borrow_mut().unwrap_region_constraints().make_subregion(origin, a, b);
869     }
870 
871     /// Require that the region `r` be equal to one of the regions in
872     /// the set `regions`.
873     #[instrument(skip(self), level = "debug")]
member_constraint( &self, key: ty::OpaqueTypeKey<'tcx>, definition_span: Span, hidden_ty: Ty<'tcx>, region: ty::Region<'tcx>, in_regions: &Lrc<Vec<ty::Region<'tcx>>>, )874     pub fn member_constraint(
875         &self,
876         key: ty::OpaqueTypeKey<'tcx>,
877         definition_span: Span,
878         hidden_ty: Ty<'tcx>,
879         region: ty::Region<'tcx>,
880         in_regions: &Lrc<Vec<ty::Region<'tcx>>>,
881     ) {
882         self.inner.borrow_mut().unwrap_region_constraints().member_constraint(
883             key,
884             definition_span,
885             hidden_ty,
886             region,
887             in_regions,
888         );
889     }
890 
891     /// Processes a `Coerce` predicate from the fulfillment context.
892     /// This is NOT the preferred way to handle coercion, which is to
893     /// invoke `FnCtxt::coerce` or a similar method (see `coercion.rs`).
894     ///
895     /// This method here is actually a fallback that winds up being
896     /// invoked when `FnCtxt::coerce` encounters unresolved type variables
897     /// and records a coercion predicate. Presently, this method is equivalent
898     /// to `subtype_predicate` -- that is, "coercing" `a` to `b` winds up
899     /// actually requiring `a <: b`. This is of course a valid coercion,
900     /// but it's not as flexible as `FnCtxt::coerce` would be.
901     ///
902     /// (We may refactor this in the future, but there are a number of
903     /// practical obstacles. Among other things, `FnCtxt::coerce` presently
904     /// records adjustments that are required on the HIR in order to perform
905     /// the coercion, and we don't currently have a way to manage that.)
coerce_predicate( &self, cause: &ObligationCause<'tcx>, param_env: ty::ParamEnv<'tcx>, predicate: ty::PolyCoercePredicate<'tcx>, ) -> Result<InferResult<'tcx, ()>, (TyVid, TyVid)>906     pub fn coerce_predicate(
907         &self,
908         cause: &ObligationCause<'tcx>,
909         param_env: ty::ParamEnv<'tcx>,
910         predicate: ty::PolyCoercePredicate<'tcx>,
911     ) -> Result<InferResult<'tcx, ()>, (TyVid, TyVid)> {
912         let subtype_predicate = predicate.map_bound(|p| ty::SubtypePredicate {
913             a_is_expected: false, // when coercing from `a` to `b`, `b` is expected
914             a: p.a,
915             b: p.b,
916         });
917         self.subtype_predicate(cause, param_env, subtype_predicate)
918     }
919 
subtype_predicate( &self, cause: &ObligationCause<'tcx>, param_env: ty::ParamEnv<'tcx>, predicate: ty::PolySubtypePredicate<'tcx>, ) -> Result<InferResult<'tcx, ()>, (TyVid, TyVid)>920     pub fn subtype_predicate(
921         &self,
922         cause: &ObligationCause<'tcx>,
923         param_env: ty::ParamEnv<'tcx>,
924         predicate: ty::PolySubtypePredicate<'tcx>,
925     ) -> Result<InferResult<'tcx, ()>, (TyVid, TyVid)> {
926         // Check for two unresolved inference variables, in which case we can
927         // make no progress. This is partly a micro-optimization, but it's
928         // also an opportunity to "sub-unify" the variables. This isn't
929         // *necessary* to prevent cycles, because they would eventually be sub-unified
930         // anyhow during generalization, but it helps with diagnostics (we can detect
931         // earlier that they are sub-unified).
932         //
933         // Note that we can just skip the binders here because
934         // type variables can't (at present, at
935         // least) capture any of the things bound by this binder.
936         //
937         // Note that this sub here is not just for diagnostics - it has semantic
938         // effects as well.
939         let r_a = self.shallow_resolve(predicate.skip_binder().a);
940         let r_b = self.shallow_resolve(predicate.skip_binder().b);
941         match (r_a.kind(), r_b.kind()) {
942             (&ty::Infer(ty::TyVar(a_vid)), &ty::Infer(ty::TyVar(b_vid))) => {
943                 self.inner.borrow_mut().type_variables().sub(a_vid, b_vid);
944                 return Err((a_vid, b_vid));
945             }
946             _ => {}
947         }
948 
949         Ok(self.commit_if_ok(|_snapshot| {
950             let ty::SubtypePredicate { a_is_expected, a, b } =
951                 self.instantiate_binder_with_placeholders(predicate);
952 
953             let ok =
954                 self.at(cause, param_env).sub_exp(DefineOpaqueTypes::No, a_is_expected, a, b)?;
955 
956             Ok(ok.unit())
957         }))
958     }
959 
region_outlives_predicate( &self, cause: &traits::ObligationCause<'tcx>, predicate: ty::PolyRegionOutlivesPredicate<'tcx>, )960     pub fn region_outlives_predicate(
961         &self,
962         cause: &traits::ObligationCause<'tcx>,
963         predicate: ty::PolyRegionOutlivesPredicate<'tcx>,
964     ) {
965         let ty::OutlivesPredicate(r_a, r_b) = self.instantiate_binder_with_placeholders(predicate);
966         let origin =
967             SubregionOrigin::from_obligation_cause(cause, || RelateRegionParamBound(cause.span));
968         self.sub_regions(origin, r_b, r_a); // `b : a` ==> `a <= b`
969     }
970 
971     /// Number of type variables created so far.
num_ty_vars(&self) -> usize972     pub fn num_ty_vars(&self) -> usize {
973         self.inner.borrow_mut().type_variables().num_vars()
974     }
975 
next_ty_var_id(&self, origin: TypeVariableOrigin) -> TyVid976     pub fn next_ty_var_id(&self, origin: TypeVariableOrigin) -> TyVid {
977         self.inner.borrow_mut().type_variables().new_var(self.universe(), origin)
978     }
979 
next_ty_var(&self, origin: TypeVariableOrigin) -> Ty<'tcx>980     pub fn next_ty_var(&self, origin: TypeVariableOrigin) -> Ty<'tcx> {
981         Ty::new_var(self.tcx, self.next_ty_var_id(origin))
982     }
983 
next_ty_var_id_in_universe( &self, origin: TypeVariableOrigin, universe: ty::UniverseIndex, ) -> TyVid984     pub fn next_ty_var_id_in_universe(
985         &self,
986         origin: TypeVariableOrigin,
987         universe: ty::UniverseIndex,
988     ) -> TyVid {
989         self.inner.borrow_mut().type_variables().new_var(universe, origin)
990     }
991 
next_ty_var_in_universe( &self, origin: TypeVariableOrigin, universe: ty::UniverseIndex, ) -> Ty<'tcx>992     pub fn next_ty_var_in_universe(
993         &self,
994         origin: TypeVariableOrigin,
995         universe: ty::UniverseIndex,
996     ) -> Ty<'tcx> {
997         let vid = self.next_ty_var_id_in_universe(origin, universe);
998         Ty::new_var(self.tcx, vid)
999     }
1000 
next_const_var(&self, ty: Ty<'tcx>, origin: ConstVariableOrigin) -> ty::Const<'tcx>1001     pub fn next_const_var(&self, ty: Ty<'tcx>, origin: ConstVariableOrigin) -> ty::Const<'tcx> {
1002         ty::Const::new_var(self.tcx, self.next_const_var_id(origin), ty)
1003     }
1004 
next_const_var_in_universe( &self, ty: Ty<'tcx>, origin: ConstVariableOrigin, universe: ty::UniverseIndex, ) -> ty::Const<'tcx>1005     pub fn next_const_var_in_universe(
1006         &self,
1007         ty: Ty<'tcx>,
1008         origin: ConstVariableOrigin,
1009         universe: ty::UniverseIndex,
1010     ) -> ty::Const<'tcx> {
1011         let vid = self
1012             .inner
1013             .borrow_mut()
1014             .const_unification_table()
1015             .new_key(ConstVarValue { origin, val: ConstVariableValue::Unknown { universe } });
1016         ty::Const::new_var(self.tcx, vid, ty)
1017     }
1018 
next_const_var_id(&self, origin: ConstVariableOrigin) -> ConstVid<'tcx>1019     pub fn next_const_var_id(&self, origin: ConstVariableOrigin) -> ConstVid<'tcx> {
1020         self.inner.borrow_mut().const_unification_table().new_key(ConstVarValue {
1021             origin,
1022             val: ConstVariableValue::Unknown { universe: self.universe() },
1023         })
1024     }
1025 
next_int_var_id(&self) -> IntVid1026     fn next_int_var_id(&self) -> IntVid {
1027         self.inner.borrow_mut().int_unification_table().new_key(None)
1028     }
1029 
next_int_var(&self) -> Ty<'tcx>1030     pub fn next_int_var(&self) -> Ty<'tcx> {
1031         Ty::new_int_var(self.tcx, self.next_int_var_id())
1032     }
1033 
next_float_var_id(&self) -> FloatVid1034     fn next_float_var_id(&self) -> FloatVid {
1035         self.inner.borrow_mut().float_unification_table().new_key(None)
1036     }
1037 
next_float_var(&self) -> Ty<'tcx>1038     pub fn next_float_var(&self) -> Ty<'tcx> {
1039         Ty::new_float_var(self.tcx, self.next_float_var_id())
1040     }
1041 
1042     /// Creates a fresh region variable with the next available index.
1043     /// The variable will be created in the maximum universe created
1044     /// thus far, allowing it to name any region created thus far.
next_region_var(&self, origin: RegionVariableOrigin) -> ty::Region<'tcx>1045     pub fn next_region_var(&self, origin: RegionVariableOrigin) -> ty::Region<'tcx> {
1046         self.next_region_var_in_universe(origin, self.universe())
1047     }
1048 
1049     /// Creates a fresh region variable with the next available index
1050     /// in the given universe; typically, you can use
1051     /// `next_region_var` and just use the maximal universe.
next_region_var_in_universe( &self, origin: RegionVariableOrigin, universe: ty::UniverseIndex, ) -> ty::Region<'tcx>1052     pub fn next_region_var_in_universe(
1053         &self,
1054         origin: RegionVariableOrigin,
1055         universe: ty::UniverseIndex,
1056     ) -> ty::Region<'tcx> {
1057         let region_var =
1058             self.inner.borrow_mut().unwrap_region_constraints().new_region_var(universe, origin);
1059         ty::Region::new_var(self.tcx, region_var)
1060     }
1061 
1062     /// Return the universe that the region `r` was created in. For
1063     /// most regions (e.g., `'static`, named regions from the user,
1064     /// etc) this is the root universe U0. For inference variables or
1065     /// placeholders, however, it will return the universe which they
1066     /// are associated.
universe_of_region(&self, r: ty::Region<'tcx>) -> ty::UniverseIndex1067     pub fn universe_of_region(&self, r: ty::Region<'tcx>) -> ty::UniverseIndex {
1068         self.inner.borrow_mut().unwrap_region_constraints().universe(r)
1069     }
1070 
1071     /// Number of region variables created so far.
num_region_vars(&self) -> usize1072     pub fn num_region_vars(&self) -> usize {
1073         self.inner.borrow_mut().unwrap_region_constraints().num_region_vars()
1074     }
1075 
1076     /// Just a convenient wrapper of `next_region_var` for using during NLL.
1077     #[instrument(skip(self), level = "debug")]
next_nll_region_var(&self, origin: NllRegionVariableOrigin) -> ty::Region<'tcx>1078     pub fn next_nll_region_var(&self, origin: NllRegionVariableOrigin) -> ty::Region<'tcx> {
1079         self.next_region_var(RegionVariableOrigin::Nll(origin))
1080     }
1081 
1082     /// Just a convenient wrapper of `next_region_var` for using during NLL.
1083     #[instrument(skip(self), level = "debug")]
next_nll_region_var_in_universe( &self, origin: NllRegionVariableOrigin, universe: ty::UniverseIndex, ) -> ty::Region<'tcx>1084     pub fn next_nll_region_var_in_universe(
1085         &self,
1086         origin: NllRegionVariableOrigin,
1087         universe: ty::UniverseIndex,
1088     ) -> ty::Region<'tcx> {
1089         self.next_region_var_in_universe(RegionVariableOrigin::Nll(origin), universe)
1090     }
1091 
var_for_def(&self, span: Span, param: &ty::GenericParamDef) -> GenericArg<'tcx>1092     pub fn var_for_def(&self, span: Span, param: &ty::GenericParamDef) -> GenericArg<'tcx> {
1093         match param.kind {
1094             GenericParamDefKind::Lifetime => {
1095                 // Create a region inference variable for the given
1096                 // region parameter definition.
1097                 self.next_region_var(EarlyBoundRegion(span, param.name)).into()
1098             }
1099             GenericParamDefKind::Type { .. } => {
1100                 // Create a type inference variable for the given
1101                 // type parameter definition. The substitutions are
1102                 // for actual parameters that may be referred to by
1103                 // the default of this type parameter, if it exists.
1104                 // e.g., `struct Foo<A, B, C = (A, B)>(...);` when
1105                 // used in a path such as `Foo::<T, U>::new()` will
1106                 // use an inference variable for `C` with `[T, U]`
1107                 // as the substitutions for the default, `(T, U)`.
1108                 let ty_var_id = self.inner.borrow_mut().type_variables().new_var(
1109                     self.universe(),
1110                     TypeVariableOrigin {
1111                         kind: TypeVariableOriginKind::TypeParameterDefinition(
1112                             param.name,
1113                             param.def_id,
1114                         ),
1115                         span,
1116                     },
1117                 );
1118 
1119                 Ty::new_var(self.tcx, ty_var_id).into()
1120             }
1121             GenericParamDefKind::Const { .. } => {
1122                 let origin = ConstVariableOrigin {
1123                     kind: ConstVariableOriginKind::ConstParameterDefinition(
1124                         param.name,
1125                         param.def_id,
1126                     ),
1127                     span,
1128                 };
1129                 let const_var_id =
1130                     self.inner.borrow_mut().const_unification_table().new_key(ConstVarValue {
1131                         origin,
1132                         val: ConstVariableValue::Unknown { universe: self.universe() },
1133                     });
1134                 ty::Const::new_var(
1135                     self.tcx,
1136                     const_var_id,
1137                     self.tcx
1138                         .type_of(param.def_id)
1139                         .no_bound_vars()
1140                         .expect("const parameter types cannot be generic"),
1141                 )
1142                 .into()
1143             }
1144         }
1145     }
1146 
1147     /// Given a set of generics defined on a type or impl, returns a substitution mapping each
1148     /// type/region parameter to a fresh inference variable.
fresh_substs_for_item(&self, span: Span, def_id: DefId) -> SubstsRef<'tcx>1149     pub fn fresh_substs_for_item(&self, span: Span, def_id: DefId) -> SubstsRef<'tcx> {
1150         InternalSubsts::for_item(self.tcx, def_id, |param, _| self.var_for_def(span, param))
1151     }
1152 
1153     /// Returns `true` if errors have been reported since this infcx was
1154     /// created. This is sometimes used as a heuristic to skip
1155     /// reporting errors that often occur as a result of earlier
1156     /// errors, but where it's hard to be 100% sure (e.g., unresolved
1157     /// inference variables, regionck errors).
1158     #[must_use = "this method does not have any side effects"]
tainted_by_errors(&self) -> Option<ErrorGuaranteed>1159     pub fn tainted_by_errors(&self) -> Option<ErrorGuaranteed> {
1160         debug!(
1161             "is_tainted_by_errors(err_count={}, err_count_on_creation={}, \
1162              tainted_by_errors={})",
1163             self.tcx.sess.err_count(),
1164             self.err_count_on_creation,
1165             self.tainted_by_errors.get().is_some()
1166         );
1167 
1168         if let Some(e) = self.tainted_by_errors.get() {
1169             return Some(e);
1170         }
1171 
1172         if self.tcx.sess.err_count() > self.err_count_on_creation {
1173             // errors reported since this infcx was made
1174             let e = self.tcx.sess.has_errors().unwrap();
1175             self.set_tainted_by_errors(e);
1176             return Some(e);
1177         }
1178 
1179         None
1180     }
1181 
1182     /// Set the "tainted by errors" flag to true. We call this when we
1183     /// observe an error from a prior pass.
set_tainted_by_errors(&self, e: ErrorGuaranteed)1184     pub fn set_tainted_by_errors(&self, e: ErrorGuaranteed) {
1185         debug!("set_tainted_by_errors(ErrorGuaranteed)");
1186         self.tainted_by_errors.set(Some(e));
1187     }
1188 
region_var_origin(&self, vid: ty::RegionVid) -> RegionVariableOrigin1189     pub fn region_var_origin(&self, vid: ty::RegionVid) -> RegionVariableOrigin {
1190         let mut inner = self.inner.borrow_mut();
1191         let inner = &mut *inner;
1192         inner
1193             .region_constraint_storage
1194             .as_mut()
1195             .expect("regions already resolved")
1196             .with_log(&mut inner.undo_log)
1197             .var_origin(vid)
1198     }
1199 
1200     /// Clone the list of variable regions. This is used only during NLL processing
1201     /// to put the set of region variables into the NLL region context.
get_region_var_origins(&self) -> VarInfos1202     pub fn get_region_var_origins(&self) -> VarInfos {
1203         let mut inner = self.inner.borrow_mut();
1204         let (var_infos, data) = inner
1205             .region_constraint_storage
1206             // We clone instead of taking because borrowck still wants to use
1207             // the inference context after calling this for diagnostics
1208             // and the new trait solver.
1209             .clone()
1210             .expect("regions already resolved")
1211             .with_log(&mut inner.undo_log)
1212             .into_infos_and_data();
1213         assert!(data.is_empty());
1214         var_infos
1215     }
1216 
1217     #[instrument(level = "debug", skip(self), ret)]
take_opaque_types(&self) -> opaque_types::OpaqueTypeMap<'tcx>1218     pub fn take_opaque_types(&self) -> opaque_types::OpaqueTypeMap<'tcx> {
1219         debug_assert_ne!(self.defining_use_anchor, DefiningAnchor::Error);
1220         std::mem::take(&mut self.inner.borrow_mut().opaque_type_storage.opaque_types)
1221     }
1222 
ty_to_string(&self, t: Ty<'tcx>) -> String1223     pub fn ty_to_string(&self, t: Ty<'tcx>) -> String {
1224         self.resolve_vars_if_possible(t).to_string()
1225     }
1226 
1227     /// If `TyVar(vid)` resolves to a type, return that type. Else, return the
1228     /// universe index of `TyVar(vid)`.
probe_ty_var(&self, vid: TyVid) -> Result<Ty<'tcx>, ty::UniverseIndex>1229     pub fn probe_ty_var(&self, vid: TyVid) -> Result<Ty<'tcx>, ty::UniverseIndex> {
1230         use self::type_variable::TypeVariableValue;
1231 
1232         match self.inner.borrow_mut().type_variables().probe(vid) {
1233             TypeVariableValue::Known { value } => Ok(value),
1234             TypeVariableValue::Unknown { universe } => Err(universe),
1235         }
1236     }
1237 
1238     /// Resolve any type variables found in `value` -- but only one
1239     /// level. So, if the variable `?X` is bound to some type
1240     /// `Foo<?Y>`, then this would return `Foo<?Y>` (but `?Y` may
1241     /// itself be bound to a type).
1242     ///
1243     /// Useful when you only need to inspect the outermost level of
1244     /// the type and don't care about nested types (or perhaps you
1245     /// will be resolving them as well, e.g. in a loop).
shallow_resolve<T>(&self, value: T) -> T where T: TypeFoldable<TyCtxt<'tcx>>,1246     pub fn shallow_resolve<T>(&self, value: T) -> T
1247     where
1248         T: TypeFoldable<TyCtxt<'tcx>>,
1249     {
1250         value.fold_with(&mut ShallowResolver { infcx: self })
1251     }
1252 
root_var(&self, var: ty::TyVid) -> ty::TyVid1253     pub fn root_var(&self, var: ty::TyVid) -> ty::TyVid {
1254         self.inner.borrow_mut().type_variables().root_var(var)
1255     }
1256 
root_const_var(&self, var: ty::ConstVid<'tcx>) -> ty::ConstVid<'tcx>1257     pub fn root_const_var(&self, var: ty::ConstVid<'tcx>) -> ty::ConstVid<'tcx> {
1258         self.inner.borrow_mut().const_unification_table().find(var)
1259     }
1260 
1261     /// Resolves an int var to a rigid int type, if it was constrained to one,
1262     /// or else the root int var in the unification table.
opportunistic_resolve_int_var(&self, vid: ty::IntVid) -> Ty<'tcx>1263     pub fn opportunistic_resolve_int_var(&self, vid: ty::IntVid) -> Ty<'tcx> {
1264         let mut inner = self.inner.borrow_mut();
1265         if let Some(value) = inner.int_unification_table().probe_value(vid) {
1266             value.to_type(self.tcx)
1267         } else {
1268             Ty::new_int_var(self.tcx, inner.int_unification_table().find(vid))
1269         }
1270     }
1271 
1272     /// Resolves a float var to a rigid int type, if it was constrained to one,
1273     /// or else the root float var in the unification table.
opportunistic_resolve_float_var(&self, vid: ty::FloatVid) -> Ty<'tcx>1274     pub fn opportunistic_resolve_float_var(&self, vid: ty::FloatVid) -> Ty<'tcx> {
1275         let mut inner = self.inner.borrow_mut();
1276         if let Some(value) = inner.float_unification_table().probe_value(vid) {
1277             value.to_type(self.tcx)
1278         } else {
1279             Ty::new_float_var(self.tcx, inner.float_unification_table().find(vid))
1280         }
1281     }
1282 
1283     /// Where possible, replaces type/const variables in
1284     /// `value` with their final value. Note that region variables
1285     /// are unaffected. If a type/const variable has not been unified, it
1286     /// is left as is. This is an idempotent operation that does
1287     /// not affect inference state in any way and so you can do it
1288     /// at will.
resolve_vars_if_possible<T>(&self, value: T) -> T where T: TypeFoldable<TyCtxt<'tcx>>,1289     pub fn resolve_vars_if_possible<T>(&self, value: T) -> T
1290     where
1291         T: TypeFoldable<TyCtxt<'tcx>>,
1292     {
1293         if !value.has_non_region_infer() {
1294             return value;
1295         }
1296         let mut r = resolve::OpportunisticVarResolver::new(self);
1297         value.fold_with(&mut r)
1298     }
1299 
resolve_numeric_literals_with_default<T>(&self, value: T) -> T where T: TypeFoldable<TyCtxt<'tcx>>,1300     pub fn resolve_numeric_literals_with_default<T>(&self, value: T) -> T
1301     where
1302         T: TypeFoldable<TyCtxt<'tcx>>,
1303     {
1304         if !value.has_infer() {
1305             return value; // Avoid duplicated subst-folding.
1306         }
1307         let mut r = InferenceLiteralEraser { tcx: self.tcx };
1308         value.fold_with(&mut r)
1309     }
1310 
1311     /// Returns the first unresolved type or const variable contained in `T`.
first_unresolved_const_or_ty_var<T>( &self, value: &T, ) -> Option<(ty::Term<'tcx>, Option<Span>)> where T: TypeVisitable<TyCtxt<'tcx>>,1312     pub fn first_unresolved_const_or_ty_var<T>(
1313         &self,
1314         value: &T,
1315     ) -> Option<(ty::Term<'tcx>, Option<Span>)>
1316     where
1317         T: TypeVisitable<TyCtxt<'tcx>>,
1318     {
1319         value.visit_with(&mut resolve::UnresolvedTypeOrConstFinder::new(self)).break_value()
1320     }
1321 
probe_const_var( &self, vid: ty::ConstVid<'tcx>, ) -> Result<ty::Const<'tcx>, ty::UniverseIndex>1322     pub fn probe_const_var(
1323         &self,
1324         vid: ty::ConstVid<'tcx>,
1325     ) -> Result<ty::Const<'tcx>, ty::UniverseIndex> {
1326         match self.inner.borrow_mut().const_unification_table().probe_value(vid).val {
1327             ConstVariableValue::Known { value } => Ok(value),
1328             ConstVariableValue::Unknown { universe } => Err(universe),
1329         }
1330     }
1331 
1332     /// Attempts to resolve all type/region/const variables in
1333     /// `value`. Region inference must have been run already (e.g.,
1334     /// by calling `resolve_regions_and_report_errors`). If some
1335     /// variable was never unified, an `Err` results.
1336     ///
1337     /// This method is idempotent, but it not typically not invoked
1338     /// except during the writeback phase.
fully_resolve<T: TypeFoldable<TyCtxt<'tcx>>>(&self, value: T) -> FixupResult<'tcx, T>1339     pub fn fully_resolve<T: TypeFoldable<TyCtxt<'tcx>>>(&self, value: T) -> FixupResult<'tcx, T> {
1340         let value = resolve::fully_resolve(self, value);
1341         assert!(
1342             value.as_ref().map_or(true, |value| !value.has_infer()),
1343             "`{value:?}` is not fully resolved"
1344         );
1345         value
1346     }
1347 
1348     // Instantiates the bound variables in a given binder with fresh inference
1349     // variables in the current universe.
1350     //
1351     // Use this method if you'd like to find some substitution of the binder's
1352     // variables (e.g. during a method call). If there isn't a [`LateBoundRegionConversionTime`]
1353     // that corresponds to your use case, consider whether or not you should
1354     // use [`InferCtxt::instantiate_binder_with_placeholders`] instead.
instantiate_binder_with_fresh_vars<T>( &self, span: Span, lbrct: LateBoundRegionConversionTime, value: ty::Binder<'tcx, T>, ) -> T where T: TypeFoldable<TyCtxt<'tcx>> + Copy,1355     pub fn instantiate_binder_with_fresh_vars<T>(
1356         &self,
1357         span: Span,
1358         lbrct: LateBoundRegionConversionTime,
1359         value: ty::Binder<'tcx, T>,
1360     ) -> T
1361     where
1362         T: TypeFoldable<TyCtxt<'tcx>> + Copy,
1363     {
1364         if let Some(inner) = value.no_bound_vars() {
1365             return inner;
1366         }
1367 
1368         struct ToFreshVars<'a, 'tcx> {
1369             infcx: &'a InferCtxt<'tcx>,
1370             span: Span,
1371             lbrct: LateBoundRegionConversionTime,
1372             map: FxHashMap<ty::BoundVar, ty::GenericArg<'tcx>>,
1373         }
1374 
1375         impl<'tcx> BoundVarReplacerDelegate<'tcx> for ToFreshVars<'_, 'tcx> {
1376             fn replace_region(&mut self, br: ty::BoundRegion) -> ty::Region<'tcx> {
1377                 self.map
1378                     .entry(br.var)
1379                     .or_insert_with(|| {
1380                         self.infcx
1381                             .next_region_var(LateBoundRegion(self.span, br.kind, self.lbrct))
1382                             .into()
1383                     })
1384                     .expect_region()
1385             }
1386             fn replace_ty(&mut self, bt: ty::BoundTy) -> Ty<'tcx> {
1387                 self.map
1388                     .entry(bt.var)
1389                     .or_insert_with(|| {
1390                         self.infcx
1391                             .next_ty_var(TypeVariableOrigin {
1392                                 kind: TypeVariableOriginKind::MiscVariable,
1393                                 span: self.span,
1394                             })
1395                             .into()
1396                     })
1397                     .expect_ty()
1398             }
1399             fn replace_const(&mut self, bv: ty::BoundVar, ty: Ty<'tcx>) -> ty::Const<'tcx> {
1400                 self.map
1401                     .entry(bv)
1402                     .or_insert_with(|| {
1403                         self.infcx
1404                             .next_const_var(
1405                                 ty,
1406                                 ConstVariableOrigin {
1407                                     kind: ConstVariableOriginKind::MiscVariable,
1408                                     span: self.span,
1409                                 },
1410                             )
1411                             .into()
1412                     })
1413                     .expect_const()
1414             }
1415         }
1416         let delegate = ToFreshVars { infcx: self, span, lbrct, map: Default::default() };
1417         self.tcx.replace_bound_vars_uncached(value, delegate)
1418     }
1419 
1420     /// See the [`region_constraints::RegionConstraintCollector::verify_generic_bound`] method.
verify_generic_bound( &self, origin: SubregionOrigin<'tcx>, kind: GenericKind<'tcx>, a: ty::Region<'tcx>, bound: VerifyBound<'tcx>, )1421     pub fn verify_generic_bound(
1422         &self,
1423         origin: SubregionOrigin<'tcx>,
1424         kind: GenericKind<'tcx>,
1425         a: ty::Region<'tcx>,
1426         bound: VerifyBound<'tcx>,
1427     ) {
1428         debug!("verify_generic_bound({:?}, {:?} <: {:?})", kind, a, bound);
1429 
1430         self.inner
1431             .borrow_mut()
1432             .unwrap_region_constraints()
1433             .verify_generic_bound(origin, kind, a, bound);
1434     }
1435 
1436     /// Obtains the latest type of the given closure; this may be a
1437     /// closure in the current function, in which case its
1438     /// `ClosureKind` may not yet be known.
closure_kind(&self, closure_substs: SubstsRef<'tcx>) -> Option<ty::ClosureKind>1439     pub fn closure_kind(&self, closure_substs: SubstsRef<'tcx>) -> Option<ty::ClosureKind> {
1440         let closure_kind_ty = closure_substs.as_closure().kind_ty();
1441         let closure_kind_ty = self.shallow_resolve(closure_kind_ty);
1442         closure_kind_ty.to_opt_closure_kind()
1443     }
1444 
1445     /// Clears the selection, evaluation, and projection caches. This is useful when
1446     /// repeatedly attempting to select an `Obligation` while changing only
1447     /// its `ParamEnv`, since `FulfillmentContext` doesn't use probing.
clear_caches(&self)1448     pub fn clear_caches(&self) {
1449         self.selection_cache.clear();
1450         self.evaluation_cache.clear();
1451         self.inner.borrow_mut().projection_cache().clear();
1452     }
1453 
universe(&self) -> ty::UniverseIndex1454     pub fn universe(&self) -> ty::UniverseIndex {
1455         self.universe.get()
1456     }
1457 
1458     /// Creates and return a fresh universe that extends all previous
1459     /// universes. Updates `self.universe` to that new universe.
create_next_universe(&self) -> ty::UniverseIndex1460     pub fn create_next_universe(&self) -> ty::UniverseIndex {
1461         let u = self.universe.get().next_universe();
1462         debug!("create_next_universe {u:?}");
1463         self.universe.set(u);
1464         u
1465     }
1466 
try_const_eval_resolve( &self, param_env: ty::ParamEnv<'tcx>, unevaluated: ty::UnevaluatedConst<'tcx>, ty: Ty<'tcx>, span: Option<Span>, ) -> Result<ty::Const<'tcx>, ErrorHandled>1467     pub fn try_const_eval_resolve(
1468         &self,
1469         param_env: ty::ParamEnv<'tcx>,
1470         unevaluated: ty::UnevaluatedConst<'tcx>,
1471         ty: Ty<'tcx>,
1472         span: Option<Span>,
1473     ) -> Result<ty::Const<'tcx>, ErrorHandled> {
1474         match self.const_eval_resolve(param_env, unevaluated, span) {
1475             Ok(Some(val)) => Ok(ty::Const::new_value(self.tcx, val, ty)),
1476             Ok(None) => {
1477                 let tcx = self.tcx;
1478                 let def_id = unevaluated.def;
1479                 span_bug!(
1480                     tcx.def_span(def_id),
1481                     "unable to construct a constant value for the unevaluated constant {:?}",
1482                     unevaluated
1483                 );
1484             }
1485             Err(err) => Err(err),
1486         }
1487     }
1488 
1489     /// Resolves and evaluates a constant.
1490     ///
1491     /// The constant can be located on a trait like `<A as B>::C`, in which case the given
1492     /// substitutions and environment are used to resolve the constant. Alternatively if the
1493     /// constant has generic parameters in scope the substitutions are used to evaluate the value of
1494     /// the constant. For example in `fn foo<T>() { let _ = [0; bar::<T>()]; }` the repeat count
1495     /// constant `bar::<T>()` requires a substitution for `T`, if the substitution for `T` is still
1496     /// too generic for the constant to be evaluated then `Err(ErrorHandled::TooGeneric)` is
1497     /// returned.
1498     ///
1499     /// This handles inferences variables within both `param_env` and `substs` by
1500     /// performing the operation on their respective canonical forms.
1501     #[instrument(skip(self), level = "debug")]
const_eval_resolve( &self, mut param_env: ty::ParamEnv<'tcx>, unevaluated: ty::UnevaluatedConst<'tcx>, span: Option<Span>, ) -> EvalToValTreeResult<'tcx>1502     pub fn const_eval_resolve(
1503         &self,
1504         mut param_env: ty::ParamEnv<'tcx>,
1505         unevaluated: ty::UnevaluatedConst<'tcx>,
1506         span: Option<Span>,
1507     ) -> EvalToValTreeResult<'tcx> {
1508         let mut substs = self.resolve_vars_if_possible(unevaluated.substs);
1509         debug!(?substs);
1510 
1511         // Postpone the evaluation of constants whose substs depend on inference
1512         // variables
1513         let tcx = self.tcx;
1514         if substs.has_non_region_infer() {
1515             if let Some(ct) = tcx.thir_abstract_const(unevaluated.def)? {
1516                 let ct = tcx.expand_abstract_consts(ct.subst(tcx, substs));
1517                 if let Err(e) = ct.error_reported() {
1518                     return Err(ErrorHandled::Reported(e.into()));
1519                 } else if ct.has_non_region_infer() || ct.has_non_region_param() {
1520                     return Err(ErrorHandled::TooGeneric);
1521                 } else {
1522                     substs = replace_param_and_infer_substs_with_placeholder(tcx, substs);
1523                 }
1524             } else {
1525                 substs = InternalSubsts::identity_for_item(tcx, unevaluated.def);
1526                 param_env = tcx.param_env(unevaluated.def);
1527             }
1528         }
1529 
1530         let param_env_erased = tcx.erase_regions(param_env);
1531         let substs_erased = tcx.erase_regions(substs);
1532         debug!(?param_env_erased);
1533         debug!(?substs_erased);
1534 
1535         let unevaluated = ty::UnevaluatedConst { def: unevaluated.def, substs: substs_erased };
1536 
1537         // The return value is the evaluated value which doesn't contain any reference to inference
1538         // variables, thus we don't need to substitute back the original values.
1539         tcx.const_eval_resolve_for_typeck(param_env_erased, unevaluated, span)
1540     }
1541 
1542     /// The returned function is used in a fast path. If it returns `true` the variable is
1543     /// unchanged, `false` indicates that the status is unknown.
1544     #[inline]
is_ty_infer_var_definitely_unchanged<'a>( &'a self, ) -> (impl Fn(TyOrConstInferVar<'tcx>) -> bool + 'a)1545     pub fn is_ty_infer_var_definitely_unchanged<'a>(
1546         &'a self,
1547     ) -> (impl Fn(TyOrConstInferVar<'tcx>) -> bool + 'a) {
1548         // This hoists the borrow/release out of the loop body.
1549         let inner = self.inner.try_borrow();
1550 
1551         return move |infer_var: TyOrConstInferVar<'tcx>| match (infer_var, &inner) {
1552             (TyOrConstInferVar::Ty(ty_var), Ok(inner)) => {
1553                 use self::type_variable::TypeVariableValue;
1554 
1555                 matches!(
1556                     inner.try_type_variables_probe_ref(ty_var),
1557                     Some(TypeVariableValue::Unknown { .. })
1558                 )
1559             }
1560             _ => false,
1561         };
1562     }
1563 
1564     /// `ty_or_const_infer_var_changed` is equivalent to one of these two:
1565     ///   * `shallow_resolve(ty) != ty` (where `ty.kind = ty::Infer(_)`)
1566     ///   * `shallow_resolve(ct) != ct` (where `ct.kind = ty::ConstKind::Infer(_)`)
1567     ///
1568     /// However, `ty_or_const_infer_var_changed` is more efficient. It's always
1569     /// inlined, despite being large, because it has only two call sites that
1570     /// are extremely hot (both in `traits::fulfill`'s checking of `stalled_on`
1571     /// inference variables), and it handles both `Ty` and `ty::Const` without
1572     /// having to resort to storing full `GenericArg`s in `stalled_on`.
1573     #[inline(always)]
ty_or_const_infer_var_changed(&self, infer_var: TyOrConstInferVar<'tcx>) -> bool1574     pub fn ty_or_const_infer_var_changed(&self, infer_var: TyOrConstInferVar<'tcx>) -> bool {
1575         match infer_var {
1576             TyOrConstInferVar::Ty(v) => {
1577                 use self::type_variable::TypeVariableValue;
1578 
1579                 // If `inlined_probe` returns a `Known` value, it never equals
1580                 // `ty::Infer(ty::TyVar(v))`.
1581                 match self.inner.borrow_mut().type_variables().inlined_probe(v) {
1582                     TypeVariableValue::Unknown { .. } => false,
1583                     TypeVariableValue::Known { .. } => true,
1584                 }
1585             }
1586 
1587             TyOrConstInferVar::TyInt(v) => {
1588                 // If `inlined_probe_value` returns a value it's always a
1589                 // `ty::Int(_)` or `ty::UInt(_)`, which never matches a
1590                 // `ty::Infer(_)`.
1591                 self.inner.borrow_mut().int_unification_table().inlined_probe_value(v).is_some()
1592             }
1593 
1594             TyOrConstInferVar::TyFloat(v) => {
1595                 // If `probe_value` returns a value it's always a
1596                 // `ty::Float(_)`, which never matches a `ty::Infer(_)`.
1597                 //
1598                 // Not `inlined_probe_value(v)` because this call site is colder.
1599                 self.inner.borrow_mut().float_unification_table().probe_value(v).is_some()
1600             }
1601 
1602             TyOrConstInferVar::Const(v) => {
1603                 // If `probe_value` returns a `Known` value, it never equals
1604                 // `ty::ConstKind::Infer(ty::InferConst::Var(v))`.
1605                 //
1606                 // Not `inlined_probe_value(v)` because this call site is colder.
1607                 match self.inner.borrow_mut().const_unification_table().probe_value(v).val {
1608                     ConstVariableValue::Unknown { .. } => false,
1609                     ConstVariableValue::Known { .. } => true,
1610                 }
1611             }
1612         }
1613     }
1614 }
1615 
1616 impl<'tcx> TypeErrCtxt<'_, 'tcx> {
1617     // [Note-Type-error-reporting]
1618     // An invariant is that anytime the expected or actual type is Error (the special
1619     // error type, meaning that an error occurred when typechecking this expression),
1620     // this is a derived error. The error cascaded from another error (that was already
1621     // reported), so it's not useful to display it to the user.
1622     // The following methods implement this logic.
1623     // They check if either the actual or expected type is Error, and don't print the error
1624     // in this case. The typechecker should only ever report type errors involving mismatched
1625     // types using one of these methods, and should not call span_err directly for such
1626     // errors.
type_error_struct_with_diag<M>( &self, sp: Span, mk_diag: M, actual_ty: Ty<'tcx>, ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> where M: FnOnce(String) -> DiagnosticBuilder<'tcx, ErrorGuaranteed>,1627     pub fn type_error_struct_with_diag<M>(
1628         &self,
1629         sp: Span,
1630         mk_diag: M,
1631         actual_ty: Ty<'tcx>,
1632     ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed>
1633     where
1634         M: FnOnce(String) -> DiagnosticBuilder<'tcx, ErrorGuaranteed>,
1635     {
1636         let actual_ty = self.resolve_vars_if_possible(actual_ty);
1637         debug!("type_error_struct_with_diag({:?}, {:?})", sp, actual_ty);
1638 
1639         let mut err = mk_diag(self.ty_to_string(actual_ty));
1640 
1641         // Don't report an error if actual type is `Error`.
1642         if actual_ty.references_error() {
1643             err.downgrade_to_delayed_bug();
1644         }
1645 
1646         err
1647     }
1648 
report_mismatched_types( &self, cause: &ObligationCause<'tcx>, expected: Ty<'tcx>, actual: Ty<'tcx>, err: TypeError<'tcx>, ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed>1649     pub fn report_mismatched_types(
1650         &self,
1651         cause: &ObligationCause<'tcx>,
1652         expected: Ty<'tcx>,
1653         actual: Ty<'tcx>,
1654         err: TypeError<'tcx>,
1655     ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
1656         self.report_and_explain_type_error(TypeTrace::types(cause, true, expected, actual), err)
1657     }
1658 
report_mismatched_consts( &self, cause: &ObligationCause<'tcx>, expected: ty::Const<'tcx>, actual: ty::Const<'tcx>, err: TypeError<'tcx>, ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed>1659     pub fn report_mismatched_consts(
1660         &self,
1661         cause: &ObligationCause<'tcx>,
1662         expected: ty::Const<'tcx>,
1663         actual: ty::Const<'tcx>,
1664         err: TypeError<'tcx>,
1665     ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
1666         self.report_and_explain_type_error(TypeTrace::consts(cause, true, expected, actual), err)
1667     }
1668 }
1669 
1670 /// Helper for [InferCtxt::ty_or_const_infer_var_changed] (see comment on that), currently
1671 /// used only for `traits::fulfill`'s list of `stalled_on` inference variables.
1672 #[derive(Copy, Clone, Debug)]
1673 pub enum TyOrConstInferVar<'tcx> {
1674     /// Equivalent to `ty::Infer(ty::TyVar(_))`.
1675     Ty(TyVid),
1676     /// Equivalent to `ty::Infer(ty::IntVar(_))`.
1677     TyInt(IntVid),
1678     /// Equivalent to `ty::Infer(ty::FloatVar(_))`.
1679     TyFloat(FloatVid),
1680 
1681     /// Equivalent to `ty::ConstKind::Infer(ty::InferConst::Var(_))`.
1682     Const(ConstVid<'tcx>),
1683 }
1684 
1685 impl<'tcx> TyOrConstInferVar<'tcx> {
1686     /// Tries to extract an inference variable from a type or a constant, returns `None`
1687     /// for types other than `ty::Infer(_)` (or `InferTy::Fresh*`) and
1688     /// for constants other than `ty::ConstKind::Infer(_)` (or `InferConst::Fresh`).
maybe_from_generic_arg(arg: GenericArg<'tcx>) -> Option<Self>1689     pub fn maybe_from_generic_arg(arg: GenericArg<'tcx>) -> Option<Self> {
1690         match arg.unpack() {
1691             GenericArgKind::Type(ty) => Self::maybe_from_ty(ty),
1692             GenericArgKind::Const(ct) => Self::maybe_from_const(ct),
1693             GenericArgKind::Lifetime(_) => None,
1694         }
1695     }
1696 
1697     /// Tries to extract an inference variable from a type, returns `None`
1698     /// for types other than `ty::Infer(_)` (or `InferTy::Fresh*`).
maybe_from_ty(ty: Ty<'tcx>) -> Option<Self>1699     fn maybe_from_ty(ty: Ty<'tcx>) -> Option<Self> {
1700         match *ty.kind() {
1701             ty::Infer(ty::TyVar(v)) => Some(TyOrConstInferVar::Ty(v)),
1702             ty::Infer(ty::IntVar(v)) => Some(TyOrConstInferVar::TyInt(v)),
1703             ty::Infer(ty::FloatVar(v)) => Some(TyOrConstInferVar::TyFloat(v)),
1704             _ => None,
1705         }
1706     }
1707 
1708     /// Tries to extract an inference variable from a constant, returns `None`
1709     /// for constants other than `ty::ConstKind::Infer(_)` (or `InferConst::Fresh`).
maybe_from_const(ct: ty::Const<'tcx>) -> Option<Self>1710     fn maybe_from_const(ct: ty::Const<'tcx>) -> Option<Self> {
1711         match ct.kind() {
1712             ty::ConstKind::Infer(InferConst::Var(v)) => Some(TyOrConstInferVar::Const(v)),
1713             _ => None,
1714         }
1715     }
1716 }
1717 
1718 /// Replace `{integer}` with `i32` and `{float}` with `f64`.
1719 /// Used only for diagnostics.
1720 struct InferenceLiteralEraser<'tcx> {
1721     tcx: TyCtxt<'tcx>,
1722 }
1723 
1724 impl<'tcx> TypeFolder<TyCtxt<'tcx>> for InferenceLiteralEraser<'tcx> {
interner(&self) -> TyCtxt<'tcx>1725     fn interner(&self) -> TyCtxt<'tcx> {
1726         self.tcx
1727     }
1728 
fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx>1729     fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
1730         match ty.kind() {
1731             ty::Infer(ty::IntVar(_) | ty::FreshIntTy(_)) => self.tcx.types.i32,
1732             ty::Infer(ty::FloatVar(_) | ty::FreshFloatTy(_)) => self.tcx.types.f64,
1733             _ => ty.super_fold_with(self),
1734         }
1735     }
1736 }
1737 
1738 struct ShallowResolver<'a, 'tcx> {
1739     infcx: &'a InferCtxt<'tcx>,
1740 }
1741 
1742 impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for ShallowResolver<'a, 'tcx> {
interner(&self) -> TyCtxt<'tcx>1743     fn interner(&self) -> TyCtxt<'tcx> {
1744         self.infcx.tcx
1745     }
1746 
1747     /// If `ty` is a type variable of some kind, resolve it one level
1748     /// (but do not resolve types found in the result). If `typ` is
1749     /// not a type variable, just return it unmodified.
1750     #[inline]
fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx>1751     fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
1752         if let ty::Infer(v) = ty.kind() { self.fold_infer_ty(*v).unwrap_or(ty) } else { ty }
1753     }
1754 
fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx>1755     fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
1756         if let ty::ConstKind::Infer(InferConst::Var(vid)) = ct.kind() {
1757             self.infcx
1758                 .inner
1759                 .borrow_mut()
1760                 .const_unification_table()
1761                 .probe_value(vid)
1762                 .val
1763                 .known()
1764                 .unwrap_or(ct)
1765         } else {
1766             ct
1767         }
1768     }
1769 }
1770 
1771 impl<'a, 'tcx> ShallowResolver<'a, 'tcx> {
1772     // This is separate from `fold_ty` to keep that method small and inlinable.
1773     #[inline(never)]
fold_infer_ty(&mut self, v: InferTy) -> Option<Ty<'tcx>>1774     fn fold_infer_ty(&mut self, v: InferTy) -> Option<Ty<'tcx>> {
1775         match v {
1776             ty::TyVar(v) => {
1777                 // Not entirely obvious: if `typ` is a type variable,
1778                 // it can be resolved to an int/float variable, which
1779                 // can then be recursively resolved, hence the
1780                 // recursion. Note though that we prevent type
1781                 // variables from unifying to other type variables
1782                 // directly (though they may be embedded
1783                 // structurally), and we prevent cycles in any case,
1784                 // so this recursion should always be of very limited
1785                 // depth.
1786                 //
1787                 // Note: if these two lines are combined into one we get
1788                 // dynamic borrow errors on `self.inner`.
1789                 let known = self.infcx.inner.borrow_mut().type_variables().probe(v).known();
1790                 known.map(|t| self.fold_ty(t))
1791             }
1792 
1793             ty::IntVar(v) => self
1794                 .infcx
1795                 .inner
1796                 .borrow_mut()
1797                 .int_unification_table()
1798                 .probe_value(v)
1799                 .map(|v| v.to_type(self.infcx.tcx)),
1800 
1801             ty::FloatVar(v) => self
1802                 .infcx
1803                 .inner
1804                 .borrow_mut()
1805                 .float_unification_table()
1806                 .probe_value(v)
1807                 .map(|v| v.to_type(self.infcx.tcx)),
1808 
1809             ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => None,
1810         }
1811     }
1812 }
1813 
1814 impl<'tcx> TypeTrace<'tcx> {
span(&self) -> Span1815     pub fn span(&self) -> Span {
1816         self.cause.span
1817     }
1818 
types( cause: &ObligationCause<'tcx>, a_is_expected: bool, a: Ty<'tcx>, b: Ty<'tcx>, ) -> TypeTrace<'tcx>1819     pub fn types(
1820         cause: &ObligationCause<'tcx>,
1821         a_is_expected: bool,
1822         a: Ty<'tcx>,
1823         b: Ty<'tcx>,
1824     ) -> TypeTrace<'tcx> {
1825         TypeTrace {
1826             cause: cause.clone(),
1827             values: Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())),
1828         }
1829     }
1830 
poly_trait_refs( cause: &ObligationCause<'tcx>, a_is_expected: bool, a: ty::PolyTraitRef<'tcx>, b: ty::PolyTraitRef<'tcx>, ) -> TypeTrace<'tcx>1831     pub fn poly_trait_refs(
1832         cause: &ObligationCause<'tcx>,
1833         a_is_expected: bool,
1834         a: ty::PolyTraitRef<'tcx>,
1835         b: ty::PolyTraitRef<'tcx>,
1836     ) -> TypeTrace<'tcx> {
1837         TypeTrace {
1838             cause: cause.clone(),
1839             values: PolyTraitRefs(ExpectedFound::new(a_is_expected, a, b)),
1840         }
1841     }
1842 
consts( cause: &ObligationCause<'tcx>, a_is_expected: bool, a: ty::Const<'tcx>, b: ty::Const<'tcx>, ) -> TypeTrace<'tcx>1843     pub fn consts(
1844         cause: &ObligationCause<'tcx>,
1845         a_is_expected: bool,
1846         a: ty::Const<'tcx>,
1847         b: ty::Const<'tcx>,
1848     ) -> TypeTrace<'tcx> {
1849         TypeTrace {
1850             cause: cause.clone(),
1851             values: Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())),
1852         }
1853     }
1854 }
1855 
1856 impl<'tcx> SubregionOrigin<'tcx> {
span(&self) -> Span1857     pub fn span(&self) -> Span {
1858         match *self {
1859             Subtype(ref a) => a.span(),
1860             RelateObjectBound(a) => a,
1861             RelateParamBound(a, ..) => a,
1862             RelateRegionParamBound(a) => a,
1863             Reborrow(a) => a,
1864             ReferenceOutlivesReferent(_, a) => a,
1865             CompareImplItemObligation { span, .. } => span,
1866             AscribeUserTypeProvePredicate(span) => span,
1867             CheckAssociatedTypeBounds { ref parent, .. } => parent.span(),
1868         }
1869     }
1870 
from_obligation_cause<F>(cause: &traits::ObligationCause<'tcx>, default: F) -> Self where F: FnOnce() -> Self,1871     pub fn from_obligation_cause<F>(cause: &traits::ObligationCause<'tcx>, default: F) -> Self
1872     where
1873         F: FnOnce() -> Self,
1874     {
1875         match *cause.code() {
1876             traits::ObligationCauseCode::ReferenceOutlivesReferent(ref_type) => {
1877                 SubregionOrigin::ReferenceOutlivesReferent(ref_type, cause.span)
1878             }
1879 
1880             traits::ObligationCauseCode::CompareImplItemObligation {
1881                 impl_item_def_id,
1882                 trait_item_def_id,
1883                 kind: _,
1884             } => SubregionOrigin::CompareImplItemObligation {
1885                 span: cause.span,
1886                 impl_item_def_id,
1887                 trait_item_def_id,
1888             },
1889 
1890             traits::ObligationCauseCode::CheckAssociatedTypeBounds {
1891                 impl_item_def_id,
1892                 trait_item_def_id,
1893             } => SubregionOrigin::CheckAssociatedTypeBounds {
1894                 impl_item_def_id,
1895                 trait_item_def_id,
1896                 parent: Box::new(default()),
1897             },
1898 
1899             traits::ObligationCauseCode::AscribeUserTypeProvePredicate(span) => {
1900                 SubregionOrigin::AscribeUserTypeProvePredicate(span)
1901             }
1902 
1903             _ => default(),
1904         }
1905     }
1906 }
1907 
1908 impl RegionVariableOrigin {
span(&self) -> Span1909     pub fn span(&self) -> Span {
1910         match *self {
1911             MiscVariable(a)
1912             | PatternRegion(a)
1913             | AddrOfRegion(a)
1914             | Autoref(a)
1915             | Coercion(a)
1916             | EarlyBoundRegion(a, ..)
1917             | LateBoundRegion(a, ..)
1918             | UpvarRegion(_, a) => a,
1919             Nll(..) => bug!("NLL variable used with `span`"),
1920         }
1921     }
1922 }
1923 
1924 /// Replaces substs that reference param or infer variables with suitable
1925 /// placeholders. This function is meant to remove these param and infer
1926 /// substs when they're not actually needed to evaluate a constant.
replace_param_and_infer_substs_with_placeholder<'tcx>( tcx: TyCtxt<'tcx>, substs: SubstsRef<'tcx>, ) -> SubstsRef<'tcx>1927 fn replace_param_and_infer_substs_with_placeholder<'tcx>(
1928     tcx: TyCtxt<'tcx>,
1929     substs: SubstsRef<'tcx>,
1930 ) -> SubstsRef<'tcx> {
1931     struct ReplaceParamAndInferWithPlaceholder<'tcx> {
1932         tcx: TyCtxt<'tcx>,
1933         idx: u32,
1934     }
1935 
1936     impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ReplaceParamAndInferWithPlaceholder<'tcx> {
1937         fn interner(&self) -> TyCtxt<'tcx> {
1938             self.tcx
1939         }
1940 
1941         fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
1942             if let ty::Infer(_) = t.kind() {
1943                 let idx = {
1944                     let idx = self.idx;
1945                     self.idx += 1;
1946                     idx
1947                 };
1948                 Ty::new_placeholder(
1949                     self.tcx,
1950                     ty::PlaceholderType {
1951                         universe: ty::UniverseIndex::ROOT,
1952                         bound: ty::BoundTy {
1953                             var: ty::BoundVar::from_u32(idx),
1954                             kind: ty::BoundTyKind::Anon,
1955                         },
1956                     },
1957                 )
1958             } else {
1959                 t.super_fold_with(self)
1960             }
1961         }
1962 
1963         fn fold_const(&mut self, c: ty::Const<'tcx>) -> ty::Const<'tcx> {
1964             if let ty::ConstKind::Infer(_) = c.kind() {
1965                 let ty = c.ty();
1966                 // If the type references param or infer then ICE ICE ICE
1967                 if ty.has_non_region_param() || ty.has_non_region_infer() {
1968                     bug!("const `{c}`'s type should not reference params or types");
1969                 }
1970                 ty::Const::new_placeholder(
1971                     self.tcx,
1972                     ty::PlaceholderConst {
1973                         universe: ty::UniverseIndex::ROOT,
1974                         bound: ty::BoundVar::from_u32({
1975                             let idx = self.idx;
1976                             self.idx += 1;
1977                             idx
1978                         }),
1979                     },
1980                     ty,
1981                 )
1982             } else {
1983                 c.super_fold_with(self)
1984             }
1985         }
1986     }
1987 
1988     substs.fold_with(&mut ReplaceParamAndInferWithPlaceholder { tcx, idx: 0 })
1989 }
1990