1
Fork 0
rust/compiler/rustc_infer/src/infer/mod.rs

1784 lines
66 KiB
Rust
Raw Normal View History

2018-08-29 13:25:47 -04:00
pub use self::freshen::TypeFreshener;
pub use self::LateBoundRegionConversionTime::*;
pub use self::RegionVariableOrigin::*;
pub use self::SubregionOrigin::*;
pub use self::ValuePairs::*;
pub(crate) use self::undo_log::{InferCtxtUndoLogs, Snapshot, UndoLog};
2019-02-05 11:20:45 -06:00
use crate::traits::{self, ObligationCause, PredicateObligations, TraitEngine};
2020-01-06 20:13:24 +01:00
2020-03-29 17:19:48 +02:00
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::sync::Lrc;
2020-04-17 08:54:28 +02:00
use rustc_data_structures::undo_log::Rollback;
2020-03-29 17:19:48 +02:00
use rustc_data_structures::unify as ut;
use rustc_errors::DiagnosticBuilder;
use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LocalDefId};
2020-03-29 16:41:09 +02:00
use rustc_middle::infer::canonical::{Canonical, CanonicalVarValues};
use rustc_middle::infer::unify_key::{ConstVarValue, ConstVariableValue};
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind, ToType};
use rustc_middle::mir::interpret::EvalToConstValueResult;
2020-03-29 16:41:09 +02:00
use rustc_middle::traits::select;
use rustc_middle::ty::error::{ExpectedFound, TypeError, UnconstrainedNumeric};
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder};
use rustc_middle::ty::relate::RelateResult;
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, SubstsRef};
pub use rustc_middle::ty::IntVarValue;
use rustc_middle::ty::{self, GenericParamDefKind, InferConst, Ty, TyCtxt};
use rustc_middle::ty::{ConstVid, FloatVid, IntVid, TyVid};
use rustc_session::config::BorrowckMode;
use rustc_span::symbol::Symbol;
use rustc_span::Span;
use std::cell::{Cell, Ref, RefCell};
2019-02-09 11:24:02 +09:00
use std::collections::BTreeMap;
use std::fmt;
use self::combine::CombineFields;
use self::free_regions::RegionRelations;
use self::lexical_region_resolve::LexicalRegionResolutions;
use self::outlives::env::OutlivesEnvironment;
2018-08-29 13:25:47 -04:00
use self::region_constraints::{GenericKind, RegionConstraintData, VarInfos, VerifyBound};
use self::region_constraints::{
RegionConstraintCollector, RegionConstraintStorage, RegionSnapshot,
};
use self::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
pub mod at;
pub mod canonical;
2016-07-25 13:08:48 -07:00
mod combine;
mod equate;
pub mod error_reporting;
pub mod free_regions;
2018-08-29 13:25:47 -04:00
mod freshen;
mod fudge;
2016-07-25 13:08:48 -07:00
mod glb;
mod higher_ranked;
pub mod lattice;
mod lexical_region_resolve;
2018-08-29 13:25:47 -04:00
mod lub;
pub mod nll_relate;
pub mod outlives;
2018-08-29 13:25:47 -04:00
pub mod region_constraints;
pub mod resolve;
2016-07-25 13:08:48 -07:00
mod sub;
pub mod type_variable;
mod undo_log;
2020-01-06 20:13:24 +01:00
use crate::infer::canonical::OriginalQueryValues;
2020-03-29 16:41:09 +02:00
pub use rustc_middle::infer::unify_key;
#[must_use]
#[derive(Debug)]
pub struct InferOk<'tcx, T> {
pub value: T,
pub obligations: PredicateObligations<'tcx>,
}
pub type InferResult<'tcx, T> = Result<InferOk<'tcx, T>, TypeError<'tcx>>;
pub type Bound<T> = Option<T>;
pub type UnitResult<'tcx> = RelateResult<'tcx, ()>; // "unify result"
pub type FixupResult<'tcx, T> = Result<T, FixupError<'tcx>>; // "fixup result"
pub(crate) type UnificationTable<'a, 'tcx, T> = ut::UnificationTable<
ut::InPlace<T, &'a mut ut::UnificationStorage<T>, &'a mut InferCtxtUndoLogs<'tcx>>,
>;
/// How we should handle region solving.
///
/// This is used so that the region values inferred by HIR region solving are
/// not exposed, and so that we can avoid doing work in HIR typeck that MIR
/// typeck will also do.
#[derive(Copy, Clone, Debug)]
pub enum RegionckMode {
/// The default mode: report region errors, don't erase regions.
Solve,
/// Erase the results of region after solving.
Erase {
/// A flag that is used to suppress region errors, when we are doing
/// region checks that the NLL borrow checker will also do -- it might
/// be set to true.
suppress_errors: bool,
},
}
impl Default for RegionckMode {
fn default() -> Self {
RegionckMode::Solve
}
}
impl RegionckMode {
/// Indicates that the MIR borrowck will repeat these region
/// checks, so we should ignore errors if NLL is (unconditionally)
/// enabled.
pub fn for_item_body(tcx: TyCtxt<'_>) -> Self {
// FIXME(Centril): Once we actually remove `::Migrate` also make
// this always `true` and then proceed to eliminate the dead code.
match tcx.borrowck_mode() {
// If we're on Migrate mode, report AST region errors
BorrowckMode::Migrate => RegionckMode::Erase { suppress_errors: false },
2019-05-03 17:34:17 -04:00
// If we're on MIR, don't report AST region errors as they should be reported by NLL
BorrowckMode::Mir => RegionckMode::Erase { suppress_errors: true },
}
}
}
/// This type contains all the things within `InferCtxt` that sit within a
/// `RefCell` and are involved with taking/rolling back snapshots. Snapshot
/// operations are hot enough that we want only one call to `borrow_mut` per
/// call to `start_snapshot` and `rollback_to`.
pub struct InferCtxtInner<'tcx> {
/// Cache for projections. This cache is snapshotted along with the infcx.
///
/// Public so that `traits::project` can use it.
pub projection_cache: traits::ProjectionCacheStorage<'tcx>,
/// We instantiate `UnificationTable` with `bounds<Ty>` because the types
/// that might instantiate a general type variable have an order,
/// represented by its upper and lower bounds.
2020-04-17 08:54:28 +02:00
type_variable_storage: type_variable::TypeVariableStorage<'tcx>,
/// Map from const parameter variable to the kind of const it represents.
2020-04-17 08:54:28 +02:00
const_unification_storage: ut::UnificationTableStorage<ty::ConstVid<'tcx>>,
/// Map from integral variable to the kind of integer it represents.
2020-04-17 08:54:28 +02:00
int_unification_storage: ut::UnificationTableStorage<ty::IntVid>,
/// Map from floating variable to the kind of float it represents.
2020-04-17 08:54:28 +02:00
float_unification_storage: ut::UnificationTableStorage<ty::FloatVid>,
/// Tracks the set of region variables and the constraints between them.
/// This is initially `Some(_)` but when
/// `resolve_regions_and_report_errors` is invoked, this gets set to `None`
/// -- further attempts to perform unification, etc., may fail if new
/// region constraints would've been added.
2020-04-17 09:15:28 +02:00
region_constraint_storage: Option<RegionConstraintStorage<'tcx>>,
/// A set of constraints that regionck must validate. Each
/// constraint has the form `T:'a`, meaning "some type `T` must
/// outlive the lifetime 'a". These constraints derive from
/// instantiated type parameters. So if you had a struct defined
/// like
///
/// struct Foo<T:'static> { ... }
///
/// then in some expression `let x = Foo { ... }` it will
/// instantiate the type parameter `T` with a fresh type `$0`. At
/// the same time, it will record a region obligation of
/// `$0:'static`. This will get checked later by regionck. (We
/// can't generally check these things right away because we have
/// to wait until types are resolved.)
///
/// These are stored in a map keyed to the id of the innermost
/// enclosing fn body / static initializer expression. This is
/// because the location where the obligation was incurred can be
/// relevant with respect to which sublifetime assumptions are in
/// place. The reason that we store under the fn-id, and not
/// something more fine-grained, is so that it is easier for
/// regionck to be sure that it has found *all* the region
/// obligations (otherwise, it's easy to fail to walk to a
/// particular node-id).
///
/// Before running `resolve_regions_and_report_errors`, the creator
/// of the inference context is expected to invoke
/// `process_region_obligations` (defined in `self::region_obligations`)
/// for each body-id in this map, which will process the
/// obligations within. This is expected to be done 'late enough'
/// that all type inference variables have been bound and so forth.
region_obligations: Vec<(hir::HirId, RegionObligation<'tcx>)>,
undo_log: InferCtxtUndoLogs<'tcx>,
}
impl<'tcx> InferCtxtInner<'tcx> {
fn new() -> InferCtxtInner<'tcx> {
InferCtxtInner {
projection_cache: Default::default(),
2020-04-17 08:54:28 +02:00
type_variable_storage: type_variable::TypeVariableStorage::new(),
undo_log: InferCtxtUndoLogs::default(),
2020-04-17 08:54:28 +02:00
const_unification_storage: ut::UnificationTableStorage::new(),
int_unification_storage: ut::UnificationTableStorage::new(),
float_unification_storage: ut::UnificationTableStorage::new(),
2020-04-17 09:15:28 +02:00
region_constraint_storage: Some(RegionConstraintStorage::new()),
region_obligations: vec![],
}
}
#[inline]
pub fn region_obligations(&self) -> &[(hir::HirId, RegionObligation<'tcx>)] {
&self.region_obligations
}
#[inline]
2020-04-17 08:54:28 +02:00
pub fn projection_cache(&mut self) -> traits::ProjectionCache<'_, 'tcx> {
self.projection_cache.with_log(&mut self.undo_log)
}
#[inline]
2020-04-17 08:54:28 +02:00
fn type_variables(&mut self) -> type_variable::TypeVariableTable<'_, 'tcx> {
self.type_variable_storage.with_log(&mut self.undo_log)
}
#[inline]
fn int_unification_table(
&mut self,
) -> ut::UnificationTable<
ut::InPlace<
ty::IntVid,
&mut ut::UnificationStorage<ty::IntVid>,
&mut InferCtxtUndoLogs<'tcx>,
>,
> {
2020-04-17 08:54:28 +02:00
self.int_unification_storage.with_log(&mut self.undo_log)
}
#[inline]
fn float_unification_table(
&mut self,
) -> ut::UnificationTable<
ut::InPlace<
ty::FloatVid,
&mut ut::UnificationStorage<ty::FloatVid>,
&mut InferCtxtUndoLogs<'tcx>,
>,
> {
2020-04-17 08:54:28 +02:00
self.float_unification_storage.with_log(&mut self.undo_log)
}
#[inline]
fn const_unification_table(
&mut self,
) -> ut::UnificationTable<
ut::InPlace<
ty::ConstVid<'tcx>,
&mut ut::UnificationStorage<ty::ConstVid<'tcx>>,
&mut InferCtxtUndoLogs<'tcx>,
>,
> {
2020-04-17 08:54:28 +02:00
self.const_unification_storage.with_log(&mut self.undo_log)
}
#[inline]
pub fn unwrap_region_constraints(&mut self) -> RegionConstraintCollector<'_, 'tcx> {
2020-04-17 09:15:28 +02:00
self.region_constraint_storage
.as_mut()
.expect("region constraints already solved")
.with_log(&mut self.undo_log)
}
}
2019-06-14 00:48:52 +03:00
pub struct InferCtxt<'a, 'tcx> {
pub tcx: TyCtxt<'tcx>,
2012-11-29 16:41:39 -08:00
2020-07-17 08:47:04 +00:00
/// During type-checking/inference of a body, `in_progress_typeck_results`
/// contains a reference to the typeck results being built up, which are
/// used for reading closure kinds/signatures as they are inferred,
/// and for error reporting logic to read arbitrary node types.
2020-07-17 08:47:04 +00:00
pub in_progress_typeck_results: Option<&'a RefCell<ty::TypeckResults<'tcx>>>,
pub inner: RefCell<InferCtxtInner<'tcx>>,
2012-11-29 16:41:39 -08:00
/// If set, this flag causes us to skip the 'leak check' during
/// higher-ranked subtyping operations. This flag is a temporary one used
/// to manage the removal of the leak-check: for the time being, we still run the
/// leak-check, but we issue warnings. This flag can only be set to true
/// when entering a snapshot.
skip_leak_check: Cell<bool>,
2019-04-22 21:29:11 +03:00
/// Once region inference is done, the values for each variable.
lexical_region_resolutions: RefCell<Option<LexicalRegionResolutions<'tcx>>>,
/// Caches the results of trait selection. This cache is used
/// for things that have to do with the parameters in scope.
2020-02-22 11:44:18 +01:00
pub selection_cache: select::SelectionCache<'tcx>,
/// Caches the results of trait evaluation.
2020-02-22 11:44:18 +01:00
pub evaluation_cache: select::EvaluationCache<'tcx>,
2019-04-22 21:29:11 +03:00
/// the set of predicates on which errors have been reported, to
/// avoid reporting the same error twice.
pub reported_trait_errors: RefCell<FxHashMap<Span, Vec<ty::Predicate<'tcx>>>>,
pub reported_closure_mismatch: RefCell<FxHashSet<(Span, Option<Span>)>>,
2019-04-22 21:29:11 +03:00
/// When an error occurs, we want to avoid reporting "derived"
/// errors that are due to this original failure. Normally, we
/// handle this with the `err_count_on_creation` count, which
/// basically just tracks how many errors were reported when we
/// started type-checking a fn and checks to see if any new errors
/// have been reported since then. Not great, but it works.
///
/// However, when errors originated in other passes -- notably
/// resolve -- this heuristic breaks down. Therefore, we have this
/// auxiliary flag that one can set whenever one creates a
/// type-error that is due to an error in a prior pass.
///
/// Don't read this flag directly, call `is_tainted_by_errors()`
/// and `set_tainted_by_errors()`.
tainted_by_errors_flag: Cell<bool>,
2019-04-22 21:29:11 +03:00
/// Track how many errors were reported when this infcx is created.
/// If the number of errors increases, that's also a sign (line
/// `tained_by_errors`) to avoid reporting certain kinds of errors.
2019-06-22 12:46:48 +01:00
// FIXME(matthewjasper) Merge into `tainted_by_errors_flag`
err_count_on_creation: usize,
2019-04-22 21:29:11 +03:00
/// This flag is true while there is an active snapshot.
in_snapshot: Cell<bool>,
/// What is the innermost universe we have created? Starts out as
/// `UniverseIndex::root()` but grows from there as we enter
/// universal quantifiers.
///
/// N.B., at present, we exclude the universal quantifiers on the
/// item we are type-checking, and just consider those names as
/// part of the root universe. So this would only get incremented
/// when we enter into a higher-ranked (`for<..>`) type or trait
/// bound.
universe: Cell<ty::UniverseIndex>,
}
2012-11-29 16:41:39 -08:00
2019-02-08 14:53:55 +01:00
/// See the `error_reporting` module for more details.
2020-10-24 02:21:18 +02:00
#[derive(Clone, Copy, Debug, PartialEq, Eq, TypeFoldable)]
pub enum ValuePairs<'tcx> {
2015-09-06 21:51:58 +03:00
Types(ExpectedFound<Ty<'tcx>>),
Regions(ExpectedFound<ty::Region<'tcx>>),
2019-05-06 15:07:46 +01:00
Consts(ExpectedFound<&'tcx ty::Const<'tcx>>),
2015-09-06 21:51:58 +03:00
TraitRefs(ExpectedFound<ty::TraitRef<'tcx>>),
PolyTraitRefs(ExpectedFound<ty::PolyTraitRef<'tcx>>),
}
/// The trace designates the path through inference that we took to
/// encounter an error or subtyping constraint.
///
2019-02-08 14:53:55 +01:00
/// See the `error_reporting` module for more details.
2019-11-11 13:14:57 -05:00
#[derive(Clone, Debug)]
pub struct TypeTrace<'tcx> {
cause: ObligationCause<'tcx>,
values: ValuePairs<'tcx>,
}
/// The origin of a `r1 <= r2` constraint.
///
/// See `error_reporting` module for more details
2015-01-28 08:34:18 -05:00
#[derive(Clone, Debug)]
pub enum SubregionOrigin<'tcx> {
2019-04-22 21:29:11 +03:00
/// Arose from a subtyping relation
Subtype(Box<TypeTrace<'tcx>>),
2019-04-22 21:29:11 +03:00
/// When casting `&'a T` to an `&'b Trait` object,
/// relating `'a` to `'b`
RelateObjectBound(Span),
2019-04-22 21:29:11 +03:00
/// Some type parameter was instantiated with the given type,
/// and that type must outlive some region.
RelateParamBound(Span, Ty<'tcx>),
2019-04-22 21:29:11 +03:00
/// The given region parameter was instantiated with a region
/// that must outlive some other region.
RelateRegionParamBound(Span),
2019-04-22 21:29:11 +03:00
/// Creating a pointer `b` to contents of another reference
Reborrow(Span),
2019-04-22 21:29:11 +03:00
/// Creating a pointer `b` to contents of an upvar
ReborrowUpvar(Span, ty::UpvarId),
2019-04-22 21:29:11 +03:00
/// Data with type `Ty<'tcx>` was borrowed
DataBorrowed(Ty<'tcx>, Span),
2019-04-22 21:29:11 +03:00
/// (&'a &'b T) where a >= b
ReferenceOutlivesReferent(Ty<'tcx>, Span),
2019-04-22 21:29:11 +03:00
/// Region in return type of invoked fn must enclose call
CallReturn(Span),
2019-04-22 21:29:11 +03:00
/// Comparing the signature and requirements of an impl method against
/// the containing trait.
CompareImplMethodObligation {
span: Span,
2020-04-19 13:00:18 +02:00
item_name: Symbol,
impl_item_def_id: DefId,
trait_item_def_id: DefId,
},
}
// `SubregionOrigin` is used a lot. Make sure it doesn't unintentionally get bigger.
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
static_assert_size!(SubregionOrigin<'_>, 32);
/// Times when we replace late-bound regions with variables:
2015-01-28 08:34:18 -05:00
#[derive(Clone, Copy, Debug)]
pub enum LateBoundRegionConversionTime {
/// when a fn is called
FnCall,
/// when two higher-ranked types are compared
HigherRankedType,
/// when projecting an associated type
AssocTypeProjection(DefId),
}
/// Reasons to create a region inference variable
///
/// See `error_reporting` module for more details
#[derive(Copy, Clone, Debug)]
pub enum RegionVariableOrigin {
2019-04-22 21:29:11 +03:00
/// Region variables created for ill-categorized reasons,
/// mostly indicates places in need of refactoring
MiscVariable(Span),
2019-04-22 21:29:11 +03:00
/// Regions created by a `&P` or `[...]` pattern
PatternRegion(Span),
2019-04-22 21:29:11 +03:00
/// Regions created by `&` operator
AddrOfRegion(Span),
2019-04-22 21:29:11 +03:00
/// Regions created as part of an autoref of a method receiver
Autoref(Span, ty::AssocItem),
2019-04-22 21:29:11 +03:00
/// Regions created as part of an automatic coercion
Coercion(Span),
2019-04-22 21:29:11 +03:00
/// Region variables created as the values for early-bound regions
EarlyBoundRegion(Span, Symbol),
2019-04-22 21:29:11 +03:00
/// Region variables created for bound regions
/// in a function or method that is called
LateBoundRegion(Span, ty::BoundRegionKind, LateBoundRegionConversionTime),
UpvarRegion(ty::UpvarId, Span),
2020-04-19 13:00:18 +02:00
BoundRegionInCoherence(Symbol),
2019-04-22 21:29:11 +03:00
/// This origin is used for the inference variables that we create
/// during NLL region processing.
Nll(NllRegionVariableOrigin),
}
#[derive(Copy, Clone, Debug)]
pub enum NllRegionVariableOrigin {
/// During NLL region processing, we create variables for free
/// regions that we encounter in the function signature and
/// elsewhere. This origin indices we've got one of those.
FreeRegion,
/// "Universal" instantiation of a higher-ranked region (e.g.,
/// from a `for<'a> T` binder). Meant to represent "any region".
2018-11-02 15:08:51 +01:00
Placeholder(ty::PlaceholderRegion),
2018-07-23 17:30:59 +03:00
2020-04-09 10:55:27 +00:00
/// The variable we create to represent `'empty(U0)`.
RootEmptyRegion,
Improve HRTB error span when -Zno-leak-check is used As described in #57374, NLL currently produces unhelpful higher-ranked trait bound (HRTB) errors when '-Zno-leak-check' is enabled. This PR tackles one half of this issue - making the error message point at the proper span. The error message itself is still the very generic "higher-ranked subtype error", but this can be improved in a follow-up PR. The root cause of the bad spans lies in how NLL attempts to compute the 'blamed' region, for which it will retrieve a span for. Consider the following code, which (correctly) does not compile: ```rust let my_val: u8 = 25; let a: &u8 = &my_val; let b = a; let c = b; let d: &'static u8 = c; ``` This will cause NLL to generate the following subtype constraints: d :< c c :< b b <: a Since normal Rust lifetimes are covariant, this results in the following region constraints (I'm using 'd to denote the lifetime of 'd', 'c to denote the lifetime of 'c, etc.): 'c: 'd 'b: 'c 'a: 'b From this, we can derive that 'a: 'd holds, which implies that 'a: 'static must hold. However, this is not the case, since 'a refers to 'my_val', which does not outlive the current function. When NLL attempts to infer regions for this code, it will see that the region 'a has grown 'too large' - it will be inferred to outlive 'static, despite the fact that is not declared as outliving 'static We can find the region responsible, 'd, by starting at the *end* of the 'constraint chain' we generated above. This works because for normal (non-higher-ranked) lifetimes, we generally build up a 'chain' of lifetime constraints *away* from the original variable/lifetime. That is, our original lifetime 'a is required to outlive progressively more regions. If it ends up living for too long, we can look at the 'end' of this chain to determine the 'most recent' usage that caused the lifetime to grow too large. However, this logic does not work correctly when higher-ranked trait bounds (HRTBs) come into play. This is because HRTBs have *contravariance* with respect to their bound regions. For example, this code snippet compiles: ```rust let a: for<'a> fn(&'a ()) = |_| {}; let b: fn(&'static ()) = a; ``` Here, we require that 'a' is a subtype of 'b'. Because of contravariance, we end up with the region constraint 'static: 'a, *not* 'a: 'static This means that our 'constraint chains' grow in the opposite direction of 'normal lifetime' constraint chains. As we introduce subtypes, our lifetime ends up being outlived by other lifetimes, rather than outliving other lifetimes. Therefore, starting at the end of the 'constraint chain' will cause us to 'blame' a lifetime close to the original definition of a variable, instead of close to where the bad lifetime constraint is introduced. This PR improves how we select the region to blame for 'too large' universal lifetimes, when bound lifetimes are involved. If the region we're checking is a 'placeholder' region (e.g. the region 'a' in for<'a>, or the implicit region in fn(&())), we start traversing the constraint chain from the beginning, rather than the end. There are two (maybe more) different ways we generate region constraints for NLL: requirements generated from trait queries, and requirements generated from MIR subtype constraints. While the former always use explicit placeholder regions, the latter is more tricky. In order to implement contravariance for HRTBs, TypeRelating replaces placeholder regions with existential regions. This requires us to keep track of whether or not an existential region was originally a placeholder region. When we look for a region to blame, we check if our starting region is either a placeholder region or is an existential region created from a placeholder region. If so, we start iterating from the beginning of the constraint chain, rather than the end.
2019-08-18 01:20:24 -04:00
Existential {
/// If this is true, then this variable was created to represent a lifetime
/// bound in a `for` binder. For example, it might have been created to
/// represent the lifetime `'a` in a type like `for<'a> fn(&'a u32)`.
/// Such variables are created when we are trying to figure out if there
/// is any valid instantiation of `'a` that could fit into some scenario.
///
/// This is used to inform error reporting: in the case that we are trying to
/// determine whether there is any valid instantiation of a `'a` variable that meets
/// some constraint C, we want to blame the "source" of that `for` type,
/// rather than blaming the source of the constraint C.
2019-12-22 17:42:04 -05:00
from_forall: bool,
Improve HRTB error span when -Zno-leak-check is used As described in #57374, NLL currently produces unhelpful higher-ranked trait bound (HRTB) errors when '-Zno-leak-check' is enabled. This PR tackles one half of this issue - making the error message point at the proper span. The error message itself is still the very generic "higher-ranked subtype error", but this can be improved in a follow-up PR. The root cause of the bad spans lies in how NLL attempts to compute the 'blamed' region, for which it will retrieve a span for. Consider the following code, which (correctly) does not compile: ```rust let my_val: u8 = 25; let a: &u8 = &my_val; let b = a; let c = b; let d: &'static u8 = c; ``` This will cause NLL to generate the following subtype constraints: d :< c c :< b b <: a Since normal Rust lifetimes are covariant, this results in the following region constraints (I'm using 'd to denote the lifetime of 'd', 'c to denote the lifetime of 'c, etc.): 'c: 'd 'b: 'c 'a: 'b From this, we can derive that 'a: 'd holds, which implies that 'a: 'static must hold. However, this is not the case, since 'a refers to 'my_val', which does not outlive the current function. When NLL attempts to infer regions for this code, it will see that the region 'a has grown 'too large' - it will be inferred to outlive 'static, despite the fact that is not declared as outliving 'static We can find the region responsible, 'd, by starting at the *end* of the 'constraint chain' we generated above. This works because for normal (non-higher-ranked) lifetimes, we generally build up a 'chain' of lifetime constraints *away* from the original variable/lifetime. That is, our original lifetime 'a is required to outlive progressively more regions. If it ends up living for too long, we can look at the 'end' of this chain to determine the 'most recent' usage that caused the lifetime to grow too large. However, this logic does not work correctly when higher-ranked trait bounds (HRTBs) come into play. This is because HRTBs have *contravariance* with respect to their bound regions. For example, this code snippet compiles: ```rust let a: for<'a> fn(&'a ()) = |_| {}; let b: fn(&'static ()) = a; ``` Here, we require that 'a' is a subtype of 'b'. Because of contravariance, we end up with the region constraint 'static: 'a, *not* 'a: 'static This means that our 'constraint chains' grow in the opposite direction of 'normal lifetime' constraint chains. As we introduce subtypes, our lifetime ends up being outlived by other lifetimes, rather than outliving other lifetimes. Therefore, starting at the end of the 'constraint chain' will cause us to 'blame' a lifetime close to the original definition of a variable, instead of close to where the bad lifetime constraint is introduced. This PR improves how we select the region to blame for 'too large' universal lifetimes, when bound lifetimes are involved. If the region we're checking is a 'placeholder' region (e.g. the region 'a' in for<'a>, or the implicit region in fn(&())), we start traversing the constraint chain from the beginning, rather than the end. There are two (maybe more) different ways we generate region constraints for NLL: requirements generated from trait queries, and requirements generated from MIR subtype constraints. While the former always use explicit placeholder regions, the latter is more tricky. In order to implement contravariance for HRTBs, TypeRelating replaces placeholder regions with existential regions. This requires us to keep track of whether or not an existential region was originally a placeholder region. When we look for a region to blame, we check if our starting region is either a placeholder region or is an existential region created from a placeholder region. If so, we start iterating from the beginning of the constraint chain, rather than the end.
2019-08-18 01:20:24 -04:00
},
2018-07-22 19:42:40 +03:00
}
// FIXME(eddyb) investigate overlap between this and `TyOrConstInferVar`.
2015-03-30 09:38:44 -04:00
#[derive(Copy, Clone, Debug)]
pub enum FixupError<'tcx> {
2015-07-10 19:16:35 -07:00
UnresolvedIntTy(IntVid),
UnresolvedFloatTy(FloatVid),
2018-08-29 13:25:47 -04:00
UnresolvedTy(TyVid),
UnresolvedConst(ConstVid<'tcx>),
2012-11-29 16:41:39 -08:00
}
/// See the `region_obligations` field for more information.
#[derive(Clone)]
pub struct RegionObligation<'tcx> {
pub sub_region: ty::Region<'tcx>,
pub sup_type: Ty<'tcx>,
pub origin: SubregionOrigin<'tcx>,
}
impl<'tcx> fmt::Display for FixupError<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use self::FixupError::*;
match *self {
2018-08-29 13:25:47 -04:00
UnresolvedIntTy(_) => write!(
f,
"cannot determine the type of this integer; \
add a suffix to specify the type explicitly"
),
UnresolvedFloatTy(_) => write!(
f,
"cannot determine the type of this number; \
add a suffix to specify the type explicitly"
),
UnresolvedTy(_) => write!(f, "unconstrained type"),
UnresolvedConst(_) => write!(f, "unconstrained const value"),
}
2012-11-29 16:41:39 -08:00
}
}
2019-02-08 14:53:55 +01:00
/// Helper type of a temporary returned by `tcx.infer_ctxt()`.
/// Necessary because we can't write the following bound:
2019-06-14 00:48:52 +03:00
/// `F: for<'b, 'tcx> where 'tcx FnOnce(InferCtxt<'b, 'tcx>)`.
pub struct InferCtxtBuilder<'tcx> {
2020-06-28 11:43:10 +02:00
tcx: TyCtxt<'tcx>,
2020-07-17 08:47:04 +00:00
fresh_typeck_results: Option<RefCell<ty::TypeckResults<'tcx>>>,
}
2020-01-06 20:13:24 +01:00
pub trait TyCtxtInferExt<'tcx> {
fn infer_ctxt(self) -> InferCtxtBuilder<'tcx>;
}
impl TyCtxtInferExt<'tcx> for TyCtxt<'tcx> {
fn infer_ctxt(self) -> InferCtxtBuilder<'tcx> {
2020-07-17 08:47:04 +00:00
InferCtxtBuilder { tcx: self, fresh_typeck_results: None }
}
}
2019-06-14 00:48:52 +03:00
impl<'tcx> InferCtxtBuilder<'tcx> {
/// Used only by `rustc_typeck` during body type-checking/inference,
2020-07-17 08:47:04 +00:00
/// will initialize `in_progress_typeck_results` with fresh `TypeckResults`.
pub fn with_fresh_in_progress_typeck_results(mut self, table_owner: LocalDefId) -> Self {
self.fresh_typeck_results = Some(RefCell::new(ty::TypeckResults::new(table_owner)));
self
}
/// Given a canonical value `C` as a starting point, create an
/// inference context that contains each of the bound values
/// within instantiated as a fresh variable. The `f` closure is
/// invoked with the new infcx, along with the instantiated value
2019-02-08 14:53:55 +01:00
/// `V` and a substitution `S`. This substitution `S` maps from
/// the bound values in `C` to their instantiated values in `V`
/// (in other words, `S(C) = V`).
pub fn enter_with_canonical<T, R>(
2019-06-14 00:48:52 +03:00
&mut self,
span: Span,
canonical: &Canonical<'tcx, T>,
2019-06-14 00:48:52 +03:00
f: impl for<'a> FnOnce(InferCtxt<'a, 'tcx>, T, CanonicalVarValues<'tcx>) -> R,
) -> R
2018-08-29 13:25:47 -04:00
where
T: TypeFoldable<'tcx>,
{
self.enter(|infcx| {
2018-10-08 06:59:37 -04:00
let (value, subst) =
infcx.instantiate_canonical_with_fresh_inference_vars(span, canonical);
f(infcx, value, subst)
})
}
2019-06-14 00:48:52 +03:00
pub fn enter<R>(&mut self, f: impl for<'a> FnOnce(InferCtxt<'a, 'tcx>) -> R) -> R {
2020-07-17 08:47:04 +00:00
let InferCtxtBuilder { tcx, ref fresh_typeck_results } = *self;
let in_progress_typeck_results = fresh_typeck_results.as_ref();
2020-06-28 11:43:10 +02:00
f(InferCtxt {
tcx,
2020-07-17 08:47:04 +00:00
in_progress_typeck_results,
2020-06-28 11:43:10 +02:00
inner: RefCell::new(InferCtxtInner::new()),
lexical_region_resolutions: RefCell::new(None),
selection_cache: Default::default(),
evaluation_cache: Default::default(),
reported_trait_errors: Default::default(),
reported_closure_mismatch: Default::default(),
tainted_by_errors_flag: Cell::new(false),
err_count_on_creation: tcx.sess.err_count(),
in_snapshot: Cell::new(false),
skip_leak_check: Cell::new(false),
universe: Cell::new(ty::UniverseIndex::ROOT),
2018-08-29 13:25:47 -04:00
})
}
2015-06-27 22:04:15 -07:00
}
impl<'tcx, T> InferOk<'tcx, T> {
pub fn unit(self) -> InferOk<'tcx, ()> {
2019-12-22 17:42:04 -05:00
InferOk { value: (), obligations: self.obligations }
}
2019-02-08 14:53:55 +01:00
/// Extracts `value`, registering any obligations into `fulfill_cx`.
pub fn into_value_registering_obligations(
self,
2019-06-14 00:48:52 +03:00
infcx: &InferCtxt<'_, 'tcx>,
fulfill_cx: &mut dyn TraitEngine<'tcx>,
) -> T {
let InferOk { value, obligations } = self;
for obligation in obligations {
fulfill_cx.register_predicate_obligation(infcx, obligation);
}
value
}
}
impl<'tcx> InferOk<'tcx, ()> {
pub fn into_obligations(self) -> PredicateObligations<'tcx> {
self.obligations
}
}
#[must_use = "once you start a snapshot, you should always consume it"]
pub struct CombinedSnapshot<'a, 'tcx> {
undo_snapshot: Snapshot<'tcx>,
region_constraints_snapshot: RegionSnapshot,
universe: ty::UniverseIndex,
was_in_snapshot: bool,
2020-07-17 08:47:04 +00:00
_in_progress_typeck_results: Option<Ref<'a, ty::TypeckResults<'tcx>>>,
2012-11-29 16:41:39 -08:00
}
2019-06-14 00:48:52 +03:00
impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
pub fn is_in_snapshot(&self) -> bool {
self.in_snapshot.get()
}
2018-08-29 13:25:47 -04:00
pub fn freshen<T: TypeFoldable<'tcx>>(&self, t: T) -> T {
t.fold_with(&mut self.freshener())
}
pub fn type_var_diverges(&'a self, ty: Ty<'_>) -> bool {
2020-08-03 00:49:11 +02:00
match *ty.kind() {
ty::Infer(ty::TyVar(vid)) => self.inner.borrow_mut().type_variables().var_diverges(vid),
2018-08-29 13:25:47 -04:00
_ => false,
}
}
2019-06-14 00:48:52 +03:00
pub fn freshener<'b>(&'b self) -> TypeFreshener<'b, 'tcx> {
freshen::TypeFreshener::new(self)
}
pub fn type_is_unconstrained_numeric(&'a self, ty: Ty<'_>) -> UnconstrainedNumeric {
2020-03-29 16:41:09 +02:00
use rustc_middle::ty::error::UnconstrainedNumeric::Neither;
use rustc_middle::ty::error::UnconstrainedNumeric::{UnconstrainedFloat, UnconstrainedInt};
2020-08-03 00:49:11 +02:00
match *ty.kind() {
ty::Infer(ty::IntVar(vid)) => {
if self.inner.borrow_mut().int_unification_table().probe_value(vid).is_some() {
Neither
} else {
UnconstrainedInt
}
2018-08-29 13:25:47 -04:00
}
ty::Infer(ty::FloatVar(vid)) => {
if self.inner.borrow_mut().float_unification_table().probe_value(vid).is_some() {
Neither
} else {
UnconstrainedFloat
}
2018-08-29 13:25:47 -04:00
}
_ => Neither,
}
}
2017-09-14 21:44:23 -04:00
pub fn unsolved_variables(&self) -> Vec<Ty<'tcx>> {
let mut inner = self.inner.borrow_mut();
let mut vars: Vec<Ty<'_>> = inner
.type_variables()
.unsolved_variables()
.into_iter()
.map(|t| self.tcx.mk_ty_var(t))
.collect();
vars.extend(
(0..inner.int_unification_table().len())
.map(|i| ty::IntVid { index: i as u32 })
.filter(|&vid| inner.int_unification_table().probe_value(vid).is_none())
.map(|v| self.tcx.mk_int_var(v)),
);
vars.extend(
(0..inner.float_unification_table().len())
.map(|i| ty::FloatVid { index: i as u32 })
.filter(|&vid| inner.float_unification_table().probe_value(vid).is_none())
.map(|v| self.tcx.mk_float_var(v)),
);
vars
}
2018-08-29 13:25:47 -04:00
fn combine_fields(
&'a self,
trace: TypeTrace<'tcx>,
param_env: ty::ParamEnv<'tcx>,
2019-06-14 00:48:52 +03:00
) -> CombineFields<'a, 'tcx> {
CombineFields {
infcx: self,
trace,
cause: None,
2017-05-23 04:19:47 -04:00
param_env,
obligations: PredicateObligations::new(),
}
2012-11-29 16:41:39 -08:00
}
2019-04-22 21:29:11 +03:00
/// Clear the "currently in a snapshot" flag, invoke the closure,
/// then restore the flag to its original value. This flag is a
/// debugging measure designed to detect cases where we start a
/// snapshot, create type variables, and register obligations
/// which may involve those type variables in the fulfillment cx,
/// potentially leaving "dangling type variables" behind.
/// In such cases, an assertion will fail when attempting to
/// register obligations, within a snapshot. Very useful, much
2019-04-30 17:27:53 +09:00
/// better than grovelling through megabytes of `RUSTC_LOG` output.
2019-04-22 21:29:11 +03:00
///
/// HOWEVER, in some cases the flag is unhelpful. In particular, we
/// sometimes create a "mini-fulfilment-cx" in which we enroll
/// obligations. As long as this fulfillment cx is fully drained
/// before we return, this is not a problem, as there won't be any
/// escaping obligations in the main cx. In those cases, you can
/// use this function.
pub fn save_and_restore_in_snapshot_flag<F, R>(&self, func: F) -> R
2018-08-29 13:25:47 -04:00
where
F: FnOnce(&Self) -> R,
{
let flag = self.in_snapshot.replace(false);
let result = func(self);
self.in_snapshot.set(flag);
result
}
fn start_snapshot(&self) -> CombinedSnapshot<'a, 'tcx> {
debug!("start_snapshot()");
let in_snapshot = self.in_snapshot.replace(true);
let mut inner = self.inner.borrow_mut();
CombinedSnapshot {
2020-02-25 15:39:01 +01:00
undo_snapshot: inner.undo_log.start_snapshot(),
region_constraints_snapshot: inner.unwrap_region_constraints().start_snapshot(),
universe: self.universe(),
was_in_snapshot: in_snapshot,
2020-07-17 08:47:04 +00:00
// Borrow typeck results "in progress" (i.e., during typeck)
// to ban writes from within a snapshot to them.
2020-07-17 08:47:04 +00:00
_in_progress_typeck_results: self
.in_progress_typeck_results
.map(|typeck_results| typeck_results.borrow()),
2012-11-29 16:41:39 -08:00
}
}
fn rollback_to(&self, cause: &str, snapshot: CombinedSnapshot<'a, 'tcx>) {
debug!("rollback_to(cause={})", cause);
let CombinedSnapshot {
undo_snapshot,
region_constraints_snapshot,
universe,
was_in_snapshot,
2020-07-17 08:47:04 +00:00
_in_progress_typeck_results,
} = snapshot;
self.in_snapshot.set(was_in_snapshot);
self.universe.set(universe);
let mut inner = self.inner.borrow_mut();
inner.rollback_to(undo_snapshot);
inner.unwrap_region_constraints().rollback_to(region_constraints_snapshot);
2012-11-29 16:41:39 -08:00
}
fn commit_from(&self, snapshot: CombinedSnapshot<'a, 'tcx>) {
debug!("commit_from()");
let CombinedSnapshot {
undo_snapshot,
region_constraints_snapshot: _,
universe: _,
was_in_snapshot,
2020-07-17 08:47:04 +00:00
_in_progress_typeck_results,
} = snapshot;
self.in_snapshot.set(was_in_snapshot);
2020-04-17 08:54:28 +02:00
self.inner.borrow_mut().commit(undo_snapshot);
}
2012-11-29 16:41:39 -08:00
2019-02-08 14:53:55 +01:00
/// Executes `f` and commit the bindings.
2018-08-29 13:25:47 -04:00
pub fn commit_unconditionally<R, F>(&self, f: F) -> R
where
F: FnOnce(&CombinedSnapshot<'a, 'tcx>) -> R,
2014-12-08 20:26:43 -05:00
{
debug!("commit_unconditionally()");
let snapshot = self.start_snapshot();
let r = f(&snapshot);
self.commit_from(snapshot);
r
}
2012-11-29 16:41:39 -08:00
/// Execute `f` and commit the bindings if closure `f` returns `Ok(_)`.
2018-08-29 13:25:47 -04:00
pub fn commit_if_ok<T, E, F>(&self, f: F) -> Result<T, E>
where
F: FnOnce(&CombinedSnapshot<'a, 'tcx>) -> Result<T, E>,
2014-12-08 20:26:43 -05:00
{
debug!("commit_if_ok()");
let snapshot = self.start_snapshot();
let r = f(&snapshot);
debug!("commit_if_ok() -- r.is_ok() = {}", r.is_ok());
match r {
2018-08-29 13:25:47 -04:00
Ok(_) => {
self.commit_from(snapshot);
}
Err(_) => {
self.rollback_to("commit_if_ok -- error", snapshot);
}
}
r
2012-11-29 16:41:39 -08:00
}
/// Execute `f` then unroll any bindings it creates.
2018-08-29 13:25:47 -04:00
pub fn probe<R, F>(&self, f: F) -> R
where
F: FnOnce(&CombinedSnapshot<'a, 'tcx>) -> R,
2014-12-08 20:26:43 -05:00
{
debug!("probe()");
let snapshot = self.start_snapshot();
let r = f(&snapshot);
self.rollback_to("probe", snapshot);
r
2012-11-29 16:41:39 -08:00
}
/// If `should_skip` is true, then execute `f` then unroll any bindings it creates.
pub fn probe_maybe_skip_leak_check<R, F>(&self, should_skip: bool, f: F) -> R
where
F: FnOnce(&CombinedSnapshot<'a, 'tcx>) -> R,
{
debug!("probe()");
let snapshot = self.start_snapshot();
let was_skip_leak_check = self.skip_leak_check.get();
if should_skip {
self.skip_leak_check.set(true);
}
let r = f(&snapshot);
self.rollback_to("probe", snapshot);
self.skip_leak_check.set(was_skip_leak_check);
r
}
/// Scan the constraints produced since `snapshot` began and returns:
///
2019-04-22 21:29:11 +03:00
/// - `None` -- if none of them involve "region outlives" constraints
/// - `Some(true)` -- if there are `'a: 'b` constraints where `'a` or `'b` is a placeholder
/// - `Some(false)` -- if there are `'a: 'b` constraints but none involve placeholders
pub fn region_constraints_added_in_snapshot(
&self,
snapshot: &CombinedSnapshot<'a, 'tcx>,
) -> Option<bool> {
self.inner
.borrow_mut()
.unwrap_region_constraints()
.region_constraints_added_in_snapshot(&snapshot.undo_snapshot)
}
2018-08-29 13:25:47 -04:00
pub fn add_given(&self, sub: ty::Region<'tcx>, sup: ty::RegionVid) {
self.inner.borrow_mut().unwrap_region_constraints().add_given(sub, sup);
}
2018-08-29 13:25:47 -04:00
pub fn can_sub<T>(&self, param_env: ty::ParamEnv<'tcx>, a: T, b: T) -> UnitResult<'tcx>
where
T: at::ToTrace<'tcx>,
{
let origin = &ObligationCause::dummy();
self.probe(|_| {
2019-12-22 17:42:04 -05:00
self.at(origin, param_env).sub(a, b).map(|InferOk { obligations: _, .. }| {
// Ignore obligations, since we are unrolling
// everything anyway.
})
})
}
2018-08-29 13:25:47 -04:00
pub fn can_eq<T>(&self, param_env: ty::ParamEnv<'tcx>, a: T, b: T) -> UnitResult<'tcx>
where
T: at::ToTrace<'tcx>,
{
let origin = &ObligationCause::dummy();
self.probe(|_| {
2019-12-22 17:42:04 -05:00
self.at(origin, param_env).eq(a, b).map(|InferOk { obligations: _, .. }| {
// Ignore obligations, since we are unrolling
// everything anyway.
})
})
}
2012-11-29 16:41:39 -08:00
2018-08-29 13:25:47 -04:00
pub fn sub_regions(
&self,
origin: SubregionOrigin<'tcx>,
a: ty::Region<'tcx>,
b: ty::Region<'tcx>,
) {
debug!("sub_regions({:?} <: {:?})", a, b);
self.inner.borrow_mut().unwrap_region_constraints().make_subregion(origin, a, b);
}
/// Require that the region `r` be equal to one of the regions in
/// the set `regions`.
2019-06-18 08:52:23 -04:00
pub fn member_constraint(
&self,
opaque_type_def_id: DefId,
definition_span: Span,
hidden_ty: Ty<'tcx>,
region: ty::Region<'tcx>,
2019-06-17 23:56:35 -04:00
in_regions: &Lrc<Vec<ty::Region<'tcx>>>,
) {
2019-06-18 08:52:23 -04:00
debug!("member_constraint({:?} <: {:?})", region, in_regions);
self.inner.borrow_mut().unwrap_region_constraints().member_constraint(
2019-12-22 17:42:04 -05:00
opaque_type_def_id,
definition_span,
hidden_ty,
region,
in_regions,
);
}
2018-08-29 13:25:47 -04:00
pub fn subtype_predicate(
&self,
cause: &ObligationCause<'tcx>,
param_env: ty::ParamEnv<'tcx>,
predicate: ty::PolySubtypePredicate<'tcx>,
2018-08-29 13:25:47 -04:00
) -> Option<InferResult<'tcx, ()>> {
2017-03-09 21:47:09 -05:00
// Subtle: it's ok to skip the binder here and resolve because
// `shallow_resolve` just ignores anything that is not a type
2017-03-09 21:47:09 -05:00
// variable, and because type variable's can't (at present, at
// least) capture any of the things bound by this binder.
//
// NOTE(nmatsakis): really, there is no *particular* reason to do this
// `shallow_resolve` here except as a micro-optimization.
// Naturally I could not resist.
2017-03-09 21:47:09 -05:00
let two_unbound_type_vars = {
let a = self.shallow_resolve(predicate.skip_binder().a);
let b = self.shallow_resolve(predicate.skip_binder().b);
2017-03-09 21:47:09 -05:00
a.is_ty_var() && b.is_ty_var()
};
if two_unbound_type_vars {
// Two unbound type variables? Can't make progress.
return None;
}
Some(self.commit_if_ok(|_snapshot| {
let ty::SubtypePredicate { a_is_expected, a, b } =
2020-10-24 02:21:18 +02:00
self.replace_bound_vars_with_placeholders(predicate);
2019-12-22 17:42:04 -05:00
let ok = self.at(cause, param_env).sub_exp(a_is_expected, a, b)?;
Ok(ok.unit())
}))
2017-03-09 21:47:09 -05:00
}
2018-08-29 13:25:47 -04:00
pub fn region_outlives_predicate(
&self,
cause: &traits::ObligationCause<'tcx>,
predicate: ty::PolyRegionOutlivesPredicate<'tcx>,
) -> UnitResult<'tcx> {
self.commit_if_ok(|_snapshot| {
let ty::OutlivesPredicate(r_a, r_b) =
2020-10-24 02:21:18 +02:00
self.replace_bound_vars_with_placeholders(predicate);
2019-12-22 17:42:04 -05:00
let origin = SubregionOrigin::from_obligation_cause(cause, || {
RelateRegionParamBound(cause.span)
});
self.sub_regions(origin, r_b, r_a); // `b : a` ==> `a <= b`
Ok(())
})
}
pub fn next_ty_var_id(&self, diverging: bool, origin: TypeVariableOrigin) -> TyVid {
self.inner.borrow_mut().type_variables().new_var(self.universe(), diverging, origin)
2012-11-29 16:41:39 -08:00
}
pub fn next_ty_var(&self, origin: TypeVariableOrigin) -> Ty<'tcx> {
self.tcx.mk_ty_var(self.next_ty_var_id(false, origin))
}
pub fn next_ty_var_in_universe(
&self,
origin: TypeVariableOrigin,
2019-12-22 17:42:04 -05:00
universe: ty::UniverseIndex,
) -> Ty<'tcx> {
let vid = self.inner.borrow_mut().type_variables().new_var(universe, false, origin);
self.tcx.mk_ty_var(vid)
}
pub fn next_diverging_ty_var(&self, origin: TypeVariableOrigin) -> Ty<'tcx> {
self.tcx.mk_ty_var(self.next_ty_var_id(true, origin))
2012-11-29 16:41:39 -08:00
}
pub fn next_const_var(
&self,
ty: Ty<'tcx>,
2019-12-22 17:42:04 -05:00
origin: ConstVariableOrigin,
2019-03-18 20:55:19 +00:00
) -> &'tcx ty::Const<'tcx> {
self.tcx.mk_const_var(self.next_const_var_id(origin), ty)
}
pub fn next_const_var_in_universe(
&self,
ty: Ty<'tcx>,
origin: ConstVariableOrigin,
universe: ty::UniverseIndex,
2019-03-18 20:55:19 +00:00
) -> &'tcx ty::Const<'tcx> {
2019-12-22 17:42:04 -05:00
let vid = self
.inner
.borrow_mut()
.const_unification_table()
2019-12-22 17:42:04 -05:00
.new_key(ConstVarValue { origin, val: ConstVariableValue::Unknown { universe } });
self.tcx.mk_const_var(vid, ty)
}
pub fn next_const_var_id(&self, origin: ConstVariableOrigin) -> ConstVid<'tcx> {
self.inner.borrow_mut().const_unification_table().new_key(ConstVarValue {
2019-12-22 17:42:04 -05:00
origin,
val: ConstVariableValue::Unknown { universe: self.universe() },
})
}
fn next_int_var_id(&self) -> IntVid {
self.inner.borrow_mut().int_unification_table().new_key(None)
2012-11-29 16:41:39 -08:00
}
pub fn next_int_var(&self) -> Ty<'tcx> {
self.tcx.mk_int_var(self.next_int_var_id())
}
fn next_float_var_id(&self) -> FloatVid {
self.inner.borrow_mut().float_unification_table().new_key(None)
2012-11-29 16:41:39 -08:00
}
pub fn next_float_var(&self) -> Ty<'tcx> {
self.tcx.mk_float_var(self.next_float_var_id())
}
2019-02-08 14:53:55 +01:00
/// Creates a fresh region variable with the next available index.
/// The variable will be created in the maximum universe created
/// thus far, allowing it to name any region created thus far.
2018-08-29 13:25:47 -04:00
pub fn next_region_var(&self, origin: RegionVariableOrigin) -> ty::Region<'tcx> {
self.next_region_var_in_universe(origin, self.universe())
}
2019-02-08 14:53:55 +01:00
/// Creates a fresh region variable with the next available index
/// in the given universe; typically, you can use
/// `next_region_var` and just use the maximal universe.
pub fn next_region_var_in_universe(
&self,
origin: RegionVariableOrigin,
universe: ty::UniverseIndex,
) -> ty::Region<'tcx> {
let region_var =
self.inner.borrow_mut().unwrap_region_constraints().new_region_var(universe, origin);
2018-02-07 12:22:47 -07:00
self.tcx.mk_region(ty::ReVar(region_var))
2012-11-29 16:41:39 -08:00
}
/// Return the universe that the region `r` was created in. For
/// most regions (e.g., `'static`, named regions from the user,
/// etc) this is the root universe U0. For inference variables or
/// placeholders, however, it will return the universe which which
/// they are associated.
2019-12-22 17:42:04 -05:00
fn universe_of_region(&self, r: ty::Region<'tcx>) -> ty::UniverseIndex {
self.inner.borrow_mut().unwrap_region_constraints().universe(r)
}
/// Number of region variables created so far.
pub fn num_region_vars(&self) -> usize {
self.inner.borrow_mut().unwrap_region_constraints().num_region_vars()
}
/// Just a convenient wrapper of `next_region_var` for using during NLL.
pub fn next_nll_region_var(&self, origin: NllRegionVariableOrigin) -> ty::Region<'tcx> {
self.next_region_var(RegionVariableOrigin::Nll(origin))
}
/// Just a convenient wrapper of `next_region_var` for using during NLL.
pub fn next_nll_region_var_in_universe(
&self,
origin: NllRegionVariableOrigin,
universe: ty::UniverseIndex,
) -> ty::Region<'tcx> {
self.next_region_var_in_universe(RegionVariableOrigin::Nll(origin), universe)
}
pub fn var_for_def(&self, span: Span, param: &ty::GenericParamDef) -> GenericArg<'tcx> {
2018-05-14 19:57:59 +01:00
match param.kind {
GenericParamDefKind::Lifetime => {
// Create a region inference variable for the given
// region parameter definition.
2019-12-22 17:42:04 -05:00
self.next_region_var(EarlyBoundRegion(span, param.name)).into()
2018-05-14 19:57:59 +01:00
}
2018-08-29 13:25:47 -04:00
GenericParamDefKind::Type { .. } => {
2018-05-14 19:57:59 +01:00
// Create a type inference variable for the given
// type parameter definition. The substitutions are
// for actual parameters that may be referred to by
// the default of this type parameter, if it exists.
// e.g., `struct Foo<A, B, C = (A, B)>(...);` when
2018-05-14 19:57:59 +01:00
// used in a path such as `Foo::<T, U>::new()` will
// use an inference variable for `C` with `[T, U]`
// as the substitutions for the default, `(T, U)`.
let ty_var_id = self.inner.borrow_mut().type_variables().new_var(
2018-08-29 13:25:47 -04:00
self.universe(),
false,
TypeVariableOrigin {
kind: TypeVariableOriginKind::TypeParameterDefinition(
param.name,
2019-12-22 17:42:04 -05:00
Some(param.def_id),
),
span,
},
2018-08-29 13:25:47 -04:00
);
2018-05-14 19:57:59 +01:00
self.tcx.mk_ty_var(ty_var_id).into()
}
GenericParamDefKind::Const { .. } => {
let origin = ConstVariableOrigin {
2020-09-23 09:24:58 +02:00
kind: ConstVariableOriginKind::ConstParameterDefinition(
param.name,
param.def_id,
),
span,
};
let const_var_id =
self.inner.borrow_mut().const_unification_table().new_key(ConstVarValue {
2019-12-22 17:42:04 -05:00
origin,
val: ConstVariableValue::Unknown { universe: self.universe() },
});
self.tcx.mk_const_var(const_var_id, self.tcx.type_of(param.def_id)).into()
2018-05-14 19:57:59 +01:00
}
}
}
/// Given a set of generics defined on a type or impl, returns a substitution mapping each
/// type/region parameter to a fresh inference variable.
2019-02-09 22:11:53 +08:00
pub fn fresh_substs_for_item(&self, span: Span, def_id: DefId) -> SubstsRef<'tcx> {
InternalSubsts::for_item(self.tcx, def_id, |param, _| self.var_for_def(span, param))
}
2019-02-08 14:53:55 +01:00
/// Returns `true` if errors have been reported since this infcx was
/// created. This is sometimes used as a heuristic to skip
/// reporting errors that often occur as a result of earlier
/// errors, but where it's hard to be 100% sure (e.g., unresolved
/// inference variables, regionck errors).
pub fn is_tainted_by_errors(&self) -> bool {
2018-08-29 13:25:47 -04:00
debug!(
"is_tainted_by_errors(err_count={}, err_count_on_creation={}, \
tainted_by_errors_flag={})",
self.tcx.sess.err_count(),
self.err_count_on_creation,
self.tainted_by_errors_flag.get()
);
if self.tcx.sess.err_count() > self.err_count_on_creation {
return true; // errors reported since this infcx was made
}
self.tainted_by_errors_flag.get()
}
/// Set the "tainted by errors" flag to true. We call this when we
/// observe an error from a prior pass.
pub fn set_tainted_by_errors(&self) {
debug!("set_tainted_by_errors()");
self.tainted_by_errors_flag.set(true)
}
/// Process the region constraints and report any errors that
/// result. After this, no more unification operations should be
/// done -- or the compiler will panic -- but it is legal to use
/// `resolve_vars_if_possible` as well as `fully_resolve`.
pub fn resolve_regions_and_report_errors(
&self,
region_context: DefId,
outlives_env: &OutlivesEnvironment<'tcx>,
mode: RegionckMode,
) {
2020-03-16 16:43:03 +01:00
let (var_infos, data) = {
let mut inner = self.inner.borrow_mut();
let inner = &mut *inner;
assert!(
self.is_tainted_by_errors() || inner.region_obligations.is_empty(),
"region_obligations not empty: {:#?}",
inner.region_obligations
);
inner
2020-04-17 09:15:28 +02:00
.region_constraint_storage
2020-03-16 16:43:03 +01:00
.take()
.expect("regions already resolved")
.with_log(&mut inner.undo_log)
.into_infos_and_data()
};
2020-05-19 21:26:18 +01:00
let region_rels =
&RegionRelations::new(self.tcx, region_context, outlives_env.free_region_map());
let (lexical_region_resolutions, errors) =
lexical_region_resolve::resolve(region_rels, var_infos, data, mode);
2019-12-22 17:42:04 -05:00
let old_value = self.lexical_region_resolutions.replace(Some(lexical_region_resolutions));
assert!(old_value.is_none());
if !self.is_tainted_by_errors() {
// As a heuristic, just skip reporting region errors
// altogether if other errors have been reported while
// this infcx was in use. This is totally hokey but
// otherwise we have a hard time separating legit region
// errors from silly ones.
2020-05-19 21:26:18 +01:00
self.report_region_errors(&errors);
}
2012-11-29 16:41:39 -08:00
}
/// Obtains (and clears) the current set of region
/// constraints. The inference context is still usable: further
/// unifications will simply add new constraints.
///
/// This method is not meant to be used with normal lexical region
/// resolution. Rather, it is used in the NLL mode as a kind of
/// interim hack: basically we run normal type-check and generate
/// region constraints as normal, but then we take them and
/// translate them into the form that the NLL solver
/// understands. See the NLL module for mode details.
pub fn take_and_reset_region_constraints(&self) -> RegionConstraintData<'tcx> {
2018-08-29 13:25:47 -04:00
assert!(
self.inner.borrow().region_obligations.is_empty(),
2018-08-29 13:25:47 -04:00
"region_obligations not empty: {:#?}",
self.inner.borrow().region_obligations
2018-08-29 13:25:47 -04:00
);
2017-11-28 00:39:38 -03:00
self.inner.borrow_mut().unwrap_region_constraints().take_and_reset_data()
}
/// Gives temporary access to the region constraint data.
pub fn with_region_constraints<R>(
&self,
op: impl FnOnce(&RegionConstraintData<'tcx>) -> R,
) -> R {
let mut inner = self.inner.borrow_mut();
op(inner.unwrap_region_constraints().data())
}
/// Takes ownership of the list of variable regions. This implies
2018-11-12 13:05:20 -05:00
/// that all the region constraints have already been taken, and
/// hence that `resolve_regions_and_report_errors` can never be
/// called. This is used only during NLL processing to "hand off" ownership
2018-11-12 13:05:20 -05:00
/// of the set of region variables into the NLL region context.
pub fn take_region_var_origins(&self) -> VarInfos {
let mut inner = self.inner.borrow_mut();
let (var_infos, data) = inner
2020-04-17 09:15:28 +02:00
.region_constraint_storage
2018-08-29 13:25:47 -04:00
.take()
.expect("regions already resolved")
.with_log(&mut inner.undo_log)
2018-08-29 13:25:47 -04:00
.into_infos_and_data();
assert!(data.is_empty());
var_infos
}
pub fn ty_to_string(&self, t: Ty<'tcx>) -> String {
2020-10-24 02:21:18 +02:00
self.resolve_vars_if_possible(t).to_string()
2012-11-29 16:41:39 -08:00
}
pub fn tys_to_string(&self, ts: &[Ty<'tcx>]) -> String {
let tstrs: Vec<String> = ts.iter().map(|t| self.ty_to_string(*t)).collect();
format!("({})", tstrs.join(", "))
}
2020-10-24 02:21:18 +02:00
pub fn trait_ref_to_string(&self, t: ty::TraitRef<'tcx>) -> String {
self.resolve_vars_if_possible(t).print_only_trait_path().to_string()
}
/// If `TyVar(vid)` resolves to a type, return that type. Else, return the
/// universe index of `TyVar(vid)`.
pub fn probe_ty_var(&self, vid: TyVid) -> Result<Ty<'tcx>, ty::UniverseIndex> {
use self::type_variable::TypeVariableValue;
match self.inner.borrow_mut().type_variables().probe(vid) {
TypeVariableValue::Known { value } => Ok(value),
TypeVariableValue::Unknown { universe } => Err(universe),
}
}
2019-10-02 14:35:01 -04:00
/// Resolve any type variables found in `value` -- but only one
/// level. So, if the variable `?X` is bound to some type
/// `Foo<?Y>`, then this would return `Foo<?Y>` (but `?Y` may
/// itself be bound to a type).
///
/// Useful when you only need to inspect the outermost level of
/// the type and don't care about nested types (or perhaps you
/// will be resolving them as well, e.g. in a loop).
pub fn shallow_resolve<T>(&self, value: T) -> T
where
T: TypeFoldable<'tcx>,
{
value.fold_with(&mut ShallowResolver { infcx: self })
}
pub fn root_var(&self, var: ty::TyVid) -> ty::TyVid {
self.inner.borrow_mut().type_variables().root_var(var)
}
/// Where possible, replaces type/const variables in
2019-04-22 21:29:11 +03:00
/// `value` with their final value. Note that region variables
/// are unaffected. If a type/const variable has not been unified, it
2019-04-22 21:29:11 +03:00
/// is left as is. This is an idempotent operation that does
/// not affect inference state in any way and so you can do it
/// at will.
2020-10-24 02:21:18 +02:00
pub fn resolve_vars_if_possible<T>(&self, value: T) -> T
2018-08-29 13:25:47 -04:00
where
T: TypeFoldable<'tcx>,
{
if !value.needs_infer() {
return value; // Avoid duplicated subst-folding.
}
let mut r = resolve::OpportunisticVarResolver::new(self);
value.fold_with(&mut r)
}
/// Returns the first unresolved variable contained in `T`. In the
/// process of visiting `T`, this will resolve (where possible)
/// type variables in `T`, but it never constructs the final,
/// resolved type, so it's more efficient than
/// `resolve_vars_if_possible()`.
pub fn unresolved_type_vars<T>(&self, value: &T) -> Option<(Ty<'tcx>, Option<Span>)>
2018-08-29 13:25:47 -04:00
where
T: TypeFoldable<'tcx>,
{
value.visit_with(&mut resolve::UnresolvedTypeFinder::new(self)).break_value()
}
pub fn probe_const_var(
&self,
2019-12-22 17:42:04 -05:00
vid: ty::ConstVid<'tcx>,
2019-03-18 20:55:19 +00:00
) -> Result<&'tcx ty::Const<'tcx>, ty::UniverseIndex> {
match self.inner.borrow_mut().const_unification_table().probe_value(vid).val {
ConstVariableValue::Known { value } => Ok(value),
ConstVariableValue::Unknown { universe } => Err(universe),
}
}
2020-10-24 02:21:18 +02:00
pub fn fully_resolve<T: TypeFoldable<'tcx>>(&self, value: T) -> FixupResult<'tcx, T> {
/*!
* Attempts to resolve all type/region/const variables in
* `value`. Region inference must have been run already (e.g.,
* by calling `resolve_regions_and_report_errors`). If some
* variable was never unified, an `Err` results.
*
* This method is idempotent, but it not typically not invoked
* except during the writeback phase.
*/
resolve::fully_resolve(self, value)
}
// [Note-Type-error-reporting]
// An invariant is that anytime the expected or actual type is Error (the special
// error type, meaning that an error occurred when typechecking this expression),
// this is a derived error. The error cascaded from another error (that was already
// reported), so it's not useful to display it to the user.
// The following methods implement this logic.
// They check if either the actual or expected type is Error, and don't print the error
// in this case. The typechecker should only ever report type errors involving mismatched
// types using one of these methods, and should not call span_err directly for such
// errors.
2012-11-29 16:41:39 -08:00
2018-08-29 13:25:47 -04:00
pub fn type_error_struct_with_diag<M>(
&self,
sp: Span,
mk_diag: M,
actual_ty: Ty<'tcx>,
) -> DiagnosticBuilder<'tcx>
where
M: FnOnce(String) -> DiagnosticBuilder<'tcx>,
2016-07-21 16:07:08 +02:00
{
2020-10-24 02:21:18 +02:00
let actual_ty = self.resolve_vars_if_possible(actual_ty);
2016-07-21 16:07:08 +02:00
debug!("type_error_struct_with_diag({:?}, {:?})", sp, actual_ty);
// Don't report an error if actual type is `Error`.
if actual_ty.references_error() {
2015-12-23 19:27:20 +13:00
return self.tcx.sess.diagnostic().struct_dummy();
}
2016-07-21 16:07:08 +02:00
mk_diag(self.ty_to_string(actual_ty))
}
2018-08-29 13:25:47 -04:00
pub fn report_mismatched_types(
&self,
cause: &ObligationCause<'tcx>,
expected: Ty<'tcx>,
actual: Ty<'tcx>,
err: TypeError<'tcx>,
) -> DiagnosticBuilder<'tcx> {
let trace = TypeTrace::types(cause, true, expected, actual);
self.report_and_explain_type_error(trace, &err)
2012-11-29 16:41:39 -08:00
}
pub fn report_mismatched_consts(
&self,
cause: &ObligationCause<'tcx>,
expected: &'tcx ty::Const<'tcx>,
actual: &'tcx ty::Const<'tcx>,
err: TypeError<'tcx>,
) -> DiagnosticBuilder<'tcx> {
let trace = TypeTrace::consts(cause, true, expected, actual);
self.report_and_explain_type_error(trace, &err)
}
pub fn replace_bound_vars_with_fresh_vars<T>(
2014-11-12 14:11:22 -05:00
&self,
span: Span,
lbrct: LateBoundRegionConversionTime,
2020-10-24 02:21:18 +02:00
value: ty::Binder<T>,
2018-08-29 13:25:47 -04:00
) -> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
where
2019-12-22 17:42:04 -05:00
T: TypeFoldable<'tcx>,
{
let fld_r =
|br: ty::BoundRegion| self.next_region_var(LateBoundRegion(span, br.kind, lbrct));
let fld_t = |_| {
self.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::MiscVariable,
span,
})
};
2019-12-22 17:42:04 -05:00
let fld_c = |_, ty| {
self.next_const_var(
ty,
ConstVariableOrigin { kind: ConstVariableOriginKind::MiscVariable, span },
)
};
self.tcx.replace_bound_vars(value, fld_r, fld_t, fld_c)
2012-11-29 16:41:39 -08:00
}
2020-05-01 22:28:15 +02:00
/// See the [`region_constraints::RegionConstraintCollector::verify_generic_bound`] method.
2018-08-29 13:25:47 -04:00
pub fn verify_generic_bound(
&self,
origin: SubregionOrigin<'tcx>,
kind: GenericKind<'tcx>,
a: ty::Region<'tcx>,
bound: VerifyBound<'tcx>,
) {
debug!("verify_generic_bound({:?}, {:?} <: {:?})", kind, a, bound);
self.inner
.borrow_mut()
.unwrap_region_constraints()
.verify_generic_bound(origin, kind, a, bound);
2018-08-29 13:25:47 -04:00
}
/// Obtains the latest type of the given closure; this may be a
/// closure in the current function, in which case its
/// `ClosureKind` may not yet be known.
pub fn closure_kind(&self, closure_substs: SubstsRef<'tcx>) -> Option<ty::ClosureKind> {
let closure_kind_ty = closure_substs.as_closure().kind_ty();
let closure_kind_ty = self.shallow_resolve(closure_kind_ty);
closure_kind_ty.to_opt_closure_kind()
}
/// Clears the selection, evaluation, and projection caches. This is useful when
2019-02-08 14:53:55 +01:00
/// repeatedly attempting to select an `Obligation` while changing only
/// its `ParamEnv`, since `FulfillmentContext` doesn't use probing.
Generate documentation for auto-trait impls A new section is added to both both struct and trait doc pages. On struct/enum pages, a new 'Auto Trait Implementations' section displays any synthetic implementations for auto traits. Currently, this is only done for Send and Sync. On trait pages, a new 'Auto Implementors' section displays all types which automatically implement the trait. Effectively, this is a list of all public types in the standard library. Synthesized impls for a particular auto trait ('synthetic impls') take into account generic bounds. For example, a type 'struct Foo<T>(T)' will have 'impl<T> Send for Foo<T> where T: Send' generated for it. Manual implementations of auto traits are also taken into account. If we have the following types: 'struct Foo<T>(T)' 'struct Wrapper<T>(Foo<T>)' 'unsafe impl<T> Send for Wrapper<T>' // pretend that Wrapper<T> makes this sound somehow Then Wrapper will have the following impl generated: 'impl<T> Send for Wrapper<T>' reflecting the fact that 'T: Send' need not hold for 'Wrapper<T>: Send' to hold Lifetimes, HRTBS, and projections (e.g. '<T as Iterator>::Item') are taken into account by synthetic impls However, if a type can *never* implement a particular auto trait (e.g. 'struct MyStruct<T>(*const T)'), then a negative impl will be generated (in this case, 'impl<T> !Send for MyStruct<T>') All of this means that a user should be able to copy-paste a synthetic impl into their code, without any observable changes in behavior (assuming the rest of the program remains unchanged).
2017-11-22 16:16:55 -05:00
pub fn clear_caches(&self) {
self.selection_cache.clear();
self.evaluation_cache.clear();
self.inner.borrow_mut().projection_cache().clear();
Generate documentation for auto-trait impls A new section is added to both both struct and trait doc pages. On struct/enum pages, a new 'Auto Trait Implementations' section displays any synthetic implementations for auto traits. Currently, this is only done for Send and Sync. On trait pages, a new 'Auto Implementors' section displays all types which automatically implement the trait. Effectively, this is a list of all public types in the standard library. Synthesized impls for a particular auto trait ('synthetic impls') take into account generic bounds. For example, a type 'struct Foo<T>(T)' will have 'impl<T> Send for Foo<T> where T: Send' generated for it. Manual implementations of auto traits are also taken into account. If we have the following types: 'struct Foo<T>(T)' 'struct Wrapper<T>(Foo<T>)' 'unsafe impl<T> Send for Wrapper<T>' // pretend that Wrapper<T> makes this sound somehow Then Wrapper will have the following impl generated: 'impl<T> Send for Wrapper<T>' reflecting the fact that 'T: Send' need not hold for 'Wrapper<T>: Send' to hold Lifetimes, HRTBS, and projections (e.g. '<T as Iterator>::Item') are taken into account by synthetic impls However, if a type can *never* implement a particular auto trait (e.g. 'struct MyStruct<T>(*const T)'), then a negative impl will be generated (in this case, 'impl<T> !Send for MyStruct<T>') All of this means that a user should be able to copy-paste a synthetic impl into their code, without any observable changes in behavior (assuming the rest of the program remains unchanged).
2017-11-22 16:16:55 -05:00
}
fn universe(&self) -> ty::UniverseIndex {
self.universe.get()
}
2018-07-23 17:30:59 +03:00
2019-02-08 14:53:55 +01:00
/// Creates and return a fresh universe that extends all previous
/// universes. Updates `self.universe` to that new universe.
pub fn create_next_universe(&self) -> ty::UniverseIndex {
let u = self.universe.get().next_universe();
2018-07-23 17:30:59 +03:00
self.universe.set(u);
u
}
/// Resolves and evaluates a constant.
///
/// The constant can be located on a trait like `<A as B>::C`, in which case the given
/// substitutions and environment are used to resolve the constant. Alternatively if the
/// constant has generic parameters in scope the substitutions are used to evaluate the value of
/// the constant. For example in `fn foo<T>() { let _ = [0; bar::<T>()]; }` the repeat count
/// constant `bar::<T>()` requires a substitution for `T`, if the substitution for `T` is still
/// too generic for the constant to be evaluated then `Err(ErrorHandled::TooGeneric)` is
/// returned.
///
/// This handles inferences variables within both `param_env` and `substs` by
/// performing the operation on their respective canonical forms.
pub fn const_eval_resolve(
&self,
param_env: ty::ParamEnv<'tcx>,
2021-03-13 16:31:38 +01:00
ty::Unevaluated { def, substs, promoted }: ty::Unevaluated<'tcx>,
span: Option<Span>,
) -> EvalToConstValueResult<'tcx> {
let mut original_values = OriginalQueryValues::default();
2020-10-24 02:21:18 +02:00
let canonical = self.canonicalize_query((param_env, substs), &mut original_values);
let (param_env, substs) = canonical.value;
// The return value is the evaluated value which doesn't contain any reference to inference
// variables, thus we don't need to substitute back the original values.
2021-03-13 16:31:38 +01:00
self.tcx.const_eval_resolve(param_env, ty::Unevaluated { def, substs, promoted }, span)
}
2019-10-02 14:35:01 -04:00
/// If `typ` is a type variable of some kind, resolve it one level
/// (but do not resolve types found in the result). If `typ` is
/// not a type variable, just return it unmodified.
// FIXME(eddyb) inline into `ShallowResolver::visit_ty`.
fn shallow_resolve_ty(&self, typ: Ty<'tcx>) -> Ty<'tcx> {
2020-08-03 00:49:11 +02:00
match *typ.kind() {
ty::Infer(ty::TyVar(v)) => {
// Not entirely obvious: if `typ` is a type variable,
// it can be resolved to an int/float variable, which
// can then be recursively resolved, hence the
// recursion. Note though that we prevent type
// variables from unifying to other type variables
// directly (though they may be embedded
// structurally), and we prevent cycles in any case,
// so this recursion should always be of very limited
// depth.
//
// Note: if these two lines are combined into one we get
// dynamic borrow errors on `self.inner`.
let known = self.inner.borrow_mut().type_variables().probe(v).known();
known.map_or(typ, |t| self.shallow_resolve_ty(t))
}
2019-12-22 17:42:04 -05:00
ty::Infer(ty::IntVar(v)) => self
.inner
.borrow_mut()
.int_unification_table()
.probe_value(v)
.map(|v| v.to_type(self.tcx))
.unwrap_or(typ),
2019-12-22 17:42:04 -05:00
ty::Infer(ty::FloatVar(v)) => self
.inner
.borrow_mut()
.float_unification_table()
.probe_value(v)
.map(|v| v.to_type(self.tcx))
.unwrap_or(typ),
_ => typ,
}
}
/// `ty_or_const_infer_var_changed` is equivalent to one of these two:
/// * `shallow_resolve(ty) != ty` (where `ty.kind = ty::Infer(_)`)
/// * `shallow_resolve(ct) != ct` (where `ct.kind = ty::ConstKind::Infer(_)`)
///
/// However, `ty_or_const_infer_var_changed` is more efficient. It's always
/// inlined, despite being large, because it has only two call sites that
/// are extremely hot (both in `traits::fulfill`'s checking of `stalled_on`
/// inference variables), and it handles both `Ty` and `ty::Const` without
/// having to resort to storing full `GenericArg`s in `stalled_on`.
#[inline(always)]
pub fn ty_or_const_infer_var_changed(&self, infer_var: TyOrConstInferVar<'tcx>) -> bool {
match infer_var {
TyOrConstInferVar::Ty(v) => {
use self::type_variable::TypeVariableValue;
// If `inlined_probe` returns a `Known` value, it never equals
// `ty::Infer(ty::TyVar(v))`.
match self.inner.borrow_mut().type_variables().inlined_probe(v) {
TypeVariableValue::Unknown { .. } => false,
TypeVariableValue::Known { .. } => true,
}
}
TyOrConstInferVar::TyInt(v) => {
// If `inlined_probe_value` returns a value it's always a
2020-03-06 12:13:55 +01:00
// `ty::Int(_)` or `ty::UInt(_)`, which never matches a
// `ty::Infer(_)`.
self.inner.borrow_mut().int_unification_table().inlined_probe_value(v).is_some()
}
TyOrConstInferVar::TyFloat(v) => {
// If `probe_value` returns a value it's always a
2020-03-06 12:13:55 +01:00
// `ty::Float(_)`, which never matches a `ty::Infer(_)`.
//
// Not `inlined_probe_value(v)` because this call site is colder.
self.inner.borrow_mut().float_unification_table().probe_value(v).is_some()
}
TyOrConstInferVar::Const(v) => {
// If `probe_value` returns a `Known` value, it never equals
// `ty::ConstKind::Infer(ty::InferConst::Var(v))`.
//
// Not `inlined_probe_value(v)` because this call site is colder.
match self.inner.borrow_mut().const_unification_table().probe_value(v).val {
ConstVariableValue::Unknown { .. } => false,
ConstVariableValue::Known { .. } => true,
}
}
}
}
}
/// Helper for `ty_or_const_infer_var_changed` (see comment on that), currently
/// used only for `traits::fulfill`'s list of `stalled_on` inference variables.
#[derive(Copy, Clone, Debug)]
pub enum TyOrConstInferVar<'tcx> {
/// Equivalent to `ty::Infer(ty::TyVar(_))`.
Ty(TyVid),
/// Equivalent to `ty::Infer(ty::IntVar(_))`.
TyInt(IntVid),
/// Equivalent to `ty::Infer(ty::FloatVar(_))`.
TyFloat(FloatVid),
/// Equivalent to `ty::ConstKind::Infer(ty::InferConst::Var(_))`.
Const(ConstVid<'tcx>),
}
impl TyOrConstInferVar<'tcx> {
/// Tries to extract an inference variable from a type or a constant, returns `None`
/// for types other than `ty::Infer(_)` (or `InferTy::Fresh*`) and
/// for constants other than `ty::ConstKind::Infer(_)` (or `InferConst::Fresh`).
pub fn maybe_from_generic_arg(arg: GenericArg<'tcx>) -> Option<Self> {
match arg.unpack() {
GenericArgKind::Type(ty) => Self::maybe_from_ty(ty),
GenericArgKind::Const(ct) => Self::maybe_from_const(ct),
GenericArgKind::Lifetime(_) => None,
}
}
/// Tries to extract an inference variable from a type, returns `None`
/// for types other than `ty::Infer(_)` (or `InferTy::Fresh*`).
pub fn maybe_from_ty(ty: Ty<'tcx>) -> Option<Self> {
2020-08-03 00:49:11 +02:00
match *ty.kind() {
ty::Infer(ty::TyVar(v)) => Some(TyOrConstInferVar::Ty(v)),
ty::Infer(ty::IntVar(v)) => Some(TyOrConstInferVar::TyInt(v)),
ty::Infer(ty::FloatVar(v)) => Some(TyOrConstInferVar::TyFloat(v)),
_ => None,
}
}
/// Tries to extract an inference variable from a constant, returns `None`
/// for constants other than `ty::ConstKind::Infer(_)` (or `InferConst::Fresh`).
pub fn maybe_from_const(ct: &'tcx ty::Const<'tcx>) -> Option<Self> {
match ct.val {
ty::ConstKind::Infer(InferConst::Var(v)) => Some(TyOrConstInferVar::Const(v)),
_ => None,
}
}
}
struct ShallowResolver<'a, 'tcx> {
infcx: &'a InferCtxt<'a, 'tcx>,
}
2019-06-14 00:48:52 +03:00
impl<'a, 'tcx> TypeFolder<'tcx> for ShallowResolver<'a, 'tcx> {
fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
self.infcx.tcx
}
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
self.infcx.shallow_resolve_ty(ty)
}
fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
if let ty::Const { val: ty::ConstKind::Infer(InferConst::Var(vid)), .. } = ct {
2019-12-22 17:42:04 -05:00
self.infcx
.inner
2019-12-22 17:42:04 -05:00
.borrow_mut()
.const_unification_table()
2019-12-22 17:42:04 -05:00
.probe_value(*vid)
.val
.known()
.unwrap_or(ct)
} else {
ct
}
}
}
2019-06-14 00:48:52 +03:00
impl<'tcx> TypeTrace<'tcx> {
pub fn span(&self) -> Span {
self.cause.span
}
2018-08-29 13:25:47 -04:00
pub fn types(
cause: &ObligationCause<'tcx>,
a_is_expected: bool,
a: Ty<'tcx>,
b: Ty<'tcx>,
) -> TypeTrace<'tcx> {
2019-12-22 17:42:04 -05:00
TypeTrace { cause: cause.clone(), values: Types(ExpectedFound::new(a_is_expected, a, b)) }
}
pub fn consts(
cause: &ObligationCause<'tcx>,
a_is_expected: bool,
a: &'tcx ty::Const<'tcx>,
b: &'tcx ty::Const<'tcx>,
) -> TypeTrace<'tcx> {
TypeTrace { cause: cause.clone(), values: Consts(ExpectedFound::new(a_is_expected, a, b)) }
}
2019-06-14 00:48:52 +03:00
pub fn dummy(tcx: TyCtxt<'tcx>) -> TypeTrace<'tcx> {
let err = tcx.ty_error();
TypeTrace {
cause: ObligationCause::dummy(),
values: Types(ExpectedFound { expected: err, found: err }),
}
}
}
impl<'tcx> SubregionOrigin<'tcx> {
pub fn span(&self) -> Span {
match *self {
2014-04-22 02:21:52 +03:00
Subtype(ref a) => a.span(),
RelateObjectBound(a) => a,
RelateParamBound(a, _) => a,
RelateRegionParamBound(a) => a,
Reborrow(a) => a,
ReborrowUpvar(a, _) => a,
DataBorrowed(_, a) => a,
ReferenceOutlivesReferent(_, a) => a,
CallReturn(a) => a,
CompareImplMethodObligation { span, .. } => span,
}
}
2018-08-29 13:25:47 -04:00
pub fn from_obligation_cause<F>(cause: &traits::ObligationCause<'tcx>, default: F) -> Self
where
F: FnOnce() -> Self,
{
match cause.code {
2018-08-29 13:25:47 -04:00
traits::ObligationCauseCode::ReferenceOutlivesReferent(ref_type) => {
SubregionOrigin::ReferenceOutlivesReferent(ref_type, cause.span)
}
traits::ObligationCauseCode::CompareImplMethodObligation {
item_name,
impl_item_def_id,
trait_item_def_id,
} => SubregionOrigin::CompareImplMethodObligation {
span: cause.span,
item_name,
impl_item_def_id,
trait_item_def_id,
},
_ => default(),
}
}
}
impl RegionVariableOrigin {
pub fn span(&self) -> Span {
match *self {
MiscVariable(a)
| PatternRegion(a)
| AddrOfRegion(a)
| Autoref(a, _)
| Coercion(a)
| EarlyBoundRegion(a, ..)
| LateBoundRegion(a, ..)
| UpvarRegion(_, a) => a,
BoundRegionInCoherence(_) => rustc_span::DUMMY_SP,
Nll(..) => bug!("NLL variable used with `span`"),
}
}
}
2016-07-16 19:38:17 +03:00
impl<'tcx> fmt::Debug for RegionObligation<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2018-08-29 13:25:47 -04:00
write!(
f,
"RegionObligation(sub_region={:?}, sup_type={:?})",
self.sub_region, self.sup_type
)
}
}