2019-05-31 21:52:35 +00:00
|
|
|
use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
|
2019-10-18 22:57:48 +00:00
|
|
|
use super::{FixupError, FixupResult, InferCtxt, Span};
|
2022-11-23 19:38:22 +00:00
|
|
|
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
|
2022-06-17 12:15:00 +00:00
|
|
|
use rustc_middle::ty::fold::{FallibleTypeFolder, TypeFolder, TypeSuperFoldable};
|
|
|
|
use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitor};
|
|
|
|
use rustc_middle::ty::{self, Const, InferConst, Ty, TyCtxt, TypeFoldable, TypeVisitable};
|
2012-08-13 22:06:13 +00:00
|
|
|
|
2020-10-21 12:24:35 +00:00
|
|
|
use std::ops::ControlFlow;
|
|
|
|
|
2014-12-01 17:27:27 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2019-05-28 19:25:21 +00:00
|
|
|
// OPPORTUNISTIC VAR RESOLVER
|
2012-08-13 22:06:13 +00:00
|
|
|
|
2019-05-28 19:25:21 +00:00
|
|
|
/// The opportunistic resolver can be used at any time. It simply replaces
|
2019-05-11 18:08:26 +00:00
|
|
|
/// type/const variables that have been unified with the things they have
|
2019-04-30 21:27:33 +00:00
|
|
|
/// been unified with (similar to `shallow_resolve`, but deep). This is
|
2014-12-01 17:27:27 +00:00
|
|
|
/// useful for printing messages etc but also required at various
|
|
|
|
/// points for correctness.
|
2019-06-14 16:39:39 +00:00
|
|
|
pub struct OpportunisticVarResolver<'a, 'tcx> {
|
2023-02-02 22:00:56 +00:00
|
|
|
// The shallow resolver is used to resolve inference variables at every
|
|
|
|
// level of the type.
|
|
|
|
shallow_resolver: crate::infer::ShallowResolver<'a, 'tcx>,
|
2012-08-13 22:06:13 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
impl<'a, 'tcx> OpportunisticVarResolver<'a, 'tcx> {
|
2018-11-29 20:13:04 +00:00
|
|
|
#[inline]
|
2022-09-09 18:01:06 +00:00
|
|
|
pub fn new(infcx: &'a InferCtxt<'tcx>) -> Self {
|
2023-02-02 22:00:56 +00:00
|
|
|
OpportunisticVarResolver { shallow_resolver: crate::infer::ShallowResolver { infcx } }
|
2013-01-08 22:00:45 +00:00
|
|
|
}
|
2012-08-13 22:06:13 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticVarResolver<'a, 'tcx> {
|
|
|
|
fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
|
2023-02-02 22:00:56 +00:00
|
|
|
TypeFolder::tcx(&self.shallow_resolver)
|
2013-10-29 10:03:32 +00:00
|
|
|
}
|
|
|
|
|
2021-12-01 00:55:57 +00:00
|
|
|
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
|
2022-10-04 09:43:34 +00:00
|
|
|
if !t.has_non_region_infer() {
|
2021-12-01 00:55:57 +00:00
|
|
|
t // micro-optimize -- if there is nothing in this type that this fold affects...
|
2014-12-01 17:27:27 +00:00
|
|
|
} else {
|
2023-02-02 22:00:56 +00:00
|
|
|
let t = self.shallow_resolver.fold_ty(t);
|
2019-05-11 17:54:14 +00:00
|
|
|
t.super_fold_with(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-02 03:24:45 +00:00
|
|
|
fn fold_const(&mut self, ct: Const<'tcx>) -> Const<'tcx> {
|
2022-10-04 09:43:34 +00:00
|
|
|
if !ct.has_non_region_infer() {
|
2021-12-01 00:55:57 +00:00
|
|
|
ct // micro-optimize -- if there is nothing in this const that this fold affects...
|
2019-05-11 17:54:14 +00:00
|
|
|
} else {
|
2023-02-02 22:00:56 +00:00
|
|
|
let ct = self.shallow_resolver.fold_const(ct);
|
2019-05-11 17:54:14 +00:00
|
|
|
ct.super_fold_with(self)
|
2012-11-08 02:40:34 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-13 22:06:13 +00:00
|
|
|
}
|
2014-12-01 15:11:59 +00:00
|
|
|
|
2020-06-16 17:27:40 +00:00
|
|
|
/// The opportunistic region resolver opportunistically resolves regions
|
|
|
|
/// variables to the variable with the least variable id. It is used when
|
2022-03-30 19:14:15 +00:00
|
|
|
/// normalizing projections to avoid hitting the recursion limit by creating
|
2020-06-16 17:27:40 +00:00
|
|
|
/// many versions of a predicate for types that in the end have to unify.
|
|
|
|
///
|
|
|
|
/// If you want to resolve type and const variables as well, call
|
|
|
|
/// [InferCtxt::resolve_vars_if_possible] first.
|
|
|
|
pub struct OpportunisticRegionResolver<'a, 'tcx> {
|
2022-09-09 18:01:06 +00:00
|
|
|
infcx: &'a InferCtxt<'tcx>,
|
2015-12-06 18:21:23 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 17:27:40 +00:00
|
|
|
impl<'a, 'tcx> OpportunisticRegionResolver<'a, 'tcx> {
|
2022-09-09 18:01:06 +00:00
|
|
|
pub fn new(infcx: &'a InferCtxt<'tcx>) -> Self {
|
2020-06-16 17:27:40 +00:00
|
|
|
OpportunisticRegionResolver { infcx }
|
2015-12-06 18:21:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-16 17:27:40 +00:00
|
|
|
impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticRegionResolver<'a, 'tcx> {
|
2019-06-13 21:48:52 +00:00
|
|
|
fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
|
2015-12-06 18:21:23 +00:00
|
|
|
self.infcx.tcx
|
|
|
|
}
|
|
|
|
|
2021-12-01 00:55:57 +00:00
|
|
|
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
|
2020-06-16 17:27:40 +00:00
|
|
|
if !t.has_infer_regions() {
|
2021-12-01 00:55:57 +00:00
|
|
|
t // micro-optimize -- if there is nothing in this type that this fold affects...
|
2015-12-06 18:21:23 +00:00
|
|
|
} else {
|
2020-06-16 17:27:40 +00:00
|
|
|
t.super_fold_with(self)
|
2015-12-06 18:21:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-01 00:55:57 +00:00
|
|
|
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
|
|
|
|
match *r {
|
2020-06-16 17:27:40 +00:00
|
|
|
ty::ReVar(rid) => {
|
|
|
|
let resolved = self
|
|
|
|
.infcx
|
|
|
|
.inner
|
|
|
|
.borrow_mut()
|
|
|
|
.unwrap_region_constraints()
|
|
|
|
.opportunistic_resolve_var(rid);
|
2022-06-20 20:10:43 +00:00
|
|
|
TypeFolder::tcx(self).reuse_or_mk_region(r, ty::ReVar(resolved))
|
2020-06-16 17:27:40 +00:00
|
|
|
}
|
2017-11-05 11:25:23 +00:00
|
|
|
_ => r,
|
2021-12-01 00:55:57 +00:00
|
|
|
}
|
2015-12-06 18:21:23 +00:00
|
|
|
}
|
2019-03-08 01:20:33 +00:00
|
|
|
|
2022-02-02 03:24:45 +00:00
|
|
|
fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
|
2020-06-16 17:27:40 +00:00
|
|
|
if !ct.has_infer_regions() {
|
2021-12-01 00:55:57 +00:00
|
|
|
ct // micro-optimize -- if there is nothing in this const that this fold affects...
|
2019-03-08 01:20:33 +00:00
|
|
|
} else {
|
2020-06-16 17:27:40 +00:00
|
|
|
ct.super_fold_with(self)
|
2019-03-08 01:20:33 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-06 18:21:23 +00:00
|
|
|
}
|
|
|
|
|
2017-09-02 12:35:07 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// UNRESOLVED TYPE FINDER
|
|
|
|
|
2019-03-11 18:46:20 +00:00
|
|
|
/// The unresolved type **finder** walks a type searching for
|
|
|
|
/// type variables that don't yet have a value. The first unresolved type is stored.
|
|
|
|
/// It does not construct the fully resolved type (which might
|
2017-09-02 12:35:07 +00:00
|
|
|
/// involve some hashing and so forth).
|
2022-11-23 19:38:22 +00:00
|
|
|
pub struct UnresolvedTypeOrConstFinder<'a, 'tcx> {
|
2022-09-09 18:01:06 +00:00
|
|
|
infcx: &'a InferCtxt<'tcx>,
|
2017-09-02 12:35:07 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 19:38:22 +00:00
|
|
|
impl<'a, 'tcx> UnresolvedTypeOrConstFinder<'a, 'tcx> {
|
2022-09-09 18:01:06 +00:00
|
|
|
pub fn new(infcx: &'a InferCtxt<'tcx>) -> Self {
|
2022-11-23 19:38:22 +00:00
|
|
|
UnresolvedTypeOrConstFinder { infcx }
|
2017-09-02 12:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 19:38:22 +00:00
|
|
|
impl<'a, 'tcx> TypeVisitor<'tcx> for UnresolvedTypeOrConstFinder<'a, 'tcx> {
|
|
|
|
type BreakTy = (ty::Term<'tcx>, Option<Span>);
|
2020-11-05 16:30:39 +00:00
|
|
|
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2019-04-30 21:27:33 +00:00
|
|
|
let t = self.infcx.shallow_resolve(t);
|
2022-11-23 19:38:22 +00:00
|
|
|
if let ty::Infer(infer_ty) = *t.kind() {
|
|
|
|
// Since we called `shallow_resolve` above, this must
|
|
|
|
// be an (as yet...) unresolved inference variable.
|
|
|
|
let ty_var_span = if let ty::TyVar(ty_vid) = infer_ty {
|
|
|
|
let mut inner = self.infcx.inner.borrow_mut();
|
|
|
|
let ty_vars = &inner.type_variables();
|
|
|
|
if let TypeVariableOrigin {
|
|
|
|
kind: TypeVariableOriginKind::TypeParameterDefinition(_, _),
|
|
|
|
span,
|
|
|
|
} = *ty_vars.var_origin(ty_vid)
|
|
|
|
{
|
|
|
|
Some(span)
|
2019-03-11 18:46:20 +00:00
|
|
|
} else {
|
|
|
|
None
|
2022-11-23 19:38:22 +00:00
|
|
|
}
|
2017-09-02 12:35:07 +00:00
|
|
|
} else {
|
2022-11-23 19:38:22 +00:00
|
|
|
None
|
|
|
|
};
|
|
|
|
ControlFlow::Break((t.into(), ty_var_span))
|
|
|
|
} else if !t.has_non_region_infer() {
|
|
|
|
// All const/type variables in inference types must already be resolved,
|
|
|
|
// no need to visit the contents.
|
2023-01-18 07:17:13 +00:00
|
|
|
ControlFlow::Continue(())
|
2017-09-02 12:35:07 +00:00
|
|
|
} else {
|
2022-11-23 19:38:22 +00:00
|
|
|
// Otherwise, keep visiting.
|
|
|
|
t.super_visit_with(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_const(&mut self, ct: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
|
|
|
|
let ct = self.infcx.shallow_resolve(ct);
|
|
|
|
if let ty::ConstKind::Infer(i) = ct.kind() {
|
|
|
|
// Since we called `shallow_resolve` above, this must
|
|
|
|
// be an (as yet...) unresolved inference variable.
|
|
|
|
let ct_var_span = if let ty::InferConst::Var(vid) = i {
|
|
|
|
let mut inner = self.infcx.inner.borrow_mut();
|
|
|
|
let ct_vars = &mut inner.const_unification_table();
|
|
|
|
if let ConstVariableOrigin {
|
|
|
|
span,
|
|
|
|
kind: ConstVariableOriginKind::ConstParameterDefinition(_, _),
|
|
|
|
} = ct_vars.probe_value(vid).origin
|
|
|
|
{
|
|
|
|
Some(span)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
ControlFlow::Break((ct.into(), ct_var_span))
|
|
|
|
} else if !ct.has_non_region_infer() {
|
|
|
|
// All const/type variables in inference types must already be resolved,
|
|
|
|
// no need to visit the contents.
|
2023-01-18 07:17:13 +00:00
|
|
|
ControlFlow::Continue(())
|
2022-11-23 19:38:22 +00:00
|
|
|
} else {
|
|
|
|
// Otherwise, keep visiting.
|
|
|
|
ct.super_visit_with(self)
|
2017-09-02 12:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-01 15:11:59 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2014-12-01 17:27:27 +00:00
|
|
|
// FULL TYPE RESOLUTION
|
|
|
|
|
|
|
|
/// Full type resolution replaces all type and region variables with
|
|
|
|
/// their concrete results. If any variable cannot be replaced (never unified, etc)
|
|
|
|
/// then an `Err` result is returned.
|
2022-09-09 18:01:06 +00:00
|
|
|
pub fn fully_resolve<'tcx, T>(infcx: &InferCtxt<'tcx>, value: T) -> FixupResult<'tcx, T>
|
2019-06-13 22:32:15 +00:00
|
|
|
where
|
|
|
|
T: TypeFoldable<'tcx>,
|
2014-12-01 17:27:27 +00:00
|
|
|
{
|
2021-12-01 00:55:57 +00:00
|
|
|
value.try_fold_with(&mut FullTypeResolver { infcx })
|
2014-12-01 15:11:59 +00:00
|
|
|
}
|
|
|
|
|
2014-12-01 17:27:27 +00:00
|
|
|
// N.B. This type is not public because the protocol around checking the
|
2020-03-06 11:13:55 +00:00
|
|
|
// `err` field is not enforceable otherwise.
|
2019-06-14 16:39:39 +00:00
|
|
|
struct FullTypeResolver<'a, 'tcx> {
|
2022-09-09 18:01:06 +00:00
|
|
|
infcx: &'a InferCtxt<'tcx>,
|
2014-12-01 15:11:59 +00:00
|
|
|
}
|
|
|
|
|
2022-06-20 20:10:43 +00:00
|
|
|
impl<'a, 'tcx> FallibleTypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> {
|
2021-05-19 21:08:32 +00:00
|
|
|
type Error = FixupError<'tcx>;
|
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
|
2014-12-01 15:11:59 +00:00
|
|
|
self.infcx.tcx
|
|
|
|
}
|
|
|
|
|
2021-12-01 00:55:57 +00:00
|
|
|
fn try_fold_ty(&mut self, t: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
|
2020-04-06 20:29:18 +00:00
|
|
|
if !t.needs_infer() {
|
2021-05-19 13:01:30 +00:00
|
|
|
Ok(t) // micro-optimize -- if there is nothing in this type that this fold affects...
|
2014-12-01 15:11:59 +00:00
|
|
|
} else {
|
2019-04-30 21:27:33 +00:00
|
|
|
let t = self.infcx.shallow_resolve(t);
|
2020-08-02 22:49:11 +00:00
|
|
|
match *t.kind() {
|
2021-05-19 21:08:32 +00:00
|
|
|
ty::Infer(ty::TyVar(vid)) => Err(FixupError::UnresolvedTy(vid)),
|
|
|
|
ty::Infer(ty::IntVar(vid)) => Err(FixupError::UnresolvedIntTy(vid)),
|
|
|
|
ty::Infer(ty::FloatVar(vid)) => Err(FixupError::UnresolvedFloatTy(vid)),
|
2018-08-22 00:35:02 +00:00
|
|
|
ty::Infer(_) => {
|
2016-03-25 00:14:29 +00:00
|
|
|
bug!("Unexpected type in full type resolver: {:?}", t);
|
2014-12-01 17:27:27 +00:00
|
|
|
}
|
2021-12-01 00:55:57 +00:00
|
|
|
_ => t.try_super_fold_with(self),
|
2014-12-01 17:27:27 +00:00
|
|
|
}
|
2014-12-01 15:11:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-01 00:55:57 +00:00
|
|
|
fn try_fold_region(&mut self, r: ty::Region<'tcx>) -> Result<ty::Region<'tcx>, Self::Error> {
|
2016-08-25 20:58:52 +00:00
|
|
|
match *r {
|
2022-06-28 04:08:35 +00:00
|
|
|
ty::ReVar(_) => Ok(self
|
2017-11-05 10:48:16 +00:00
|
|
|
.infcx
|
|
|
|
.lexical_region_resolutions
|
|
|
|
.borrow()
|
|
|
|
.as_ref()
|
|
|
|
.expect("region resolution not performed")
|
2022-06-28 04:08:35 +00:00
|
|
|
.resolve_region(self.infcx.tcx, r)),
|
2021-05-19 13:01:30 +00:00
|
|
|
_ => Ok(r),
|
2014-12-01 17:27:27 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-08 01:20:33 +00:00
|
|
|
|
2022-02-02 03:24:45 +00:00
|
|
|
fn try_fold_const(&mut self, c: ty::Const<'tcx>) -> Result<ty::Const<'tcx>, Self::Error> {
|
2020-04-06 20:29:18 +00:00
|
|
|
if !c.needs_infer() {
|
2021-05-19 13:01:30 +00:00
|
|
|
Ok(c) // micro-optimize -- if there is nothing in this const that this fold affects...
|
2019-03-08 01:20:33 +00:00
|
|
|
} else {
|
2019-04-30 21:27:33 +00:00
|
|
|
let c = self.infcx.shallow_resolve(c);
|
2022-06-10 01:18:06 +00:00
|
|
|
match c.kind() {
|
2019-11-08 20:32:56 +00:00
|
|
|
ty::ConstKind::Infer(InferConst::Var(vid)) => {
|
2021-05-19 21:08:32 +00:00
|
|
|
return Err(FixupError::UnresolvedConst(vid));
|
2019-03-18 20:55:19 +00:00
|
|
|
}
|
2019-11-08 20:32:56 +00:00
|
|
|
ty::ConstKind::Infer(InferConst::Fresh(_)) => {
|
2019-03-18 20:55:19 +00:00
|
|
|
bug!("Unexpected const in full const resolver: {:?}", c);
|
2019-03-08 01:20:33 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2021-12-01 00:55:57 +00:00
|
|
|
c.try_super_fold_with(self)
|
2019-03-08 01:20:33 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-01 17:27:27 +00:00
|
|
|
}
|