Skip to content

Commit d1d7c44

Browse files
Auto merge of #150239 - nnethercote:simplify-TypeFreshener-methods, r=<try>
Simplify `TypeFreshener` methods.
2 parents 99ff3fb + ec5e96e commit d1d7c44

File tree

6 files changed

+72
-98
lines changed

6 files changed

+72
-98
lines changed

compiler/rustc_infer/src/infer/freshen.rs

Lines changed: 60 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -60,45 +60,35 @@ impl<'a, 'tcx> TypeFreshener<'a, 'tcx> {
6060
}
6161
}
6262

63-
fn freshen_ty<F>(&mut self, input: Result<Ty<'tcx>, ty::InferTy>, mk_fresh: F) -> Ty<'tcx>
63+
fn freshen_ty<F>(&mut self, input: ty::InferTy, mk_fresh: F) -> Ty<'tcx>
6464
where
6565
F: FnOnce(u32) -> Ty<'tcx>,
6666
{
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-
},
67+
match self.ty_freshen_map.entry(input) {
68+
Entry::Occupied(entry) => *entry.get(),
69+
Entry::Vacant(entry) => {
70+
let index = self.ty_freshen_count;
71+
self.ty_freshen_count += 1;
72+
let t = mk_fresh(index);
73+
entry.insert(t);
74+
t
75+
}
7976
}
8077
}
8178

82-
fn freshen_const<F>(
83-
&mut self,
84-
input: Result<ty::Const<'tcx>, ty::InferConst>,
85-
freshener: F,
86-
) -> ty::Const<'tcx>
79+
fn freshen_const<F>(&mut self, input: ty::InferConst, freshener: F) -> ty::Const<'tcx>
8780
where
8881
F: FnOnce(u32) -> ty::InferConst,
8982
{
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-
},
83+
match self.const_freshen_map.entry(input) {
84+
Entry::Occupied(entry) => *entry.get(),
85+
Entry::Vacant(entry) => {
86+
let index = self.const_freshen_count;
87+
self.const_freshen_count += 1;
88+
let ct = ty::Const::new_infer(self.infcx.tcx, freshener(index));
89+
entry.insert(ct);
90+
ct
91+
}
10292
}
10393
}
10494
}
@@ -130,7 +120,7 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for TypeFreshener<'a, 'tcx> {
130120
t
131121
} else {
132122
match *t.kind() {
133-
ty::Infer(v) => self.fold_infer_ty(v).unwrap_or(t),
123+
ty::Infer(v) => self.fold_infer_ty(v),
134124

135125
// This code is hot enough that a non-debug assertion here makes a noticeable
136126
// difference on benchmarks like `wg-grammar`.
@@ -146,27 +136,24 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for TypeFreshener<'a, 'tcx> {
146136
match ct.kind() {
147137
ty::ConstKind::Infer(ty::InferConst::Var(v)) => {
148138
let mut inner = self.infcx.inner.borrow_mut();
149-
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-
);
139+
match inner.const_unification_table().probe_value(v).known() {
140+
Some(const_) => {
141+
drop(inner);
142+
const_.fold_with(self)
143+
}
144+
None => {
145+
let input =
146+
ty::InferConst::Var(inner.const_unification_table().find(v).vid);
147+
self.freshen_const(input, ty::InferConst::Fresh)
148+
}
164149
}
165-
ct
150+
}
151+
ty::ConstKind::Infer(ty::InferConst::Fresh(_)) => {
152+
bug!("trying to freshen already-freshened const {ct:?}");
166153
}
167154

168155
ty::ConstKind::Bound(..) | ty::ConstKind::Placeholder(_) => {
169-
bug!("unexpected const {:?}", ct)
156+
bug!("unexpected const {ct:?}")
170157
}
171158

172159
ty::ConstKind::Param(_)
@@ -181,56 +168,49 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for TypeFreshener<'a, 'tcx> {
181168
impl<'a, 'tcx> TypeFreshener<'a, 'tcx> {
182169
// This is separate from `fold_ty` to keep that method small and inlinable.
183170
#[inline(never)]
184-
fn fold_infer_ty(&mut self, v: ty::InferTy) -> Option<Ty<'tcx>> {
185-
match v {
171+
fn fold_infer_ty(&mut self, ty: ty::InferTy) -> Ty<'tcx> {
172+
match ty {
186173
ty::TyVar(v) => {
187174
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)))
175+
match inner.type_variables().probe(v).known() {
176+
Some(ty) => {
177+
drop(inner);
178+
ty.fold_with(self)
179+
}
180+
None => {
181+
let input = ty::TyVar(inner.type_variables().root_var(v));
182+
self.freshen_ty(input, |n| Ty::new_fresh(self.infcx.tcx, n))
183+
}
184+
}
195185
}
196186

197187
ty::IntVar(v) => {
198188
let mut inner = self.infcx.inner.borrow_mut();
199189
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)),
190+
match value {
191+
ty::IntVarValue::IntType(ty) => Ty::new_int(self.infcx.tcx, ty),
192+
ty::IntVarValue::UintType(ty) => Ty::new_uint(self.infcx.tcx, ty),
203193
ty::IntVarValue::Unknown => {
204-
Err(ty::IntVar(inner.int_unification_table().find(v)))
194+
let input = ty::IntVar(inner.int_unification_table().find(v));
195+
self.freshen_ty(input, |n| Ty::new_fresh_int(self.infcx.tcx, n))
205196
}
206-
};
207-
drop(inner);
208-
Some(self.freshen_ty(input, |n| Ty::new_fresh_int(self.infcx.tcx, n)))
197+
}
209198
}
210199

211200
ty::FloatVar(v) => {
212201
let mut inner = self.infcx.inner.borrow_mut();
213202
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)),
203+
match value {
204+
ty::FloatVarValue::Known(ty) => Ty::new_float(self.infcx.tcx, ty),
216205
ty::FloatVarValue::Unknown => {
217-
Err(ty::FloatVar(inner.float_unification_table().find(v)))
206+
let input = ty::FloatVar(inner.float_unification_table().find(v));
207+
self.freshen_ty(input, |n| Ty::new_fresh_float(self.infcx.tcx, n))
218208
}
219-
};
220-
drop(inner);
221-
Some(self.freshen_ty(input, |n| Ty::new_fresh_float(self.infcx.tcx, n)))
209+
}
222210
}
223211

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
212+
ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => {
213+
bug!("trying to freshen already-freshened type {ty:?}");
234214
}
235215
}
236216
}

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -637,10 +637,6 @@ impl<'tcx> InferCtxt<'tcx> {
637637
self.typing_mode
638638
}
639639

640-
pub fn freshen<T: TypeFoldable<TyCtxt<'tcx>>>(&self, t: T) -> T {
641-
t.fold_with(&mut self.freshener())
642-
}
643-
644640
/// Returns the origin of the type variable identified by `vid`.
645641
///
646642
/// No attempt is made to resolve `vid` to its root variable.
@@ -658,10 +654,6 @@ impl<'tcx> InferCtxt<'tcx> {
658654
}
659655
}
660656

661-
pub fn freshener<'b>(&'b self) -> TypeFreshener<'b, 'tcx> {
662-
freshen::TypeFreshener::new(self)
663-
}
664-
665657
pub fn unresolved_variables(&self) -> Vec<Ty<'tcx>> {
666658
let mut inner = self.inner.borrow_mut();
667659
let mut vars: Vec<Ty<'_>> = inner

compiler/rustc_trait_selection/src/traits/auto_trait.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use tracing::debug;
1313

1414
use super::*;
1515
use crate::errors::UnableToConstructConstantValue;
16+
use crate::infer::TypeFreshener;
1617
use crate::infer::region_constraints::{ConstraintKind, RegionConstraintData};
1718
use crate::regions::OutlivesEnvironmentBuildExt;
1819
use crate::traits::project::ProjectAndUnifyResult;
@@ -817,6 +818,6 @@ impl<'tcx> AutoTraitFinder<'tcx> {
817818
infcx: &InferCtxt<'tcx>,
818819
p: ty::Predicate<'tcx>,
819820
) -> ty::Predicate<'tcx> {
820-
infcx.freshen(p)
821+
p.fold_with(&mut TypeFreshener::new(infcx))
821822
}
822823
}

compiler/rustc_trait_selection/src/traits/select/mod.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
195195
pub fn new(infcx: &'cx InferCtxt<'tcx>) -> SelectionContext<'cx, 'tcx> {
196196
SelectionContext {
197197
infcx,
198-
freshener: infcx.freshener(),
198+
freshener: TypeFreshener::new(infcx),
199199
intercrate_ambiguity_causes: None,
200200
query_mode: TraitQueryMode::Standard,
201201
}
@@ -321,10 +321,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
321321
self.check_recursion_limit(stack.obligation, stack.obligation)?;
322322

323323
// Check the cache. Note that we freshen the trait-ref
324-
// separately rather than using `stack.fresh_trait_ref` --
324+
// separately rather than using `stack.fresh_trait_pred` --
325325
// this is because we want the unbound variables to be
326326
// replaced with fresh types starting from index 0.
327-
let cache_fresh_trait_pred = self.infcx.freshen(stack.obligation.predicate);
327+
let cache_fresh_trait_pred =
328+
stack.obligation.predicate.fold_with(&mut TypeFreshener::new(self.infcx));
328329
debug!(?cache_fresh_trait_pred);
329330
debug_assert!(!stack.obligation.predicate.has_escaping_bound_vars());
330331

@@ -1135,7 +1136,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11351136
/// `Option<Box<List<T>>>` is `Send` if `Box<List<T>>` is
11361137
/// `Send`.
11371138
///
1138-
/// Note that we do this comparison using the `fresh_trait_ref`
1139+
/// Note that we do this comparison using the `fresh_trait_pred`
11391140
/// fields. Because these have all been freshened using
11401141
/// `self.freshener`, we can be sure that (a) this will not
11411142
/// affect the inferencer state and (b) that if we see two
@@ -1219,7 +1220,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
12191220
if unbound_input_types
12201221
&& stack.iter().skip(1).any(|prev| {
12211222
stack.obligation.param_env == prev.obligation.param_env
1222-
&& self.match_fresh_trait_refs(stack.fresh_trait_pred, prev.fresh_trait_pred)
1223+
&& self.match_fresh_trait_preds(stack.fresh_trait_pred, prev.fresh_trait_pred)
12231224
})
12241225
{
12251226
debug!("evaluate_stack --> unbound argument, recursive --> giving up",);
@@ -2742,7 +2743,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
27422743
///////////////////////////////////////////////////////////////////////////
27432744
// Miscellany
27442745

2745-
fn match_fresh_trait_refs(
2746+
fn match_fresh_trait_preds(
27462747
&self,
27472748
previous: ty::PolyTraitPredicate<'tcx>,
27482749
current: ty::PolyTraitPredicate<'tcx>,
@@ -3031,7 +3032,7 @@ impl<'tcx> ProvisionalEvaluationCache<'tcx> {
30313032
}
30323033

30333034
/// Check the provisional cache for any result for
3034-
/// `fresh_trait_ref`. If there is a hit, then you must consider
3035+
/// `fresh_trait_pred`. If there is a hit, then you must consider
30353036
/// it an access to the stack slots at depth
30363037
/// `reached_depth` (from the returned value).
30373038
fn get_provisional(

compiler/rustc_type_ir/src/const_kind.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ rustc_index::newtype_index! {
103103
pub enum InferConst {
104104
/// Infer the value of the const.
105105
Var(ConstVid),
106-
/// A fresh const variable. See `infer::freshen` for more details.
106+
/// A fresh const variable. See `TypeFreshener` for more details.
107107
Fresh(u32),
108108
}
109109

compiler/rustc_type_ir/src/ty_kind.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ pub enum InferTy {
594594

595595
/// A [`FreshTy`][Self::FreshTy] is one that is generated as a replacement
596596
/// for an unbound type variable. This is convenient for caching etc. See
597-
/// `rustc_infer::infer::freshen` for more details.
597+
/// `TypeFreshener` for more details.
598598
///
599599
/// Compare with [`TyVar`][Self::TyVar].
600600
FreshTy(u32),

0 commit comments

Comments
 (0)