Remove InferCtxt::freshen.

Sometimes we freshen using a new `TypeFreshener`, and sometimes we
freshen with an existing `TypeFreshener`. For the former we have the
method `InferCtxt::freshen`. For the latter we just call `fold_with`.
This asymmetry has been confusing to me.

This commit removes `InferCtxt::freshen` so that all the freshening
sites consistently use `fold_with` and it's obvious if each one is using
a new or existing `TypeFreshener`.
This commit is contained in:
Nicholas Nethercote 2025-12-22 17:56:49 +11:00
parent 1bb9ff05c0
commit 5c49aee9ec
5 changed files with 6 additions and 8 deletions

View file

@ -637,10 +637,6 @@ impl<'tcx> InferCtxt<'tcx> {
self.typing_mode
}
pub fn freshen<T: TypeFoldable<TyCtxt<'tcx>>>(&self, t: T) -> T {
t.fold_with(&mut TypeFreshener::new(self))
}
/// Returns the origin of the type variable identified by `vid`.
///
/// No attempt is made to resolve `vid` to its root variable.

View file

@ -13,6 +13,7 @@ use tracing::debug;
use super::*;
use crate::errors::UnableToConstructConstantValue;
use crate::infer::TypeFreshener;
use crate::infer::region_constraints::{ConstraintKind, RegionConstraintData};
use crate::regions::OutlivesEnvironmentBuildExt;
use crate::traits::project::ProjectAndUnifyResult;
@ -817,6 +818,6 @@ impl<'tcx> AutoTraitFinder<'tcx> {
infcx: &InferCtxt<'tcx>,
p: ty::Predicate<'tcx>,
) -> ty::Predicate<'tcx> {
infcx.freshen(p)
p.fold_with(&mut TypeFreshener::new(infcx))
}
}

View file

@ -324,7 +324,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
// separately rather than using `stack.fresh_trait_ref` --
// this is because we want the unbound variables to be
// replaced with fresh types starting from index 0.
let cache_fresh_trait_pred = self.infcx.freshen(stack.obligation.predicate);
let cache_fresh_trait_pred =
stack.obligation.predicate.fold_with(&mut TypeFreshener::new(self.infcx));
debug!(?cache_fresh_trait_pred);
debug_assert!(!stack.obligation.predicate.has_escaping_bound_vars());

View file

@ -103,7 +103,7 @@ rustc_index::newtype_index! {
pub enum InferConst {
/// Infer the value of the const.
Var(ConstVid),
/// A fresh const variable. See `infer::freshen` for more details.
/// A fresh const variable. See `TypeFreshener` for more details.
Fresh(u32),
}

View file

@ -594,7 +594,7 @@ pub enum InferTy {
/// A [`FreshTy`][Self::FreshTy] is one that is generated as a replacement
/// for an unbound type variable. This is convenient for caching etc. See
/// `rustc_infer::infer::freshen` for more details.
/// `TypeFreshener` for more details.
///
/// Compare with [`TyVar`][Self::TyVar].
FreshTy(u32),