From 5c49aee9ec94ab4e6d64560e973c47b22141a1e1 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Mon, 22 Dec 2025 17:56:49 +1100 Subject: [PATCH] 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`. --- compiler/rustc_infer/src/infer/mod.rs | 4 ---- compiler/rustc_trait_selection/src/traits/auto_trait.rs | 3 ++- compiler/rustc_trait_selection/src/traits/select/mod.rs | 3 ++- compiler/rustc_type_ir/src/const_kind.rs | 2 +- compiler/rustc_type_ir/src/ty_kind.rs | 2 +- 5 files changed, 6 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_infer/src/infer/mod.rs b/compiler/rustc_infer/src/infer/mod.rs index 571d5117bc91..c9ea420944e2 100644 --- a/compiler/rustc_infer/src/infer/mod.rs +++ b/compiler/rustc_infer/src/infer/mod.rs @@ -637,10 +637,6 @@ impl<'tcx> InferCtxt<'tcx> { self.typing_mode } - pub fn freshen>>(&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. diff --git a/compiler/rustc_trait_selection/src/traits/auto_trait.rs b/compiler/rustc_trait_selection/src/traits/auto_trait.rs index 09709291a4b9..2da7c4448ce5 100644 --- a/compiler/rustc_trait_selection/src/traits/auto_trait.rs +++ b/compiler/rustc_trait_selection/src/traits/auto_trait.rs @@ -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)) } } diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index 43f9d6249771..0a1542c1d16d 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -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()); diff --git a/compiler/rustc_type_ir/src/const_kind.rs b/compiler/rustc_type_ir/src/const_kind.rs index f315e8b3e11c..9a83055d06c4 100644 --- a/compiler/rustc_type_ir/src/const_kind.rs +++ b/compiler/rustc_type_ir/src/const_kind.rs @@ -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), } diff --git a/compiler/rustc_type_ir/src/ty_kind.rs b/compiler/rustc_type_ir/src/ty_kind.rs index 34bb3cd8a6c3..498797bef653 100644 --- a/compiler/rustc_type_ir/src/ty_kind.rs +++ b/compiler/rustc_type_ir/src/ty_kind.rs @@ -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),