2024-05-08 08:06:19 +00:00
|
|
|
use rustc_middle::bug;
|
2023-02-22 02:18:40 +00:00
|
|
|
use rustc_middle::ty::fold::{FallibleTypeFolder, TypeFolder, TypeSuperFoldable};
|
2024-01-16 10:06:42 +00:00
|
|
|
use rustc_middle::ty::visit::TypeVisitableExt;
|
2023-02-09 14:02:47 +00:00
|
|
|
use rustc_middle::ty::{self, Const, InferConst, Ty, TyCtxt, TypeFoldable};
|
2024-09-30 08:18:55 +00:00
|
|
|
use rustc_type_ir::data_structures::DelayedMap;
|
2012-08-13 22:06:13 +00:00
|
|
|
|
2024-01-16 10:06:42 +00:00
|
|
|
use super::{FixupError, FixupResult, InferCtxt};
|
2024-07-28 22:13:50 +00:00
|
|
|
|
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> {
|
2024-04-06 06:05:17 +00:00
|
|
|
infcx: &'a InferCtxt<'tcx>,
|
2024-09-30 08:18:55 +00:00
|
|
|
/// We're able to use a cache here as the folder does
|
|
|
|
/// not have any mutable state.
|
|
|
|
cache: DelayedMap<Ty<'tcx>, Ty<'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 {
|
2024-09-30 08:18:55 +00:00
|
|
|
OpportunisticVarResolver { infcx, cache: Default::default() }
|
2013-01-08 22:00:45 +00:00
|
|
|
}
|
2012-08-13 22:06:13 +00:00
|
|
|
}
|
|
|
|
|
2023-02-11 09:13:27 +00:00
|
|
|
impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for OpportunisticVarResolver<'a, 'tcx> {
|
2024-06-18 23:13:54 +00:00
|
|
|
fn cx(&self) -> TyCtxt<'tcx> {
|
2024-04-06 06:05:17 +00:00
|
|
|
self.infcx.tcx
|
2013-10-29 10:03:32 +00:00
|
|
|
}
|
|
|
|
|
2023-02-02 23:31:11 +00:00
|
|
|
#[inline]
|
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...
|
2024-09-30 08:18:55 +00:00
|
|
|
} else if let Some(&ty) = self.cache.get(&t) {
|
|
|
|
return ty;
|
2014-12-01 17:27:27 +00:00
|
|
|
} else {
|
2024-09-30 08:18:55 +00:00
|
|
|
let shallow = self.infcx.shallow_resolve(t);
|
|
|
|
let res = shallow.super_fold_with(self);
|
|
|
|
assert!(self.cache.insert(t, res));
|
|
|
|
res
|
2019-05-11 17:54:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2024-04-06 06:05:17 +00:00
|
|
|
let ct = self.infcx.shallow_resolve_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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-11 09:13:27 +00:00
|
|
|
impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for OpportunisticRegionResolver<'a, 'tcx> {
|
2024-06-18 23:13:54 +00:00
|
|
|
fn cx(&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 {
|
2023-02-16 11:40:53 +00:00
|
|
|
ty::ReVar(vid) => self
|
|
|
|
.infcx
|
|
|
|
.inner
|
|
|
|
.borrow_mut()
|
|
|
|
.unwrap_region_constraints()
|
2024-06-18 23:13:54 +00:00
|
|
|
.opportunistic_resolve_var(TypeFolder::cx(self), vid),
|
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
|
|
|
}
|
|
|
|
|
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.
|
2023-10-24 20:13:36 +00:00
|
|
|
pub fn fully_resolve<'tcx, T>(infcx: &InferCtxt<'tcx>, value: T) -> FixupResult<T>
|
2019-06-13 22:32:15 +00:00
|
|
|
where
|
2023-02-22 02:18:40 +00:00
|
|
|
T: TypeFoldable<TyCtxt<'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
|
|
|
}
|
|
|
|
|
2023-02-11 09:13:27 +00:00
|
|
|
impl<'a, 'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for FullTypeResolver<'a, 'tcx> {
|
2023-10-24 20:13:36 +00:00
|
|
|
type Error = FixupError;
|
2021-05-19 21:08:32 +00:00
|
|
|
|
2024-06-18 23:13:54 +00:00
|
|
|
fn cx(&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> {
|
2023-04-27 07:34:11 +00:00
|
|
|
if !t.has_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> {
|
2023-04-27 07:34:11 +00:00
|
|
|
if !c.has_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 {
|
2024-04-06 06:05:17 +00:00
|
|
|
let c = self.infcx.shallow_resolve_const(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
|
|
|
}
|
2024-06-01 18:59:13 +00:00
|
|
|
ty::ConstKind::Infer(InferConst::EffectVar(evid)) => {
|
|
|
|
return Err(FixupError::UnresolvedEffect(evid));
|
|
|
|
}
|
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
|
|
|
}
|