Skip to content

Remove indices from fresh ty/const vars. #144581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions compiler/rustc_const_eval/src/const_eval/valtrees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ fn const_to_valtree_inner<'tcx>(
ty::Never
| ty::Error(_)
| ty::Foreign(..)
| ty::Infer(ty::FreshIntTy(_))
| ty::Infer(ty::FreshFloatTy(_))
| ty::Infer(ty::FreshIntTy)
| ty::Infer(ty::FreshFloatTy)
// FIXME(oli-obk): we could look behind opaque types
| ty::Alias(..)
| ty::Param(_)
Expand Down Expand Up @@ -326,8 +326,8 @@ pub fn valtree_to_const_value<'tcx>(
ty::Never
| ty::Error(_)
| ty::Foreign(..)
| ty::Infer(ty::FreshIntTy(_))
| ty::Infer(ty::FreshFloatTy(_))
| ty::Infer(ty::FreshIntTy)
| ty::Infer(ty::FreshFloatTy)
| ty::Alias(..)
| ty::Param(_)
| ty::Bound(..)
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_const_eval/src/interpret/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {

ty::Infer(ty::TyVar(_)) => false,

ty::Bound(..)
| ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
ty::Bound(..) | ty::Infer(ty::FreshTy | ty::FreshIntTy | ty::FreshFloatTy) => {
bug!("`is_very_trivially_sized` applied to unexpected type: {}", ty)
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
ty::TyVar(_) => self.next_ty_var(DUMMY_SP),
ty::IntVar(_) => self.next_int_var(),
ty::FloatVar(_) => self.next_float_var(),
ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => {
ty::FreshTy | ty::FreshIntTy | ty::FreshFloatTy => {
bug!("unexpected fresh ty outside of the trait solver")
}
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_infer/src/infer/canonical/canonicalizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ impl<'cx, 'tcx> TypeFolder<TyCtxt<'tcx>> for Canonicalizer<'cx, 'tcx> {
}
}

ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
ty::Infer(ty::FreshTy | ty::FreshIntTy | ty::FreshFloatTy) => {
bug!("encountered a fresh type during canonicalization")
}

Expand Down Expand Up @@ -470,7 +470,7 @@ impl<'cx, 'tcx> TypeFolder<TyCtxt<'tcx>> for Canonicalizer<'cx, 'tcx> {
}
}
}
ty::ConstKind::Infer(InferConst::Fresh(_)) => {
ty::ConstKind::Infer(InferConst::Fresh) => {
bug!("encountered a fresh const during canonicalization")
}
ty::ConstKind::Bound(debruijn, _) => {
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_infer/src/infer/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ impl<'tcx> rustc_type_ir::InferCtxtLike for InferCtxt<'tcx> {
if inner.float_unification_table().find(vid) == vid
)
}
ty::InferTy::FreshTy(_)
| ty::InferTy::FreshIntTy(_)
| ty::InferTy::FreshFloatTy(_) => true,
ty::InferTy::FreshTy
| ty::InferTy::FreshIntTy
| ty::InferTy::FreshFloatTy => true,
}
} else {
true
Expand All @@ -131,7 +131,7 @@ impl<'tcx> rustc_type_ir::InferCtxtLike for InferCtxt<'tcx> {
ty::InferConst::Var(vid) => !self
.probe_const_var(vid)
.is_err_and(|_| self.root_const_var(vid) == vid),
ty::InferConst::Fresh(_) => true,
ty::InferConst::Fresh => true,
}
} else {
true
Expand Down
135 changes: 25 additions & 110 deletions compiler/rustc_infer/src/infer/freshen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
//! variable only once, and it does so as soon as it can, so it is reasonable to ask what the type
//! inferencer knows "so far".

use std::collections::hash_map::Entry;

use rustc_data_structures::fx::FxHashMap;
use rustc_middle::bug;
use rustc_middle::ty::{
self, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt,
Expand All @@ -43,63 +40,11 @@ use super::InferCtxt;

pub struct TypeFreshener<'a, 'tcx> {
infcx: &'a InferCtxt<'tcx>,
ty_freshen_count: u32,
const_freshen_count: u32,
ty_freshen_map: FxHashMap<ty::InferTy, Ty<'tcx>>,
const_freshen_map: FxHashMap<ty::InferConst, ty::Const<'tcx>>,
}

impl<'a, 'tcx> TypeFreshener<'a, 'tcx> {
pub fn new(infcx: &'a InferCtxt<'tcx>) -> TypeFreshener<'a, 'tcx> {
TypeFreshener {
infcx,
ty_freshen_count: 0,
const_freshen_count: 0,
ty_freshen_map: Default::default(),
const_freshen_map: Default::default(),
}
}

fn freshen_ty<F>(&mut self, input: Result<Ty<'tcx>, ty::InferTy>, mk_fresh: F) -> Ty<'tcx>
where
F: FnOnce(u32) -> Ty<'tcx>,
{
match input {
Ok(ty) => ty.fold_with(self),
Err(key) => match self.ty_freshen_map.entry(key) {
Entry::Occupied(entry) => *entry.get(),
Entry::Vacant(entry) => {
let index = self.ty_freshen_count;
self.ty_freshen_count += 1;
let t = mk_fresh(index);
entry.insert(t);
t
}
},
}
}

fn freshen_const<F>(
&mut self,
input: Result<ty::Const<'tcx>, ty::InferConst>,
freshener: F,
) -> ty::Const<'tcx>
where
F: FnOnce(u32) -> ty::InferConst,
{
match input {
Ok(ct) => ct.fold_with(self),
Err(key) => match self.const_freshen_map.entry(key) {
Entry::Occupied(entry) => *entry.get(),
Entry::Vacant(entry) => {
let index = self.const_freshen_count;
self.const_freshen_count += 1;
let ct = ty::Const::new_infer(self.infcx.tcx, freshener(index));
entry.insert(ct);
ct
}
},
}
TypeFreshener { infcx }
}
}

Expand Down Expand Up @@ -145,25 +90,14 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for TypeFreshener<'a, 'tcx> {
fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
match ct.kind() {
ty::ConstKind::Infer(ty::InferConst::Var(v)) => {
let mut inner = self.infcx.inner.borrow_mut();
let input =
inner.const_unification_table().probe_value(v).known().ok_or_else(|| {
ty::InferConst::Var(inner.const_unification_table().find(v).vid)
});
drop(inner);
self.freshen_const(input, ty::InferConst::Fresh)
}
ty::ConstKind::Infer(ty::InferConst::Fresh(i)) => {
if i >= self.const_freshen_count {
bug!(
"Encountered a freshend const with id {} \
but our counter is only at {}",
i,
self.const_freshen_count,
);
self.infcx.inner.borrow_mut().const_unification_table().probe_value(v).known();
match input {
Some(ct) => ct.fold_with(self),
None => self.infcx.tcx.consts.fresh_const,
}
ct
}
ty::ConstKind::Infer(ty::InferConst::Fresh) => ct,

ty::ConstKind::Bound(..) | ty::ConstKind::Placeholder(_) => {
bug!("unexpected const {:?}", ct)
Expand All @@ -184,54 +118,35 @@ impl<'a, 'tcx> TypeFreshener<'a, 'tcx> {
fn fold_infer_ty(&mut self, v: ty::InferTy) -> Option<Ty<'tcx>> {
match v {
ty::TyVar(v) => {
let mut inner = self.infcx.inner.borrow_mut();
let input = inner
.type_variables()
.probe(v)
.known()
.ok_or_else(|| ty::TyVar(inner.type_variables().root_var(v)));
drop(inner);
Some(self.freshen_ty(input, |n| Ty::new_fresh(self.infcx.tcx, n)))
let value = self.infcx.inner.borrow_mut().type_variables().probe(v).known();
Some(match value {
Some(ty) => ty.fold_with(self),
None => self.infcx.tcx.types.fresh_ty,
})
}

ty::IntVar(v) => {
let mut inner = self.infcx.inner.borrow_mut();
let value = inner.int_unification_table().probe_value(v);
let input = match value {
ty::IntVarValue::IntType(ty) => Ok(Ty::new_int(self.infcx.tcx, ty)),
ty::IntVarValue::UintType(ty) => Ok(Ty::new_uint(self.infcx.tcx, ty)),
ty::IntVarValue::Unknown => {
Err(ty::IntVar(inner.int_unification_table().find(v)))
let value = self.infcx.inner.borrow_mut().int_unification_table().probe_value(v);
Some(match value {
ty::IntVarValue::IntType(ty) => Ty::new_int(self.infcx.tcx, ty).fold_with(self),
ty::IntVarValue::UintType(ty) => {
Ty::new_uint(self.infcx.tcx, ty).fold_with(self)
}
};
drop(inner);
Some(self.freshen_ty(input, |n| Ty::new_fresh_int(self.infcx.tcx, n)))
ty::IntVarValue::Unknown => self.infcx.tcx.types.fresh_int_ty,
})
}

ty::FloatVar(v) => {
let mut inner = self.infcx.inner.borrow_mut();
let value = inner.float_unification_table().probe_value(v);
let input = match value {
ty::FloatVarValue::Known(ty) => Ok(Ty::new_float(self.infcx.tcx, ty)),
ty::FloatVarValue::Unknown => {
Err(ty::FloatVar(inner.float_unification_table().find(v)))
let value = self.infcx.inner.borrow_mut().float_unification_table().probe_value(v);
Some(match value {
ty::FloatVarValue::Known(ty) => {
Ty::new_float(self.infcx.tcx, ty).fold_with(self)
}
};
drop(inner);
Some(self.freshen_ty(input, |n| Ty::new_fresh_float(self.infcx.tcx, n)))
ty::FloatVarValue::Unknown => self.infcx.tcx.types.fresh_float_ty,
})
}

ty::FreshTy(ct) | ty::FreshIntTy(ct) | ty::FreshFloatTy(ct) => {
if ct >= self.ty_freshen_count {
bug!(
"Encountered a freshend type with id {} \
but our counter is only at {}",
ct,
self.ty_freshen_count
);
}
None
}
ty::FreshTy | ty::FreshIntTy | ty::FreshFloatTy => None,
}
}
}
8 changes: 4 additions & 4 deletions compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,7 @@ impl<'tcx> InferCtxt<'tcx> {
}
}

ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => ty,
ty::FreshTy | ty::FreshIntTy | ty::FreshFloatTy => ty,
}
} else {
ty
Expand All @@ -1045,7 +1045,7 @@ impl<'tcx> InferCtxt<'tcx> {
.probe_value(vid)
.known()
.unwrap_or(ct),
InferConst::Fresh(_) => ct,
InferConst::Fresh => ct,
},
ty::ConstKind::Param(_)
| ty::ConstKind::Bound(_, _)
Expand Down Expand Up @@ -1448,8 +1448,8 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for InferenceLiteralEraser<'tcx> {

fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
match ty.kind() {
ty::Infer(ty::IntVar(_) | ty::FreshIntTy(_)) => self.tcx.types.i32,
ty::Infer(ty::FloatVar(_) | ty::FreshFloatTy(_)) => self.tcx.types.f64,
ty::Infer(ty::IntVar(_) | ty::FreshIntTy) => self.tcx.types.i32,
ty::Infer(ty::FloatVar(_) | ty::FreshFloatTy) => self.tcx.types.f64,
_ => ty.super_fold_with(self),
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_infer/src/infer/relate/generalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for Generalizer<'_, 'tcx> {
// subtyping. This is basically our "occurs check", preventing
// us from creating infinitely sized types.
let g = match *t.kind() {
ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
ty::Infer(ty::FreshTy | ty::FreshIntTy | ty::FreshFloatTy) => {
bug!("unexpected infer type: {t}")
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_infer/src/infer/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl<'a, 'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for FullTypeResolver<'a, 'tcx> {
ty::ConstKind::Infer(InferConst::Var(vid)) => {
return Err(FixupError { unresolved: super::TyOrConstInferVar::Const(vid) });
}
ty::ConstKind::Infer(InferConst::Fresh(_)) => {
ty::ConstKind::Infer(InferConst::Fresh) => {
bug!("Unexpected const in full const resolver: {:?}", c);
}
_ => {}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_infer/src/infer/snapshot/fudge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for InferenceFudger<'a, 'tcx> {
ty
}
}
ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => {
ty::FreshTy | ty::FreshIntTy | ty::FreshFloatTy => {
unreachable!("unexpected fresh infcx var")
}
}
Expand Down Expand Up @@ -244,7 +244,7 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for InferenceFudger<'a, 'tcx> {
ct
}
}
ty::InferConst::Fresh(_) => {
ty::InferConst::Fresh => {
unreachable!("unexpected fresh infcx var")
}
}
Expand Down
5 changes: 0 additions & 5 deletions compiler/rustc_middle/src/ty/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,6 @@ impl<'tcx> Const<'tcx> {
Const::new(tcx, ty::ConstKind::Infer(ty::InferConst::Var(infer)))
}

#[inline]
pub fn new_fresh(tcx: TyCtxt<'tcx>, fresh: u32) -> Const<'tcx> {
Const::new(tcx, ty::ConstKind::Infer(ty::InferConst::Fresh(fresh)))
}

#[inline]
pub fn new_infer(tcx: TyCtxt<'tcx>, infer: ty::InferConst) -> Const<'tcx> {
Const::new(tcx, ty::ConstKind::Infer(infer))
Expand Down
Loading
Loading