From 4652295a3e60b569536dcf703ed5081c6d36787a Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Mon, 22 Dec 2025 14:51:02 +1100 Subject: [PATCH] Simplify `TypeFreshener` methods. `freshen_{ty,const}` take a `Result` and just do a fold if the input is `Ok`. It's simpler to do those folds at the call site, and only call `freshen_{ty,const}` in the `Err` case. That way we can also avoid useless fold operations on the results of `new_{int,uint,float}`. Also, make some `bug!` calls more concise. --- compiler/rustc_infer/src/infer/freshen.rs | 122 ++++++++++------------ 1 file changed, 54 insertions(+), 68 deletions(-) diff --git a/compiler/rustc_infer/src/infer/freshen.rs b/compiler/rustc_infer/src/infer/freshen.rs index ddc0b1168062..d614fac40831 100644 --- a/compiler/rustc_infer/src/infer/freshen.rs +++ b/compiler/rustc_infer/src/infer/freshen.rs @@ -60,45 +60,35 @@ impl<'a, 'tcx> TypeFreshener<'a, 'tcx> { } } - fn freshen_ty(&mut self, input: Result, ty::InferTy>, mk_fresh: F) -> Ty<'tcx> + fn freshen_ty(&mut self, input: 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 - } - }, + match self.ty_freshen_map.entry(input) { + 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( - &mut self, - input: Result, ty::InferConst>, - freshener: F, - ) -> ty::Const<'tcx> + fn freshen_const(&mut self, input: 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 - } - }, + match self.const_freshen_map.entry(input) { + 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 + } } } } @@ -146,21 +136,21 @@ impl<'a, 'tcx> TypeFolder> for TypeFreshener<'a, '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) + match inner.const_unification_table().probe_value(v).known() { + Some(const_) => { + drop(inner); + const_.fold_with(self) + } + None => { + let input = + ty::InferConst::Var(inner.const_unification_table().find(v).vid); + 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, - ); + bug!("freshened const id {} exceeds counter {}", i, self.const_freshen_count); } ct } @@ -185,50 +175,46 @@ impl<'a, 'tcx> TypeFreshener<'a, '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))) + match inner.type_variables().probe(v).known() { + Some(ty) => { + drop(inner); + Some(ty.fold_with(self)) + } + None => { + let input = ty::TyVar(inner.type_variables().root_var(v)); + Some(self.freshen_ty(input, |n| Ty::new_fresh(self.infcx.tcx, n))) + } + } } 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)), + match value { + ty::IntVarValue::IntType(ty) => Some(Ty::new_int(self.infcx.tcx, ty)), + ty::IntVarValue::UintType(ty) => Some(Ty::new_uint(self.infcx.tcx, ty)), ty::IntVarValue::Unknown => { - Err(ty::IntVar(inner.int_unification_table().find(v))) + let input = ty::IntVar(inner.int_unification_table().find(v)); + Some(self.freshen_ty(input, |n| Ty::new_fresh_int(self.infcx.tcx, n))) } - }; - drop(inner); - Some(self.freshen_ty(input, |n| Ty::new_fresh_int(self.infcx.tcx, n))) + } } 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)), + match value { + ty::FloatVarValue::Known(ty) => Some(Ty::new_float(self.infcx.tcx, ty)), ty::FloatVarValue::Unknown => { - Err(ty::FloatVar(inner.float_unification_table().find(v))) + let input = ty::FloatVar(inner.float_unification_table().find(v)); + Some(self.freshen_ty(input, |n| Ty::new_fresh_float(self.infcx.tcx, n))) } - }; - drop(inner); - Some(self.freshen_ty(input, |n| Ty::new_fresh_float(self.infcx.tcx, n))) + } } 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 - ); + bug!("freshened type id {} exceeds counter {}", ct, self.ty_freshen_count); } None }