1 //! See the Book for more information.
3 pub use self::freshen::TypeFreshener;
4 pub use self::LateBoundRegionConversionTime::*;
5 pub use self::RegionVariableOrigin::*;
6 pub use self::SubregionOrigin::*;
7 pub use self::ValuePairs::*;
9 pub(crate) use self::undo_log::{InferCtxtUndoLogs, Snapshot, UndoLog};
11 use crate::traits::{self, ObligationCause, PredicateObligations, TraitEngine};
13 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
14 use rustc_data_structures::sync::Lrc;
15 use rustc_data_structures::undo_log::Rollback;
16 use rustc_data_structures::unify as ut;
17 use rustc_errors::DiagnosticBuilder;
19 use rustc_hir::def_id::{DefId, LocalDefId};
20 use rustc_middle::infer::canonical::{Canonical, CanonicalVarValues};
21 use rustc_middle::infer::unify_key::{ConstVarValue, ConstVariableValue};
22 use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind, ToType};
23 use rustc_middle::middle::region;
24 use rustc_middle::mir;
25 use rustc_middle::mir::interpret::ConstEvalResult;
26 use rustc_middle::traits::select;
27 use rustc_middle::ty::error::{ExpectedFound, TypeError, UnconstrainedNumeric};
28 use rustc_middle::ty::fold::{TypeFoldable, TypeFolder};
29 use rustc_middle::ty::relate::RelateResult;
30 use rustc_middle::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, SubstsRef};
31 pub use rustc_middle::ty::IntVarValue;
32 use rustc_middle::ty::{self, GenericParamDefKind, InferConst, Ty, TyCtxt};
33 use rustc_middle::ty::{ConstVid, FloatVid, IntVid, TyVid};
34 use rustc_session::config::BorrowckMode;
35 use rustc_span::symbol::Symbol;
38 use std::cell::{Cell, Ref, RefCell};
39 use std::collections::BTreeMap;
42 use self::combine::CombineFields;
43 use self::free_regions::RegionRelations;
44 use self::lexical_region_resolve::LexicalRegionResolutions;
45 use self::outlives::env::OutlivesEnvironment;
46 use self::region_constraints::{GenericKind, RegionConstraintData, VarInfos, VerifyBound};
47 use self::region_constraints::{
48 RegionConstraintCollector, RegionConstraintStorage, RegionSnapshot,
50 use self::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
56 pub mod error_reporting;
63 mod lexical_region_resolve;
67 pub mod region_constraints;
70 pub mod type_variable;
73 use crate::infer::canonical::OriginalQueryValues;
74 pub use rustc_middle::infer::unify_key;
78 pub struct InferOk<'tcx, T> {
80 pub obligations: PredicateObligations<'tcx>,
82 pub type InferResult<'tcx, T> = Result<InferOk<'tcx, T>, TypeError<'tcx>>;
84 pub type Bound<T> = Option<T>;
85 pub type UnitResult<'tcx> = RelateResult<'tcx, ()>; // "unify result"
86 pub type FixupResult<'tcx, T> = Result<T, FixupError<'tcx>>; // "fixup result"
88 pub(crate) type UnificationTable<'a, 'tcx, T> = ut::UnificationTable<
89 ut::InPlace<T, &'a mut ut::UnificationStorage<T>, &'a mut InferCtxtUndoLogs<'tcx>>,
92 /// How we should handle region solving.
94 /// This is used so that the region values inferred by HIR region solving are
95 /// not exposed, and so that we can avoid doing work in HIR typeck that MIR
96 /// typeck will also do.
97 #[derive(Copy, Clone, Debug)]
98 pub enum RegionckMode {
99 /// The default mode: report region errors, don't erase regions.
101 /// Erase the results of region after solving.
103 /// A flag that is used to suppress region errors, when we are doing
104 /// region checks that the NLL borrow checker will also do -- it might
106 suppress_errors: bool,
110 impl Default for RegionckMode {
111 fn default() -> Self {
117 pub fn suppressed(self) -> bool {
119 Self::Solve => false,
120 Self::Erase { suppress_errors } => suppress_errors,
124 /// Indicates that the MIR borrowck will repeat these region
125 /// checks, so we should ignore errors if NLL is (unconditionally)
127 pub fn for_item_body(tcx: TyCtxt<'_>) -> Self {
128 // FIXME(Centril): Once we actually remove `::Migrate` also make
129 // this always `true` and then proceed to eliminate the dead code.
130 match tcx.borrowck_mode() {
131 // If we're on Migrate mode, report AST region errors
132 BorrowckMode::Migrate => RegionckMode::Erase { suppress_errors: false },
134 // If we're on MIR, don't report AST region errors as they should be reported by NLL
135 BorrowckMode::Mir => RegionckMode::Erase { suppress_errors: true },
140 /// This type contains all the things within `InferCtxt` that sit within a
141 /// `RefCell` and are involved with taking/rolling back snapshots. Snapshot
142 /// operations are hot enough that we want only one call to `borrow_mut` per
143 /// call to `start_snapshot` and `rollback_to`.
144 pub struct InferCtxtInner<'tcx> {
145 /// Cache for projections. This cache is snapshotted along with the infcx.
147 /// Public so that `traits::project` can use it.
148 pub projection_cache: traits::ProjectionCacheStorage<'tcx>,
150 /// We instantiate `UnificationTable` with `bounds<Ty>` because the types
151 /// that might instantiate a general type variable have an order,
152 /// represented by its upper and lower bounds.
153 type_variable_storage: type_variable::TypeVariableStorage<'tcx>,
155 /// Map from const parameter variable to the kind of const it represents.
156 const_unification_storage: ut::UnificationTableStorage<ty::ConstVid<'tcx>>,
158 /// Map from integral variable to the kind of integer it represents.
159 int_unification_storage: ut::UnificationTableStorage<ty::IntVid>,
161 /// Map from floating variable to the kind of float it represents.
162 float_unification_storage: ut::UnificationTableStorage<ty::FloatVid>,
164 /// Tracks the set of region variables and the constraints between them.
165 /// This is initially `Some(_)` but when
166 /// `resolve_regions_and_report_errors` is invoked, this gets set to `None`
167 /// -- further attempts to perform unification, etc., may fail if new
168 /// region constraints would've been added.
169 region_constraint_storage: Option<RegionConstraintStorage<'tcx>>,
171 /// A set of constraints that regionck must validate. Each
172 /// constraint has the form `T:'a`, meaning "some type `T` must
173 /// outlive the lifetime 'a". These constraints derive from
174 /// instantiated type parameters. So if you had a struct defined
177 /// struct Foo<T:'static> { ... }
179 /// then in some expression `let x = Foo { ... }` it will
180 /// instantiate the type parameter `T` with a fresh type `$0`. At
181 /// the same time, it will record a region obligation of
182 /// `$0:'static`. This will get checked later by regionck. (We
183 /// can't generally check these things right away because we have
184 /// to wait until types are resolved.)
186 /// These are stored in a map keyed to the id of the innermost
187 /// enclosing fn body / static initializer expression. This is
188 /// because the location where the obligation was incurred can be
189 /// relevant with respect to which sublifetime assumptions are in
190 /// place. The reason that we store under the fn-id, and not
191 /// something more fine-grained, is so that it is easier for
192 /// regionck to be sure that it has found *all* the region
193 /// obligations (otherwise, it's easy to fail to walk to a
194 /// particular node-id).
196 /// Before running `resolve_regions_and_report_errors`, the creator
197 /// of the inference context is expected to invoke
198 /// `process_region_obligations` (defined in `self::region_obligations`)
199 /// for each body-id in this map, which will process the
200 /// obligations within. This is expected to be done 'late enough'
201 /// that all type inference variables have been bound and so forth.
202 region_obligations: Vec<(hir::HirId, RegionObligation<'tcx>)>,
204 undo_log: InferCtxtUndoLogs<'tcx>,
207 impl<'tcx> InferCtxtInner<'tcx> {
208 fn new() -> InferCtxtInner<'tcx> {
210 projection_cache: Default::default(),
211 type_variable_storage: type_variable::TypeVariableStorage::new(),
212 undo_log: InferCtxtUndoLogs::default(),
213 const_unification_storage: ut::UnificationTableStorage::new(),
214 int_unification_storage: ut::UnificationTableStorage::new(),
215 float_unification_storage: ut::UnificationTableStorage::new(),
216 region_constraint_storage: Some(RegionConstraintStorage::new()),
217 region_obligations: vec![],
221 pub fn region_obligations(&self) -> &[(hir::HirId, RegionObligation<'tcx>)] {
222 &self.region_obligations
225 pub fn projection_cache(&mut self) -> traits::ProjectionCache<'_, 'tcx> {
226 self.projection_cache.with_log(&mut self.undo_log)
229 fn type_variables(&mut self) -> type_variable::TypeVariableTable<'_, 'tcx> {
230 self.type_variable_storage.with_log(&mut self.undo_log)
233 fn int_unification_table(
235 ) -> ut::UnificationTable<
238 &mut ut::UnificationStorage<ty::IntVid>,
239 &mut InferCtxtUndoLogs<'tcx>,
242 self.int_unification_storage.with_log(&mut self.undo_log)
245 fn float_unification_table(
247 ) -> ut::UnificationTable<
250 &mut ut::UnificationStorage<ty::FloatVid>,
251 &mut InferCtxtUndoLogs<'tcx>,
254 self.float_unification_storage.with_log(&mut self.undo_log)
257 fn const_unification_table(
259 ) -> ut::UnificationTable<
262 &mut ut::UnificationStorage<ty::ConstVid<'tcx>>,
263 &mut InferCtxtUndoLogs<'tcx>,
266 self.const_unification_storage.with_log(&mut self.undo_log)
269 pub fn unwrap_region_constraints(&mut self) -> RegionConstraintCollector<'_, 'tcx> {
270 self.region_constraint_storage
272 .expect("region constraints already solved")
273 .with_log(&mut self.undo_log)
277 pub struct InferCtxt<'a, 'tcx> {
278 pub tcx: TyCtxt<'tcx>,
280 /// During type-checking/inference of a body, `in_progress_tables`
281 /// contains a reference to the tables being built up, which are
282 /// used for reading closure kinds/signatures as they are inferred,
283 /// and for error reporting logic to read arbitrary node types.
284 pub in_progress_tables: Option<&'a RefCell<ty::TypeckTables<'tcx>>>,
286 pub inner: RefCell<InferCtxtInner<'tcx>>,
288 /// If set, this flag causes us to skip the 'leak check' during
289 /// higher-ranked subtyping operations. This flag is a temporary one used
290 /// to manage the removal of the leak-check: for the time being, we still run the
291 /// leak-check, but we issue warnings. This flag can only be set to true
292 /// when entering a snapshot.
293 skip_leak_check: Cell<bool>,
295 /// Once region inference is done, the values for each variable.
296 lexical_region_resolutions: RefCell<Option<LexicalRegionResolutions<'tcx>>>,
298 /// Caches the results of trait selection. This cache is used
299 /// for things that have to do with the parameters in scope.
300 pub selection_cache: select::SelectionCache<'tcx>,
302 /// Caches the results of trait evaluation.
303 pub evaluation_cache: select::EvaluationCache<'tcx>,
305 /// the set of predicates on which errors have been reported, to
306 /// avoid reporting the same error twice.
307 pub reported_trait_errors: RefCell<FxHashMap<Span, Vec<ty::Predicate<'tcx>>>>,
309 pub reported_closure_mismatch: RefCell<FxHashSet<(Span, Option<Span>)>>,
311 /// When an error occurs, we want to avoid reporting "derived"
312 /// errors that are due to this original failure. Normally, we
313 /// handle this with the `err_count_on_creation` count, which
314 /// basically just tracks how many errors were reported when we
315 /// started type-checking a fn and checks to see if any new errors
316 /// have been reported since then. Not great, but it works.
318 /// However, when errors originated in other passes -- notably
319 /// resolve -- this heuristic breaks down. Therefore, we have this
320 /// auxiliary flag that one can set whenever one creates a
321 /// type-error that is due to an error in a prior pass.
323 /// Don't read this flag directly, call `is_tainted_by_errors()`
324 /// and `set_tainted_by_errors()`.
325 tainted_by_errors_flag: Cell<bool>,
327 /// Track how many errors were reported when this infcx is created.
328 /// If the number of errors increases, that's also a sign (line
329 /// `tained_by_errors`) to avoid reporting certain kinds of errors.
330 // FIXME(matthewjasper) Merge into `tainted_by_errors_flag`
331 err_count_on_creation: usize,
333 /// This flag is true while there is an active snapshot.
334 in_snapshot: Cell<bool>,
336 /// What is the innermost universe we have created? Starts out as
337 /// `UniverseIndex::root()` but grows from there as we enter
338 /// universal quantifiers.
340 /// N.B., at present, we exclude the universal quantifiers on the
341 /// item we are type-checking, and just consider those names as
342 /// part of the root universe. So this would only get incremented
343 /// when we enter into a higher-ranked (`for<..>`) type or trait
345 universe: Cell<ty::UniverseIndex>,
348 /// A map returned by `replace_bound_vars_with_placeholders()`
349 /// indicating the placeholder region that each late-bound region was
351 pub type PlaceholderMap<'tcx> = BTreeMap<ty::BoundRegion, ty::Region<'tcx>>;
353 /// See the `error_reporting` module for more details.
354 #[derive(Clone, Debug, PartialEq, Eq, TypeFoldable)]
355 pub enum ValuePairs<'tcx> {
356 Types(ExpectedFound<Ty<'tcx>>),
357 Regions(ExpectedFound<ty::Region<'tcx>>),
358 Consts(ExpectedFound<&'tcx ty::Const<'tcx>>),
359 TraitRefs(ExpectedFound<ty::TraitRef<'tcx>>),
360 PolyTraitRefs(ExpectedFound<ty::PolyTraitRef<'tcx>>),
363 /// The trace designates the path through inference that we took to
364 /// encounter an error or subtyping constraint.
366 /// See the `error_reporting` module for more details.
367 #[derive(Clone, Debug)]
368 pub struct TypeTrace<'tcx> {
369 cause: ObligationCause<'tcx>,
370 values: ValuePairs<'tcx>,
373 /// The origin of a `r1 <= r2` constraint.
375 /// See `error_reporting` module for more details
376 #[derive(Clone, Debug)]
377 pub enum SubregionOrigin<'tcx> {
378 /// Arose from a subtyping relation
379 Subtype(Box<TypeTrace<'tcx>>),
381 /// When casting `&'a T` to an `&'b Trait` object,
382 /// relating `'a` to `'b`
383 RelateObjectBound(Span),
385 /// Some type parameter was instantiated with the given type,
386 /// and that type must outlive some region.
387 RelateParamBound(Span, Ty<'tcx>),
389 /// The given region parameter was instantiated with a region
390 /// that must outlive some other region.
391 RelateRegionParamBound(Span),
393 /// Creating a pointer `b` to contents of another reference
396 /// Creating a pointer `b` to contents of an upvar
397 ReborrowUpvar(Span, ty::UpvarId),
399 /// Data with type `Ty<'tcx>` was borrowed
400 DataBorrowed(Ty<'tcx>, Span),
402 /// (&'a &'b T) where a >= b
403 ReferenceOutlivesReferent(Ty<'tcx>, Span),
405 /// Region in return type of invoked fn must enclose call
408 /// Comparing the signature and requirements of an impl method against
409 /// the containing trait.
410 CompareImplMethodObligation {
413 impl_item_def_id: DefId,
414 trait_item_def_id: DefId,
418 // `SubregionOrigin` is used a lot. Make sure it doesn't unintentionally get bigger.
419 #[cfg(target_arch = "x86_64")]
420 static_assert_size!(SubregionOrigin<'_>, 32);
422 /// Places that type/region parameters can appear.
423 #[derive(Clone, Copy, Debug)]
424 pub enum ParameterOrigin {
426 MethodCall, // foo.bar() <-- parameters on impl providing bar()
427 OverloadedOperator, // a + b when overloaded
428 OverloadedDeref, // *a when overloaded
431 /// Times when we replace late-bound regions with variables:
432 #[derive(Clone, Copy, Debug)]
433 pub enum LateBoundRegionConversionTime {
434 /// when a fn is called
437 /// when two higher-ranked types are compared
440 /// when projecting an associated type
441 AssocTypeProjection(DefId),
444 /// Reasons to create a region inference variable
446 /// See `error_reporting` module for more details
447 #[derive(Copy, Clone, Debug)]
448 pub enum RegionVariableOrigin {
449 /// Region variables created for ill-categorized reasons,
450 /// mostly indicates places in need of refactoring
453 /// Regions created by a `&P` or `[...]` pattern
456 /// Regions created by `&` operator
459 /// Regions created as part of an autoref of a method receiver
462 /// Regions created as part of an automatic coercion
465 /// Region variables created as the values for early-bound regions
466 EarlyBoundRegion(Span, Symbol),
468 /// Region variables created for bound regions
469 /// in a function or method that is called
470 LateBoundRegion(Span, ty::BoundRegion, LateBoundRegionConversionTime),
472 UpvarRegion(ty::UpvarId, Span),
474 BoundRegionInCoherence(Symbol),
476 /// This origin is used for the inference variables that we create
477 /// during NLL region processing.
478 NLL(NLLRegionVariableOrigin),
481 #[derive(Copy, Clone, Debug)]
482 pub enum NLLRegionVariableOrigin {
483 /// During NLL region processing, we create variables for free
484 /// regions that we encounter in the function signature and
485 /// elsewhere. This origin indices we've got one of those.
488 /// "Universal" instantiation of a higher-ranked region (e.g.,
489 /// from a `for<'a> T` binder). Meant to represent "any region".
490 Placeholder(ty::PlaceholderRegion),
492 /// The variable we create to represent `'empty(U0)`.
496 /// If this is true, then this variable was created to represent a lifetime
497 /// bound in a `for` binder. For example, it might have been created to
498 /// represent the lifetime `'a` in a type like `for<'a> fn(&'a u32)`.
499 /// Such variables are created when we are trying to figure out if there
500 /// is any valid instantiation of `'a` that could fit into some scenario.
502 /// This is used to inform error reporting: in the case that we are trying to
503 /// determine whether there is any valid instantiation of a `'a` variable that meets
504 /// some constraint C, we want to blame the "source" of that `for` type,
505 /// rather than blaming the source of the constraint C.
510 impl NLLRegionVariableOrigin {
511 pub fn is_universal(self) -> bool {
513 NLLRegionVariableOrigin::FreeRegion => true,
514 NLLRegionVariableOrigin::Placeholder(..) => true,
515 NLLRegionVariableOrigin::Existential { .. } => false,
516 NLLRegionVariableOrigin::RootEmptyRegion => false,
520 pub fn is_existential(self) -> bool {
525 // FIXME(eddyb) investigate overlap between this and `TyOrConstInferVar`.
526 #[derive(Copy, Clone, Debug)]
527 pub enum FixupError<'tcx> {
528 UnresolvedIntTy(IntVid),
529 UnresolvedFloatTy(FloatVid),
531 UnresolvedConst(ConstVid<'tcx>),
534 /// See the `region_obligations` field for more information.
536 pub struct RegionObligation<'tcx> {
537 pub sub_region: ty::Region<'tcx>,
538 pub sup_type: Ty<'tcx>,
539 pub origin: SubregionOrigin<'tcx>,
542 impl<'tcx> fmt::Display for FixupError<'tcx> {
543 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
544 use self::FixupError::*;
547 UnresolvedIntTy(_) => write!(
549 "cannot determine the type of this integer; \
550 add a suffix to specify the type explicitly"
552 UnresolvedFloatTy(_) => write!(
554 "cannot determine the type of this number; \
555 add a suffix to specify the type explicitly"
557 UnresolvedTy(_) => write!(f, "unconstrained type"),
558 UnresolvedConst(_) => write!(f, "unconstrained const value"),
563 /// Helper type of a temporary returned by `tcx.infer_ctxt()`.
564 /// Necessary because we can't write the following bound:
565 /// `F: for<'b, 'tcx> where 'tcx FnOnce(InferCtxt<'b, 'tcx>)`.
566 pub struct InferCtxtBuilder<'tcx> {
567 global_tcx: TyCtxt<'tcx>,
568 fresh_tables: Option<RefCell<ty::TypeckTables<'tcx>>>,
571 pub trait TyCtxtInferExt<'tcx> {
572 fn infer_ctxt(self) -> InferCtxtBuilder<'tcx>;
575 impl TyCtxtInferExt<'tcx> for TyCtxt<'tcx> {
576 fn infer_ctxt(self) -> InferCtxtBuilder<'tcx> {
577 InferCtxtBuilder { global_tcx: self, fresh_tables: None }
581 impl<'tcx> InferCtxtBuilder<'tcx> {
582 /// Used only by `rustc_typeck` during body type-checking/inference,
583 /// will initialize `in_progress_tables` with fresh `TypeckTables`.
584 pub fn with_fresh_in_progress_tables(mut self, table_owner: LocalDefId) -> Self {
585 self.fresh_tables = Some(RefCell::new(ty::TypeckTables::empty(Some(table_owner))));
589 /// Given a canonical value `C` as a starting point, create an
590 /// inference context that contains each of the bound values
591 /// within instantiated as a fresh variable. The `f` closure is
592 /// invoked with the new infcx, along with the instantiated value
593 /// `V` and a substitution `S`. This substitution `S` maps from
594 /// the bound values in `C` to their instantiated values in `V`
595 /// (in other words, `S(C) = V`).
596 pub fn enter_with_canonical<T, R>(
599 canonical: &Canonical<'tcx, T>,
600 f: impl for<'a> FnOnce(InferCtxt<'a, 'tcx>, T, CanonicalVarValues<'tcx>) -> R,
603 T: TypeFoldable<'tcx>,
607 infcx.instantiate_canonical_with_fresh_inference_vars(span, canonical);
608 f(infcx, value, subst)
612 pub fn enter<R>(&mut self, f: impl for<'a> FnOnce(InferCtxt<'a, 'tcx>) -> R) -> R {
613 let InferCtxtBuilder { global_tcx, ref fresh_tables } = *self;
614 let in_progress_tables = fresh_tables.as_ref();
615 global_tcx.enter_local(|tcx| {
619 inner: RefCell::new(InferCtxtInner::new()),
620 lexical_region_resolutions: RefCell::new(None),
621 selection_cache: Default::default(),
622 evaluation_cache: Default::default(),
623 reported_trait_errors: Default::default(),
624 reported_closure_mismatch: Default::default(),
625 tainted_by_errors_flag: Cell::new(false),
626 err_count_on_creation: tcx.sess.err_count(),
627 in_snapshot: Cell::new(false),
628 skip_leak_check: Cell::new(false),
629 universe: Cell::new(ty::UniverseIndex::ROOT),
635 impl<'tcx, T> InferOk<'tcx, T> {
636 pub fn unit(self) -> InferOk<'tcx, ()> {
637 InferOk { value: (), obligations: self.obligations }
640 /// Extracts `value`, registering any obligations into `fulfill_cx`.
641 pub fn into_value_registering_obligations(
643 infcx: &InferCtxt<'_, 'tcx>,
644 fulfill_cx: &mut dyn TraitEngine<'tcx>,
646 let InferOk { value, obligations } = self;
647 for obligation in obligations {
648 fulfill_cx.register_predicate_obligation(infcx, obligation);
654 impl<'tcx> InferOk<'tcx, ()> {
655 pub fn into_obligations(self) -> PredicateObligations<'tcx> {
660 #[must_use = "once you start a snapshot, you should always consume it"]
661 pub struct CombinedSnapshot<'a, 'tcx> {
662 undo_snapshot: Snapshot<'tcx>,
663 region_constraints_snapshot: RegionSnapshot,
664 universe: ty::UniverseIndex,
665 was_in_snapshot: bool,
666 _in_progress_tables: Option<Ref<'a, ty::TypeckTables<'tcx>>>,
669 impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
670 pub fn is_in_snapshot(&self) -> bool {
671 self.in_snapshot.get()
674 pub fn freshen<T: TypeFoldable<'tcx>>(&self, t: T) -> T {
675 t.fold_with(&mut self.freshener())
678 pub fn type_var_diverges(&'a self, ty: Ty<'_>) -> bool {
680 ty::Infer(ty::TyVar(vid)) => self.inner.borrow_mut().type_variables().var_diverges(vid),
685 pub fn freshener<'b>(&'b self) -> TypeFreshener<'b, 'tcx> {
686 freshen::TypeFreshener::new(self)
689 pub fn type_is_unconstrained_numeric(&'a self, ty: Ty<'_>) -> UnconstrainedNumeric {
690 use rustc_middle::ty::error::UnconstrainedNumeric::Neither;
691 use rustc_middle::ty::error::UnconstrainedNumeric::{UnconstrainedFloat, UnconstrainedInt};
693 ty::Infer(ty::IntVar(vid)) => {
694 if self.inner.borrow_mut().int_unification_table().probe_value(vid).is_some() {
700 ty::Infer(ty::FloatVar(vid)) => {
701 if self.inner.borrow_mut().float_unification_table().probe_value(vid).is_some() {
711 pub fn unsolved_variables(&self) -> Vec<Ty<'tcx>> {
712 let mut inner = self.inner.borrow_mut();
713 // FIXME(const_generics): should there be an equivalent function for const variables?
715 let mut vars: Vec<Ty<'_>> = inner
717 .unsolved_variables()
719 .map(|t| self.tcx.mk_ty_var(t))
722 (0..inner.int_unification_table().len())
723 .map(|i| ty::IntVid { index: i as u32 })
724 .filter(|&vid| inner.int_unification_table().probe_value(vid).is_none())
725 .map(|v| self.tcx.mk_int_var(v)),
728 (0..inner.float_unification_table().len())
729 .map(|i| ty::FloatVid { index: i as u32 })
730 .filter(|&vid| inner.float_unification_table().probe_value(vid).is_none())
731 .map(|v| self.tcx.mk_float_var(v)),
738 trace: TypeTrace<'tcx>,
739 param_env: ty::ParamEnv<'tcx>,
740 ) -> CombineFields<'a, 'tcx> {
746 obligations: PredicateObligations::new(),
750 /// Clear the "currently in a snapshot" flag, invoke the closure,
751 /// then restore the flag to its original value. This flag is a
752 /// debugging measure designed to detect cases where we start a
753 /// snapshot, create type variables, and register obligations
754 /// which may involve those type variables in the fulfillment cx,
755 /// potentially leaving "dangling type variables" behind.
756 /// In such cases, an assertion will fail when attempting to
757 /// register obligations, within a snapshot. Very useful, much
758 /// better than grovelling through megabytes of `RUSTC_LOG` output.
760 /// HOWEVER, in some cases the flag is unhelpful. In particular, we
761 /// sometimes create a "mini-fulfilment-cx" in which we enroll
762 /// obligations. As long as this fulfillment cx is fully drained
763 /// before we return, this is not a problem, as there won't be any
764 /// escaping obligations in the main cx. In those cases, you can
765 /// use this function.
766 pub fn save_and_restore_in_snapshot_flag<F, R>(&self, func: F) -> R
768 F: FnOnce(&Self) -> R,
770 let flag = self.in_snapshot.replace(false);
771 let result = func(self);
772 self.in_snapshot.set(flag);
776 fn start_snapshot(&self) -> CombinedSnapshot<'a, 'tcx> {
777 debug!("start_snapshot()");
779 let in_snapshot = self.in_snapshot.replace(true);
781 let mut inner = self.inner.borrow_mut();
784 undo_snapshot: inner.undo_log.start_snapshot(),
785 region_constraints_snapshot: inner.unwrap_region_constraints().start_snapshot(),
786 universe: self.universe(),
787 was_in_snapshot: in_snapshot,
788 // Borrow tables "in progress" (i.e., during typeck)
789 // to ban writes from within a snapshot to them.
790 _in_progress_tables: self.in_progress_tables.map(|tables| tables.borrow()),
794 fn rollback_to(&self, cause: &str, snapshot: CombinedSnapshot<'a, 'tcx>) {
795 debug!("rollback_to(cause={})", cause);
796 let CombinedSnapshot {
798 region_constraints_snapshot,
804 self.in_snapshot.set(was_in_snapshot);
805 self.universe.set(universe);
807 let mut inner = self.inner.borrow_mut();
808 inner.rollback_to(undo_snapshot);
809 inner.unwrap_region_constraints().rollback_to(region_constraints_snapshot);
812 fn commit_from(&self, snapshot: CombinedSnapshot<'a, 'tcx>) {
813 debug!("commit_from()");
814 let CombinedSnapshot {
816 region_constraints_snapshot: _,
822 self.in_snapshot.set(was_in_snapshot);
824 self.inner.borrow_mut().commit(undo_snapshot);
827 /// Executes `f` and commit the bindings.
828 pub fn commit_unconditionally<R, F>(&self, f: F) -> R
830 F: FnOnce(&CombinedSnapshot<'a, 'tcx>) -> R,
832 debug!("commit_unconditionally()");
833 let snapshot = self.start_snapshot();
834 let r = f(&snapshot);
835 self.commit_from(snapshot);
839 /// Execute `f` and commit the bindings if closure `f` returns `Ok(_)`.
840 pub fn commit_if_ok<T, E, F>(&self, f: F) -> Result<T, E>
842 F: FnOnce(&CombinedSnapshot<'a, 'tcx>) -> Result<T, E>,
844 debug!("commit_if_ok()");
845 let snapshot = self.start_snapshot();
846 let r = f(&snapshot);
847 debug!("commit_if_ok() -- r.is_ok() = {}", r.is_ok());
850 self.commit_from(snapshot);
853 self.rollback_to("commit_if_ok -- error", snapshot);
859 /// Execute `f` then unroll any bindings it creates.
860 pub fn probe<R, F>(&self, f: F) -> R
862 F: FnOnce(&CombinedSnapshot<'a, 'tcx>) -> R,
865 let snapshot = self.start_snapshot();
866 let r = f(&snapshot);
867 self.rollback_to("probe", snapshot);
871 /// If `should_skip` is true, then execute `f` then unroll any bindings it creates.
872 pub fn probe_maybe_skip_leak_check<R, F>(&self, should_skip: bool, f: F) -> R
874 F: FnOnce(&CombinedSnapshot<'a, 'tcx>) -> R,
877 let snapshot = self.start_snapshot();
878 let was_skip_leak_check = self.skip_leak_check.get();
880 self.skip_leak_check.set(true);
882 let r = f(&snapshot);
883 self.rollback_to("probe", snapshot);
884 self.skip_leak_check.set(was_skip_leak_check);
888 /// Scan the constraints produced since `snapshot` began and returns:
890 /// - `None` -- if none of them involve "region outlives" constraints
891 /// - `Some(true)` -- if there are `'a: 'b` constraints where `'a` or `'b` is a placeholder
892 /// - `Some(false)` -- if there are `'a: 'b` constraints but none involve placeholders
893 pub fn region_constraints_added_in_snapshot(
895 snapshot: &CombinedSnapshot<'a, 'tcx>,
899 .unwrap_region_constraints()
900 .region_constraints_added_in_snapshot(&snapshot.undo_snapshot)
903 pub fn add_given(&self, sub: ty::Region<'tcx>, sup: ty::RegionVid) {
904 self.inner.borrow_mut().unwrap_region_constraints().add_given(sub, sup);
907 pub fn can_sub<T>(&self, param_env: ty::ParamEnv<'tcx>, a: T, b: T) -> UnitResult<'tcx>
909 T: at::ToTrace<'tcx>,
911 let origin = &ObligationCause::dummy();
913 self.at(origin, param_env).sub(a, b).map(|InferOk { obligations: _, .. }| {
914 // Ignore obligations, since we are unrolling
915 // everything anyway.
920 pub fn can_eq<T>(&self, param_env: ty::ParamEnv<'tcx>, a: T, b: T) -> UnitResult<'tcx>
922 T: at::ToTrace<'tcx>,
924 let origin = &ObligationCause::dummy();
926 self.at(origin, param_env).eq(a, b).map(|InferOk { obligations: _, .. }| {
927 // Ignore obligations, since we are unrolling
928 // everything anyway.
935 origin: SubregionOrigin<'tcx>,
939 debug!("sub_regions({:?} <: {:?})", a, b);
940 self.inner.borrow_mut().unwrap_region_constraints().make_subregion(origin, a, b);
943 /// Require that the region `r` be equal to one of the regions in
944 /// the set `regions`.
945 pub fn member_constraint(
947 opaque_type_def_id: DefId,
948 definition_span: Span,
950 region: ty::Region<'tcx>,
951 in_regions: &Lrc<Vec<ty::Region<'tcx>>>,
953 debug!("member_constraint({:?} <: {:?})", region, in_regions);
954 self.inner.borrow_mut().unwrap_region_constraints().member_constraint(
963 pub fn subtype_predicate(
965 cause: &ObligationCause<'tcx>,
966 param_env: ty::ParamEnv<'tcx>,
967 predicate: &ty::PolySubtypePredicate<'tcx>,
968 ) -> Option<InferResult<'tcx, ()>> {
969 // Subtle: it's ok to skip the binder here and resolve because
970 // `shallow_resolve` just ignores anything that is not a type
971 // variable, and because type variable's can't (at present, at
972 // least) capture any of the things bound by this binder.
974 // NOTE(nmatsakis): really, there is no *particular* reason to do this
975 // `shallow_resolve` here except as a micro-optimization.
976 // Naturally I could not resist.
977 let two_unbound_type_vars = {
978 let a = self.shallow_resolve(predicate.skip_binder().a);
979 let b = self.shallow_resolve(predicate.skip_binder().b);
980 a.is_ty_var() && b.is_ty_var()
983 if two_unbound_type_vars {
984 // Two unbound type variables? Can't make progress.
988 Some(self.commit_if_ok(|snapshot| {
989 let (ty::SubtypePredicate { a_is_expected, a, b }, placeholder_map) =
990 self.replace_bound_vars_with_placeholders(predicate);
992 let ok = self.at(cause, param_env).sub_exp(a_is_expected, a, b)?;
994 self.leak_check(false, &placeholder_map, snapshot)?;
1000 pub fn region_outlives_predicate(
1002 cause: &traits::ObligationCause<'tcx>,
1003 predicate: &ty::PolyRegionOutlivesPredicate<'tcx>,
1004 ) -> UnitResult<'tcx> {
1005 self.commit_if_ok(|snapshot| {
1006 let (ty::OutlivesPredicate(r_a, r_b), placeholder_map) =
1007 self.replace_bound_vars_with_placeholders(predicate);
1008 let origin = SubregionOrigin::from_obligation_cause(cause, || {
1009 RelateRegionParamBound(cause.span)
1011 self.sub_regions(origin, r_b, r_a); // `b : a` ==> `a <= b`
1012 self.leak_check(false, &placeholder_map, snapshot)?;
1017 pub fn next_ty_var_id(&self, diverging: bool, origin: TypeVariableOrigin) -> TyVid {
1018 self.inner.borrow_mut().type_variables().new_var(self.universe(), diverging, origin)
1021 pub fn next_ty_var(&self, origin: TypeVariableOrigin) -> Ty<'tcx> {
1022 self.tcx.mk_ty_var(self.next_ty_var_id(false, origin))
1025 pub fn next_ty_var_in_universe(
1027 origin: TypeVariableOrigin,
1028 universe: ty::UniverseIndex,
1030 let vid = self.inner.borrow_mut().type_variables().new_var(universe, false, origin);
1031 self.tcx.mk_ty_var(vid)
1034 pub fn next_diverging_ty_var(&self, origin: TypeVariableOrigin) -> Ty<'tcx> {
1035 self.tcx.mk_ty_var(self.next_ty_var_id(true, origin))
1038 pub fn next_const_var(
1041 origin: ConstVariableOrigin,
1042 ) -> &'tcx ty::Const<'tcx> {
1043 self.tcx.mk_const_var(self.next_const_var_id(origin), ty)
1046 pub fn next_const_var_in_universe(
1049 origin: ConstVariableOrigin,
1050 universe: ty::UniverseIndex,
1051 ) -> &'tcx ty::Const<'tcx> {
1055 .const_unification_table()
1056 .new_key(ConstVarValue { origin, val: ConstVariableValue::Unknown { universe } });
1057 self.tcx.mk_const_var(vid, ty)
1060 pub fn next_const_var_id(&self, origin: ConstVariableOrigin) -> ConstVid<'tcx> {
1061 self.inner.borrow_mut().const_unification_table().new_key(ConstVarValue {
1063 val: ConstVariableValue::Unknown { universe: self.universe() },
1067 fn next_int_var_id(&self) -> IntVid {
1068 self.inner.borrow_mut().int_unification_table().new_key(None)
1071 pub fn next_int_var(&self) -> Ty<'tcx> {
1072 self.tcx.mk_int_var(self.next_int_var_id())
1075 fn next_float_var_id(&self) -> FloatVid {
1076 self.inner.borrow_mut().float_unification_table().new_key(None)
1079 pub fn next_float_var(&self) -> Ty<'tcx> {
1080 self.tcx.mk_float_var(self.next_float_var_id())
1083 /// Creates a fresh region variable with the next available index.
1084 /// The variable will be created in the maximum universe created
1085 /// thus far, allowing it to name any region created thus far.
1086 pub fn next_region_var(&self, origin: RegionVariableOrigin) -> ty::Region<'tcx> {
1087 self.next_region_var_in_universe(origin, self.universe())
1090 /// Creates a fresh region variable with the next available index
1091 /// in the given universe; typically, you can use
1092 /// `next_region_var` and just use the maximal universe.
1093 pub fn next_region_var_in_universe(
1095 origin: RegionVariableOrigin,
1096 universe: ty::UniverseIndex,
1097 ) -> ty::Region<'tcx> {
1099 self.inner.borrow_mut().unwrap_region_constraints().new_region_var(universe, origin);
1100 self.tcx.mk_region(ty::ReVar(region_var))
1103 /// Return the universe that the region `r` was created in. For
1104 /// most regions (e.g., `'static`, named regions from the user,
1105 /// etc) this is the root universe U0. For inference variables or
1106 /// placeholders, however, it will return the universe which which
1107 /// they are associated.
1108 fn universe_of_region(&self, r: ty::Region<'tcx>) -> ty::UniverseIndex {
1109 self.inner.borrow_mut().unwrap_region_constraints().universe(r)
1112 /// Number of region variables created so far.
1113 pub fn num_region_vars(&self) -> usize {
1114 self.inner.borrow_mut().unwrap_region_constraints().num_region_vars()
1117 /// Just a convenient wrapper of `next_region_var` for using during NLL.
1118 pub fn next_nll_region_var(&self, origin: NLLRegionVariableOrigin) -> ty::Region<'tcx> {
1119 self.next_region_var(RegionVariableOrigin::NLL(origin))
1122 /// Just a convenient wrapper of `next_region_var` for using during NLL.
1123 pub fn next_nll_region_var_in_universe(
1125 origin: NLLRegionVariableOrigin,
1126 universe: ty::UniverseIndex,
1127 ) -> ty::Region<'tcx> {
1128 self.next_region_var_in_universe(RegionVariableOrigin::NLL(origin), universe)
1131 pub fn var_for_def(&self, span: Span, param: &ty::GenericParamDef) -> GenericArg<'tcx> {
1133 GenericParamDefKind::Lifetime => {
1134 // Create a region inference variable for the given
1135 // region parameter definition.
1136 self.next_region_var(EarlyBoundRegion(span, param.name)).into()
1138 GenericParamDefKind::Type { .. } => {
1139 // Create a type inference variable for the given
1140 // type parameter definition. The substitutions are
1141 // for actual parameters that may be referred to by
1142 // the default of this type parameter, if it exists.
1143 // e.g., `struct Foo<A, B, C = (A, B)>(...);` when
1144 // used in a path such as `Foo::<T, U>::new()` will
1145 // use an inference variable for `C` with `[T, U]`
1146 // as the substitutions for the default, `(T, U)`.
1147 let ty_var_id = self.inner.borrow_mut().type_variables().new_var(
1150 TypeVariableOrigin {
1151 kind: TypeVariableOriginKind::TypeParameterDefinition(
1159 self.tcx.mk_ty_var(ty_var_id).into()
1161 GenericParamDefKind::Const { .. } => {
1162 let origin = ConstVariableOrigin {
1163 kind: ConstVariableOriginKind::ConstParameterDefinition(param.name),
1167 self.inner.borrow_mut().const_unification_table().new_key(ConstVarValue {
1169 val: ConstVariableValue::Unknown { universe: self.universe() },
1171 self.tcx.mk_const_var(const_var_id, self.tcx.type_of(param.def_id)).into()
1176 /// Given a set of generics defined on a type or impl, returns a substitution mapping each
1177 /// type/region parameter to a fresh inference variable.
1178 pub fn fresh_substs_for_item(&self, span: Span, def_id: DefId) -> SubstsRef<'tcx> {
1179 InternalSubsts::for_item(self.tcx, def_id, |param, _| self.var_for_def(span, param))
1182 /// Returns `true` if errors have been reported since this infcx was
1183 /// created. This is sometimes used as a heuristic to skip
1184 /// reporting errors that often occur as a result of earlier
1185 /// errors, but where it's hard to be 100% sure (e.g., unresolved
1186 /// inference variables, regionck errors).
1187 pub fn is_tainted_by_errors(&self) -> bool {
1189 "is_tainted_by_errors(err_count={}, err_count_on_creation={}, \
1190 tainted_by_errors_flag={})",
1191 self.tcx.sess.err_count(),
1192 self.err_count_on_creation,
1193 self.tainted_by_errors_flag.get()
1196 if self.tcx.sess.err_count() > self.err_count_on_creation {
1197 return true; // errors reported since this infcx was made
1199 self.tainted_by_errors_flag.get()
1202 /// Set the "tainted by errors" flag to true. We call this when we
1203 /// observe an error from a prior pass.
1204 pub fn set_tainted_by_errors(&self) {
1205 debug!("set_tainted_by_errors()");
1206 self.tainted_by_errors_flag.set(true)
1209 /// Process the region constraints and report any errors that
1210 /// result. After this, no more unification operations should be
1211 /// done -- or the compiler will panic -- but it is legal to use
1212 /// `resolve_vars_if_possible` as well as `fully_resolve`.
1213 pub fn resolve_regions_and_report_errors(
1215 region_context: DefId,
1216 region_map: ®ion::ScopeTree,
1217 outlives_env: &OutlivesEnvironment<'tcx>,
1220 let (var_infos, data) = {
1221 let mut inner = self.inner.borrow_mut();
1222 let inner = &mut *inner;
1224 self.is_tainted_by_errors() || inner.region_obligations.is_empty(),
1225 "region_obligations not empty: {:#?}",
1226 inner.region_obligations
1229 .region_constraint_storage
1231 .expect("regions already resolved")
1232 .with_log(&mut inner.undo_log)
1233 .into_infos_and_data()
1236 let region_rels = &RegionRelations::new(
1240 outlives_env.free_region_map(),
1243 let (lexical_region_resolutions, errors) =
1244 lexical_region_resolve::resolve(region_rels, var_infos, data, mode);
1246 let old_value = self.lexical_region_resolutions.replace(Some(lexical_region_resolutions));
1247 assert!(old_value.is_none());
1249 if !self.is_tainted_by_errors() {
1250 // As a heuristic, just skip reporting region errors
1251 // altogether if other errors have been reported while
1252 // this infcx was in use. This is totally hokey but
1253 // otherwise we have a hard time separating legit region
1254 // errors from silly ones.
1255 self.report_region_errors(region_map, &errors);
1259 /// Obtains (and clears) the current set of region
1260 /// constraints. The inference context is still usable: further
1261 /// unifications will simply add new constraints.
1263 /// This method is not meant to be used with normal lexical region
1264 /// resolution. Rather, it is used in the NLL mode as a kind of
1265 /// interim hack: basically we run normal type-check and generate
1266 /// region constraints as normal, but then we take them and
1267 /// translate them into the form that the NLL solver
1268 /// understands. See the NLL module for mode details.
1269 pub fn take_and_reset_region_constraints(&self) -> RegionConstraintData<'tcx> {
1271 self.inner.borrow().region_obligations.is_empty(),
1272 "region_obligations not empty: {:#?}",
1273 self.inner.borrow().region_obligations
1276 self.inner.borrow_mut().unwrap_region_constraints().take_and_reset_data()
1279 /// Gives temporary access to the region constraint data.
1280 #[allow(non_camel_case_types)] // bug with impl trait
1281 pub fn with_region_constraints<R>(
1283 op: impl FnOnce(&RegionConstraintData<'tcx>) -> R,
1285 let mut inner = self.inner.borrow_mut();
1286 op(inner.unwrap_region_constraints().data())
1289 /// Takes ownership of the list of variable regions. This implies
1290 /// that all the region constraints have already been taken, and
1291 /// hence that `resolve_regions_and_report_errors` can never be
1292 /// called. This is used only during NLL processing to "hand off" ownership
1293 /// of the set of region variables into the NLL region context.
1294 pub fn take_region_var_origins(&self) -> VarInfos {
1295 let mut inner = self.inner.borrow_mut();
1296 let (var_infos, data) = inner
1297 .region_constraint_storage
1299 .expect("regions already resolved")
1300 .with_log(&mut inner.undo_log)
1301 .into_infos_and_data();
1302 assert!(data.is_empty());
1306 pub fn ty_to_string(&self, t: Ty<'tcx>) -> String {
1307 self.resolve_vars_if_possible(&t).to_string()
1310 pub fn tys_to_string(&self, ts: &[Ty<'tcx>]) -> String {
1311 let tstrs: Vec<String> = ts.iter().map(|t| self.ty_to_string(*t)).collect();
1312 format!("({})", tstrs.join(", "))
1315 pub fn trait_ref_to_string(&self, t: &ty::TraitRef<'tcx>) -> String {
1316 self.resolve_vars_if_possible(t).print_only_trait_path().to_string()
1319 /// If `TyVar(vid)` resolves to a type, return that type. Else, return the
1320 /// universe index of `TyVar(vid)`.
1321 pub fn probe_ty_var(&self, vid: TyVid) -> Result<Ty<'tcx>, ty::UniverseIndex> {
1322 use self::type_variable::TypeVariableValue;
1324 match self.inner.borrow_mut().type_variables().probe(vid) {
1325 TypeVariableValue::Known { value } => Ok(value),
1326 TypeVariableValue::Unknown { universe } => Err(universe),
1330 /// Resolve any type variables found in `value` -- but only one
1331 /// level. So, if the variable `?X` is bound to some type
1332 /// `Foo<?Y>`, then this would return `Foo<?Y>` (but `?Y` may
1333 /// itself be bound to a type).
1335 /// Useful when you only need to inspect the outermost level of
1336 /// the type and don't care about nested types (or perhaps you
1337 /// will be resolving them as well, e.g. in a loop).
1338 pub fn shallow_resolve<T>(&self, value: T) -> T
1340 T: TypeFoldable<'tcx>,
1342 value.fold_with(&mut ShallowResolver { infcx: self })
1345 pub fn root_var(&self, var: ty::TyVid) -> ty::TyVid {
1346 self.inner.borrow_mut().type_variables().root_var(var)
1349 /// Where possible, replaces type/const variables in
1350 /// `value` with their final value. Note that region variables
1351 /// are unaffected. If a type/const variable has not been unified, it
1352 /// is left as is. This is an idempotent operation that does
1353 /// not affect inference state in any way and so you can do it
1355 pub fn resolve_vars_if_possible<T>(&self, value: &T) -> T
1357 T: TypeFoldable<'tcx>,
1359 if !value.needs_infer() {
1360 return value.clone(); // Avoid duplicated subst-folding.
1362 let mut r = resolve::OpportunisticVarResolver::new(self);
1363 value.fold_with(&mut r)
1366 /// Returns the first unresolved variable contained in `T`. In the
1367 /// process of visiting `T`, this will resolve (where possible)
1368 /// type variables in `T`, but it never constructs the final,
1369 /// resolved type, so it's more efficient than
1370 /// `resolve_vars_if_possible()`.
1371 pub fn unresolved_type_vars<T>(&self, value: &T) -> Option<(Ty<'tcx>, Option<Span>)>
1373 T: TypeFoldable<'tcx>,
1375 let mut r = resolve::UnresolvedTypeFinder::new(self);
1376 value.visit_with(&mut r);
1380 pub fn probe_const_var(
1382 vid: ty::ConstVid<'tcx>,
1383 ) -> Result<&'tcx ty::Const<'tcx>, ty::UniverseIndex> {
1384 match self.inner.borrow_mut().const_unification_table().probe_value(vid).val {
1385 ConstVariableValue::Known { value } => Ok(value),
1386 ConstVariableValue::Unknown { universe } => Err(universe),
1390 pub fn fully_resolve<T: TypeFoldable<'tcx>>(&self, value: &T) -> FixupResult<'tcx, T> {
1392 * Attempts to resolve all type/region/const variables in
1393 * `value`. Region inference must have been run already (e.g.,
1394 * by calling `resolve_regions_and_report_errors`). If some
1395 * variable was never unified, an `Err` results.
1397 * This method is idempotent, but it not typically not invoked
1398 * except during the writeback phase.
1401 resolve::fully_resolve(self, value)
1404 // [Note-Type-error-reporting]
1405 // An invariant is that anytime the expected or actual type is Error (the special
1406 // error type, meaning that an error occurred when typechecking this expression),
1407 // this is a derived error. The error cascaded from another error (that was already
1408 // reported), so it's not useful to display it to the user.
1409 // The following methods implement this logic.
1410 // They check if either the actual or expected type is Error, and don't print the error
1411 // in this case. The typechecker should only ever report type errors involving mismatched
1412 // types using one of these methods, and should not call span_err directly for such
1415 pub fn type_error_struct_with_diag<M>(
1419 actual_ty: Ty<'tcx>,
1420 ) -> DiagnosticBuilder<'tcx>
1422 M: FnOnce(String) -> DiagnosticBuilder<'tcx>,
1424 let actual_ty = self.resolve_vars_if_possible(&actual_ty);
1425 debug!("type_error_struct_with_diag({:?}, {:?})", sp, actual_ty);
1427 // Don't report an error if actual type is `Error`.
1428 if actual_ty.references_error() {
1429 return self.tcx.sess.diagnostic().struct_dummy();
1432 mk_diag(self.ty_to_string(actual_ty))
1435 pub fn report_mismatched_types(
1437 cause: &ObligationCause<'tcx>,
1440 err: TypeError<'tcx>,
1441 ) -> DiagnosticBuilder<'tcx> {
1442 let trace = TypeTrace::types(cause, true, expected, actual);
1443 self.report_and_explain_type_error(trace, &err)
1446 pub fn report_mismatched_consts(
1448 cause: &ObligationCause<'tcx>,
1449 expected: &'tcx ty::Const<'tcx>,
1450 actual: &'tcx ty::Const<'tcx>,
1451 err: TypeError<'tcx>,
1452 ) -> DiagnosticBuilder<'tcx> {
1453 let trace = TypeTrace::consts(cause, true, expected, actual);
1454 self.report_and_explain_type_error(trace, &err)
1457 pub fn replace_bound_vars_with_fresh_vars<T>(
1460 lbrct: LateBoundRegionConversionTime,
1461 value: &ty::Binder<T>,
1462 ) -> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
1464 T: TypeFoldable<'tcx>,
1466 let fld_r = |br| self.next_region_var(LateBoundRegion(span, br, lbrct));
1468 self.next_ty_var(TypeVariableOrigin {
1469 kind: TypeVariableOriginKind::MiscVariable,
1473 let fld_c = |_, ty| {
1474 self.next_const_var(
1476 ConstVariableOrigin { kind: ConstVariableOriginKind::MiscVariable, span },
1479 self.tcx.replace_bound_vars(value, fld_r, fld_t, fld_c)
1482 /// See the [`region_constraints::RegionConstraintCollector::verify_generic_bound`] method.
1483 pub fn verify_generic_bound(
1485 origin: SubregionOrigin<'tcx>,
1486 kind: GenericKind<'tcx>,
1487 a: ty::Region<'tcx>,
1488 bound: VerifyBound<'tcx>,
1490 debug!("verify_generic_bound({:?}, {:?} <: {:?})", kind, a, bound);
1494 .unwrap_region_constraints()
1495 .verify_generic_bound(origin, kind, a, bound);
1498 /// Obtains the latest type of the given closure; this may be a
1499 /// closure in the current function, in which case its
1500 /// `ClosureKind` may not yet be known.
1501 pub fn closure_kind(&self, closure_substs: SubstsRef<'tcx>) -> Option<ty::ClosureKind> {
1502 let closure_kind_ty = closure_substs.as_closure().kind_ty();
1503 let closure_kind_ty = self.shallow_resolve(closure_kind_ty);
1504 closure_kind_ty.to_opt_closure_kind()
1507 /// Clears the selection, evaluation, and projection caches. This is useful when
1508 /// repeatedly attempting to select an `Obligation` while changing only
1509 /// its `ParamEnv`, since `FulfillmentContext` doesn't use probing.
1510 pub fn clear_caches(&self) {
1511 self.selection_cache.clear();
1512 self.evaluation_cache.clear();
1513 self.inner.borrow_mut().projection_cache().clear();
1516 fn universe(&self) -> ty::UniverseIndex {
1520 /// Creates and return a fresh universe that extends all previous
1521 /// universes. Updates `self.universe` to that new universe.
1522 pub fn create_next_universe(&self) -> ty::UniverseIndex {
1523 let u = self.universe.get().next_universe();
1524 self.universe.set(u);
1528 /// Resolves and evaluates a constant.
1530 /// The constant can be located on a trait like `<A as B>::C`, in which case the given
1531 /// substitutions and environment are used to resolve the constant. Alternatively if the
1532 /// constant has generic parameters in scope the substitutions are used to evaluate the value of
1533 /// the constant. For example in `fn foo<T>() { let _ = [0; bar::<T>()]; }` the repeat count
1534 /// constant `bar::<T>()` requires a substitution for `T`, if the substitution for `T` is still
1535 /// too generic for the constant to be evaluated then `Err(ErrorHandled::TooGeneric)` is
1538 /// This handles inferences variables within both `param_env` and `substs` by
1539 /// performing the operation on their respective canonical forms.
1540 pub fn const_eval_resolve(
1542 param_env: ty::ParamEnv<'tcx>,
1544 substs: SubstsRef<'tcx>,
1545 promoted: Option<mir::Promoted>,
1547 ) -> ConstEvalResult<'tcx> {
1548 let mut original_values = OriginalQueryValues::default();
1549 let canonical = self.canonicalize_query(&(param_env, substs), &mut original_values);
1551 let (param_env, substs) = canonical.value;
1552 // The return value is the evaluated value which doesn't contain any reference to inference
1553 // variables, thus we don't need to substitute back the original values.
1554 self.tcx.const_eval_resolve(param_env, def_id, substs, promoted, span)
1557 /// If `typ` is a type variable of some kind, resolve it one level
1558 /// (but do not resolve types found in the result). If `typ` is
1559 /// not a type variable, just return it unmodified.
1560 // FIXME(eddyb) inline into `ShallowResolver::visit_ty`.
1561 fn shallow_resolve_ty(&self, typ: Ty<'tcx>) -> Ty<'tcx> {
1563 ty::Infer(ty::TyVar(v)) => {
1564 // Not entirely obvious: if `typ` is a type variable,
1565 // it can be resolved to an int/float variable, which
1566 // can then be recursively resolved, hence the
1567 // recursion. Note though that we prevent type
1568 // variables from unifying to other type variables
1569 // directly (though they may be embedded
1570 // structurally), and we prevent cycles in any case,
1571 // so this recursion should always be of very limited
1574 // Note: if these two lines are combined into one we get
1575 // dynamic borrow errors on `self.inner`.
1576 let known = self.inner.borrow_mut().type_variables().probe(v).known();
1577 known.map(|t| self.shallow_resolve_ty(t)).unwrap_or(typ)
1580 ty::Infer(ty::IntVar(v)) => self
1583 .int_unification_table()
1585 .map(|v| v.to_type(self.tcx))
1588 ty::Infer(ty::FloatVar(v)) => self
1591 .float_unification_table()
1593 .map(|v| v.to_type(self.tcx))
1600 /// `ty_or_const_infer_var_changed` is equivalent to one of these two:
1601 /// * `shallow_resolve(ty) != ty` (where `ty.kind = ty::Infer(_)`)
1602 /// * `shallow_resolve(ct) != ct` (where `ct.kind = ty::ConstKind::Infer(_)`)
1604 /// However, `ty_or_const_infer_var_changed` is more efficient. It's always
1605 /// inlined, despite being large, because it has only two call sites that
1606 /// are extremely hot (both in `traits::fulfill`'s checking of `stalled_on`
1607 /// inference variables), and it handles both `Ty` and `ty::Const` without
1608 /// having to resort to storing full `GenericArg`s in `stalled_on`.
1610 pub fn ty_or_const_infer_var_changed(&self, infer_var: TyOrConstInferVar<'tcx>) -> bool {
1611 let mut inner = self.inner.borrow_mut();
1613 TyOrConstInferVar::Ty(v) => {
1614 use self::type_variable::TypeVariableValue;
1616 // If `inlined_probe` returns a `Known` value, it never equals
1617 // `ty::Infer(ty::TyVar(v))`.
1618 match inner.type_variables().inlined_probe(v) {
1619 TypeVariableValue::Unknown { .. } => false,
1620 TypeVariableValue::Known { .. } => true,
1624 TyOrConstInferVar::TyInt(v) => {
1625 // If `inlined_probe_value` returns a value it's always a
1626 // `ty::Int(_)` or `ty::UInt(_)`, which never matches a
1628 inner.int_unification_table().inlined_probe_value(v).is_some()
1631 TyOrConstInferVar::TyFloat(v) => {
1632 // If `probe_value` returns a value it's always a
1633 // `ty::Float(_)`, which never matches a `ty::Infer(_)`.
1635 // Not `inlined_probe_value(v)` because this call site is colder.
1636 inner.float_unification_table().probe_value(v).is_some()
1639 TyOrConstInferVar::Const(v) => {
1640 // If `probe_value` returns a `Known` value, it never equals
1641 // `ty::ConstKind::Infer(ty::InferConst::Var(v))`.
1643 // Not `inlined_probe_value(v)` because this call site is colder.
1644 match inner.const_unification_table().probe_value(v).val {
1645 ConstVariableValue::Unknown { .. } => false,
1646 ConstVariableValue::Known { .. } => true,
1653 /// Helper for `ty_or_const_infer_var_changed` (see comment on that), currently
1654 /// used only for `traits::fulfill`'s list of `stalled_on` inference variables.
1655 #[derive(Copy, Clone, Debug)]
1656 pub enum TyOrConstInferVar<'tcx> {
1657 /// Equivalent to `ty::Infer(ty::TyVar(_))`.
1659 /// Equivalent to `ty::Infer(ty::IntVar(_))`.
1661 /// Equivalent to `ty::Infer(ty::FloatVar(_))`.
1664 /// Equivalent to `ty::ConstKind::Infer(ty::InferConst::Var(_))`.
1665 Const(ConstVid<'tcx>),
1668 impl TyOrConstInferVar<'tcx> {
1669 /// Tries to extract an inference variable from a type or a constant, returns `None`
1670 /// for types other than `ty::Infer(_)` (or `InferTy::Fresh*`) and
1671 /// for constants other than `ty::ConstKind::Infer(_)` (or `InferConst::Fresh`).
1672 pub fn maybe_from_generic_arg(arg: GenericArg<'tcx>) -> Option<Self> {
1673 match arg.unpack() {
1674 GenericArgKind::Type(ty) => Self::maybe_from_ty(ty),
1675 GenericArgKind::Const(ct) => Self::maybe_from_const(ct),
1676 GenericArgKind::Lifetime(_) => None,
1680 /// Tries to extract an inference variable from a type, returns `None`
1681 /// for types other than `ty::Infer(_)` (or `InferTy::Fresh*`).
1682 pub fn maybe_from_ty(ty: Ty<'tcx>) -> Option<Self> {
1684 ty::Infer(ty::TyVar(v)) => Some(TyOrConstInferVar::Ty(v)),
1685 ty::Infer(ty::IntVar(v)) => Some(TyOrConstInferVar::TyInt(v)),
1686 ty::Infer(ty::FloatVar(v)) => Some(TyOrConstInferVar::TyFloat(v)),
1691 /// Tries to extract an inference variable from a constant, returns `None`
1692 /// for constants other than `ty::ConstKind::Infer(_)` (or `InferConst::Fresh`).
1693 pub fn maybe_from_const(ct: &'tcx ty::Const<'tcx>) -> Option<Self> {
1695 ty::ConstKind::Infer(InferConst::Var(v)) => Some(TyOrConstInferVar::Const(v)),
1701 struct ShallowResolver<'a, 'tcx> {
1702 infcx: &'a InferCtxt<'a, 'tcx>,
1705 impl<'a, 'tcx> TypeFolder<'tcx> for ShallowResolver<'a, 'tcx> {
1706 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
1710 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
1711 self.infcx.shallow_resolve_ty(ty)
1714 fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
1715 if let ty::Const { val: ty::ConstKind::Infer(InferConst::Var(vid)), .. } = ct {
1719 .const_unification_table()
1730 impl<'tcx> TypeTrace<'tcx> {
1731 pub fn span(&self) -> Span {
1736 cause: &ObligationCause<'tcx>,
1737 a_is_expected: bool,
1740 ) -> TypeTrace<'tcx> {
1741 TypeTrace { cause: cause.clone(), values: Types(ExpectedFound::new(a_is_expected, a, b)) }
1745 cause: &ObligationCause<'tcx>,
1746 a_is_expected: bool,
1747 a: &'tcx ty::Const<'tcx>,
1748 b: &'tcx ty::Const<'tcx>,
1749 ) -> TypeTrace<'tcx> {
1750 TypeTrace { cause: cause.clone(), values: Consts(ExpectedFound::new(a_is_expected, a, b)) }
1753 pub fn dummy(tcx: TyCtxt<'tcx>) -> TypeTrace<'tcx> {
1755 cause: ObligationCause::dummy(),
1756 values: Types(ExpectedFound { expected: tcx.types.err, found: tcx.types.err }),
1761 impl<'tcx> SubregionOrigin<'tcx> {
1762 pub fn span(&self) -> Span {
1764 Subtype(ref a) => a.span(),
1765 RelateObjectBound(a) => a,
1766 RelateParamBound(a, _) => a,
1767 RelateRegionParamBound(a) => a,
1769 ReborrowUpvar(a, _) => a,
1770 DataBorrowed(_, a) => a,
1771 ReferenceOutlivesReferent(_, a) => a,
1773 CompareImplMethodObligation { span, .. } => span,
1777 pub fn from_obligation_cause<F>(cause: &traits::ObligationCause<'tcx>, default: F) -> Self
1779 F: FnOnce() -> Self,
1782 traits::ObligationCauseCode::ReferenceOutlivesReferent(ref_type) => {
1783 SubregionOrigin::ReferenceOutlivesReferent(ref_type, cause.span)
1786 traits::ObligationCauseCode::CompareImplMethodObligation {
1790 } => SubregionOrigin::CompareImplMethodObligation {
1802 impl RegionVariableOrigin {
1803 pub fn span(&self) -> Span {
1805 MiscVariable(a) => a,
1806 PatternRegion(a) => a,
1807 AddrOfRegion(a) => a,
1810 EarlyBoundRegion(a, ..) => a,
1811 LateBoundRegion(a, ..) => a,
1812 BoundRegionInCoherence(_) => rustc_span::DUMMY_SP,
1813 UpvarRegion(_, a) => a,
1814 NLL(..) => bug!("NLL variable used with `span`"),
1819 impl<'tcx> fmt::Debug for RegionObligation<'tcx> {
1820 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1823 "RegionObligation(sub_region={:?}, sup_type={:?})",
1824 self.sub_region, self.sup_type