2023-03-15 14:00:15 +01:00
|
|
|
pub use self::at::DefineOpaqueTypes;
|
2018-08-29 13:25:47 -04:00
|
|
|
pub use self::freshen::TypeFreshener;
|
2021-03-03 12:14:35 +00:00
|
|
|
pub use self::lexical_region_resolve::RegionResolutionError;
|
2014-11-17 15:25:37 -05:00
|
|
|
pub use self::LateBoundRegionConversionTime::*;
|
|
|
|
pub use self::RegionVariableOrigin::*;
|
|
|
|
pub use self::SubregionOrigin::*;
|
2014-11-06 00:05:53 -08:00
|
|
|
pub use self::ValuePairs::*;
|
2023-02-10 14:29:28 +00:00
|
|
|
pub use combine::ObligationEmittingRelation;
|
2013-01-29 16:26:16 -08:00
|
|
|
|
2022-02-14 16:10:22 +00:00
|
|
|
use self::opaque_types::OpaqueTypeStorage;
|
2020-02-25 17:09:01 +01:00
|
|
|
pub(crate) use self::undo_log::{InferCtxtUndoLogs, Snapshot, UndoLog};
|
|
|
|
|
2022-07-06 12:21:17 +02:00
|
|
|
use crate::traits::{self, ObligationCause, PredicateObligations, TraitEngine, TraitEngineExt};
|
2020-01-06 20:13:24 +01:00
|
|
|
|
2022-10-28 23:32:41 +02:00
|
|
|
use rustc_data_structures::fx::FxIndexMap;
|
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;
|
2022-01-23 12:34:26 -06:00
|
|
|
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed};
|
2020-03-29 17:19:48 +02:00
|
|
|
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};
|
2022-02-16 10:56:01 +01:00
|
|
|
use rustc_middle::mir::interpret::{ErrorHandled, EvalToValTreeResult};
|
2022-09-16 17:00:11 -04:00
|
|
|
use rustc_middle::mir::ConstraintCategory;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::traits::select;
|
2020-11-22 06:10:50 -05:00
|
|
|
use rustc_middle::ty::error::{ExpectedFound, TypeError};
|
2022-07-25 19:27:52 +02:00
|
|
|
use rustc_middle::ty::fold::BoundVarReplacerDelegate;
|
2023-02-22 02:18:40 +00:00
|
|
|
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::ty::relate::RelateResult;
|
|
|
|
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, SubstsRef};
|
2023-02-22 02:18:40 +00:00
|
|
|
use rustc_middle::ty::visit::{TypeVisitable, TypeVisitableExt};
|
2020-03-29 16:41:09 +02:00
|
|
|
pub use rustc_middle::ty::IntVarValue;
|
2023-02-03 12:36:44 +11:00
|
|
|
use rustc_middle::ty::{self, GenericParamDefKind, InferConst, InferTy, Ty, TyCtxt};
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::ty::{ConstVid, FloatVid, IntVid, TyVid};
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::symbol::Symbol;
|
2022-11-18 11:30:21 +00:00
|
|
|
use rustc_span::Span;
|
2020-03-11 12:49:08 +01:00
|
|
|
|
2022-09-09 13:01:06 -05:00
|
|
|
use std::cell::{Cell, RefCell};
|
2019-02-09 11:24:02 +09:00
|
|
|
use std::fmt;
|
|
|
|
|
2015-03-22 15:11:56 -04:00
|
|
|
use self::combine::CombineFields;
|
2022-09-09 15:08:06 -05:00
|
|
|
use self::error_reporting::TypeErrCtxt;
|
2020-04-22 15:48:30 -07:00
|
|
|
use self::free_regions::RegionRelations;
|
2017-11-05 05:48:16 -05:00
|
|
|
use self::lexical_region_resolve::LexicalRegionResolutions;
|
2017-12-01 05:07:52 -05:00
|
|
|
use self::outlives::env::OutlivesEnvironment;
|
2018-08-29 13:25:47 -04:00
|
|
|
use self::region_constraints::{GenericKind, RegionConstraintData, VarInfos, VerifyBound};
|
2020-04-28 21:05:12 +02:00
|
|
|
use self::region_constraints::{
|
|
|
|
RegionConstraintCollector, RegionConstraintStorage, RegionSnapshot,
|
|
|
|
};
|
2020-11-23 09:06:37 -05:00
|
|
|
use self::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
|
2014-10-09 17:19:50 -04:00
|
|
|
|
2017-05-24 09:43:20 -04:00
|
|
|
pub mod at;
|
2018-02-09 10:39:36 -05:00
|
|
|
pub mod canonical;
|
2016-07-25 13:08:48 -07:00
|
|
|
mod combine;
|
|
|
|
mod equate;
|
2014-07-22 07:46:36 -04:00
|
|
|
pub mod error_reporting;
|
2020-04-22 15:48:30 -07:00
|
|
|
pub mod free_regions;
|
2018-08-29 13:25:47 -04:00
|
|
|
mod freshen;
|
2016-11-08 16:18:44 -05:00
|
|
|
mod fudge;
|
2016-07-25 13:08:48 -07:00
|
|
|
mod glb;
|
2014-12-12 14:55:07 -05:00
|
|
|
mod higher_ranked;
|
2013-01-29 16:26:16 -08:00
|
|
|
pub mod lattice;
|
2017-11-15 16:58:14 -05:00
|
|
|
mod lexical_region_resolve;
|
2018-08-29 13:25:47 -04:00
|
|
|
mod lub;
|
2018-10-15 16:21:37 -04:00
|
|
|
pub mod nll_relate;
|
2021-07-26 16:57:18 +00:00
|
|
|
pub mod opaque_types;
|
2017-11-10 14:59:17 -05:00
|
|
|
pub mod outlives;
|
2021-08-10 11:12:11 +00:00
|
|
|
mod projection;
|
2018-08-29 13:25:47 -04:00
|
|
|
pub mod region_constraints;
|
2013-01-29 16:26:16 -08:00
|
|
|
pub mod resolve;
|
2016-07-25 13:08:48 -07:00
|
|
|
mod sub;
|
2014-07-22 07:46:36 -04:00
|
|
|
pub mod type_variable;
|
2020-02-25 17:09:01 +01:00
|
|
|
mod undo_log;
|
2020-01-06 20:13:24 +01:00
|
|
|
|
2016-05-21 08:16:07 -04:00
|
|
|
#[must_use]
|
2018-02-25 10:58:54 -05:00
|
|
|
#[derive(Debug)]
|
2016-03-29 20:06:42 -07:00
|
|
|
pub struct InferOk<'tcx, T> {
|
|
|
|
pub value: T,
|
|
|
|
pub obligations: PredicateObligations<'tcx>,
|
|
|
|
}
|
|
|
|
pub type InferResult<'tcx, T> = Result<InferOk<'tcx, T>, TypeError<'tcx>>;
|
|
|
|
|
2015-03-22 15:11:56 -04:00
|
|
|
pub type UnitResult<'tcx> = RelateResult<'tcx, ()>; // "unify result"
|
2019-03-08 01:18:02 +00:00
|
|
|
pub type FixupResult<'tcx, T> = Result<T, FixupError<'tcx>>; // "fixup result"
|
2013-01-29 16:26:16 -08:00
|
|
|
|
2020-02-25 17:09:01 +01:00
|
|
|
pub(crate) type UnificationTable<'a, 'tcx, T> = ut::UnificationTable<
|
|
|
|
ut::InPlace<T, &'a mut ut::UnificationStorage<T>, &'a mut InferCtxtUndoLogs<'tcx>>,
|
|
|
|
>;
|
|
|
|
|
2020-01-31 08:45:57 +11:00
|
|
|
/// 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`.
|
2022-02-09 19:37:10 -03:00
|
|
|
#[derive(Clone)]
|
2020-01-31 08:45:57 +11:00
|
|
|
pub struct InferCtxtInner<'tcx> {
|
2023-03-04 07:01:08 +02:00
|
|
|
/// Cache for projections.
|
2020-01-31 08:45:57 +11:00
|
|
|
///
|
2023-03-04 07:01:08 +02:00
|
|
|
/// This cache is snapshotted along with the infcx.
|
|
|
|
projection_cache: traits::ProjectionCacheStorage<'tcx>,
|
2020-01-31 08:45:57 +11:00
|
|
|
|
|
|
|
/// 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>,
|
2020-01-31 08:45:57 +11:00
|
|
|
|
|
|
|
/// 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>>,
|
2020-01-31 08:45:57 +11:00
|
|
|
|
|
|
|
/// 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>,
|
2020-01-31 08:45:57 +11:00
|
|
|
|
|
|
|
/// 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>,
|
2020-01-31 08:45:57 +11:00
|
|
|
|
|
|
|
/// Tracks the set of region variables and the constraints between them.
|
2023-03-04 07:38:37 +02:00
|
|
|
///
|
2020-01-31 08:45:57 +11:00
|
|
|
/// 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>>,
|
2020-01-31 08:45:57 +11:00
|
|
|
|
2023-03-04 07:38:37 +02:00
|
|
|
/// A set of constraints that regionck must validate.
|
|
|
|
///
|
|
|
|
/// Each constraint has the form `T:'a`, meaning "some type `T` must
|
2020-01-31 08:45:57 +11:00
|
|
|
/// outlive the lifetime 'a". These constraints derive from
|
|
|
|
/// instantiated type parameters. So if you had a struct defined
|
2023-03-04 07:38:37 +02:00
|
|
|
/// like the following:
|
2022-04-15 15:04:34 -07:00
|
|
|
/// ```ignore (illustrative)
|
2023-03-04 07:38:37 +02:00
|
|
|
/// struct Foo<T: 'static> { ... }
|
2022-04-15 15:04:34 -07:00
|
|
|
/// ```
|
2023-03-04 07:38:37 +02:00
|
|
|
/// In some expression `let x = Foo { ... }`, it will
|
2020-01-31 08:45:57 +11:00
|
|
|
/// instantiate the type parameter `T` with a fresh type `$0`. At
|
|
|
|
/// the same time, it will record a region obligation of
|
2023-03-04 07:38:37 +02:00
|
|
|
/// `$0: 'static`. This will get checked later by regionck. (We
|
2020-01-31 08:45:57 +11:00
|
|
|
/// 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
|
2022-03-25 14:49:14 +01:00
|
|
|
/// [`InferCtxt::process_registered_region_obligations`]
|
2020-01-31 08:45:57 +11:00
|
|
|
/// 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.
|
2022-06-29 18:14:11 +02:00
|
|
|
region_obligations: Vec<RegionObligation<'tcx>>,
|
2020-02-25 16:41:29 +01:00
|
|
|
|
2022-01-27 15:06:33 +00:00
|
|
|
undo_log: InferCtxtUndoLogs<'tcx>,
|
|
|
|
|
2022-02-14 16:10:22 +00:00
|
|
|
/// Caches for opaque type inference.
|
2023-02-28 11:49:05 +00:00
|
|
|
opaque_type_storage: OpaqueTypeStorage<'tcx>,
|
2020-01-31 08:45:57 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
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(),
|
2020-02-25 16:41:29 +01:00
|
|
|
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()),
|
2020-01-31 08:45:57 +11:00
|
|
|
region_obligations: vec![],
|
2022-02-14 16:10:22 +00:00
|
|
|
opaque_type_storage: Default::default(),
|
2020-01-31 08:45:57 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-24 15:22:23 +02:00
|
|
|
#[inline]
|
2022-06-29 18:14:11 +02:00
|
|
|
pub fn region_obligations(&self) -> &[RegionObligation<'tcx>] {
|
2020-02-25 14:45:07 +01:00
|
|
|
&self.region_obligations
|
|
|
|
}
|
|
|
|
|
2020-05-24 15:22:23 +02:00
|
|
|
#[inline]
|
2020-04-17 08:54:28 +02:00
|
|
|
pub fn projection_cache(&mut self) -> traits::ProjectionCache<'_, 'tcx> {
|
2020-02-25 13:08:38 +01:00
|
|
|
self.projection_cache.with_log(&mut self.undo_log)
|
|
|
|
}
|
|
|
|
|
2023-03-06 15:07:02 +01:00
|
|
|
#[inline]
|
|
|
|
fn try_type_variables_probe_ref(
|
|
|
|
&self,
|
|
|
|
vid: ty::TyVid,
|
|
|
|
) -> Option<&type_variable::TypeVariableValue<'tcx>> {
|
|
|
|
// Uses a read-only view of the unification table, this way we don't
|
|
|
|
// need an undo log.
|
|
|
|
self.type_variable_storage.eq_relations_ref().try_probe_value(vid)
|
|
|
|
}
|
|
|
|
|
2020-05-24 15:22:23 +02:00
|
|
|
#[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)
|
2020-02-24 10:40:36 +01:00
|
|
|
}
|
|
|
|
|
2022-02-14 16:10:22 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn opaque_types(&mut self) -> opaque_types::OpaqueTypeTable<'_, 'tcx> {
|
|
|
|
self.opaque_type_storage.with_log(&mut self.undo_log)
|
|
|
|
}
|
|
|
|
|
2020-05-24 15:22:23 +02:00
|
|
|
#[inline]
|
2023-02-28 11:49:05 +00:00
|
|
|
fn int_unification_table(&mut self) -> UnificationTable<'_, 'tcx, ty::IntVid> {
|
2020-04-17 08:54:28 +02:00
|
|
|
self.int_unification_storage.with_log(&mut self.undo_log)
|
2020-02-24 10:40:36 +01:00
|
|
|
}
|
|
|
|
|
2020-05-24 15:22:23 +02:00
|
|
|
#[inline]
|
2023-02-28 11:49:05 +00:00
|
|
|
fn float_unification_table(&mut self) -> UnificationTable<'_, 'tcx, ty::FloatVid> {
|
2020-04-17 08:54:28 +02:00
|
|
|
self.float_unification_storage.with_log(&mut self.undo_log)
|
2020-02-24 10:40:36 +01:00
|
|
|
}
|
|
|
|
|
2020-05-24 15:22:23 +02:00
|
|
|
#[inline]
|
2023-02-28 11:49:05 +00:00
|
|
|
fn const_unification_table(&mut self) -> UnificationTable<'_, 'tcx, ty::ConstVid<'tcx>> {
|
2020-04-17 08:54:28 +02:00
|
|
|
self.const_unification_storage.with_log(&mut self.undo_log)
|
2020-02-24 10:40:36 +01:00
|
|
|
}
|
|
|
|
|
2020-05-24 15:22:23 +02:00
|
|
|
#[inline]
|
2020-04-28 21:05:12 +02:00
|
|
|
pub fn unwrap_region_constraints(&mut self) -> RegionConstraintCollector<'_, 'tcx> {
|
2020-04-17 09:15:28 +02:00
|
|
|
self.region_constraint_storage
|
2020-02-25 09:47:07 +01:00
|
|
|
.as_mut()
|
|
|
|
.expect("region constraints already solved")
|
|
|
|
.with_log(&mut self.undo_log)
|
2020-01-31 08:45:57 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-02 16:37:49 +03:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
|
|
pub enum DefiningAnchor {
|
|
|
|
/// `DefId` of the item.
|
|
|
|
Bind(LocalDefId),
|
|
|
|
/// When opaque types are not resolved, we `Bubble` up, meaning
|
|
|
|
/// return the opaque/hidden type pair from query, for caller of query to handle it.
|
|
|
|
Bubble,
|
|
|
|
/// Used to catch type mismatch errors when handling opaque types.
|
|
|
|
Error,
|
|
|
|
}
|
|
|
|
|
2022-09-09 13:01:06 -05:00
|
|
|
pub struct InferCtxt<'tcx> {
|
2019-06-14 00:48:52 +03:00
|
|
|
pub tcx: TyCtxt<'tcx>,
|
2012-11-29 16:41:39 -08:00
|
|
|
|
2021-07-29 15:59:59 +00:00
|
|
|
/// The `DefId` of the item in whose context we are performing inference or typeck.
|
|
|
|
/// It is used to check whether an opaque type use is a defining use.
|
2021-12-08 17:44:50 +00:00
|
|
|
///
|
2022-07-02 16:37:49 +03:00
|
|
|
/// If it is `DefiningAnchor::Bubble`, we can't resolve opaque types here and need to bubble up
|
2021-12-08 17:44:50 +00:00
|
|
|
/// the obligation. This frequently happens for
|
|
|
|
/// short lived InferCtxt within queries. The opaque type obligations are forwarded
|
|
|
|
/// to the outside until the end up in an `InferCtxt` for typeck or borrowck.
|
2022-07-02 16:37:49 +03:00
|
|
|
///
|
2023-02-10 16:42:53 +00:00
|
|
|
/// Its default value is `DefiningAnchor::Error`, this way it is easier to catch errors that
|
2022-07-02 16:37:49 +03:00
|
|
|
/// might come up during inference or typeck.
|
|
|
|
pub defining_use_anchor: DefiningAnchor,
|
2021-07-29 15:59:59 +00:00
|
|
|
|
2022-07-20 11:40:15 +02:00
|
|
|
/// Whether this inference context should care about region obligations in
|
|
|
|
/// the root universe. Most notably, this is used during hir typeck as region
|
|
|
|
/// solving is left to borrowck instead.
|
|
|
|
pub considering_regions: bool,
|
|
|
|
|
2020-01-31 08:45:57 +11:00
|
|
|
pub inner: RefCell<InferCtxtInner<'tcx>>,
|
2012-11-29 16:41:39 -08:00
|
|
|
|
2020-01-18 07:59:16 -05: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.
|
2017-11-05 05:48:16 -05:00
|
|
|
lexical_region_resolutions: RefCell<Option<LexicalRegionResolutions<'tcx>>>,
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
/// 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>,
|
2016-04-29 06:00:23 +03:00
|
|
|
|
|
|
|
/// Caches the results of trait evaluation.
|
2020-02-22 11:44:18 +01:00
|
|
|
pub evaluation_cache: select::EvaluationCache<'tcx>,
|
2016-04-29 06:00:23 +03:00
|
|
|
|
2023-03-04 07:38:37 +02:00
|
|
|
/// The set of predicates on which errors have been reported, to
|
2019-04-22 21:29:11 +03:00
|
|
|
/// avoid reporting the same error twice.
|
2022-10-28 23:32:41 +02:00
|
|
|
pub reported_trait_errors: RefCell<FxIndexMap<Span, Vec<ty::Predicate<'tcx>>>>,
|
2015-09-24 19:58:00 +03:00
|
|
|
|
2019-09-30 17:48:22 -07:00
|
|
|
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()`.
|
2022-08-10 22:21:34 +08:00
|
|
|
tainted_by_errors: Cell<Option<ErrorGuaranteed>>,
|
2016-03-15 04:49:10 -04:00
|
|
|
|
2019-04-22 21:29:11 +03:00
|
|
|
/// Track how many errors were reported when this infcx is created.
|
2023-03-04 07:38:37 +02:00
|
|
|
/// If the number of errors increases, that's also a sign (like
|
2022-03-30 15:14:15 -04:00
|
|
|
/// `tainted_by_errors`) to avoid reporting certain kinds of errors.
|
2022-08-10 22:21:34 +08:00
|
|
|
// FIXME(matthewjasper) Merge into `tainted_by_errors`
|
2015-06-25 13:08:10 -07:00
|
|
|
err_count_on_creation: usize,
|
2016-05-25 21:12:35 +03:00
|
|
|
|
2019-04-22 21:29:11 +03:00
|
|
|
/// This flag is true while there is an active snapshot.
|
2017-04-15 23:51:58 +03:00
|
|
|
in_snapshot: Cell<bool>,
|
2017-11-01 13:41:07 -04:00
|
|
|
|
2018-02-07 10:16:44 -07:00
|
|
|
/// What is the innermost universe we have created? Starts out as
|
|
|
|
/// `UniverseIndex::root()` but grows from there as we enter
|
|
|
|
/// universal quantifiers.
|
|
|
|
///
|
2018-11-27 02:59:49 +00:00
|
|
|
/// N.B., at present, we exclude the universal quantifiers on the
|
2018-02-07 10:16:44 -07:00
|
|
|
/// 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.
|
2018-02-08 13:14:24 -07:00
|
|
|
universe: Cell<ty::UniverseIndex>,
|
2022-08-13 02:15:46 +00:00
|
|
|
|
2022-10-27 12:00:21 +00:00
|
|
|
/// During coherence we have to assume that other crates may add
|
|
|
|
/// additional impls which we currently don't know about.
|
|
|
|
///
|
2023-03-04 07:38:37 +02:00
|
|
|
/// To deal with this evaluation, we should be conservative
|
2022-10-27 12:00:21 +00:00
|
|
|
/// and consider the possibility of impls from outside this crate.
|
|
|
|
/// This comes up primarily when resolving ambiguity. Imagine
|
|
|
|
/// there is some trait reference `$0: Bar` where `$0` is an
|
|
|
|
/// inference variable. If `intercrate` is true, then we can never
|
|
|
|
/// say for sure that this reference is not implemented, even if
|
|
|
|
/// there are *no impls at all for `Bar`*, because `$0` could be
|
|
|
|
/// bound to some type that in a downstream crate that implements
|
|
|
|
/// `Bar`.
|
|
|
|
///
|
2023-03-04 07:38:37 +02:00
|
|
|
/// Outside of coherence, we set this to false because we are only
|
2022-10-27 12:00:21 +00:00
|
|
|
/// interested in types that the user could actually have written.
|
|
|
|
/// In other words, we consider `$0: Bar` to be unimplemented if
|
|
|
|
/// there is no type that the user could *actually name* that
|
|
|
|
/// would satisfy it. This avoids crippling inference, basically.
|
|
|
|
pub intercrate: bool,
|
2013-01-08 14:00:45 -08:00
|
|
|
}
|
2012-11-29 16:41:39 -08:00
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// See the `error_reporting` module for more details.
|
2022-06-17 10:53:29 +01:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, TypeFoldable, TypeVisitable)]
|
2014-09-29 22:11:30 +03:00
|
|
|
pub enum ValuePairs<'tcx> {
|
2018-02-09 10:43:15 -05:00
|
|
|
Regions(ExpectedFound<ty::Region<'tcx>>),
|
2022-02-07 04:57:45 +00:00
|
|
|
Terms(ExpectedFound<ty::Term<'tcx>>),
|
2023-03-14 23:12:42 +00:00
|
|
|
Aliases(ExpectedFound<ty::AliasTy<'tcx>>),
|
2015-09-06 21:51:58 +03:00
|
|
|
TraitRefs(ExpectedFound<ty::TraitRef<'tcx>>),
|
|
|
|
PolyTraitRefs(ExpectedFound<ty::PolyTraitRef<'tcx>>),
|
2022-12-24 23:17:25 +00:00
|
|
|
Sigs(ExpectedFound<ty::FnSig<'tcx>>),
|
2013-05-23 21:37:37 -04:00
|
|
|
}
|
|
|
|
|
2022-02-07 16:40:16 +00:00
|
|
|
impl<'tcx> ValuePairs<'tcx> {
|
|
|
|
pub fn ty(&self) -> Option<(Ty<'tcx>, Ty<'tcx>)> {
|
2022-09-05 14:03:53 +10:00
|
|
|
if let ValuePairs::Terms(ExpectedFound { expected, found }) = self
|
|
|
|
&& let Some(expected) = expected.ty()
|
|
|
|
&& let Some(found) = found.ty()
|
2022-02-07 16:40:16 +00:00
|
|
|
{
|
2022-09-05 14:03:53 +10:00
|
|
|
Some((expected, found))
|
2022-02-07 16:40:16 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-23 21:37:37 -04:00
|
|
|
/// 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)]
|
2014-09-29 22:11:30 +03:00
|
|
|
pub struct TypeTrace<'tcx> {
|
Implementation for 65853
This attempts to bring better error messages to invalid method calls, by applying some heuristics to identify common mistakes.
The algorithm is inspired by Levenshtein distance and longest common sub-sequence. In essence, we treat the types of the function, and the types of the arguments you provided as two "words" and compute the edits to get from one to the other.
We then modify that algorithm to detect 4 cases:
- A function input is missing
- An extra argument was provided
- The type of an argument is straight up invalid
- Two arguments have been swapped
- A subset of the arguments have been shuffled
(We detect the last two as separate cases so that we can detect two swaps, instead of 4 parameters permuted.)
It helps to understand this argument by paying special attention to terminology: "inputs" refers to the inputs being *expected* by the function, and "arguments" refers to what has been provided at the call site.
The basic sketch of the algorithm is as follows:
- Construct a boolean grid, with a row for each argument, and a column for each input. The cell [i, j] is true if the i'th argument could satisfy the j'th input.
- If we find an argument that could satisfy no inputs, provided for an input that can't be satisfied by any other argument, we consider this an "invalid type".
- Extra arguments are those that can't satisfy any input, provided for an input that *could* be satisfied by another argument.
- Missing inputs are inputs that can't be satisfied by any argument, where the provided argument could satisfy another input
- Swapped / Permuted arguments are identified with a cycle detection algorithm.
As each issue is found, we remove the relevant inputs / arguments and check for more issues. If we find no issues, we match up any "valid" arguments, and start again.
Note that there's a lot of extra complexity:
- We try to stay efficient on the happy path, only computing the diagonal until we find a problem, and then filling in the rest of the matrix.
- Closure arguments are wrapped in a tuple and need to be unwrapped
- We need to resolve closure types after the rest, to allow the most specific type constraints
- We need to handle imported C functions that might be variadic in their inputs.
I tried to document a lot of this in comments in the code and keep the naming clear.
2022-01-21 23:50:54 -05:00
|
|
|
pub cause: ObligationCause<'tcx>,
|
|
|
|
pub values: ValuePairs<'tcx>,
|
2013-05-23 21:37:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The origin of a `r1 <= r2` constraint.
|
|
|
|
///
|
2017-02-27 22:09:13 +03:00
|
|
|
/// See `error_reporting` module for more details
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, Debug)]
|
2014-09-29 22:11:30 +03:00
|
|
|
pub enum SubregionOrigin<'tcx> {
|
2019-04-22 21:29:11 +03:00
|
|
|
/// Arose from a subtyping relation
|
2019-09-12 13:29:32 +10:00
|
|
|
Subtype(Box<TypeTrace<'tcx>>),
|
2013-05-23 21:37:37 -04:00
|
|
|
|
2019-04-22 21:29:11 +03:00
|
|
|
/// When casting `&'a T` to an `&'b Trait` object,
|
2023-03-04 07:38:37 +02:00
|
|
|
/// relating `'a` to `'b`.
|
2013-08-31 18:13:04 +02:00
|
|
|
RelateObjectBound(Span),
|
2013-05-23 21:37:37 -04:00
|
|
|
|
2019-04-22 21:29:11 +03:00
|
|
|
/// Some type parameter was instantiated with the given type,
|
|
|
|
/// and that type must outlive some region.
|
2021-07-30 08:12:10 -07:00
|
|
|
RelateParamBound(Span, Ty<'tcx>, Option<Span>),
|
2014-08-27 21:46:52 -04:00
|
|
|
|
2019-04-22 21:29:11 +03:00
|
|
|
/// The given region parameter was instantiated with a region
|
|
|
|
/// that must outlive some other region.
|
2014-08-27 21:46:52 -04:00
|
|
|
RelateRegionParamBound(Span),
|
|
|
|
|
2023-03-04 07:38:37 +02:00
|
|
|
/// Creating a pointer `b` to contents of another reference.
|
2013-08-31 18:13:04 +02:00
|
|
|
Reborrow(Span),
|
2013-05-23 21:37:37 -04:00
|
|
|
|
2019-04-22 21:29:11 +03:00
|
|
|
/// (&'a &'b T) where a >= b
|
2014-09-29 22:11:30 +03:00
|
|
|
ReferenceOutlivesReferent(Ty<'tcx>, Span),
|
2013-05-23 21:37:37 -04:00
|
|
|
|
2019-04-22 21:29:11 +03:00
|
|
|
/// Comparing the signature and requirements of an impl method against
|
|
|
|
/// the containing trait.
|
2022-09-16 17:20:11 -04:00
|
|
|
CompareImplItemObligation {
|
|
|
|
span: Span,
|
|
|
|
impl_item_def_id: LocalDefId,
|
|
|
|
trait_item_def_id: DefId,
|
|
|
|
},
|
2022-01-08 23:30:19 -05:00
|
|
|
|
2023-03-04 07:38:37 +02:00
|
|
|
/// Checking that the bounds of a trait's associated type hold for a given impl.
|
2022-01-08 23:30:19 -05:00
|
|
|
CheckAssociatedTypeBounds {
|
|
|
|
parent: Box<SubregionOrigin<'tcx>>,
|
2022-04-01 13:38:43 +02:00
|
|
|
impl_item_def_id: LocalDefId,
|
2022-01-08 23:30:19 -05:00
|
|
|
trait_item_def_id: DefId,
|
|
|
|
},
|
2022-09-16 17:20:11 -04:00
|
|
|
|
|
|
|
AscribeUserTypeProvePredicate(Span),
|
2013-05-23 21:37:37 -04:00
|
|
|
}
|
|
|
|
|
2019-09-12 13:29:32 +10:00
|
|
|
// `SubregionOrigin` is used a lot. Make sure it doesn't unintentionally get bigger.
|
2021-03-06 16:02:48 +00:00
|
|
|
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
|
2019-09-12 13:29:32 +10:00
|
|
|
static_assert_size!(SubregionOrigin<'_>, 32);
|
|
|
|
|
2022-09-16 17:00:11 -04:00
|
|
|
impl<'tcx> SubregionOrigin<'tcx> {
|
2022-10-27 16:15:11 +00:00
|
|
|
pub fn to_constraint_category(&self) -> ConstraintCategory<'tcx> {
|
2022-09-16 17:00:11 -04:00
|
|
|
match self {
|
|
|
|
Self::Subtype(type_trace) => type_trace.cause.to_constraint_category(),
|
2022-09-16 17:43:45 -04:00
|
|
|
Self::AscribeUserTypeProvePredicate(span) => ConstraintCategory::Predicate(*span),
|
2022-09-16 17:00:11 -04:00
|
|
|
_ => ConstraintCategory::BoringNoLocation,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-05 22:06:04 -05:00
|
|
|
/// Times when we replace late-bound regions with variables:
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
2014-11-05 22:06:04 -05:00
|
|
|
pub enum LateBoundRegionConversionTime {
|
|
|
|
/// when a fn is called
|
|
|
|
FnCall,
|
|
|
|
|
2014-11-15 16:22:22 -05:00
|
|
|
/// when two higher-ranked types are compared
|
|
|
|
HigherRankedType,
|
2014-12-17 14:16:28 -05:00
|
|
|
|
|
|
|
/// when projecting an associated type
|
2017-08-19 13:41:10 -07:00
|
|
|
AssocTypeProjection(DefId),
|
2014-11-05 22:06:04 -05:00
|
|
|
}
|
|
|
|
|
2023-03-04 07:38:37 +02:00
|
|
|
/// Reasons to create a region inference variable.
|
2013-05-23 21:37:37 -04:00
|
|
|
///
|
2023-03-04 07:38:37 +02:00
|
|
|
/// See `error_reporting` module for more details.
|
2017-11-06 05:21:48 -05:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2015-03-10 07:02:27 -04:00
|
|
|
pub enum RegionVariableOrigin {
|
2023-03-04 07:38:37 +02:00
|
|
|
/// Region variables created for ill-categorized reasons.
|
|
|
|
///
|
|
|
|
/// They mostly indicate places in need of refactoring.
|
2013-08-31 18:13:04 +02:00
|
|
|
MiscVariable(Span),
|
2013-05-23 21:37:37 -04:00
|
|
|
|
2023-03-04 07:38:37 +02:00
|
|
|
/// Regions created by a `&P` or `[...]` pattern.
|
2013-08-31 18:13:04 +02:00
|
|
|
PatternRegion(Span),
|
2013-05-23 21:37:37 -04:00
|
|
|
|
2023-03-04 07:38:37 +02:00
|
|
|
/// Regions created by `&` operator.
|
|
|
|
///
|
2013-08-31 18:13:04 +02:00
|
|
|
AddrOfRegion(Span),
|
2023-03-04 07:38:37 +02:00
|
|
|
/// Regions created as part of an autoref of a method receiver.
|
2021-11-11 23:15:47 +00:00
|
|
|
Autoref(Span),
|
2013-05-23 21:37:37 -04:00
|
|
|
|
2023-03-04 07:38:37 +02:00
|
|
|
/// Regions created as part of an automatic coercion.
|
2015-03-10 07:02:27 -04:00
|
|
|
Coercion(Span),
|
2013-05-23 21:37:37 -04:00
|
|
|
|
2023-03-04 07:38:37 +02:00
|
|
|
/// Region variables created as the values for early-bound regions.
|
2019-10-18 13:22:50 +11:00
|
|
|
EarlyBoundRegion(Span, Symbol),
|
2014-03-07 08:43:39 +01:00
|
|
|
|
2019-04-22 21:29:11 +03:00
|
|
|
/// Region variables created for bound regions
|
2023-03-04 07:38:37 +02:00
|
|
|
/// in a function or method that is called.
|
2020-12-18 13:24:55 -05:00
|
|
|
LateBoundRegion(Span, ty::BoundRegionKind, LateBoundRegionConversionTime),
|
2013-05-23 21:37:37 -04:00
|
|
|
|
2014-02-07 14:43:48 -05:00
|
|
|
UpvarRegion(ty::UpvarId, Span),
|
|
|
|
|
2019-04-22 21:29:11 +03:00
|
|
|
/// This origin is used for the inference variables that we create
|
|
|
|
/// during NLL region processing.
|
2021-01-28 16:18:25 +09:00
|
|
|
Nll(NllRegionVariableOrigin),
|
2017-11-06 05:21:48 -05:00
|
|
|
}
|
|
|
|
|
2019-10-20 15:54:53 +11:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2021-01-28 16:18:25 +09:00
|
|
|
pub enum NllRegionVariableOrigin {
|
2018-09-27 17:57:35 -04:00
|
|
|
/// 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.
|
2017-11-06 05:21:48 -05:00
|
|
|
FreeRegion,
|
|
|
|
|
2018-09-27 17:57:35 -04:00
|
|
|
/// "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
|
|
|
|
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 {
|
2019-10-01 21:39:19 -04:00
|
|
|
/// 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.
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-03-21 09:27:08 +02:00
|
|
|
// FIXME(eddyb) investigate overlap between this and `TyOrConstInferVar`.
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2019-03-08 01:18:02 +00:00
|
|
|
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),
|
2019-03-08 01:18:02 +00:00
|
|
|
UnresolvedConst(ConstVid<'tcx>),
|
2012-11-29 16:41:39 -08:00
|
|
|
}
|
|
|
|
|
2017-11-01 13:41:07 -04:00
|
|
|
/// See the `region_obligations` field for more information.
|
2022-08-29 02:00:08 -04:00
|
|
|
#[derive(Clone, Debug)]
|
2017-11-01 13:41:07 -04:00
|
|
|
pub struct RegionObligation<'tcx> {
|
|
|
|
pub sub_region: ty::Region<'tcx>,
|
|
|
|
pub sup_type: Ty<'tcx>,
|
2018-09-12 17:28:47 -04:00
|
|
|
pub origin: SubregionOrigin<'tcx>,
|
2017-11-01 13:41:07 -04:00
|
|
|
}
|
|
|
|
|
2019-03-08 01:18:02 +00:00
|
|
|
impl<'tcx> fmt::Display for FixupError<'tcx> {
|
2018-08-29 22:02:42 -07:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-03-17 00:15:31 +02:00
|
|
|
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"),
|
2019-03-08 01:18:02 +00:00
|
|
|
UnresolvedConst(_) => write!(f, "unconstrained const value"),
|
2016-03-17 00:15:31 +02:00
|
|
|
}
|
2012-11-29 16:41:39 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-04 07:38:37 +02:00
|
|
|
/// Used to configure inference contexts before their creation.
|
2019-06-14 00:48:52 +03:00
|
|
|
pub struct InferCtxtBuilder<'tcx> {
|
2020-06-28 11:43:10 +02:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2022-07-02 16:37:49 +03:00
|
|
|
defining_use_anchor: DefiningAnchor,
|
2022-07-20 11:40:15 +02:00
|
|
|
considering_regions: bool,
|
2022-11-14 17:35:40 +00:00
|
|
|
/// Whether we are in coherence mode.
|
|
|
|
intercrate: bool,
|
2016-05-11 04:14:41 +03:00
|
|
|
}
|
|
|
|
|
2020-01-06 20:13:24 +01:00
|
|
|
pub trait TyCtxtInferExt<'tcx> {
|
|
|
|
fn infer_ctxt(self) -> InferCtxtBuilder<'tcx>;
|
|
|
|
}
|
|
|
|
|
2021-12-13 16:50:58 -05:00
|
|
|
impl<'tcx> TyCtxtInferExt<'tcx> for TyCtxt<'tcx> {
|
2020-01-06 20:13:24 +01:00
|
|
|
fn infer_ctxt(self) -> InferCtxtBuilder<'tcx> {
|
2022-07-02 16:37:49 +03:00
|
|
|
InferCtxtBuilder {
|
|
|
|
tcx: self,
|
|
|
|
defining_use_anchor: DefiningAnchor::Error,
|
2022-07-20 11:40:15 +02:00
|
|
|
considering_regions: true,
|
2022-10-27 12:00:21 +00:00
|
|
|
intercrate: false,
|
2022-07-02 16:37:49 +03:00
|
|
|
}
|
2016-05-11 04:14:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'tcx> InferCtxtBuilder<'tcx> {
|
2021-07-29 15:59:59 +00:00
|
|
|
/// Whenever the `InferCtxt` should be able to handle defining uses of opaque types,
|
|
|
|
/// you need to call this function. Otherwise the opaque type will be treated opaquely.
|
2021-08-06 10:49:35 +00:00
|
|
|
///
|
|
|
|
/// It is only meant to be called in two places, for typeck
|
2022-09-09 13:01:06 -05:00
|
|
|
/// (via `Inherited::build`) and for the inference context used
|
2021-08-06 10:49:35 +00:00
|
|
|
/// in mir borrowck.
|
2022-07-02 16:37:49 +03:00
|
|
|
pub fn with_opaque_type_inference(mut self, defining_use_anchor: DefiningAnchor) -> Self {
|
|
|
|
self.defining_use_anchor = defining_use_anchor;
|
2017-06-09 10:55:16 +03:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-03-21 16:34:04 +01:00
|
|
|
pub fn intercrate(mut self, intercrate: bool) -> Self {
|
|
|
|
self.intercrate = intercrate;
|
2022-10-27 12:00:21 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-07-20 11:40:15 +02:00
|
|
|
pub fn ignoring_regions(mut self) -> Self {
|
|
|
|
self.considering_regions = false;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-09-24 15:15:25 -04:00
|
|
|
/// 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
|
2018-09-24 15:15:25 -04:00
|
|
|
/// the bound values in `C` to their instantiated values in `V`
|
|
|
|
/// (in other words, `S(C) = V`).
|
|
|
|
pub fn build_with_canonical<T>(
|
2019-06-14 00:48:52 +03:00
|
|
|
&mut self,
|
2018-09-24 15:15:25 -04:00
|
|
|
span: Span,
|
|
|
|
canonical: &Canonical<'tcx, T>,
|
2022-09-09 13:01:06 -05:00
|
|
|
) -> (InferCtxt<'tcx>, T, CanonicalVarValues<'tcx>)
|
2018-08-29 13:25:47 -04:00
|
|
|
where
|
2023-02-22 02:18:40 +00:00
|
|
|
T: TypeFoldable<TyCtxt<'tcx>>,
|
2018-09-24 15:15:25 -04:00
|
|
|
{
|
|
|
|
let infcx = self.build();
|
2018-10-08 06:59:37 -04:00
|
|
|
let (value, subst) = infcx.instantiate_canonical_with_fresh_inference_vars(span, canonical);
|
2018-09-24 15:15:25 -04:00
|
|
|
(infcx, value, subst)
|
|
|
|
}
|
|
|
|
|
2022-09-09 13:01:06 -05:00
|
|
|
pub fn build(&mut self) -> InferCtxt<'tcx> {
|
2022-12-02 19:09:40 +00:00
|
|
|
let InferCtxtBuilder { tcx, defining_use_anchor, considering_regions, intercrate } = *self;
|
2020-06-28 11:43:10 +02:00
|
|
|
InferCtxt {
|
|
|
|
tcx,
|
2021-07-29 15:59:59 +00:00
|
|
|
defining_use_anchor,
|
2022-07-20 11:40:15 +02:00
|
|
|
considering_regions,
|
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(),
|
2022-08-10 22:21:34 +08:00
|
|
|
tainted_by_errors: Cell::new(None),
|
2020-06-28 11:43:10 +02:00
|
|
|
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),
|
2022-10-27 12:00:21 +00:00
|
|
|
intercrate,
|
2018-08-29 13:25:47 -04:00
|
|
|
}
|
2016-03-11 02:31:38 +02:00
|
|
|
}
|
2015-06-27 22:04:15 -07:00
|
|
|
}
|
|
|
|
|
2016-03-17 00:15:31 +02:00
|
|
|
impl<'tcx, T> InferOk<'tcx, T> {
|
2016-04-20 19:51:56 -04:00
|
|
|
pub fn unit(self) -> InferOk<'tcx, ()> {
|
2018-08-29 13:25:47 -04:00
|
|
|
InferOk { value: (), obligations: self.obligations }
|
2013-05-23 21:37:37 -04:00
|
|
|
}
|
2018-06-27 16:04:32 -04:00
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Extracts `value`, registering any obligations into `fulfill_cx`.
|
2018-06-27 16:04:32 -04:00
|
|
|
pub fn into_value_registering_obligations(
|
|
|
|
self,
|
2022-09-09 13:01:06 -05:00
|
|
|
infcx: &InferCtxt<'tcx>,
|
2018-11-28 23:29:01 +01:00
|
|
|
fulfill_cx: &mut dyn TraitEngine<'tcx>,
|
2018-06-27 16:04:32 -04:00
|
|
|
) -> T {
|
|
|
|
let InferOk { value, obligations } = self;
|
2022-07-06 12:21:17 +02:00
|
|
|
fulfill_cx.register_predicate_obligations(infcx, obligations);
|
2018-06-27 16:04:32 -04:00
|
|
|
value
|
|
|
|
}
|
2013-05-23 21:37:37 -04:00
|
|
|
}
|
|
|
|
|
2018-02-09 10:39:36 -05:00
|
|
|
impl<'tcx> InferOk<'tcx, ()> {
|
|
|
|
pub fn into_obligations(self) -> PredicateObligations<'tcx> {
|
|
|
|
self.obligations
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 13:32:58 +01:00
|
|
|
#[must_use = "once you start a snapshot, you should always consume it"]
|
2022-09-09 13:01:06 -05:00
|
|
|
pub struct CombinedSnapshot<'tcx> {
|
2020-02-25 13:32:58 +01:00
|
|
|
undo_snapshot: Snapshot<'tcx>,
|
2020-04-28 21:05:12 +02:00
|
|
|
region_constraints_snapshot: RegionSnapshot,
|
2018-02-08 14:12:32 -07:00
|
|
|
universe: ty::UniverseIndex,
|
2017-04-15 23:51:58 +03:00
|
|
|
was_in_snapshot: bool,
|
2012-11-29 16:41:39 -08:00
|
|
|
}
|
|
|
|
|
2022-09-09 13:01:06 -05:00
|
|
|
impl<'tcx> InferCtxt<'tcx> {
|
2022-09-09 15:08:06 -05:00
|
|
|
/// Creates a `TypeErrCtxt` for emitting various inference errors.
|
2022-11-02 01:34:17 +00:00
|
|
|
/// During typeck, use `FnCtxt::err_ctxt` instead.
|
2022-09-09 13:01:06 -05:00
|
|
|
pub fn err_ctxt(&self) -> TypeErrCtxt<'_, 'tcx> {
|
2022-12-02 19:09:40 +00:00
|
|
|
TypeErrCtxt {
|
|
|
|
infcx: self,
|
|
|
|
typeck_results: None,
|
|
|
|
fallback_has_occurred: false,
|
|
|
|
normalize_fn_sig: Box::new(|fn_sig| fn_sig),
|
2022-12-27 00:39:36 +00:00
|
|
|
autoderef_steps: Box::new(|ty| {
|
|
|
|
debug_assert!(false, "shouldn't be using autoderef_steps outside of typeck");
|
|
|
|
vec![(ty, vec![])]
|
|
|
|
}),
|
2022-12-02 19:09:40 +00:00
|
|
|
}
|
2022-09-09 15:08:06 -05:00
|
|
|
}
|
|
|
|
|
2017-04-15 23:51:58 +03:00
|
|
|
pub fn is_in_snapshot(&self) -> bool {
|
|
|
|
self.in_snapshot.get()
|
|
|
|
}
|
|
|
|
|
2023-02-22 02:18:40 +00:00
|
|
|
pub fn freshen<T: TypeFoldable<TyCtxt<'tcx>>>(&self, t: T) -> T {
|
2021-12-01 00:55:57 +00:00
|
|
|
t.fold_with(&mut self.freshener())
|
2014-10-09 17:19:50 -04:00
|
|
|
}
|
|
|
|
|
2020-11-22 06:10:50 -05:00
|
|
|
/// Returns the origin of the type variable identified by `vid`, or `None`
|
|
|
|
/// if this is not a type variable.
|
|
|
|
///
|
|
|
|
/// No attempt is made to resolve `ty`.
|
2022-09-09 13:01:06 -05:00
|
|
|
pub fn type_var_origin(&self, ty: Ty<'tcx>) -> Option<TypeVariableOrigin> {
|
2020-11-22 06:10:50 -05:00
|
|
|
match *ty.kind() {
|
|
|
|
ty::Infer(ty::TyVar(vid)) => {
|
|
|
|
Some(*self.inner.borrow_mut().type_variables().var_origin(vid))
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
pub fn freshener<'b>(&'b self) -> TypeFreshener<'b, 'tcx> {
|
2021-07-07 10:56:26 -05:00
|
|
|
freshen::TypeFreshener::new(self, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Like `freshener`, but does not replace `'static` regions.
|
|
|
|
pub fn freshener_keep_static<'b>(&'b self) -> TypeFreshener<'b, 'tcx> {
|
|
|
|
freshen::TypeFreshener::new(self, true)
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
|
2017-09-14 21:44:23 -04:00
|
|
|
pub fn unsolved_variables(&self) -> Vec<Ty<'tcx>> {
|
2020-01-31 08:45:57 +11:00
|
|
|
let mut inner = self.inner.borrow_mut();
|
|
|
|
let mut vars: Vec<Ty<'_>> = inner
|
2020-02-24 10:40:36 +01:00
|
|
|
.type_variables()
|
2018-07-16 19:35:45 +02:00
|
|
|
.unsolved_variables()
|
|
|
|
.into_iter()
|
2019-02-20 01:14:18 +00:00
|
|
|
.map(|t| self.tcx.mk_ty_var(t))
|
2020-01-31 08:45:57 +11:00
|
|
|
.collect();
|
|
|
|
vars.extend(
|
2020-02-24 10:40:36 +01:00
|
|
|
(0..inner.int_unification_table().len())
|
2020-01-31 08:45:57 +11:00
|
|
|
.map(|i| ty::IntVid { index: i as u32 })
|
2020-02-24 10:40:36 +01:00
|
|
|
.filter(|&vid| inner.int_unification_table().probe_value(vid).is_none())
|
2020-01-31 08:45:57 +11:00
|
|
|
.map(|v| self.tcx.mk_int_var(v)),
|
|
|
|
);
|
|
|
|
vars.extend(
|
2020-02-24 10:40:36 +01:00
|
|
|
(0..inner.float_unification_table().len())
|
2020-01-31 08:45:57 +11:00
|
|
|
.map(|i| ty::FloatVid { index: i as u32 })
|
2020-02-24 10:40:36 +01:00
|
|
|
.filter(|&vid| inner.float_unification_table().probe_value(vid).is_none())
|
2020-01-31 08:45:57 +11:00
|
|
|
.map(|v| self.tcx.mk_float_var(v)),
|
|
|
|
);
|
|
|
|
vars
|
2015-07-07 15:50:02 -07:00
|
|
|
}
|
|
|
|
|
2022-09-09 13:01:06 -05:00
|
|
|
fn combine_fields<'a>(
|
2018-08-29 13:25:47 -04:00
|
|
|
&'a self,
|
|
|
|
trace: TypeTrace<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2023-03-15 14:00:15 +01:00
|
|
|
define_opaque_types: DefineOpaqueTypes,
|
2019-06-14 00:48:52 +03:00
|
|
|
) -> CombineFields<'a, 'tcx> {
|
2016-03-29 20:06:42 -07:00
|
|
|
CombineFields {
|
|
|
|
infcx: self,
|
2017-07-03 11:19:51 -07:00
|
|
|
trace,
|
2016-03-29 20:06:42 -07:00
|
|
|
cause: None,
|
2017-05-23 04:19:47 -04:00
|
|
|
param_env,
|
2016-03-29 20:06:42 -07:00
|
|
|
obligations: PredicateObligations::new(),
|
2022-02-14 16:10:22 +00:00
|
|
|
define_opaque_types,
|
2016-03-29 20:06:42 -07:00
|
|
|
}
|
2012-11-29 16:41:39 -08:00
|
|
|
}
|
|
|
|
|
2022-09-09 13:01:06 -05:00
|
|
|
fn start_snapshot(&self) -> CombinedSnapshot<'tcx> {
|
2016-05-21 05:41:40 -04:00
|
|
|
debug!("start_snapshot()");
|
|
|
|
|
2020-02-24 16:23:55 +00:00
|
|
|
let in_snapshot = self.in_snapshot.replace(true);
|
2016-05-25 21:12:35 +03:00
|
|
|
|
2020-01-31 08:45:57 +11:00
|
|
|
let mut inner = self.inner.borrow_mut();
|
2020-02-24 10:40:36 +01:00
|
|
|
|
2014-06-20 06:35:06 -04:00
|
|
|
CombinedSnapshot {
|
2020-02-25 15:39:01 +01:00
|
|
|
undo_snapshot: inner.undo_log.start_snapshot(),
|
2020-04-28 21:05:12 +02:00
|
|
|
region_constraints_snapshot: inner.unwrap_region_constraints().start_snapshot(),
|
2018-02-08 14:12:32 -07:00
|
|
|
universe: self.universe(),
|
2017-04-15 23:51:58 +03:00
|
|
|
was_in_snapshot: in_snapshot,
|
2012-11-29 16:41:39 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-20 13:36:04 +00:00
|
|
|
#[instrument(skip(self, snapshot), level = "debug")]
|
2022-09-09 13:01:06 -05:00
|
|
|
fn rollback_to(&self, cause: &str, snapshot: CombinedSnapshot<'tcx>) {
|
2020-04-28 21:05:12 +02:00
|
|
|
let CombinedSnapshot {
|
|
|
|
undo_snapshot,
|
|
|
|
region_constraints_snapshot,
|
|
|
|
universe,
|
|
|
|
was_in_snapshot,
|
|
|
|
} = snapshot;
|
2016-05-25 21:12:35 +03:00
|
|
|
|
2017-04-15 23:51:58 +03:00
|
|
|
self.in_snapshot.set(was_in_snapshot);
|
2018-02-08 14:12:32 -07:00
|
|
|
self.universe.set(universe);
|
2014-06-20 06:35:06 -04:00
|
|
|
|
2020-04-28 21:05:12 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-08-20 13:36:04 +00:00
|
|
|
#[instrument(skip(self, snapshot), level = "debug")]
|
2022-09-09 13:01:06 -05:00
|
|
|
fn commit_from(&self, snapshot: CombinedSnapshot<'tcx>) {
|
2020-04-28 21:05:12 +02:00
|
|
|
let CombinedSnapshot {
|
|
|
|
undo_snapshot,
|
|
|
|
region_constraints_snapshot: _,
|
|
|
|
universe: _,
|
|
|
|
was_in_snapshot,
|
|
|
|
} = snapshot;
|
2016-05-25 21:12:35 +03:00
|
|
|
|
2017-04-15 23:51:58 +03:00
|
|
|
self.in_snapshot.set(was_in_snapshot);
|
2014-06-20 06:35:06 -04:00
|
|
|
|
2020-04-17 08:54:28 +02:00
|
|
|
self.inner.borrow_mut().commit(undo_snapshot);
|
2014-06-20 06:35:06 -04:00
|
|
|
}
|
2012-11-29 16:41:39 -08:00
|
|
|
|
2019-09-30 14:42:11 +10:00
|
|
|
/// Execute `f` and commit the bindings if closure `f` returns `Ok(_)`.
|
2021-08-20 13:36:04 +00:00
|
|
|
#[instrument(skip(self, f), level = "debug")]
|
2018-08-29 13:25:47 -04:00
|
|
|
pub fn commit_if_ok<T, E, F>(&self, f: F) -> Result<T, E>
|
|
|
|
where
|
2022-09-09 13:01:06 -05:00
|
|
|
F: FnOnce(&CombinedSnapshot<'tcx>) -> Result<T, E>,
|
2014-12-08 20:26:43 -05:00
|
|
|
{
|
2015-03-10 07:02:27 -04:00
|
|
|
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);
|
|
|
|
}
|
2015-03-10 07:02:27 -04:00
|
|
|
}
|
|
|
|
r
|
2012-11-29 16:41:39 -08:00
|
|
|
}
|
|
|
|
|
2019-09-30 14:42:11 +10:00
|
|
|
/// Execute `f` then unroll any bindings it creates.
|
2021-08-20 13:36:04 +00:00
|
|
|
#[instrument(skip(self, f), level = "debug")]
|
2018-08-29 13:25:47 -04:00
|
|
|
pub fn probe<R, F>(&self, f: F) -> R
|
|
|
|
where
|
2022-09-09 13:01:06 -05:00
|
|
|
F: FnOnce(&CombinedSnapshot<'tcx>) -> R,
|
2014-12-08 20:26:43 -05:00
|
|
|
{
|
2014-06-20 06:35:06 -04:00
|
|
|
let snapshot = self.start_snapshot();
|
2014-12-14 07:17:23 -05:00
|
|
|
let r = f(&snapshot);
|
2015-07-24 10:23:35 -04:00
|
|
|
self.rollback_to("probe", snapshot);
|
2014-06-20 06:35:06 -04:00
|
|
|
r
|
2012-11-29 16:41:39 -08:00
|
|
|
}
|
2014-08-27 21:46:52 -04:00
|
|
|
|
2020-01-24 15:57:01 -05:00
|
|
|
/// If `should_skip` is true, then execute `f` then unroll any bindings it creates.
|
2021-08-20 13:36:04 +00:00
|
|
|
#[instrument(skip(self, f), level = "debug")]
|
2020-01-24 15:57:01 -05:00
|
|
|
pub fn probe_maybe_skip_leak_check<R, F>(&self, should_skip: bool, f: F) -> R
|
2020-01-18 07:59:16 -05:00
|
|
|
where
|
2022-09-09 13:01:06 -05:00
|
|
|
F: FnOnce(&CombinedSnapshot<'tcx>) -> R,
|
2020-01-18 07:59:16 -05:00
|
|
|
{
|
|
|
|
let snapshot = self.start_snapshot();
|
2020-02-25 14:31:07 +01:00
|
|
|
let was_skip_leak_check = self.skip_leak_check.get();
|
|
|
|
if should_skip {
|
|
|
|
self.skip_leak_check.set(true);
|
|
|
|
}
|
2020-01-18 07:59:16 -05:00
|
|
|
let r = f(&snapshot);
|
|
|
|
self.rollback_to("probe", snapshot);
|
2020-02-25 14:31:07 +01:00
|
|
|
self.skip_leak_check.set(was_skip_leak_check);
|
2020-01-18 07:59:16 -05:00
|
|
|
r
|
|
|
|
}
|
|
|
|
|
2018-11-20 10:24:38 -05:00
|
|
|
/// Scan the constraints produced since `snapshot` began and returns:
|
|
|
|
///
|
2023-03-04 07:38:37 +02:00
|
|
|
/// - `None` -- if none of them involves "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.
|
2018-10-16 05:42:18 -04:00
|
|
|
pub fn region_constraints_added_in_snapshot(
|
|
|
|
&self,
|
2022-09-09 13:01:06 -05:00
|
|
|
snapshot: &CombinedSnapshot<'tcx>,
|
2018-11-20 10:24:38 -05:00
|
|
|
) -> Option<bool> {
|
2020-01-31 08:45:57 +11:00
|
|
|
self.inner
|
|
|
|
.borrow_mut()
|
|
|
|
.unwrap_region_constraints()
|
2020-02-25 09:47:07 +01:00
|
|
|
.region_constraints_added_in_snapshot(&snapshot.undo_snapshot)
|
2018-10-16 05:42:18 -04:00
|
|
|
}
|
|
|
|
|
2022-09-09 13:01:06 -05:00
|
|
|
pub fn opaque_types_added_in_snapshot(&self, snapshot: &CombinedSnapshot<'tcx>) -> bool {
|
2022-06-30 14:23:31 +00:00
|
|
|
self.inner.borrow().undo_log.opaque_types_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) {
|
2020-01-31 08:45:57 +11:00
|
|
|
self.inner.borrow_mut().unwrap_region_constraints().add_given(sub, sup);
|
2014-08-27 21:46:52 -04:00
|
|
|
}
|
2014-09-12 10:53:35 -04:00
|
|
|
|
2023-02-13 19:28:10 +00:00
|
|
|
pub fn can_sub<T>(&self, param_env: ty::ParamEnv<'tcx>, a: T, b: T) -> bool
|
2018-08-29 13:25:47 -04:00
|
|
|
where
|
|
|
|
T: at::ToTrace<'tcx>,
|
2016-03-17 00:15:31 +02:00
|
|
|
{
|
2017-05-24 09:43:20 -04:00
|
|
|
let origin = &ObligationCause::dummy();
|
2023-03-15 14:00:15 +01:00
|
|
|
self.probe(|_| self.at(origin, param_env).sub(DefineOpaqueTypes::No, a, b).is_ok())
|
2016-03-17 00:15:31 +02:00
|
|
|
}
|
|
|
|
|
2023-02-13 19:28:10 +00:00
|
|
|
pub fn can_eq<T>(&self, param_env: ty::ParamEnv<'tcx>, a: T, b: T) -> bool
|
2018-08-29 13:25:47 -04:00
|
|
|
where
|
|
|
|
T: at::ToTrace<'tcx>,
|
2014-09-12 10:53:35 -04:00
|
|
|
{
|
2017-05-24 09:43:20 -04:00
|
|
|
let origin = &ObligationCause::dummy();
|
2023-03-15 14:00:15 +01:00
|
|
|
self.probe(|_| self.at(origin, param_env).eq(DefineOpaqueTypes::No, a, b).is_ok())
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
2012-11-29 16:41:39 -08:00
|
|
|
|
2021-08-20 13:36:04 +00:00
|
|
|
#[instrument(skip(self), level = "debug")]
|
2018-08-29 13:25:47 -04:00
|
|
|
pub fn sub_regions(
|
|
|
|
&self,
|
|
|
|
origin: SubregionOrigin<'tcx>,
|
|
|
|
a: ty::Region<'tcx>,
|
|
|
|
b: ty::Region<'tcx>,
|
|
|
|
) {
|
2020-01-31 08:45:57 +11:00
|
|
|
self.inner.borrow_mut().unwrap_region_constraints().make_subregion(origin, a, b);
|
2014-12-14 07:17:23 -05:00
|
|
|
}
|
|
|
|
|
2019-05-30 10:36:35 -04:00
|
|
|
/// Require that the region `r` be equal to one of the regions in
|
|
|
|
/// the set `regions`.
|
2021-08-20 13:36:04 +00:00
|
|
|
#[instrument(skip(self), level = "debug")]
|
2019-06-18 08:52:23 -04:00
|
|
|
pub fn member_constraint(
|
2019-05-30 10:36:35 -04:00
|
|
|
&self,
|
2022-07-26 06:19:58 +00:00
|
|
|
key: ty::OpaqueTypeKey<'tcx>,
|
2019-06-17 08:40:50 -04:00
|
|
|
definition_span: Span,
|
2019-06-03 16:31:44 -04:00
|
|
|
hidden_ty: Ty<'tcx>,
|
2019-05-30 10:36:35 -04:00
|
|
|
region: ty::Region<'tcx>,
|
2019-06-17 23:56:35 -04:00
|
|
|
in_regions: &Lrc<Vec<ty::Region<'tcx>>>,
|
2019-05-30 10:36:35 -04:00
|
|
|
) {
|
2020-01-31 08:45:57 +11:00
|
|
|
self.inner.borrow_mut().unwrap_region_constraints().member_constraint(
|
2022-07-26 06:19:58 +00:00
|
|
|
key,
|
2019-06-18 08:52:23 -04:00
|
|
|
definition_span,
|
|
|
|
hidden_ty,
|
|
|
|
region,
|
|
|
|
in_regions,
|
|
|
|
);
|
2019-05-30 10:36:35 -04:00
|
|
|
}
|
|
|
|
|
2020-11-21 07:06:16 -05:00
|
|
|
/// Processes a `Coerce` predicate from the fulfillment context.
|
|
|
|
/// This is NOT the preferred way to handle coercion, which is to
|
|
|
|
/// invoke `FnCtxt::coerce` or a similar method (see `coercion.rs`).
|
|
|
|
///
|
|
|
|
/// This method here is actually a fallback that winds up being
|
|
|
|
/// invoked when `FnCtxt::coerce` encounters unresolved type variables
|
|
|
|
/// and records a coercion predicate. Presently, this method is equivalent
|
|
|
|
/// to `subtype_predicate` -- that is, "coercing" `a` to `b` winds up
|
|
|
|
/// actually requiring `a <: b`. This is of course a valid coercion,
|
|
|
|
/// but it's not as flexible as `FnCtxt::coerce` would be.
|
|
|
|
///
|
|
|
|
/// (We may refactor this in the future, but there are a number of
|
|
|
|
/// practical obstacles. Among other things, `FnCtxt::coerce` presently
|
|
|
|
/// records adjustments that are required on the HIR in order to perform
|
|
|
|
/// the coercion, and we don't currently have a way to manage that.)
|
|
|
|
pub fn coerce_predicate(
|
|
|
|
&self,
|
|
|
|
cause: &ObligationCause<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
predicate: ty::PolyCoercePredicate<'tcx>,
|
2022-09-08 15:18:43 +00:00
|
|
|
) -> Result<InferResult<'tcx, ()>, (TyVid, TyVid)> {
|
2020-11-21 07:06:16 -05:00
|
|
|
let subtype_predicate = predicate.map_bound(|p| ty::SubtypePredicate {
|
|
|
|
a_is_expected: false, // when coercing from `a` to `b`, `b` is expected
|
|
|
|
a: p.a,
|
|
|
|
b: p.b,
|
|
|
|
});
|
|
|
|
self.subtype_predicate(cause, param_env, subtype_predicate)
|
|
|
|
}
|
|
|
|
|
2018-08-29 13:25:47 -04:00
|
|
|
pub fn subtype_predicate(
|
|
|
|
&self,
|
|
|
|
cause: &ObligationCause<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2020-05-23 11:09:32 +02:00
|
|
|
predicate: ty::PolySubtypePredicate<'tcx>,
|
2022-09-08 15:18:43 +00:00
|
|
|
) -> Result<InferResult<'tcx, ()>, (TyVid, TyVid)> {
|
2020-11-21 15:33:36 -05:00
|
|
|
// Check for two unresolved inference variables, in which case we can
|
|
|
|
// make no progress. This is partly a micro-optimization, but it's
|
|
|
|
// also an opportunity to "sub-unify" the variables. This isn't
|
|
|
|
// *necessary* to prevent cycles, because they would eventually be sub-unified
|
|
|
|
// anyhow during generalization, but it helps with diagnostics (we can detect
|
|
|
|
// earlier that they are sub-unified).
|
|
|
|
//
|
|
|
|
// Note that we can just skip the binders here because
|
|
|
|
// type variables can't (at present, at
|
2017-03-09 21:47:09 -05:00
|
|
|
// least) capture any of the things bound by this binder.
|
|
|
|
//
|
2020-11-21 15:33:36 -05:00
|
|
|
// Note that this sub here is not just for diagnostics - it has semantic
|
|
|
|
// effects as well.
|
|
|
|
let r_a = self.shallow_resolve(predicate.skip_binder().a);
|
|
|
|
let r_b = self.shallow_resolve(predicate.skip_binder().b);
|
|
|
|
match (r_a.kind(), r_b.kind()) {
|
|
|
|
(&ty::Infer(ty::TyVar(a_vid)), &ty::Infer(ty::TyVar(b_vid))) => {
|
|
|
|
self.inner.borrow_mut().type_variables().sub(a_vid, b_vid);
|
2022-09-08 15:18:43 +00:00
|
|
|
return Err((a_vid, b_vid));
|
2020-11-21 15:33:36 -05:00
|
|
|
}
|
|
|
|
_ => {}
|
2017-03-09 21:47:09 -05:00
|
|
|
}
|
|
|
|
|
2022-09-08 15:18:43 +00:00
|
|
|
Ok(self.commit_if_ok(|_snapshot| {
|
2020-06-28 20:27:59 +01:00
|
|
|
let ty::SubtypePredicate { a_is_expected, a, b } =
|
2023-02-07 23:13:22 +00:00
|
|
|
self.instantiate_binder_with_placeholders(predicate);
|
2019-12-22 17:42:04 -05:00
|
|
|
|
2023-03-15 14:00:15 +01:00
|
|
|
let ok =
|
|
|
|
self.at(cause, param_env).sub_exp(DefineOpaqueTypes::No, a_is_expected, a, b)?;
|
2019-02-20 05:22:23 -05:00
|
|
|
|
|
|
|
Ok(ok.unit())
|
2019-02-19 18:48:29 -05:00
|
|
|
}))
|
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>,
|
2020-05-23 11:09:32 +02:00
|
|
|
predicate: ty::PolyRegionOutlivesPredicate<'tcx>,
|
2022-07-20 11:49:20 +02:00
|
|
|
) {
|
2023-02-07 23:13:22 +00:00
|
|
|
let ty::OutlivesPredicate(r_a, r_b) = self.instantiate_binder_with_placeholders(predicate);
|
2022-07-20 11:49:20 +02:00
|
|
|
let origin =
|
|
|
|
SubregionOrigin::from_obligation_cause(cause, || RelateRegionParamBound(cause.span));
|
|
|
|
self.sub_regions(origin, r_b, r_a); // `b : a` ==> `a <= b`
|
2014-12-12 06:37:42 -05:00
|
|
|
}
|
|
|
|
|
2020-11-22 09:58:58 -05:00
|
|
|
/// Number of type variables created so far.
|
|
|
|
pub fn num_ty_vars(&self) -> usize {
|
|
|
|
self.inner.borrow_mut().type_variables().num_vars()
|
|
|
|
}
|
|
|
|
|
2020-11-23 09:06:37 -05:00
|
|
|
pub fn next_ty_var_id(&self, origin: TypeVariableOrigin) -> TyVid {
|
|
|
|
self.inner.borrow_mut().type_variables().new_var(self.universe(), origin)
|
2012-11-29 16:41:39 -08:00
|
|
|
}
|
|
|
|
|
2018-03-06 09:07:27 -07:00
|
|
|
pub fn next_ty_var(&self, origin: TypeVariableOrigin) -> Ty<'tcx> {
|
2020-11-23 09:06:37 -05:00
|
|
|
self.tcx.mk_ty_var(self.next_ty_var_id(origin))
|
2014-10-24 21:14:37 +02:00
|
|
|
}
|
|
|
|
|
2022-02-14 16:10:22 +00:00
|
|
|
pub fn next_ty_var_id_in_universe(
|
|
|
|
&self,
|
|
|
|
origin: TypeVariableOrigin,
|
|
|
|
universe: ty::UniverseIndex,
|
|
|
|
) -> TyVid {
|
|
|
|
self.inner.borrow_mut().type_variables().new_var(universe, origin)
|
|
|
|
}
|
|
|
|
|
2018-11-02 19:46:30 +01:00
|
|
|
pub fn next_ty_var_in_universe(
|
|
|
|
&self,
|
|
|
|
origin: TypeVariableOrigin,
|
|
|
|
universe: ty::UniverseIndex,
|
|
|
|
) -> Ty<'tcx> {
|
2022-02-14 16:10:22 +00:00
|
|
|
let vid = self.next_ty_var_id_in_universe(origin, universe);
|
2019-02-20 01:14:18 +00:00
|
|
|
self.tcx.mk_ty_var(vid)
|
2018-11-02 19:46:30 +01:00
|
|
|
}
|
|
|
|
|
2022-02-02 14:24:45 +11:00
|
|
|
pub fn next_const_var(&self, ty: Ty<'tcx>, origin: ConstVariableOrigin) -> ty::Const<'tcx> {
|
2022-11-28 11:27:18 +00:00
|
|
|
self.tcx.mk_const(self.next_const_var_id(origin), ty)
|
2019-03-08 01:18:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn next_const_var_in_universe(
|
|
|
|
&self,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
origin: ConstVariableOrigin,
|
|
|
|
universe: ty::UniverseIndex,
|
2022-02-02 14:24:45 +11:00
|
|
|
) -> ty::Const<'tcx> {
|
2019-03-08 01:18:02 +00:00
|
|
|
let vid = self
|
2020-01-31 08:45:57 +11:00
|
|
|
.inner
|
2019-03-08 01:18:02 +00:00
|
|
|
.borrow_mut()
|
2020-02-24 10:40:36 +01:00
|
|
|
.const_unification_table()
|
2019-03-13 15:19:35 +00:00
|
|
|
.new_key(ConstVarValue { origin, val: ConstVariableValue::Unknown { universe } });
|
2022-11-28 11:27:18 +00:00
|
|
|
self.tcx.mk_const(vid, ty)
|
2019-03-08 01:18:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn next_const_var_id(&self, origin: ConstVariableOrigin) -> ConstVid<'tcx> {
|
2020-02-24 10:40:36 +01:00
|
|
|
self.inner.borrow_mut().const_unification_table().new_key(ConstVarValue {
|
2019-03-13 15:19:35 +00:00
|
|
|
origin,
|
|
|
|
val: ConstVariableValue::Unknown { universe: self.universe() },
|
|
|
|
})
|
2019-03-08 01:18:02 +00:00
|
|
|
}
|
|
|
|
|
2019-03-21 13:47:57 +00:00
|
|
|
fn next_int_var_id(&self) -> IntVid {
|
2020-02-24 10:40:36 +01:00
|
|
|
self.inner.borrow_mut().int_unification_table().new_key(None)
|
2012-11-29 16:41:39 -08:00
|
|
|
}
|
|
|
|
|
2019-03-21 13:47:57 +00: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 {
|
2020-02-24 10:40:36 +01:00
|
|
|
self.inner.borrow_mut().float_unification_table().new_key(None)
|
2012-11-29 16:41:39 -08:00
|
|
|
}
|
|
|
|
|
2019-03-21 13:47:57 +00: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.
|
2018-08-29 14:33:38 -04:00
|
|
|
/// 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> {
|
2018-08-29 14:33:38 -04:00
|
|
|
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
|
2018-08-29 14:33:38 -04:00
|
|
|
/// 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> {
|
2020-01-31 08:45:57 +11:00
|
|
|
let region_var =
|
|
|
|
self.inner.borrow_mut().unwrap_region_constraints().new_region_var(universe, origin);
|
2023-02-13 13:03:45 +11:00
|
|
|
self.tcx.mk_re_var(region_var)
|
2012-11-29 16:41:39 -08:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:34:16 +00:00
|
|
|
/// Return the universe that the region `r` was created in. For
|
make generalization code create new variables in correct universe
In our type inference system, when we "generalize" a type T to become
a suitable value for a type variable V, we sometimes wind up creating
new inference variables. So, for example, if we are making V be some
subtype of `&'X u32`, then we might instantiate V with `&'Y u32`.
This generalized type is then related `&'Y u32 <: &'X u32`, resulting
in a region constriant `'Y: 'X`. Previously, however, we were making
these fresh variables like `'Y` in the "current universe", but they
should be created in the universe of V. Moreover, we sometimes cheat
in an invariant context and avoid creating fresh variables if we know
the result must be equal -- we can only do that when the universes
work out.
2019-02-01 10:41:08 -05:00
|
|
|
/// most regions (e.g., `'static`, named regions from the user,
|
|
|
|
/// etc) this is the root universe U0. For inference variables or
|
2022-10-14 00:25:34 +08:00
|
|
|
/// placeholders, however, it will return the universe which they
|
|
|
|
/// are associated.
|
2021-06-07 20:25:19 +01:00
|
|
|
pub fn universe_of_region(&self, r: ty::Region<'tcx>) -> ty::UniverseIndex {
|
2020-01-31 08:45:57 +11:00
|
|
|
self.inner.borrow_mut().unwrap_region_constraints().universe(r)
|
make generalization code create new variables in correct universe
In our type inference system, when we "generalize" a type T to become
a suitable value for a type variable V, we sometimes wind up creating
new inference variables. So, for example, if we are making V be some
subtype of `&'X u32`, then we might instantiate V with `&'Y u32`.
This generalized type is then related `&'Y u32 <: &'X u32`, resulting
in a region constriant `'Y: 'X`. Previously, however, we were making
these fresh variables like `'Y` in the "current universe", but they
should be created in the universe of V. Moreover, we sometimes cheat
in an invariant context and avoid creating fresh variables if we know
the result must be equal -- we can only do that when the universes
work out.
2019-02-01 10:41:08 -05:00
|
|
|
}
|
|
|
|
|
2017-11-22 17:38:51 -05:00
|
|
|
/// Number of region variables created so far.
|
|
|
|
pub fn num_region_vars(&self) -> usize {
|
2020-01-31 08:45:57 +11:00
|
|
|
self.inner.borrow_mut().unwrap_region_constraints().num_region_vars()
|
2017-11-22 17:38:51 -05:00
|
|
|
}
|
|
|
|
|
2017-11-06 05:21:48 -05:00
|
|
|
/// Just a convenient wrapper of `next_region_var` for using during NLL.
|
2022-11-08 16:27:01 +01:00
|
|
|
#[instrument(skip(self), level = "debug")]
|
2021-01-28 16:18:25 +09:00
|
|
|
pub fn next_nll_region_var(&self, origin: NllRegionVariableOrigin) -> ty::Region<'tcx> {
|
|
|
|
self.next_region_var(RegionVariableOrigin::Nll(origin))
|
2017-11-06 05:21:48 -05:00
|
|
|
}
|
|
|
|
|
2018-08-29 14:33:38 -04:00
|
|
|
/// Just a convenient wrapper of `next_region_var` for using during NLL.
|
2022-11-08 16:27:01 +01:00
|
|
|
#[instrument(skip(self), level = "debug")]
|
2018-08-29 14:33:38 -04:00
|
|
|
pub fn next_nll_region_var_in_universe(
|
|
|
|
&self,
|
2021-01-28 16:18:25 +09:00
|
|
|
origin: NllRegionVariableOrigin,
|
2018-08-29 14:33:38 -04:00
|
|
|
universe: ty::UniverseIndex,
|
|
|
|
) -> ty::Region<'tcx> {
|
2021-01-28 16:18:25 +09:00
|
|
|
self.next_region_var_in_universe(RegionVariableOrigin::Nll(origin), universe)
|
2018-08-29 14:33:38 -04:00
|
|
|
}
|
|
|
|
|
2019-09-25 16:39:44 +01:00
|
|
|
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.
|
2018-08-29 13:25:47 -04: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.
|
2018-11-27 02:59:49 +00:00
|
|
|
// 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)`.
|
2020-02-24 10:40:36 +01:00
|
|
|
let ty_var_id = self.inner.borrow_mut().type_variables().new_var(
|
2018-08-29 13:25:47 -04:00
|
|
|
self.universe(),
|
2019-03-21 00:00:24 +00:00
|
|
|
TypeVariableOrigin {
|
2019-12-13 19:54:18 +01:00
|
|
|
kind: TypeVariableOriginKind::TypeParameterDefinition(
|
|
|
|
param.name,
|
|
|
|
Some(param.def_id),
|
|
|
|
),
|
2019-03-21 00:00:24 +00:00
|
|
|
span,
|
|
|
|
},
|
2018-08-29 13:25:47 -04:00
|
|
|
);
|
2018-05-14 19:57:59 +01:00
|
|
|
|
2019-02-20 01:14:18 +00:00
|
|
|
self.tcx.mk_ty_var(ty_var_id).into()
|
|
|
|
}
|
|
|
|
GenericParamDefKind::Const { .. } => {
|
2019-05-31 22:52:35 +01:00
|
|
|
let origin = ConstVariableOrigin {
|
2020-09-23 09:24:58 +02:00
|
|
|
kind: ConstVariableOriginKind::ConstParameterDefinition(
|
|
|
|
param.name,
|
|
|
|
param.def_id,
|
|
|
|
),
|
2019-05-31 22:52:35 +01:00
|
|
|
span,
|
|
|
|
};
|
2019-03-08 01:18:02 +00:00
|
|
|
let const_var_id =
|
2020-02-24 10:40:36 +01:00
|
|
|
self.inner.borrow_mut().const_unification_table().new_key(ConstVarValue {
|
2019-03-13 15:19:35 +00:00
|
|
|
origin,
|
|
|
|
val: ConstVariableValue::Unknown { universe: self.universe() },
|
|
|
|
});
|
2023-02-06 17:48:12 -07:00
|
|
|
self.tcx
|
2023-02-14 14:17:38 -07:00
|
|
|
.mk_const(
|
|
|
|
const_var_id,
|
|
|
|
self.tcx
|
|
|
|
.type_of(param.def_id)
|
|
|
|
.no_bound_vars()
|
|
|
|
.expect("const parameter types cannot be generic"),
|
|
|
|
)
|
2023-02-06 17:48:12 -07:00
|
|
|
.into()
|
2018-05-14 19:57:59 +01:00
|
|
|
}
|
|
|
|
}
|
2016-08-17 03:56:18 +03:00
|
|
|
}
|
|
|
|
|
2014-11-25 21:17:11 -05: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> {
|
2019-02-26 09:30:34 +08:00
|
|
|
InternalSubsts::for_item(self.tcx, def_id, |param, _| self.var_for_def(span, param))
|
2014-05-31 18:53:13 -04:00
|
|
|
}
|
|
|
|
|
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
|
2016-03-15 04:49:10 -04:00
|
|
|
/// 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).
|
2022-11-18 10:25:32 +00:00
|
|
|
#[must_use = "this method does not have any side effects"]
|
|
|
|
pub fn tainted_by_errors(&self) -> Option<ErrorGuaranteed> {
|
2018-08-29 13:25:47 -04:00
|
|
|
debug!(
|
|
|
|
"is_tainted_by_errors(err_count={}, err_count_on_creation={}, \
|
2022-08-10 22:21:34 +08:00
|
|
|
tainted_by_errors={})",
|
2018-08-29 13:25:47 -04:00
|
|
|
self.tcx.sess.err_count(),
|
|
|
|
self.err_count_on_creation,
|
2022-08-10 22:21:34 +08:00
|
|
|
self.tainted_by_errors.get().is_some()
|
2018-08-29 13:25:47 -04:00
|
|
|
);
|
2016-03-15 04:49:10 -04:00
|
|
|
|
2022-11-18 10:25:32 +00:00
|
|
|
if let Some(e) = self.tainted_by_errors.get() {
|
|
|
|
return Some(e);
|
|
|
|
}
|
|
|
|
|
2016-03-15 04:49:10 -04:00
|
|
|
if self.tcx.sess.err_count() > self.err_count_on_creation {
|
2022-11-17 20:57:45 +00:00
|
|
|
// errors reported since this infcx was made
|
2022-11-18 11:30:21 +00:00
|
|
|
let e = self.tcx.sess.has_errors().unwrap();
|
|
|
|
self.set_tainted_by_errors(e);
|
|
|
|
return Some(e);
|
2016-03-15 04:49:10 -04:00
|
|
|
}
|
2022-11-18 10:25:32 +00:00
|
|
|
|
|
|
|
None
|
2016-03-15 04:49:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the "tainted by errors" flag to true. We call this when we
|
|
|
|
/// observe an error from a prior pass.
|
2022-11-18 11:30:21 +00:00
|
|
|
pub fn set_tainted_by_errors(&self, e: ErrorGuaranteed) {
|
|
|
|
debug!("set_tainted_by_errors(ErrorGuaranteed)");
|
|
|
|
self.tainted_by_errors.set(Some(e));
|
2016-03-09 16:53:19 -05:00
|
|
|
}
|
|
|
|
|
2022-06-13 01:11:16 -04:00
|
|
|
pub fn skip_region_resolution(&self) {
|
|
|
|
let (var_infos, _) = {
|
|
|
|
let mut inner = self.inner.borrow_mut();
|
|
|
|
let inner = &mut *inner;
|
|
|
|
// Note: `inner.region_obligations` may not be empty, because we
|
|
|
|
// didn't necessarily call `process_registered_region_obligations`.
|
|
|
|
// This is okay, because that doesn't introduce new vars.
|
|
|
|
inner
|
|
|
|
.region_constraint_storage
|
|
|
|
.take()
|
|
|
|
.expect("regions already resolved")
|
|
|
|
.with_log(&mut inner.undo_log)
|
|
|
|
.into_infos_and_data()
|
|
|
|
};
|
|
|
|
|
|
|
|
let lexical_region_resolutions = LexicalRegionResolutions {
|
|
|
|
values: rustc_index::vec::IndexVec::from_elem_n(
|
|
|
|
crate::infer::lexical_region_resolve::VarValue::Value(self.tcx.lifetimes.re_erased),
|
|
|
|
var_infos.len(),
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
|
|
|
let old_value = self.lexical_region_resolutions.replace(Some(lexical_region_resolutions));
|
|
|
|
assert!(old_value.is_none());
|
|
|
|
}
|
|
|
|
|
2022-10-13 00:12:19 +08:00
|
|
|
/// Process the region constraints and return any errors that
|
2017-11-05 07:22:39 -05:00
|
|
|
/// result. After this, no more unification operations should be
|
|
|
|
/// done -- or the compiler will panic -- but it is legal to use
|
2019-05-11 19:08:26 +01:00
|
|
|
/// `resolve_vars_if_possible` as well as `fully_resolve`.
|
2021-10-16 02:29:59 -04:00
|
|
|
pub fn resolve_regions(
|
2017-12-20 04:54:35 -05:00
|
|
|
&self,
|
|
|
|
outlives_env: &OutlivesEnvironment<'tcx>,
|
2021-10-16 02:29:59 -04:00
|
|
|
) -> Vec<RegionResolutionError<'tcx>> {
|
2020-03-16 16:43:03 +01:00
|
|
|
let (var_infos, data) = {
|
|
|
|
let mut inner = self.inner.borrow_mut();
|
|
|
|
let inner = &mut *inner;
|
|
|
|
assert!(
|
2022-11-18 10:25:32 +00:00
|
|
|
self.tainted_by_errors().is_some() || inner.region_obligations.is_empty(),
|
2020-03-16 16:43:03 +01:00
|
|
|
"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-02-15 12:11:59 +00:00
|
|
|
|
2022-06-27 17:18:49 +02:00
|
|
|
let region_rels = &RegionRelations::new(self.tcx, outlives_env.free_region_map());
|
2020-02-15 12:11:59 +00:00
|
|
|
|
2017-11-05 14:37:55 -05:00
|
|
|
let (lexical_region_resolutions, errors) =
|
2022-06-15 05:55:05 -04:00
|
|
|
lexical_region_resolve::resolve(outlives_env.param_env, region_rels, var_infos, data);
|
2017-11-05 05:48:16 -05:00
|
|
|
|
2018-08-29 13:25:47 -04:00
|
|
|
let old_value = self.lexical_region_resolutions.replace(Some(lexical_region_resolutions));
|
2017-11-05 05:48:16 -05:00
|
|
|
assert!(old_value.is_none());
|
2017-06-12 11:45:19 -07:00
|
|
|
|
2021-10-16 02:29:59 -04:00
|
|
|
errors
|
|
|
|
}
|
2017-11-05 14:42:56 -05: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!(
|
2020-01-31 08:45:57 +11:00
|
|
|
self.inner.borrow().region_obligations.is_empty(),
|
2018-08-29 13:25:47 -04:00
|
|
|
"region_obligations not empty: {:#?}",
|
2020-01-31 08:45:57 +11:00
|
|
|
self.inner.borrow().region_obligations
|
2018-08-29 13:25:47 -04:00
|
|
|
);
|
2017-11-28 00:39:38 -03:00
|
|
|
|
2020-01-31 08:45:57 +11:00
|
|
|
self.inner.borrow_mut().unwrap_region_constraints().take_and_reset_data()
|
2017-11-05 14:42:56 -05:00
|
|
|
}
|
|
|
|
|
2018-02-25 10:58:54 -05:00
|
|
|
/// Gives temporary access to the region constraint data.
|
|
|
|
pub fn with_region_constraints<R>(
|
|
|
|
&self,
|
|
|
|
op: impl FnOnce(&RegionConstraintData<'tcx>) -> R,
|
|
|
|
) -> R {
|
2020-01-31 08:45:57 +11:00
|
|
|
let mut inner = self.inner.borrow_mut();
|
|
|
|
op(inner.unwrap_region_constraints().data())
|
2018-02-25 10:58:54 -05:00
|
|
|
}
|
|
|
|
|
2021-06-07 20:25:19 +01:00
|
|
|
pub fn region_var_origin(&self, vid: ty::RegionVid) -> RegionVariableOrigin {
|
|
|
|
let mut inner = self.inner.borrow_mut();
|
|
|
|
let inner = &mut *inner;
|
|
|
|
inner
|
|
|
|
.region_constraint_storage
|
|
|
|
.as_mut()
|
|
|
|
.expect("regions already resolved")
|
|
|
|
.with_log(&mut inner.undo_log)
|
|
|
|
.var_origin(vid)
|
|
|
|
}
|
|
|
|
|
2017-11-06 05:21:48 -05:00
|
|
|
/// 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
|
2017-11-06 05:21:48 -05:00
|
|
|
/// 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.
|
2018-02-07 11:47:57 -07:00
|
|
|
pub fn take_region_var_origins(&self) -> VarInfos {
|
2020-02-25 09:47:07 +01:00
|
|
|
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")
|
2020-02-25 09:47:07 +01:00
|
|
|
.with_log(&mut inner.undo_log)
|
2018-08-29 13:25:47 -04:00
|
|
|
.into_infos_and_data();
|
2017-11-06 05:21:48 -05:00
|
|
|
assert!(data.is_empty());
|
2018-02-07 11:47:57 -07:00
|
|
|
var_infos
|
2017-11-06 04:29:50 -05:00
|
|
|
}
|
|
|
|
|
2023-01-09 18:14:28 +00:00
|
|
|
#[instrument(level = "debug", skip(self), ret)]
|
|
|
|
pub fn take_opaque_types(&self) -> opaque_types::OpaqueTypeMap<'tcx> {
|
|
|
|
debug_assert_ne!(self.defining_use_anchor, DefiningAnchor::Error);
|
|
|
|
std::mem::take(&mut self.inner.borrow_mut().opaque_type_storage.opaque_types)
|
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-11-02 19:46:30 +01:00
|
|
|
/// 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;
|
|
|
|
|
2020-02-24 10:40:36 +01:00
|
|
|
match self.inner.borrow_mut().type_variables().probe(vid) {
|
2018-11-02 19:46:30 +01:00
|
|
|
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
|
2022-11-16 20:34:16 +00:00
|
|
|
/// level. So, if the variable `?X` is bound to some type
|
2019-10-02 14:35:01 -04:00
|
|
|
/// `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).
|
2019-04-30 22:27:33 +01:00
|
|
|
pub fn shallow_resolve<T>(&self, value: T) -> T
|
|
|
|
where
|
2023-02-22 02:18:40 +00:00
|
|
|
T: TypeFoldable<TyCtxt<'tcx>>,
|
2019-04-30 22:27:33 +01:00
|
|
|
{
|
2021-12-01 00:55:57 +00:00
|
|
|
value.fold_with(&mut ShallowResolver { infcx: self })
|
2018-08-17 16:05:24 +10:00
|
|
|
}
|
|
|
|
|
2018-11-25 16:14:54 +02:00
|
|
|
pub fn root_var(&self, var: ty::TyVid) -> ty::TyVid {
|
2020-02-24 10:40:36 +01:00
|
|
|
self.inner.borrow_mut().type_variables().root_var(var)
|
2018-11-25 16:14:54 +02:00
|
|
|
}
|
|
|
|
|
2023-03-07 04:17:37 +00:00
|
|
|
pub fn root_const_var(&self, var: ty::ConstVid<'tcx>) -> ty::ConstVid<'tcx> {
|
|
|
|
self.inner.borrow_mut().const_unification_table().find(var)
|
|
|
|
}
|
|
|
|
|
2023-03-08 22:10:24 +00:00
|
|
|
/// Resolves an int var to a rigid int type, if it was constrained to one,
|
|
|
|
/// or else the root int var in the unification table.
|
|
|
|
pub fn opportunistic_resolve_int_var(&self, vid: ty::IntVid) -> Ty<'tcx> {
|
|
|
|
let mut inner = self.inner.borrow_mut();
|
|
|
|
if let Some(value) = inner.int_unification_table().probe_value(vid) {
|
|
|
|
value.to_type(self.tcx)
|
|
|
|
} else {
|
|
|
|
self.tcx.mk_int_var(inner.int_unification_table().find(vid))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Resolves a float var to a rigid int type, if it was constrained to one,
|
|
|
|
/// or else the root float var in the unification table.
|
|
|
|
pub fn opportunistic_resolve_float_var(&self, vid: ty::FloatVid) -> Ty<'tcx> {
|
|
|
|
let mut inner = self.inner.borrow_mut();
|
|
|
|
if let Some(value) = inner.float_unification_table().probe_value(vid) {
|
|
|
|
value.to_type(self.tcx)
|
|
|
|
} else {
|
|
|
|
self.tcx.mk_float_var(inner.float_unification_table().find(vid))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-11 19:08:26 +01:00
|
|
|
/// Where possible, replaces type/const variables in
|
2019-04-22 21:29:11 +03:00
|
|
|
/// `value` with their final value. Note that region variables
|
2019-05-11 19:08:26 +01:00
|
|
|
/// 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
|
2023-02-22 02:18:40 +00:00
|
|
|
T: TypeFoldable<TyCtxt<'tcx>>,
|
2015-08-17 23:50:24 +03:00
|
|
|
{
|
2023-02-03 10:15:24 +11:00
|
|
|
if !value.has_non_region_infer() {
|
|
|
|
return value;
|
2015-08-17 23:50:24 +03:00
|
|
|
}
|
2019-05-11 19:08:26 +01:00
|
|
|
let mut r = resolve::OpportunisticVarResolver::new(self);
|
2021-12-01 00:55:57 +00:00
|
|
|
value.fold_with(&mut r)
|
2013-03-27 06:16:28 -04:00
|
|
|
}
|
|
|
|
|
2021-12-15 22:59:32 +00:00
|
|
|
pub fn resolve_numeric_literals_with_default<T>(&self, value: T) -> T
|
|
|
|
where
|
2023-02-22 02:18:40 +00:00
|
|
|
T: TypeFoldable<TyCtxt<'tcx>>,
|
2021-12-15 22:59:32 +00:00
|
|
|
{
|
|
|
|
if !value.needs_infer() {
|
|
|
|
return value; // Avoid duplicated subst-folding.
|
|
|
|
}
|
2022-03-27 02:40:07 +00:00
|
|
|
let mut r = InferenceLiteralEraser { tcx: self.tcx };
|
2021-12-15 22:59:32 +00:00
|
|
|
value.fold_with(&mut r)
|
|
|
|
}
|
|
|
|
|
2022-11-23 19:38:22 +00:00
|
|
|
/// Returns the first unresolved type or const variable contained in `T`.
|
|
|
|
pub fn first_unresolved_const_or_ty_var<T>(
|
|
|
|
&self,
|
|
|
|
value: &T,
|
|
|
|
) -> Option<(ty::Term<'tcx>, Option<Span>)>
|
2018-08-29 13:25:47 -04:00
|
|
|
where
|
2023-02-22 02:18:40 +00:00
|
|
|
T: TypeVisitable<TyCtxt<'tcx>>,
|
2017-09-02 08:35:07 -04:00
|
|
|
{
|
2022-11-23 19:38:22 +00:00
|
|
|
value.visit_with(&mut resolve::UnresolvedTypeOrConstFinder::new(self)).break_value()
|
2017-09-02 08:35:07 -04:00
|
|
|
}
|
|
|
|
|
2019-03-08 01:18:02 +00:00
|
|
|
pub fn probe_const_var(
|
|
|
|
&self,
|
|
|
|
vid: ty::ConstVid<'tcx>,
|
2022-02-02 14:24:45 +11:00
|
|
|
) -> Result<ty::Const<'tcx>, ty::UniverseIndex> {
|
2020-02-24 10:40:36 +01:00
|
|
|
match self.inner.borrow_mut().const_unification_table().probe_value(vid).val {
|
2019-03-08 01:18:02 +00:00
|
|
|
ConstVariableValue::Known { value } => Ok(value),
|
|
|
|
ConstVariableValue::Unknown { universe } => Err(universe),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-28 11:49:05 +00:00
|
|
|
/// 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.
|
2023-02-22 02:18:40 +00:00
|
|
|
pub fn fully_resolve<T: TypeFoldable<TyCtxt<'tcx>>>(&self, value: T) -> FixupResult<'tcx, T> {
|
2022-10-10 22:39:41 +00:00
|
|
|
let value = resolve::fully_resolve(self, value);
|
|
|
|
assert!(
|
|
|
|
value.as_ref().map_or(true, |value| !value.needs_infer()),
|
|
|
|
"`{value:?}` is not fully resolved"
|
|
|
|
);
|
|
|
|
value
|
2014-12-01 12:27:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-07 23:13:22 +00:00
|
|
|
// Instantiates the bound variables in a given binder with fresh inference
|
|
|
|
// variables in the current universe.
|
|
|
|
//
|
|
|
|
// Use this method if you'd like to find some substitution of the binder's
|
|
|
|
// variables (e.g. during a method call). If there isn't a [`LateBoundRegionConversionTime`]
|
|
|
|
// that corresponds to your use case, consider whether or not you should
|
|
|
|
// use [`InferCtxt::instantiate_binder_with_placeholders`] instead.
|
|
|
|
pub fn instantiate_binder_with_fresh_vars<T>(
|
2014-11-12 14:11:22 -05:00
|
|
|
&self,
|
|
|
|
span: Span,
|
|
|
|
lbrct: LateBoundRegionConversionTime,
|
2020-10-05 16:51:33 -04:00
|
|
|
value: ty::Binder<'tcx, T>,
|
2022-06-02 12:48:56 +02:00
|
|
|
) -> T
|
2018-08-29 13:25:47 -04:00
|
|
|
where
|
2023-02-22 02:18:40 +00:00
|
|
|
T: TypeFoldable<TyCtxt<'tcx>> + Copy,
|
2014-11-05 22:06:04 -05:00
|
|
|
{
|
2022-06-08 18:45:49 +02:00
|
|
|
if let Some(inner) = value.no_bound_vars() {
|
|
|
|
return inner;
|
|
|
|
}
|
|
|
|
|
2022-07-25 19:27:52 +02:00
|
|
|
struct ToFreshVars<'a, 'tcx> {
|
2022-09-09 13:01:06 -05:00
|
|
|
infcx: &'a InferCtxt<'tcx>,
|
2022-07-25 19:27:52 +02:00
|
|
|
span: Span,
|
|
|
|
lbrct: LateBoundRegionConversionTime,
|
|
|
|
map: FxHashMap<ty::BoundVar, ty::GenericArg<'tcx>>,
|
|
|
|
}
|
2022-06-02 12:42:57 +02:00
|
|
|
|
2022-07-25 19:27:52 +02:00
|
|
|
impl<'tcx> BoundVarReplacerDelegate<'tcx> for ToFreshVars<'_, 'tcx> {
|
|
|
|
fn replace_region(&mut self, br: ty::BoundRegion) -> ty::Region<'tcx> {
|
|
|
|
self.map
|
|
|
|
.entry(br.var)
|
|
|
|
.or_insert_with(|| {
|
|
|
|
self.infcx
|
|
|
|
.next_region_var(LateBoundRegion(self.span, br.kind, self.lbrct))
|
|
|
|
.into()
|
|
|
|
})
|
|
|
|
.expect_region()
|
|
|
|
}
|
|
|
|
fn replace_ty(&mut self, bt: ty::BoundTy) -> Ty<'tcx> {
|
|
|
|
self.map
|
|
|
|
.entry(bt.var)
|
|
|
|
.or_insert_with(|| {
|
|
|
|
self.infcx
|
|
|
|
.next_ty_var(TypeVariableOrigin {
|
|
|
|
kind: TypeVariableOriginKind::MiscVariable,
|
|
|
|
span: self.span,
|
|
|
|
})
|
|
|
|
.into()
|
|
|
|
})
|
|
|
|
.expect_ty()
|
|
|
|
}
|
|
|
|
fn replace_const(&mut self, bv: ty::BoundVar, ty: Ty<'tcx>) -> ty::Const<'tcx> {
|
|
|
|
self.map
|
|
|
|
.entry(bv)
|
|
|
|
.or_insert_with(|| {
|
|
|
|
self.infcx
|
|
|
|
.next_const_var(
|
|
|
|
ty,
|
|
|
|
ConstVariableOrigin {
|
|
|
|
kind: ConstVariableOriginKind::MiscVariable,
|
|
|
|
span: self.span,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.into()
|
|
|
|
})
|
|
|
|
.expect_const()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let delegate = ToFreshVars { infcx: self, span, lbrct, map: Default::default() };
|
|
|
|
self.tcx.replace_bound_vars_uncached(value, delegate)
|
2012-11-29 16:41:39 -08:00
|
|
|
}
|
2015-01-03 04:40:33 -05: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);
|
|
|
|
|
2020-01-31 08:45:57 +11:00
|
|
|
self.inner
|
|
|
|
.borrow_mut()
|
|
|
|
.unwrap_region_constraints()
|
|
|
|
.verify_generic_bound(origin, kind, a, bound);
|
2018-08-29 13:25:47 -04:00
|
|
|
}
|
|
|
|
|
2017-11-08 09:45:48 -05: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.
|
2020-03-13 03:23:38 +02:00
|
|
|
pub fn closure_kind(&self, closure_substs: SubstsRef<'tcx>) -> Option<ty::ClosureKind> {
|
|
|
|
let closure_kind_ty = closure_substs.as_closure().kind_ty();
|
2019-04-30 22:27:33 +01:00
|
|
|
let closure_kind_ty = self.shallow_resolve(closure_kind_ty);
|
2017-11-08 09:45:48 -05:00
|
|
|
closure_kind_ty.to_opt_closure_kind()
|
2015-06-30 02:18:03 -07:00
|
|
|
}
|
|
|
|
|
2018-09-30 20:09:05 +03:00
|
|
|
/// 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();
|
2020-02-25 13:08:38 +01:00
|
|
|
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
|
|
|
}
|
2018-02-08 13:14:24 -07:00
|
|
|
|
2021-03-07 14:41:45 +00:00
|
|
|
pub fn universe(&self) -> ty::UniverseIndex {
|
2018-02-08 13:14:24 -07:00
|
|
|
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
|
2018-10-08 07:17:36 -04:00
|
|
|
/// 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
|
|
|
|
}
|
2020-01-24 20:05:07 +13:00
|
|
|
|
2022-02-16 10:56:01 +01:00
|
|
|
pub fn try_const_eval_resolve(
|
|
|
|
&self,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2022-09-22 12:34:23 +02:00
|
|
|
unevaluated: ty::UnevaluatedConst<'tcx>,
|
2022-02-16 10:56:01 +01:00
|
|
|
ty: Ty<'tcx>,
|
|
|
|
span: Option<Span>,
|
|
|
|
) -> Result<ty::Const<'tcx>, ErrorHandled> {
|
|
|
|
match self.const_eval_resolve(param_env, unevaluated, span) {
|
2022-11-28 12:38:15 +00:00
|
|
|
Ok(Some(val)) => Ok(self.tcx.mk_const(val, ty)),
|
2022-02-16 10:56:01 +01:00
|
|
|
Ok(None) => {
|
|
|
|
let tcx = self.tcx;
|
|
|
|
let def_id = unevaluated.def.did;
|
|
|
|
span_bug!(
|
|
|
|
tcx.def_span(def_id),
|
|
|
|
"unable to construct a constant value for the unevaluated constant {:?}",
|
|
|
|
unevaluated
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Err(err) => Err(err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 20:05:07 +13:00
|
|
|
/// 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.
|
2022-03-21 18:26:53 +01:00
|
|
|
#[instrument(skip(self), level = "debug")]
|
2020-01-24 20:05:07 +13:00
|
|
|
pub fn const_eval_resolve(
|
|
|
|
&self,
|
2022-07-22 01:46:43 +00:00
|
|
|
mut param_env: ty::ParamEnv<'tcx>,
|
2022-09-22 12:34:23 +02:00
|
|
|
unevaluated: ty::UnevaluatedConst<'tcx>,
|
2020-01-24 20:05:07 +13:00
|
|
|
span: Option<Span>,
|
2022-02-16 10:56:01 +01:00
|
|
|
) -> EvalToValTreeResult<'tcx> {
|
2022-07-12 07:11:05 +00:00
|
|
|
let mut substs = self.resolve_vars_if_possible(unevaluated.substs);
|
2022-03-21 18:26:53 +01:00
|
|
|
debug!(?substs);
|
2021-10-18 17:36:45 +02:00
|
|
|
|
|
|
|
// Postpone the evaluation of constants whose substs depend on inference
|
|
|
|
// variables
|
2022-07-27 07:27:52 +00:00
|
|
|
let tcx = self.tcx;
|
2022-10-04 09:43:34 +00:00
|
|
|
if substs.has_non_region_infer() {
|
2022-11-24 11:09:15 +00:00
|
|
|
if let Some(ct) = tcx.bound_abstract_const(unevaluated.def)? {
|
|
|
|
let ct = tcx.expand_abstract_consts(ct.subst(tcx, substs));
|
|
|
|
if let Err(e) = ct.error_reported() {
|
|
|
|
return Err(ErrorHandled::Reported(e));
|
|
|
|
} else if ct.has_non_region_infer() || ct.has_non_region_param() {
|
|
|
|
return Err(ErrorHandled::TooGeneric);
|
|
|
|
} else {
|
|
|
|
substs = replace_param_and_infer_substs_with_placeholder(tcx, substs);
|
2022-07-22 01:46:43 +00:00
|
|
|
}
|
2022-11-24 11:09:15 +00:00
|
|
|
} else {
|
|
|
|
substs = InternalSubsts::identity_for_item(tcx, unevaluated.def.did);
|
|
|
|
param_env = tcx.param_env(unevaluated.def.did);
|
2022-07-12 07:11:05 +00:00
|
|
|
}
|
2021-10-18 17:36:45 +02:00
|
|
|
}
|
|
|
|
|
2022-07-27 07:27:52 +00:00
|
|
|
let param_env_erased = tcx.erase_regions(param_env);
|
|
|
|
let substs_erased = tcx.erase_regions(substs);
|
2022-03-21 18:26:53 +01:00
|
|
|
debug!(?param_env_erased);
|
|
|
|
debug!(?substs_erased);
|
2021-11-05 18:11:08 +01:00
|
|
|
|
2022-09-22 12:34:23 +02:00
|
|
|
let unevaluated = ty::UnevaluatedConst { def: unevaluated.def, substs: substs_erased };
|
2020-01-24 20:05:07 +13:00
|
|
|
|
|
|
|
// 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.
|
2022-07-27 07:27:52 +00:00
|
|
|
tcx.const_eval_resolve_for_typeck(param_env_erased, unevaluated, span)
|
2020-01-24 20:05:07 +13:00
|
|
|
}
|
2019-04-30 22:27:33 +01:00
|
|
|
|
2023-03-06 15:07:02 +01:00
|
|
|
/// The returned function is used in a fast path. If it returns `true` the variable is
|
|
|
|
/// unchanged, `false` indicates that the status is unknown.
|
|
|
|
#[inline]
|
|
|
|
pub fn is_ty_infer_var_definitely_unchanged<'a>(
|
|
|
|
&'a self,
|
|
|
|
) -> (impl Fn(TyOrConstInferVar<'tcx>) -> bool + 'a) {
|
|
|
|
// This hoists the borrow/release out of the loop body.
|
|
|
|
let inner = self.inner.try_borrow();
|
|
|
|
|
|
|
|
return move |infer_var: TyOrConstInferVar<'tcx>| match (infer_var, &inner) {
|
|
|
|
(TyOrConstInferVar::Ty(ty_var), Ok(inner)) => {
|
|
|
|
use self::type_variable::TypeVariableValue;
|
|
|
|
|
|
|
|
match inner.try_type_variables_probe_ref(ty_var) {
|
|
|
|
Some(TypeVariableValue::Unknown { .. }) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-21 09:27:08 +02:00
|
|
|
/// `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
|
2020-03-21 08:40:23 +02:00
|
|
|
/// inlined, despite being large, because it has only two call sites that
|
2020-03-21 09:27:08 +02:00
|
|
|
/// 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`.
|
2019-09-17 18:05:57 +10:00
|
|
|
#[inline(always)]
|
2020-03-21 09:27:08 +02:00
|
|
|
pub fn ty_or_const_infer_var_changed(&self, infer_var: TyOrConstInferVar<'tcx>) -> bool {
|
|
|
|
match infer_var {
|
|
|
|
TyOrConstInferVar::Ty(v) => {
|
2019-09-17 18:05:57 +10:00
|
|
|
use self::type_variable::TypeVariableValue;
|
|
|
|
|
2020-03-21 08:40:23 +02:00
|
|
|
// If `inlined_probe` returns a `Known` value, it never equals
|
|
|
|
// `ty::Infer(ty::TyVar(v))`.
|
2020-05-24 15:22:23 +02:00
|
|
|
match self.inner.borrow_mut().type_variables().inlined_probe(v) {
|
2019-09-17 18:05:57 +10:00
|
|
|
TypeVariableValue::Unknown { .. } => false,
|
2019-12-06 15:36:14 +11:00
|
|
|
TypeVariableValue::Known { .. } => true,
|
2019-09-17 18:05:57 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-21 09:27:08 +02:00
|
|
|
TyOrConstInferVar::TyInt(v) => {
|
2020-03-21 08:40:23 +02:00
|
|
|
// 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
|
2019-12-06 15:36:14 +11:00
|
|
|
// `ty::Infer(_)`.
|
2020-05-24 15:22:23 +02:00
|
|
|
self.inner.borrow_mut().int_unification_table().inlined_probe_value(v).is_some()
|
2019-09-17 18:05:57 +10:00
|
|
|
}
|
|
|
|
|
2020-03-21 09:27:08 +02:00
|
|
|
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(_)`.
|
2019-12-06 15:36:14 +11:00
|
|
|
//
|
2019-09-20 11:23:47 +10:00
|
|
|
// Not `inlined_probe_value(v)` because this call site is colder.
|
2020-05-24 15:22:23 +02:00
|
|
|
self.inner.borrow_mut().float_unification_table().probe_value(v).is_some()
|
2019-09-17 18:05:57 +10:00
|
|
|
}
|
|
|
|
|
2020-03-21 09:27:08 +02:00
|
|
|
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.
|
2020-05-24 15:22:23 +02:00
|
|
|
match self.inner.borrow_mut().const_unification_table().probe_value(v).val {
|
2020-03-21 09:27:08 +02:00
|
|
|
ConstVariableValue::Unknown { .. } => false,
|
|
|
|
ConstVariableValue::Known { .. } => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-09 15:08:06 -05:00
|
|
|
impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
2022-12-26 22:41:49 +00:00
|
|
|
/// Processes registered region obliations and resolves regions, reporting
|
|
|
|
/// any errors if any were raised. Prefer using this function over manually
|
|
|
|
/// calling `resolve_regions_and_report_errors`.
|
|
|
|
pub fn check_region_obligations_and_report_errors(
|
|
|
|
&self,
|
|
|
|
generic_param_scope: LocalDefId,
|
|
|
|
outlives_env: &OutlivesEnvironment<'tcx>,
|
|
|
|
) -> Result<(), ErrorGuaranteed> {
|
|
|
|
self.process_registered_region_obligations(
|
|
|
|
outlives_env.region_bound_pairs(),
|
|
|
|
outlives_env.param_env,
|
|
|
|
);
|
|
|
|
|
|
|
|
self.resolve_regions_and_report_errors(generic_param_scope, outlives_env)
|
|
|
|
}
|
|
|
|
|
2022-09-09 15:08:06 -05:00
|
|
|
/// 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`.
|
|
|
|
///
|
|
|
|
/// Make sure to call [`InferCtxt::process_registered_region_obligations`]
|
2022-12-26 22:41:49 +00:00
|
|
|
/// first, or preferably use [`TypeErrCtxt::check_region_obligations_and_report_errors`]
|
2022-09-09 15:08:06 -05:00
|
|
|
/// to do both of these operations together.
|
|
|
|
pub fn resolve_regions_and_report_errors(
|
|
|
|
&self,
|
|
|
|
generic_param_scope: LocalDefId,
|
|
|
|
outlives_env: &OutlivesEnvironment<'tcx>,
|
2022-12-24 21:26:14 +00:00
|
|
|
) -> Result<(), ErrorGuaranteed> {
|
2022-09-09 15:08:06 -05:00
|
|
|
let errors = self.resolve_regions(outlives_env);
|
|
|
|
|
2022-11-18 10:25:32 +00:00
|
|
|
if let None = self.tainted_by_errors() {
|
2022-09-09 15:08:06 -05:00
|
|
|
// As a heuristic, just skip reporting region errors
|
|
|
|
// altogether if other errors have been reported while
|
2022-11-16 20:34:16 +00:00
|
|
|
// this infcx was in use. This is totally hokey but
|
2022-09-09 15:08:06 -05:00
|
|
|
// otherwise we have a hard time separating legit region
|
|
|
|
// errors from silly ones.
|
|
|
|
self.report_region_errors(generic_param_scope, &errors);
|
|
|
|
}
|
2022-12-11 21:16:43 +00:00
|
|
|
|
2022-12-24 21:26:14 +00:00
|
|
|
if errors.is_empty() {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(self
|
|
|
|
.tcx
|
|
|
|
.sess
|
|
|
|
.delay_span_bug(rustc_span::DUMMY_SP, "error should have been emitted"))
|
|
|
|
}
|
2022-09-09 15:08:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// [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.
|
|
|
|
pub fn type_error_struct_with_diag<M>(
|
|
|
|
&self,
|
|
|
|
sp: Span,
|
|
|
|
mk_diag: M,
|
|
|
|
actual_ty: Ty<'tcx>,
|
|
|
|
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed>
|
|
|
|
where
|
|
|
|
M: FnOnce(String) -> DiagnosticBuilder<'tcx, ErrorGuaranteed>,
|
|
|
|
{
|
|
|
|
let actual_ty = self.resolve_vars_if_possible(actual_ty);
|
|
|
|
debug!("type_error_struct_with_diag({:?}, {:?})", sp, actual_ty);
|
|
|
|
|
|
|
|
let mut err = mk_diag(self.ty_to_string(actual_ty));
|
|
|
|
|
|
|
|
// Don't report an error if actual type is `Error`.
|
|
|
|
if actual_ty.references_error() {
|
|
|
|
err.downgrade_to_delayed_bug();
|
|
|
|
}
|
|
|
|
|
|
|
|
err
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn report_mismatched_types(
|
|
|
|
&self,
|
|
|
|
cause: &ObligationCause<'tcx>,
|
|
|
|
expected: Ty<'tcx>,
|
|
|
|
actual: Ty<'tcx>,
|
|
|
|
err: TypeError<'tcx>,
|
|
|
|
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
|
|
|
|
self.report_and_explain_type_error(TypeTrace::types(cause, true, expected, actual), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn report_mismatched_consts(
|
|
|
|
&self,
|
|
|
|
cause: &ObligationCause<'tcx>,
|
|
|
|
expected: ty::Const<'tcx>,
|
|
|
|
actual: ty::Const<'tcx>,
|
|
|
|
err: TypeError<'tcx>,
|
|
|
|
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
|
|
|
|
self.report_and_explain_type_error(TypeTrace::consts(cause, true, expected, actual), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-04 07:38:37 +02:00
|
|
|
/// Helper for [InferCtxt::ty_or_const_infer_var_changed] (see comment on that), currently
|
2020-03-21 09:27:08 +02:00
|
|
|
/// 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>),
|
|
|
|
}
|
|
|
|
|
2021-12-13 16:50:58 -05:00
|
|
|
impl<'tcx> TyOrConstInferVar<'tcx> {
|
2020-03-21 09:27:08 +02:00
|
|
|
/// 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,
|
2019-09-17 18:05:57 +10:00
|
|
|
}
|
|
|
|
}
|
2020-03-21 09:27:08 +02:00
|
|
|
|
|
|
|
/// Tries to extract an inference variable from a type, returns `None`
|
|
|
|
/// for types other than `ty::Infer(_)` (or `InferTy::Fresh*`).
|
2022-09-08 15:18:43 +00:00
|
|
|
fn maybe_from_ty(ty: Ty<'tcx>) -> Option<Self> {
|
2020-08-03 00:49:11 +02:00
|
|
|
match *ty.kind() {
|
2020-03-21 09:27:08 +02:00
|
|
|
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`).
|
2022-09-08 15:18:43 +00:00
|
|
|
fn maybe_from_const(ct: ty::Const<'tcx>) -> Option<Self> {
|
2022-06-10 11:18:06 +10:00
|
|
|
match ct.kind() {
|
2020-03-21 09:27:08 +02:00
|
|
|
ty::ConstKind::Infer(InferConst::Var(v)) => Some(TyOrConstInferVar::Const(v)),
|
|
|
|
_ => None,
|
2019-09-17 18:05:57 +10:00
|
|
|
}
|
|
|
|
}
|
2019-04-30 22:27:33 +01:00
|
|
|
}
|
|
|
|
|
2021-12-15 22:59:32 +00:00
|
|
|
/// Replace `{integer}` with `i32` and `{float}` with `f64`.
|
|
|
|
/// Used only for diagnostics.
|
2022-03-27 02:40:07 +00:00
|
|
|
struct InferenceLiteralEraser<'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2021-12-15 22:59:32 +00:00
|
|
|
}
|
|
|
|
|
2023-02-11 09:13:27 +00:00
|
|
|
impl<'tcx> TypeFolder<TyCtxt<'tcx>> for InferenceLiteralEraser<'tcx> {
|
2023-02-11 09:18:12 +00:00
|
|
|
fn interner(&self) -> TyCtxt<'tcx> {
|
2022-03-27 02:40:07 +00:00
|
|
|
self.tcx
|
2021-12-15 22:59:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
|
|
|
|
match ty.kind() {
|
2022-03-27 02:40:07 +00:00
|
|
|
ty::Infer(ty::IntVar(_) | ty::FreshIntTy(_)) => self.tcx.types.i32,
|
|
|
|
ty::Infer(ty::FloatVar(_) | ty::FreshFloatTy(_)) => self.tcx.types.f64,
|
2021-12-15 22:59:32 +00:00
|
|
|
_ => ty.super_fold_with(self),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-21 08:40:23 +02:00
|
|
|
struct ShallowResolver<'a, 'tcx> {
|
2022-09-09 13:01:06 -05:00
|
|
|
infcx: &'a InferCtxt<'tcx>,
|
2019-04-30 22:27:33 +01:00
|
|
|
}
|
|
|
|
|
2023-02-11 09:13:27 +00:00
|
|
|
impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for ShallowResolver<'a, 'tcx> {
|
2023-02-11 09:18:12 +00:00
|
|
|
fn interner(&self) -> TyCtxt<'tcx> {
|
2019-04-30 22:27:33 +01:00
|
|
|
self.infcx.tcx
|
|
|
|
}
|
|
|
|
|
2022-04-10 12:23:42 -07:00
|
|
|
/// If `ty` 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.
|
2023-02-03 12:36:44 +11:00
|
|
|
#[inline]
|
2021-12-01 00:55:57 +00:00
|
|
|
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
|
2023-02-03 12:36:44 +11:00
|
|
|
if let ty::Infer(v) = ty.kind() { self.fold_infer_ty(*v).unwrap_or(ty) } else { ty }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
|
|
|
|
if let ty::ConstKind::Infer(InferConst::Var(vid)) = ct.kind() {
|
|
|
|
self.infcx
|
|
|
|
.inner
|
|
|
|
.borrow_mut()
|
|
|
|
.const_unification_table()
|
|
|
|
.probe_value(vid)
|
|
|
|
.val
|
|
|
|
.known()
|
|
|
|
.unwrap_or(ct)
|
|
|
|
} else {
|
|
|
|
ct
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> ShallowResolver<'a, 'tcx> {
|
|
|
|
// This is separate from `fold_ty` to keep that method small and inlinable.
|
|
|
|
#[inline(never)]
|
|
|
|
fn fold_infer_ty(&mut self, v: InferTy) -> Option<Ty<'tcx>> {
|
|
|
|
match v {
|
|
|
|
ty::TyVar(v) => {
|
2022-04-10 12:23:42 -07:00
|
|
|
// 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.infcx.inner.borrow_mut().type_variables().probe(v).known();
|
2023-02-03 12:36:44 +11:00
|
|
|
known.map(|t| self.fold_ty(t))
|
2022-04-10 12:23:42 -07:00
|
|
|
}
|
|
|
|
|
2023-02-03 12:36:44 +11:00
|
|
|
ty::IntVar(v) => self
|
2022-04-10 12:23:42 -07:00
|
|
|
.infcx
|
|
|
|
.inner
|
|
|
|
.borrow_mut()
|
|
|
|
.int_unification_table()
|
|
|
|
.probe_value(v)
|
2023-02-03 12:36:44 +11:00
|
|
|
.map(|v| v.to_type(self.infcx.tcx)),
|
2022-04-10 12:23:42 -07:00
|
|
|
|
2023-02-03 12:36:44 +11:00
|
|
|
ty::FloatVar(v) => self
|
2022-04-10 12:23:42 -07:00
|
|
|
.infcx
|
|
|
|
.inner
|
|
|
|
.borrow_mut()
|
|
|
|
.float_unification_table()
|
|
|
|
.probe_value(v)
|
2023-02-03 12:36:44 +11:00
|
|
|
.map(|v| v.to_type(self.infcx.tcx)),
|
2019-04-30 22:27:33 +01:00
|
|
|
|
2023-02-03 12:36:44 +11:00
|
|
|
ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => None,
|
2021-12-01 00:55:57 +00:00
|
|
|
}
|
2019-04-30 22:27:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'tcx> TypeTrace<'tcx> {
|
2013-08-31 18:13:04 +02:00
|
|
|
pub fn span(&self) -> Span {
|
2016-11-07 13:25:06 -05:00
|
|
|
self.cause.span
|
2013-05-23 21:37:37 -04:00
|
|
|
}
|
2014-11-15 20:30:33 -05:00
|
|
|
|
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> {
|
2022-02-07 16:40:16 +00:00
|
|
|
TypeTrace {
|
|
|
|
cause: cause.clone(),
|
|
|
|
values: Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())),
|
|
|
|
}
|
2015-01-16 05:48:15 +02:00
|
|
|
}
|
|
|
|
|
2022-07-30 09:41:19 +00:00
|
|
|
pub fn poly_trait_refs(
|
|
|
|
cause: &ObligationCause<'tcx>,
|
|
|
|
a_is_expected: bool,
|
|
|
|
a: ty::PolyTraitRef<'tcx>,
|
|
|
|
b: ty::PolyTraitRef<'tcx>,
|
|
|
|
) -> TypeTrace<'tcx> {
|
|
|
|
TypeTrace {
|
|
|
|
cause: cause.clone(),
|
2022-12-18 15:01:26 +01:00
|
|
|
values: PolyTraitRefs(ExpectedFound::new(a_is_expected, a, b)),
|
2022-07-30 09:41:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-29 10:03:04 +13:00
|
|
|
pub fn consts(
|
|
|
|
cause: &ObligationCause<'tcx>,
|
|
|
|
a_is_expected: bool,
|
2022-02-02 14:24:45 +11:00
|
|
|
a: ty::Const<'tcx>,
|
|
|
|
b: ty::Const<'tcx>,
|
2020-02-29 10:03:04 +13:00
|
|
|
) -> TypeTrace<'tcx> {
|
2022-02-07 16:40:16 +00:00
|
|
|
TypeTrace {
|
|
|
|
cause: cause.clone(),
|
|
|
|
values: Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())),
|
|
|
|
}
|
2020-02-29 10:03:04 +13:00
|
|
|
}
|
2013-05-23 21:37:37 -04:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
impl<'tcx> SubregionOrigin<'tcx> {
|
2013-08-31 18:13:04 +02:00
|
|
|
pub fn span(&self) -> Span {
|
2013-05-23 21:37:37 -04:00
|
|
|
match *self {
|
2014-04-22 02:21:52 +03:00
|
|
|
Subtype(ref a) => a.span(),
|
2013-05-23 21:37:37 -04:00
|
|
|
RelateObjectBound(a) => a,
|
2021-07-30 08:12:10 -07:00
|
|
|
RelateParamBound(a, ..) => a,
|
2014-08-27 21:46:52 -04:00
|
|
|
RelateRegionParamBound(a) => a,
|
2013-05-23 21:37:37 -04:00
|
|
|
Reborrow(a) => a,
|
|
|
|
ReferenceOutlivesReferent(_, a) => a,
|
2022-07-24 19:33:26 +00:00
|
|
|
CompareImplItemObligation { span, .. } => span,
|
2022-09-16 17:20:11 -04:00
|
|
|
AscribeUserTypeProvePredicate(span) => span,
|
2022-01-08 23:30:19 -05:00
|
|
|
CheckAssociatedTypeBounds { ref parent, .. } => parent.span(),
|
2016-10-05 10:17:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2016-10-05 10:17:14 -04:00
|
|
|
{
|
2021-11-11 12:01:12 +11:00
|
|
|
match *cause.code() {
|
2018-08-29 13:25:47 -04:00
|
|
|
traits::ObligationCauseCode::ReferenceOutlivesReferent(ref_type) => {
|
|
|
|
SubregionOrigin::ReferenceOutlivesReferent(ref_type, cause.span)
|
|
|
|
}
|
|
|
|
|
2022-07-24 19:33:26 +00:00
|
|
|
traits::ObligationCauseCode::CompareImplItemObligation {
|
2021-08-25 13:43:00 -04:00
|
|
|
impl_item_def_id,
|
|
|
|
trait_item_def_id,
|
2022-07-24 19:33:26 +00:00
|
|
|
kind: _,
|
|
|
|
} => SubregionOrigin::CompareImplItemObligation {
|
2021-08-25 13:43:00 -04:00
|
|
|
span: cause.span,
|
|
|
|
impl_item_def_id,
|
|
|
|
trait_item_def_id,
|
|
|
|
},
|
|
|
|
|
2022-01-08 23:30:19 -05:00
|
|
|
traits::ObligationCauseCode::CheckAssociatedTypeBounds {
|
|
|
|
impl_item_def_id,
|
|
|
|
trait_item_def_id,
|
|
|
|
} => SubregionOrigin::CheckAssociatedTypeBounds {
|
|
|
|
impl_item_def_id,
|
|
|
|
trait_item_def_id,
|
|
|
|
parent: Box::new(default()),
|
|
|
|
},
|
|
|
|
|
2022-09-16 17:20:11 -04:00
|
|
|
traits::ObligationCauseCode::AscribeUserTypeProvePredicate(span) => {
|
|
|
|
SubregionOrigin::AscribeUserTypeProvePredicate(span)
|
|
|
|
}
|
|
|
|
|
2016-10-05 10:17:14 -04:00
|
|
|
_ => default(),
|
2013-05-23 21:37:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-10 07:02:27 -04:00
|
|
|
impl RegionVariableOrigin {
|
2013-08-31 18:13:04 +02:00
|
|
|
pub fn span(&self) -> Span {
|
2013-05-23 21:37:37 -04:00
|
|
|
match *self {
|
2020-06-28 15:26:12 -07:00
|
|
|
MiscVariable(a)
|
|
|
|
| PatternRegion(a)
|
|
|
|
| AddrOfRegion(a)
|
2021-11-11 23:15:47 +00:00
|
|
|
| Autoref(a)
|
2020-06-28 15:26:12 -07:00
|
|
|
| Coercion(a)
|
|
|
|
| EarlyBoundRegion(a, ..)
|
|
|
|
| LateBoundRegion(a, ..)
|
|
|
|
| UpvarRegion(_, a) => a,
|
2021-01-28 16:18:25 +09:00
|
|
|
Nll(..) => bug!("NLL variable used with `span`"),
|
2013-05-23 21:37:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-16 19:38:17 +03:00
|
|
|
|
2022-07-26 00:43:29 +00:00
|
|
|
/// Replaces substs that reference param or infer variables with suitable
|
|
|
|
/// placeholders. This function is meant to remove these param and infer
|
|
|
|
/// substs when they're not actually needed to evaluate a constant.
|
|
|
|
fn replace_param_and_infer_substs_with_placeholder<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
substs: SubstsRef<'tcx>,
|
|
|
|
) -> SubstsRef<'tcx> {
|
2022-12-12 14:28:08 +00:00
|
|
|
struct ReplaceParamAndInferWithPlaceholder<'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2023-01-17 03:27:48 +00:00
|
|
|
idx: u32,
|
2022-12-12 14:28:08 +00:00
|
|
|
}
|
|
|
|
|
2023-02-11 09:13:27 +00:00
|
|
|
impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ReplaceParamAndInferWithPlaceholder<'tcx> {
|
2023-02-11 09:18:12 +00:00
|
|
|
fn interner(&self) -> TyCtxt<'tcx> {
|
2022-12-12 14:28:08 +00:00
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
|
|
|
|
if let ty::Infer(_) = t.kind() {
|
2023-02-08 12:28:03 +11:00
|
|
|
self.tcx.mk_placeholder(ty::PlaceholderType {
|
2022-07-26 00:43:29 +00:00
|
|
|
universe: ty::UniverseIndex::ROOT,
|
2023-01-17 03:27:48 +00:00
|
|
|
name: ty::BoundTyKind::Anon({
|
2022-12-12 14:28:08 +00:00
|
|
|
let idx = self.idx;
|
|
|
|
self.idx += 1;
|
|
|
|
idx
|
|
|
|
}),
|
2023-02-08 12:28:03 +11:00
|
|
|
})
|
2022-12-12 14:28:08 +00:00
|
|
|
} else {
|
|
|
|
t.super_fold_with(self)
|
2022-07-26 00:43:29 +00:00
|
|
|
}
|
2022-12-12 14:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_const(&mut self, c: ty::Const<'tcx>) -> ty::Const<'tcx> {
|
|
|
|
if let ty::ConstKind::Infer(_) = c.kind() {
|
|
|
|
let ty = c.ty();
|
|
|
|
// If the type references param or infer then ICE ICE ICE
|
2022-10-04 09:43:34 +00:00
|
|
|
if ty.has_non_region_param() || ty.has_non_region_infer() {
|
2022-12-12 14:28:08 +00:00
|
|
|
bug!("const `{c}`'s type should not reference params or types");
|
2022-07-26 00:43:29 +00:00
|
|
|
}
|
2022-12-12 14:28:08 +00:00
|
|
|
self.tcx.mk_const(
|
2022-11-28 12:28:32 +00:00
|
|
|
ty::PlaceholderConst {
|
2022-07-26 00:43:29 +00:00
|
|
|
universe: ty::UniverseIndex::ROOT,
|
2023-01-17 03:27:48 +00:00
|
|
|
name: ty::BoundVar::from_u32({
|
2022-12-12 14:28:08 +00:00
|
|
|
let idx = self.idx;
|
|
|
|
self.idx += 1;
|
|
|
|
idx
|
|
|
|
}),
|
2022-11-28 12:28:32 +00:00
|
|
|
},
|
2022-11-04 20:33:32 +00:00
|
|
|
ty,
|
|
|
|
)
|
2022-12-12 14:28:08 +00:00
|
|
|
} else {
|
|
|
|
c.super_fold_with(self)
|
2022-07-26 00:43:29 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-12 14:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
substs.fold_with(&mut ReplaceParamAndInferWithPlaceholder { tcx, idx: 0 })
|
2022-07-26 00:43:29 +00:00
|
|
|
}
|