Skip to content

Commit db9c00f

Browse files
committed
Remove indices from fresh ty/const vars.
Each of `InferTy::Fresh{Ty,IntTy,FloatTy}` and `InferConst::Fresh` have a `u32` index. It turns out the index is only used for some weak sanity checking during freshening and can be removed, making these values a bit like `ReErased`. This change simplifies things quite a bit, e.g. `TypeFreshener` no longers needs maps, and we can eliminate a bunch of pre-interned fresh types.
1 parent f8e355c commit db9c00f

File tree

35 files changed

+131
-260
lines changed

35 files changed

+131
-260
lines changed

compiler/rustc_const_eval/src/const_eval/valtrees.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ fn const_to_valtree_inner<'tcx>(
164164
ty::Never
165165
| ty::Error(_)
166166
| ty::Foreign(..)
167-
| ty::Infer(ty::FreshIntTy(_))
168-
| ty::Infer(ty::FreshFloatTy(_))
167+
| ty::Infer(ty::FreshIntTy)
168+
| ty::Infer(ty::FreshFloatTy)
169169
// FIXME(oli-obk): we could look behind opaque types
170170
| ty::Alias(..)
171171
| ty::Param(_)
@@ -326,8 +326,8 @@ pub fn valtree_to_const_value<'tcx>(
326326
ty::Never
327327
| ty::Error(_)
328328
| ty::Foreign(..)
329-
| ty::Infer(ty::FreshIntTy(_))
330-
| ty::Infer(ty::FreshFloatTy(_))
329+
| ty::Infer(ty::FreshIntTy)
330+
| ty::Infer(ty::FreshFloatTy)
331331
| ty::Alias(..)
332332
| ty::Param(_)
333333
| ty::Bound(..)

compiler/rustc_const_eval/src/interpret/stack.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,8 +521,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
521521

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

524-
ty::Bound(..)
525-
| ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
524+
ty::Bound(..) | ty::Infer(ty::FreshTy | ty::FreshIntTy | ty::FreshFloatTy) => {
526525
bug!("`is_very_trivially_sized` applied to unexpected type: {}", ty)
527526
}
528527
}

compiler/rustc_hir_typeck/src/demand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
347347
ty::TyVar(_) => self.next_ty_var(DUMMY_SP),
348348
ty::IntVar(_) => self.next_int_var(),
349349
ty::FloatVar(_) => self.next_float_var(),
350-
ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => {
350+
ty::FreshTy | ty::FreshIntTy | ty::FreshFloatTy => {
351351
bug!("unexpected fresh ty outside of the trait solver")
352352
}
353353
}

compiler/rustc_infer/src/infer/canonical/canonicalizer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ impl<'cx, 'tcx> TypeFolder<TyCtxt<'tcx>> for Canonicalizer<'cx, 'tcx> {
386386
}
387387
}
388388

389-
ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
389+
ty::Infer(ty::FreshTy | ty::FreshIntTy | ty::FreshFloatTy) => {
390390
bug!("encountered a fresh type during canonicalization")
391391
}
392392

@@ -470,7 +470,7 @@ impl<'cx, 'tcx> TypeFolder<TyCtxt<'tcx>> for Canonicalizer<'cx, 'tcx> {
470470
}
471471
}
472472
}
473-
ty::ConstKind::Infer(InferConst::Fresh(_)) => {
473+
ty::ConstKind::Infer(InferConst::Fresh) => {
474474
bug!("encountered a fresh const during canonicalization")
475475
}
476476
ty::ConstKind::Bound(debruijn, _) => {

compiler/rustc_infer/src/infer/context.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ impl<'tcx> rustc_type_ir::InferCtxtLike for InferCtxt<'tcx> {
117117
if inner.float_unification_table().find(vid) == vid
118118
)
119119
}
120-
ty::InferTy::FreshTy(_)
121-
| ty::InferTy::FreshIntTy(_)
122-
| ty::InferTy::FreshFloatTy(_) => true,
120+
ty::InferTy::FreshTy
121+
| ty::InferTy::FreshIntTy
122+
| ty::InferTy::FreshFloatTy => true,
123123
}
124124
} else {
125125
true
@@ -131,7 +131,7 @@ impl<'tcx> rustc_type_ir::InferCtxtLike for InferCtxt<'tcx> {
131131
ty::InferConst::Var(vid) => !self
132132
.probe_const_var(vid)
133133
.is_err_and(|_| self.root_const_var(vid) == vid),
134-
ty::InferConst::Fresh(_) => true,
134+
ty::InferConst::Fresh => true,
135135
}
136136
} else {
137137
true

compiler/rustc_infer/src/infer/freshen.rs

Lines changed: 25 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@
3131
//! variable only once, and it does so as soon as it can, so it is reasonable to ask what the type
3232
//! inferencer knows "so far".
3333
34-
use std::collections::hash_map::Entry;
35-
36-
use rustc_data_structures::fx::FxHashMap;
3734
use rustc_middle::bug;
3835
use rustc_middle::ty::{
3936
self, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt,
@@ -43,63 +40,11 @@ use super::InferCtxt;
4340

4441
pub struct TypeFreshener<'a, 'tcx> {
4542
infcx: &'a InferCtxt<'tcx>,
46-
ty_freshen_count: u32,
47-
const_freshen_count: u32,
48-
ty_freshen_map: FxHashMap<ty::InferTy, Ty<'tcx>>,
49-
const_freshen_map: FxHashMap<ty::InferConst, ty::Const<'tcx>>,
5043
}
5144

5245
impl<'a, 'tcx> TypeFreshener<'a, 'tcx> {
5346
pub fn new(infcx: &'a InferCtxt<'tcx>) -> TypeFreshener<'a, 'tcx> {
54-
TypeFreshener {
55-
infcx,
56-
ty_freshen_count: 0,
57-
const_freshen_count: 0,
58-
ty_freshen_map: Default::default(),
59-
const_freshen_map: Default::default(),
60-
}
61-
}
62-
63-
fn freshen_ty<F>(&mut self, input: Result<Ty<'tcx>, ty::InferTy>, mk_fresh: F) -> Ty<'tcx>
64-
where
65-
F: FnOnce(u32) -> Ty<'tcx>,
66-
{
67-
match input {
68-
Ok(ty) => ty.fold_with(self),
69-
Err(key) => match self.ty_freshen_map.entry(key) {
70-
Entry::Occupied(entry) => *entry.get(),
71-
Entry::Vacant(entry) => {
72-
let index = self.ty_freshen_count;
73-
self.ty_freshen_count += 1;
74-
let t = mk_fresh(index);
75-
entry.insert(t);
76-
t
77-
}
78-
},
79-
}
80-
}
81-
82-
fn freshen_const<F>(
83-
&mut self,
84-
input: Result<ty::Const<'tcx>, ty::InferConst>,
85-
freshener: F,
86-
) -> ty::Const<'tcx>
87-
where
88-
F: FnOnce(u32) -> ty::InferConst,
89-
{
90-
match input {
91-
Ok(ct) => ct.fold_with(self),
92-
Err(key) => match self.const_freshen_map.entry(key) {
93-
Entry::Occupied(entry) => *entry.get(),
94-
Entry::Vacant(entry) => {
95-
let index = self.const_freshen_count;
96-
self.const_freshen_count += 1;
97-
let ct = ty::Const::new_infer(self.infcx.tcx, freshener(index));
98-
entry.insert(ct);
99-
ct
100-
}
101-
},
102-
}
47+
TypeFreshener { infcx }
10348
}
10449
}
10550

@@ -145,25 +90,14 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for TypeFreshener<'a, 'tcx> {
14590
fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
14691
match ct.kind() {
14792
ty::ConstKind::Infer(ty::InferConst::Var(v)) => {
148-
let mut inner = self.infcx.inner.borrow_mut();
14993
let input =
150-
inner.const_unification_table().probe_value(v).known().ok_or_else(|| {
151-
ty::InferConst::Var(inner.const_unification_table().find(v).vid)
152-
});
153-
drop(inner);
154-
self.freshen_const(input, ty::InferConst::Fresh)
155-
}
156-
ty::ConstKind::Infer(ty::InferConst::Fresh(i)) => {
157-
if i >= self.const_freshen_count {
158-
bug!(
159-
"Encountered a freshend const with id {} \
160-
but our counter is only at {}",
161-
i,
162-
self.const_freshen_count,
163-
);
94+
self.infcx.inner.borrow_mut().const_unification_table().probe_value(v).known();
95+
match input {
96+
Some(ct) => ct.fold_with(self),
97+
None => self.infcx.tcx.consts.fresh_const,
16498
}
165-
ct
16699
}
100+
ty::ConstKind::Infer(ty::InferConst::Fresh) => ct,
167101

168102
ty::ConstKind::Bound(..) | ty::ConstKind::Placeholder(_) => {
169103
bug!("unexpected const {:?}", ct)
@@ -184,54 +118,35 @@ impl<'a, 'tcx> TypeFreshener<'a, 'tcx> {
184118
fn fold_infer_ty(&mut self, v: ty::InferTy) -> Option<Ty<'tcx>> {
185119
match v {
186120
ty::TyVar(v) => {
187-
let mut inner = self.infcx.inner.borrow_mut();
188-
let input = inner
189-
.type_variables()
190-
.probe(v)
191-
.known()
192-
.ok_or_else(|| ty::TyVar(inner.type_variables().root_var(v)));
193-
drop(inner);
194-
Some(self.freshen_ty(input, |n| Ty::new_fresh(self.infcx.tcx, n)))
121+
let value = self.infcx.inner.borrow_mut().type_variables().probe(v).known();
122+
Some(match value {
123+
Some(ty) => ty.fold_with(self),
124+
None => self.infcx.tcx.types.fresh_ty,
125+
})
195126
}
196127

197128
ty::IntVar(v) => {
198-
let mut inner = self.infcx.inner.borrow_mut();
199-
let value = inner.int_unification_table().probe_value(v);
200-
let input = match value {
201-
ty::IntVarValue::IntType(ty) => Ok(Ty::new_int(self.infcx.tcx, ty)),
202-
ty::IntVarValue::UintType(ty) => Ok(Ty::new_uint(self.infcx.tcx, ty)),
203-
ty::IntVarValue::Unknown => {
204-
Err(ty::IntVar(inner.int_unification_table().find(v)))
129+
let value = self.infcx.inner.borrow_mut().int_unification_table().probe_value(v);
130+
Some(match value {
131+
ty::IntVarValue::IntType(ty) => Ty::new_int(self.infcx.tcx, ty).fold_with(self),
132+
ty::IntVarValue::UintType(ty) => {
133+
Ty::new_uint(self.infcx.tcx, ty).fold_with(self)
205134
}
206-
};
207-
drop(inner);
208-
Some(self.freshen_ty(input, |n| Ty::new_fresh_int(self.infcx.tcx, n)))
135+
ty::IntVarValue::Unknown => self.infcx.tcx.types.fresh_int_ty,
136+
})
209137
}
210138

211139
ty::FloatVar(v) => {
212-
let mut inner = self.infcx.inner.borrow_mut();
213-
let value = inner.float_unification_table().probe_value(v);
214-
let input = match value {
215-
ty::FloatVarValue::Known(ty) => Ok(Ty::new_float(self.infcx.tcx, ty)),
216-
ty::FloatVarValue::Unknown => {
217-
Err(ty::FloatVar(inner.float_unification_table().find(v)))
140+
let value = self.infcx.inner.borrow_mut().float_unification_table().probe_value(v);
141+
Some(match value {
142+
ty::FloatVarValue::Known(ty) => {
143+
Ty::new_float(self.infcx.tcx, ty).fold_with(self)
218144
}
219-
};
220-
drop(inner);
221-
Some(self.freshen_ty(input, |n| Ty::new_fresh_float(self.infcx.tcx, n)))
145+
ty::FloatVarValue::Unknown => self.infcx.tcx.types.fresh_float_ty,
146+
})
222147
}
223148

224-
ty::FreshTy(ct) | ty::FreshIntTy(ct) | ty::FreshFloatTy(ct) => {
225-
if ct >= self.ty_freshen_count {
226-
bug!(
227-
"Encountered a freshend type with id {} \
228-
but our counter is only at {}",
229-
ct,
230-
self.ty_freshen_count
231-
);
232-
}
233-
None
234-
}
149+
ty::FreshTy | ty::FreshIntTy | ty::FreshFloatTy => None,
235150
}
236151
}
237152
}

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,7 @@ impl<'tcx> InferCtxt<'tcx> {
10281028
}
10291029
}
10301030

1031-
ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => ty,
1031+
ty::FreshTy | ty::FreshIntTy | ty::FreshFloatTy => ty,
10321032
}
10331033
} else {
10341034
ty
@@ -1045,7 +1045,7 @@ impl<'tcx> InferCtxt<'tcx> {
10451045
.probe_value(vid)
10461046
.known()
10471047
.unwrap_or(ct),
1048-
InferConst::Fresh(_) => ct,
1048+
InferConst::Fresh => ct,
10491049
},
10501050
ty::ConstKind::Param(_)
10511051
| ty::ConstKind::Bound(_, _)
@@ -1448,8 +1448,8 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for InferenceLiteralEraser<'tcx> {
14481448

14491449
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
14501450
match ty.kind() {
1451-
ty::Infer(ty::IntVar(_) | ty::FreshIntTy(_)) => self.tcx.types.i32,
1452-
ty::Infer(ty::FloatVar(_) | ty::FreshFloatTy(_)) => self.tcx.types.f64,
1451+
ty::Infer(ty::IntVar(_) | ty::FreshIntTy) => self.tcx.types.i32,
1452+
ty::Infer(ty::FloatVar(_) | ty::FreshFloatTy) => self.tcx.types.f64,
14531453
_ => ty.super_fold_with(self),
14541454
}
14551455
}

compiler/rustc_infer/src/infer/relate/generalize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for Generalizer<'_, 'tcx> {
462462
// subtyping. This is basically our "occurs check", preventing
463463
// us from creating infinitely sized types.
464464
let g = match *t.kind() {
465-
ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
465+
ty::Infer(ty::FreshTy | ty::FreshIntTy | ty::FreshFloatTy) => {
466466
bug!("unexpected infer type: {t}")
467467
}
468468

compiler/rustc_infer/src/infer/resolve.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ impl<'a, 'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for FullTypeResolver<'a, 'tcx> {
186186
ty::ConstKind::Infer(InferConst::Var(vid)) => {
187187
return Err(FixupError { unresolved: super::TyOrConstInferVar::Const(vid) });
188188
}
189-
ty::ConstKind::Infer(InferConst::Fresh(_)) => {
189+
ty::ConstKind::Infer(InferConst::Fresh) => {
190190
bug!("Unexpected const in full const resolver: {:?}", c);
191191
}
192192
_ => {}

compiler/rustc_infer/src/infer/snapshot/fudge.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for InferenceFudger<'a, 'tcx> {
207207
ty
208208
}
209209
}
210-
ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => {
210+
ty::FreshTy | ty::FreshIntTy | ty::FreshFloatTy => {
211211
unreachable!("unexpected fresh infcx var")
212212
}
213213
}
@@ -244,7 +244,7 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for InferenceFudger<'a, 'tcx> {
244244
ct
245245
}
246246
}
247-
ty::InferConst::Fresh(_) => {
247+
ty::InferConst::Fresh => {
248248
unreachable!("unexpected fresh infcx var")
249249
}
250250
}

0 commit comments

Comments
 (0)