diff --git a/compiler/rustc_macros/src/type_foldable.rs b/compiler/rustc_macros/src/type_foldable.rs index 91b747f18569d..253398e031fe2 100644 --- a/compiler/rustc_macros/src/type_foldable.rs +++ b/compiler/rustc_macros/src/type_foldable.rs @@ -28,22 +28,6 @@ pub(super) fn type_foldable_derive(mut s: synstructure::Structure<'_>) -> proc_m }) }); - let body_fold = s.each_variant(|vi| { - let bindings = vi.bindings(); - vi.construct(|_, index| { - let bind = &bindings[index]; - - // retain value of fields with #[type_foldable(identity)] - if has_ignore_attr(&bind.ast().attrs, "type_foldable", "identity") { - bind.to_token_stream() - } else { - quote! { - ::rustc_middle::ty::TypeFoldable::fold_with(#bind, __folder) - } - } - }) - }); - s.bound_impl( quote!(::rustc_middle::ty::TypeFoldable<::rustc_middle::ty::TyCtxt<'tcx>>), quote! { @@ -53,13 +37,6 @@ pub(super) fn type_foldable_derive(mut s: synstructure::Structure<'_>) -> proc_m ) -> Result { Ok(match self { #try_body_fold }) } - - fn fold_with<__F: ::rustc_middle::ty::TypeFolder<::rustc_middle::ty::TyCtxt<'tcx>>>( - self, - __folder: &mut __F - ) -> Self { - match self { #body_fold } - } }, ) } diff --git a/compiler/rustc_middle/src/macros.rs b/compiler/rustc_middle/src/macros.rs index 3d0971ed1ffd9..bd8b3ee957abf 100644 --- a/compiler/rustc_middle/src/macros.rs +++ b/compiler/rustc_middle/src/macros.rs @@ -66,14 +66,6 @@ macro_rules! TrivialTypeTraversalImpls { ) -> ::std::result::Result { Ok(self) } - - #[inline] - fn fold_with>>( - self, - _: &mut F, - ) -> Self { - self - } } impl<'tcx> $crate::ty::TypeVisitable<$crate::ty::TyCtxt<'tcx>> for $ty { diff --git a/compiler/rustc_middle/src/traits/solve.rs b/compiler/rustc_middle/src/traits/solve.rs index c424ca71c0801..c5b31bb80c2bd 100644 --- a/compiler/rustc_middle/src/traits/solve.rs +++ b/compiler/rustc_middle/src/traits/solve.rs @@ -4,8 +4,7 @@ use rustc_type_ir as ir; pub use rustc_type_ir::solve::*; use crate::ty::{ - self, FallibleTypeFolder, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeVisitable, TypeVisitor, - try_visit, + self, FallibleTypeFolder, Ty, TyCtxt, TypeFoldable, TypeVisitable, TypeVisitor, try_visit, }; pub type Goal<'tcx, P> = ir::solve::Goal, P>; @@ -66,21 +65,6 @@ impl<'tcx> TypeFoldable> for ExternalConstraints<'tcx> { .try_fold_with(folder)?, })) } - - fn fold_with>>(self, folder: &mut F) -> Self { - // Perf testing has found that this check is slightly faster than - // folding and re-interning an empty `ExternalConstraintsData`. - // See: . - if self.is_empty() { - return self; - } - - TypeFolder::cx(folder).mk_external_constraints(ExternalConstraintsData { - region_constraints: self.region_constraints.clone().fold_with(folder), - opaque_types: self.opaque_types.iter().map(|opaque| opaque.fold_with(folder)).collect(), - normalization_nested_goals: self.normalization_nested_goals.clone().fold_with(folder), - }) - } } impl<'tcx> TypeVisitable> for ExternalConstraints<'tcx> { diff --git a/compiler/rustc_middle/src/ty/generic_args.rs b/compiler/rustc_middle/src/ty/generic_args.rs index a9ca6bfef5534..c93ca68b27bd9 100644 --- a/compiler/rustc_middle/src/ty/generic_args.rs +++ b/compiler/rustc_middle/src/ty/generic_args.rs @@ -17,7 +17,7 @@ use smallvec::SmallVec; use crate::ty::codec::{TyDecoder, TyEncoder}; use crate::ty::{ self, ClosureArgs, CoroutineArgs, CoroutineClosureArgs, FallibleTypeFolder, InlineConstArgs, - Lift, List, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeVisitable, TypeVisitor, VisitorResult, + Lift, List, Ty, TyCtxt, TypeFoldable, TypeVisitable, TypeVisitor, VisitorResult, walk_visitable_list, }; @@ -340,14 +340,6 @@ impl<'tcx> TypeFoldable> for GenericArg<'tcx> { GenericArgKind::Const(ct) => ct.try_fold_with(folder).map(Into::into), } } - - fn fold_with>>(self, folder: &mut F) -> Self { - match self.kind() { - GenericArgKind::Lifetime(lt) => lt.fold_with(folder).into(), - GenericArgKind::Type(ty) => ty.fold_with(folder).into(), - GenericArgKind::Const(ct) => ct.fold_with(folder).into(), - } - } } impl<'tcx> TypeVisitable> for GenericArg<'tcx> { @@ -630,27 +622,6 @@ impl<'tcx> TypeFoldable> for GenericArgsRef<'tcx> { _ => ty::util::try_fold_list(self, folder, |tcx, v| tcx.mk_args(v)), } } - - fn fold_with>>(self, folder: &mut F) -> Self { - // See justification for this behavior in `try_fold_with`. - match self.len() { - 1 => { - let param0 = self[0].fold_with(folder); - if param0 == self[0] { self } else { folder.cx().mk_args(&[param0]) } - } - 2 => { - let param0 = self[0].fold_with(folder); - let param1 = self[1].fold_with(folder); - if param0 == self[0] && param1 == self[1] { - self - } else { - folder.cx().mk_args(&[param0, param1]) - } - } - 0 => self, - _ => ty::util::fold_list(self, folder, |tcx, v| tcx.mk_args(v)), - } - } } impl<'tcx> TypeFoldable> for &'tcx ty::List> { @@ -686,22 +657,6 @@ impl<'tcx> TypeFoldable> for &'tcx ty::List> { _ => ty::util::try_fold_list(self, folder, |tcx, v| tcx.mk_type_list(v)), } } - - fn fold_with>>(self, folder: &mut F) -> Self { - // See comment justifying behavior in `try_fold_with`. - match self.len() { - 2 => { - let param0 = self[0].fold_with(folder); - let param1 = self[1].fold_with(folder); - if param0 == self[0] && param1 == self[1] { - self - } else { - folder.cx().mk_type_list(&[param0, param1]) - } - } - _ => ty::util::fold_list(self, folder, |tcx, v| tcx.mk_type_list(v)), - } - } } impl<'tcx, T: TypeVisitable>> TypeVisitable> for &'tcx ty::List { diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 9b582eeb2c520..5a25c126615dc 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -630,13 +630,6 @@ impl<'tcx> TypeFoldable> for Term<'tcx> { ty::TermKind::Const(ct) => ct.try_fold_with(folder).map(Into::into), } } - - fn fold_with>>(self, folder: &mut F) -> Self { - match self.kind() { - ty::TermKind::Ty(ty) => ty.fold_with(folder).into(), - ty::TermKind::Const(ct) => ct.fold_with(folder).into(), - } - } } impl<'tcx> TypeVisitable> for Term<'tcx> { diff --git a/compiler/rustc_middle/src/ty/structural_impls.rs b/compiler/rustc_middle/src/ty/structural_impls.rs index 68de7805a52e6..16d7eeb43edf6 100644 --- a/compiler/rustc_middle/src/ty/structural_impls.rs +++ b/compiler/rustc_middle/src/ty/structural_impls.rs @@ -9,7 +9,7 @@ use rustc_abi::TyAndLayout; use rustc_hir::def::Namespace; use rustc_hir::def_id::LocalDefId; use rustc_span::Spanned; -use rustc_type_ir::{ConstKind, TypeFolder, VisitorResult, try_visit}; +use rustc_type_ir::{ConstKind, VisitorResult, try_visit}; use super::{GenericArg, GenericArgKind, Pattern}; use crate::mir::PlaceElem; @@ -313,11 +313,6 @@ impl<'tcx> TypeFoldable> for Pattern<'tcx> { let pat = (*self).clone().try_fold_with(folder)?; Ok(if pat == *self { self } else { folder.cx().mk_pat(pat) }) } - - fn fold_with>>(self, folder: &mut F) -> Self { - let pat = (*self).clone().fold_with(folder); - if pat == *self { self } else { folder.cx().mk_pat(pat) } - } } impl<'tcx> TypeVisitable> for Pattern<'tcx> { @@ -333,10 +328,6 @@ impl<'tcx> TypeFoldable> for Ty<'tcx> { ) -> Result { folder.try_fold_ty(self) } - - fn fold_with>>(self, folder: &mut F) -> Self { - folder.fold_ty(self) - } } impl<'tcx> TypeVisitable> for Ty<'tcx> { @@ -393,45 +384,6 @@ impl<'tcx> TypeSuperFoldable> for Ty<'tcx> { Ok(if *self.kind() == kind { self } else { folder.cx().mk_ty_from_kind(kind) }) } - - fn super_fold_with>>(self, folder: &mut F) -> Self { - let kind = match *self.kind() { - ty::RawPtr(ty, mutbl) => ty::RawPtr(ty.fold_with(folder), mutbl), - ty::Array(typ, sz) => ty::Array(typ.fold_with(folder), sz.fold_with(folder)), - ty::Slice(typ) => ty::Slice(typ.fold_with(folder)), - ty::Adt(tid, args) => ty::Adt(tid, args.fold_with(folder)), - ty::Dynamic(trait_ty, region) => { - ty::Dynamic(trait_ty.fold_with(folder), region.fold_with(folder)) - } - ty::Tuple(ts) => ty::Tuple(ts.fold_with(folder)), - ty::FnDef(def_id, args) => ty::FnDef(def_id, args.fold_with(folder)), - ty::FnPtr(sig_tys, hdr) => ty::FnPtr(sig_tys.fold_with(folder), hdr), - ty::UnsafeBinder(f) => ty::UnsafeBinder(f.fold_with(folder)), - ty::Ref(r, ty, mutbl) => ty::Ref(r.fold_with(folder), ty.fold_with(folder), mutbl), - ty::Coroutine(did, args) => ty::Coroutine(did, args.fold_with(folder)), - ty::CoroutineWitness(did, args) => ty::CoroutineWitness(did, args.fold_with(folder)), - ty::Closure(did, args) => ty::Closure(did, args.fold_with(folder)), - ty::CoroutineClosure(did, args) => ty::CoroutineClosure(did, args.fold_with(folder)), - ty::Alias(is_rigid, data) => ty::Alias(is_rigid, data.fold_with(folder)), - ty::Pat(ty, pat) => ty::Pat(ty.fold_with(folder), pat.fold_with(folder)), - - ty::Bool - | ty::Char - | ty::Str - | ty::Int(_) - | ty::Uint(_) - | ty::Float(_) - | ty::Error(_) - | ty::Infer(_) - | ty::Param(..) - | ty::Bound(..) - | ty::Placeholder(..) - | ty::Never - | ty::Foreign(..) => return self, - }; - - if *self.kind() == kind { self } else { folder.cx().mk_ty_from_kind(kind) } - } } impl<'tcx> TypeSuperVisitable> for Ty<'tcx> { @@ -492,10 +444,6 @@ impl<'tcx> TypeFoldable> for ty::Predicate<'tcx> { ) -> Result { folder.try_fold_predicate(self) } - - fn fold_with>>(self, folder: &mut F) -> Self { - folder.fold_predicate(self) - } } // FIXME(clause): This is wonky @@ -506,10 +454,6 @@ impl<'tcx> TypeFoldable> for ty::Clause<'tcx> { ) -> Result { Ok(folder.try_fold_predicate(self.as_predicate())?.expect_clause()) } - - fn fold_with>>(self, folder: &mut F) -> Self { - folder.fold_predicate(self.as_predicate()).expect_clause() - } } impl<'tcx> TypeFoldable> for ty::Clauses<'tcx> { @@ -519,10 +463,6 @@ impl<'tcx> TypeFoldable> for ty::Clauses<'tcx> { ) -> Result { folder.try_fold_clauses(self) } - - fn fold_with>>(self, folder: &mut F) -> Self { - folder.fold_clauses(self) - } } impl<'tcx> TypeVisitable> for ty::Predicate<'tcx> { @@ -551,12 +491,6 @@ impl<'tcx> TypeSuperFoldable> for ty::Predicate<'tcx> { let new = self.kind().try_fold_with(folder)?; Ok(folder.cx().reuse_or_mk_predicate(self, new)) } - - fn super_fold_with>>(self, folder: &mut F) -> Self { - // See comment in `Predicate::try_super_fold_with`. - let new = self.kind().fold_with(folder); - folder.cx().reuse_or_mk_predicate(self, new) - } } impl<'tcx> TypeSuperVisitable> for ty::Predicate<'tcx> { @@ -585,10 +519,6 @@ impl<'tcx> TypeSuperFoldable> for ty::Clauses<'tcx> { ) -> Result { ty::util::try_fold_list(self, folder, |tcx, v| tcx.mk_clauses(v)) } - - fn super_fold_with>>(self, folder: &mut F) -> Self { - ty::util::fold_list(self, folder, |tcx, v| tcx.mk_clauses(v)) - } } impl<'tcx> TypeFoldable> for ty::Const<'tcx> { @@ -598,10 +528,6 @@ impl<'tcx> TypeFoldable> for ty::Const<'tcx> { ) -> Result { folder.try_fold_const(self) } - - fn fold_with>>(self, folder: &mut F) -> Self { - folder.fold_const(self) - } } impl<'tcx> TypeVisitable> for ty::Const<'tcx> { @@ -630,23 +556,6 @@ impl<'tcx> TypeSuperFoldable> for ty::Const<'tcx> { }; if kind != self.kind() { Ok(folder.cx().mk_ct_from_kind(kind)) } else { Ok(self) } } - - fn super_fold_with>>(self, folder: &mut F) -> Self { - let kind = match self.kind() { - ConstKind::Alias(is_rigid, alias_const) => { - ConstKind::Alias(is_rigid, alias_const.fold_with(folder)) - } - ConstKind::Value(v) => ConstKind::Value(v.fold_with(folder)), - ConstKind::Expr(e) => ConstKind::Expr(e.fold_with(folder)), - - ConstKind::Param(_) - | ConstKind::Infer(_) - | ConstKind::Bound(..) - | ConstKind::Placeholder(_) - | ConstKind::Error(_) => return self, - }; - if kind != self.kind() { folder.cx().mk_ct_from_kind(kind) } else { self } - } } impl<'tcx> TypeSuperVisitable> for ty::Const<'tcx> { @@ -687,13 +596,6 @@ impl<'tcx> TypeFoldable> for ty::ValTree<'tcx> { Ok(valtree) } } - - fn fold_with>>(self, folder: &mut F) -> Self { - let inner: &ty::ValTreeKind> = &*self; - let new_inner = inner.clone().fold_with(folder); - - if inner == &new_inner { self } else { folder.cx().intern_valtree(new_inner) } - } } impl<'tcx> TypeVisitable> for rustc_span::ErrorGuaranteed { @@ -709,10 +611,6 @@ impl<'tcx> TypeFoldable> for rustc_span::ErrorGuaranteed { ) -> Result { Ok(self) } - - fn fold_with>>(self, _folder: &mut F) -> Self { - self - } } impl<'tcx> TypeVisitable> for TyAndLayout<'tcx, Ty<'tcx>> { @@ -742,10 +640,6 @@ impl<'tcx, T: TypeFoldable> + Debug + Clone> TypeFoldable>>(self, folder: &mut F) -> Self { - Spanned { node: self.node.fold_with(folder), span: self.span.fold_with(folder) } - } } impl<'tcx> TypeFoldable> for &'tcx ty::List { @@ -755,10 +649,6 @@ impl<'tcx> TypeFoldable> for &'tcx ty::List { ) -> Result { Ok(self) } - - fn fold_with>>(self, _folder: &mut F) -> Self { - self - } } macro_rules! list_fold { @@ -771,13 +661,6 @@ macro_rules! list_fold { ) -> Result { ty::util::try_fold_list(self, folder, |tcx, v| tcx.$mk(v)) } - - fn fold_with>>( - self, - folder: &mut F, - ) -> Self { - ty::util::fold_list(self, folder, |tcx, v| tcx.$mk(v)) - } } )* } diff --git a/compiler/rustc_middle/src/ty/util.rs b/compiler/rustc_middle/src/ty/util.rs index c4d9ec9f64289..ef5b37787265c 100644 --- a/compiler/rustc_middle/src/ty/util.rs +++ b/compiler/rustc_middle/src/ty/util.rs @@ -1597,42 +1597,6 @@ pub fn needs_drop_components_with_async<'tcx>( } } -/// Does the equivalent of -/// ```ignore (illustrative) -/// let v = self.iter().map(|p| p.fold_with(folder)).collect::>(); -/// folder.tcx().intern_*(&v) -/// ``` -pub fn fold_list<'tcx, F, L, T>( - list: L, - folder: &mut F, - intern: impl FnOnce(TyCtxt<'tcx>, &[T]) -> L, -) -> L -where - F: TypeFolder>, - L: AsRef<[T]>, - T: TypeFoldable> + PartialEq + Copy, -{ - let slice = list.as_ref(); - let mut iter = slice.iter().copied(); - // Look for the first element that changed - match iter.by_ref().enumerate().find_map(|(i, t)| { - let new_t = t.fold_with(folder); - if new_t != t { Some((i, new_t)) } else { None } - }) { - Some((i, new_t)) => { - // An element changed, prepare to intern the resulting list - let mut new_list = SmallVec::<[_; 8]>::with_capacity(slice.len()); - new_list.extend_from_slice(&slice[..i]); - new_list.push(new_t); - for t in iter { - new_list.push(t.fold_with(folder)) - } - intern(folder.cx(), &new_list) - } - None => list, - } -} - /// Does the equivalent of /// ```ignore (illustrative) /// let v = self.iter().map(|p| p.try_fold_with(folder)).collect::>(); diff --git a/compiler/rustc_type_ir/src/binder.rs b/compiler/rustc_type_ir/src/binder.rs index a3ba407b4d7c7..1057b9944c539 100644 --- a/compiler/rustc_type_ir/src/binder.rs +++ b/compiler/rustc_type_ir/src/binder.rs @@ -63,10 +63,6 @@ impl> TypeFoldable for Binder { fn try_fold_with>(self, folder: &mut F) -> Result { folder.try_fold_binder(self) } - - fn fold_with>(self, folder: &mut F) -> Self { - folder.fold_binder(self) - } } impl> TypeVisitable for Binder { @@ -82,10 +78,6 @@ impl> TypeSuperFoldable for Binder { ) -> Result { self.try_map_bound(|t| t.try_fold_with(folder)) } - - fn super_fold_with>(self, folder: &mut F) -> Self { - self.map_bound(|t| t.fold_with(folder)) - } } impl> TypeSuperVisitable for Binder { diff --git a/compiler/rustc_type_ir/src/fold.rs b/compiler/rustc_type_ir/src/fold.rs index f57a9aba69302..805a7250de4d9 100644 --- a/compiler/rustc_type_ir/src/fold.rs +++ b/compiler/rustc_type_ir/src/fold.rs @@ -55,7 +55,7 @@ use tracing::{debug, instrument}; use crate::inherent::*; use crate::visit::{TypeVisitable, TypeVisitableExt as _}; -use crate::{self as ty, BoundVarIndexKind, Interner, Region}; +use crate::{self as ty, Binder, BoundVarIndexKind, Interner, Region}; /// This trait is implemented for every type that can be folded, /// providing the skeleton of the traversal. @@ -94,7 +94,11 @@ pub trait TypeFoldable: TypeVisitable + Clone { /// /// Same as [`TypeFoldable::try_fold_with`], but not fallible. Make sure to keep /// the behavior in sync across functions. - fn fold_with>(self, folder: &mut F) -> Self; + #[inline] + fn fold_with>(self, folder: &mut F) -> Self { + let Ok(v) = self.try_fold_with(InfallibleTypeFolder::from_mut(folder)); + v + } } // This trait is implemented for types of interest. @@ -113,7 +117,10 @@ pub trait TypeSuperFoldable: TypeFoldable { /// A convenient alternative to `try_super_fold_with` for use with /// infallible folders. Do not override this method, to ensure coherence /// with `try_super_fold_with`. - fn super_fold_with>(self, folder: &mut F) -> Self; + fn super_fold_with>(self, folder: &mut F) -> Self { + let Ok(v) = self.try_super_fold_with(InfallibleTypeFolder::from_mut(folder)); + v + } } /// This trait is implemented for every infallible folding traversal. There is @@ -196,6 +203,53 @@ pub trait FallibleTypeFolder: Sized { } } +/// Changes a `TypeFolder` into a `FallibleTypeFolder` with `!` as the `Error`. +/// This is used to implement `TypeFoldable::fold_with` based on `try_fold_with`. +#[repr(transparent)] +pub struct InfallibleTypeFolder(F); + +impl InfallibleTypeFolder { + fn from_mut(f: &mut F) -> &mut InfallibleTypeFolder { + // SAFETY: repr(transparent) + unsafe { mem::transmute(f) } + } +} + +impl, I: Interner> FallibleTypeFolder for InfallibleTypeFolder { + type Error = Infallible; + + fn cx(&self) -> I { + self.0.cx() + } + + fn try_fold_binder(&mut self, t: Binder) -> Result, Self::Error> + where + T: TypeFoldable, + { + Ok(self.0.fold_binder(t)) + } + + fn try_fold_ty(&mut self, t: I::Ty) -> Result { + Ok(self.0.fold_ty(t)) + } + + fn try_fold_region(&mut self, r: Region) -> Result, Self::Error> { + Ok(self.0.fold_region(r)) + } + + fn try_fold_const(&mut self, c: I::Const) -> Result { + Ok(self.0.fold_const(c)) + } + + fn try_fold_predicate(&mut self, p: I::Predicate) -> Result { + Ok(self.0.fold_predicate(p)) + } + + fn try_fold_clauses(&mut self, c: I::Clauses) -> Result { + Ok(self.0.fold_clauses(c)) + } +} + /////////////////////////////////////////////////////////////////////////// // Traversal implementations. @@ -203,10 +257,6 @@ impl, U: TypeFoldable> TypeFoldable for (T fn try_fold_with>(self, folder: &mut F) -> Result<(T, U), F::Error> { Ok((self.0.try_fold_with(folder)?, self.1.try_fold_with(folder)?)) } - - fn fold_with>(self, folder: &mut F) -> Self { - (self.0.fold_with(folder), self.1.fold_with(folder)) - } } impl, B: TypeFoldable, C: TypeFoldable> TypeFoldable @@ -222,10 +272,6 @@ impl, B: TypeFoldable, C: TypeFoldable> Ty self.2.try_fold_with(folder)?, )) } - - fn fold_with>(self, folder: &mut F) -> Self { - (self.0.fold_with(folder), self.1.fold_with(folder), self.2.fold_with(folder)) - } } impl> TypeFoldable for Option { @@ -235,10 +281,6 @@ impl> TypeFoldable for Option { None => None, }) } - - fn fold_with>(self, folder: &mut F) -> Self { - Some(self?.fold_with(folder)) - } } impl, E: TypeFoldable> TypeFoldable for Result { @@ -248,13 +290,6 @@ impl, E: TypeFoldable> TypeFoldable for Re Err(e) => Err(e.try_fold_with(folder)?), }) } - - fn fold_with>(self, folder: &mut F) -> Self { - match self { - Ok(v) => Ok(v.fold_with(folder)), - Err(e) => Err(e.fold_with(folder)), - } - } } fn fold_arc( @@ -299,12 +334,6 @@ impl> TypeFoldable for Arc { fn try_fold_with>(self, folder: &mut F) -> Result { fold_arc(self, |t| t.try_fold_with(folder)) } - - fn fold_with>(self, folder: &mut F) -> Self { - match fold_arc::(self, |t| Ok(t.fold_with(folder))) { - Ok(t) => t, - } - } } impl> TypeFoldable for Box { @@ -312,51 +341,30 @@ impl> TypeFoldable for Box { *self = (*self).try_fold_with(folder)?; Ok(self) } - - fn fold_with>(mut self, folder: &mut F) -> Self { - *self = (*self).fold_with(folder); - self - } } impl> TypeFoldable for Vec { fn try_fold_with>(self, folder: &mut F) -> Result { self.into_iter().map(|t| t.try_fold_with(folder)).collect() } - - fn fold_with>(self, folder: &mut F) -> Self { - self.into_iter().map(|t| t.fold_with(folder)).collect() - } } impl> TypeFoldable for ThinVec { fn try_fold_with>(self, folder: &mut F) -> Result { self.into_iter().map(|t| t.try_fold_with(folder)).collect() } - - fn fold_with>(self, folder: &mut F) -> Self { - self.into_iter().map(|t| t.fold_with(folder)).collect() - } } impl> TypeFoldable for Box<[T]> { fn try_fold_with>(self, folder: &mut F) -> Result { Vec::from(self).try_fold_with(folder).map(Vec::into_boxed_slice) } - - fn fold_with>(self, folder: &mut F) -> Self { - Vec::into_boxed_slice(Vec::from(self).fold_with(folder)) - } } impl, Ix: Idx> TypeFoldable for IndexVec { fn try_fold_with>(self, folder: &mut F) -> Result { self.raw.try_fold_with(folder).map(IndexVec::from_raw) } - - fn fold_with>(self, folder: &mut F) -> Self { - IndexVec::from_raw(self.raw.fold_with(folder)) - } } /////////////////////////////////////////////////////////////////////////// diff --git a/compiler/rustc_type_ir/src/macros.rs b/compiler/rustc_type_ir/src/macros.rs index 6a2d86b4419c3..7c6415109a430 100644 --- a/compiler/rustc_type_ir/src/macros.rs +++ b/compiler/rustc_type_ir/src/macros.rs @@ -11,14 +11,6 @@ macro_rules! TrivialTypeTraversalImpls { ) -> ::std::result::Result { Ok(self) } - - #[inline] - fn fold_with>( - self, - _: &mut F, - ) -> Self { - self - } } impl $crate::TypeVisitable for $ty { diff --git a/compiler/rustc_type_ir/src/region_constraint.rs b/compiler/rustc_type_ir/src/region_constraint.rs index b4ec6dbf5126d..8fcd64fe1a7d6 100644 --- a/compiler/rustc_type_ir/src/region_constraint.rs +++ b/compiler/rustc_type_ir/src/region_constraint.rs @@ -191,30 +191,6 @@ impl TypeFoldable for RegionConstraint { } }) } - - fn fold_with>(self, f: &mut F) -> Self { - use RegionConstraint::*; - match self { - Ambiguity => self, - RegionOutlives(a, b) => RegionOutlives(a.fold_with(f), b.fold_with(f)), - AliasTyOutlivesViaEnv(outlives) => AliasTyOutlivesViaEnv(outlives.fold_with(f)), - PlaceholderTyOutlives(a, b) => PlaceholderTyOutlives(a.fold_with(f), b.fold_with(f)), - And(and) => { - let mut new_and = Vec::new(); - for a in and { - new_and.push(a.fold_with(f)); - } - And(new_and.into_boxed_slice()) - } - Or(or) => { - let mut new_or = Vec::new(); - for a in or { - new_or.push(a.fold_with(f)); - } - Or(new_or.into_boxed_slice()) - } - } - } } impl TypeVisitable for RegionConstraint { diff --git a/compiler/rustc_type_ir/src/sty/mod.rs b/compiler/rustc_type_ir/src/sty/mod.rs index e2211063524f9..0923d53392d97 100644 --- a/compiler/rustc_type_ir/src/sty/mod.rs +++ b/compiler/rustc_type_ir/src/sty/mod.rs @@ -11,8 +11,8 @@ use crate::intern::Interned; use crate::relate::{Relate, RelateResult, TypeRelation}; use crate::{ BoundRegion, BoundRegionKind, BoundVar, BoundVarIndexKind, DebruijnIndex, FallibleTypeFolder, - Flags, Interner, PlaceholderRegion, RegionKind, TypeFlags, TypeFoldable, TypeFolder, - TypeVisitable, TypeVisitor, + Flags, Interner, PlaceholderRegion, RegionKind, TypeFlags, TypeFoldable, TypeVisitable, + TypeVisitor, }; /// Use this rather than `RegionKind`, whenever possible. @@ -156,8 +156,4 @@ impl TypeFoldable for Region { fn try_fold_with>(self, folder: &mut F) -> Result { folder.try_fold_region(self) } - - fn fold_with>(self, folder: &mut F) -> Self { - folder.fold_region(self) - } } diff --git a/compiler/rustc_type_ir/src/unnormalized.rs b/compiler/rustc_type_ir/src/unnormalized.rs index a685f32bced2c..7e584fa4efa74 100644 --- a/compiler/rustc_type_ir/src/unnormalized.rs +++ b/compiler/rustc_type_ir/src/unnormalized.rs @@ -5,7 +5,7 @@ use derive_where::derive_where; use rustc_macros::StableHash_NoContext; use rustc_type_ir_macros::TypeVisitable_Generic; -use crate::fold::{FallibleTypeFolder, TypeFoldable, TypeFolder}; +use crate::fold::{FallibleTypeFolder, TypeFoldable}; use crate::inherent::*; use crate::upcast::Upcast; use crate::{ @@ -95,10 +95,6 @@ impl> TypeFoldable for Unnormalized { fn try_fold_with>(self, folder: &mut F) -> Result { Ok(Unnormalized::new(self.value.try_fold_with(folder)?)) } - - fn fold_with>(self, folder: &mut F) -> Self { - Unnormalized::new(self.value.fold_with(folder)) - } } impl Unnormalized { diff --git a/compiler/rustc_type_ir_macros/src/lib.rs b/compiler/rustc_type_ir_macros/src/lib.rs index bafd8d72dc437..d180ce6776a0f 100644 --- a/compiler/rustc_type_ir_macros/src/lib.rs +++ b/compiler/rustc_type_ir_macros/src/lib.rs @@ -131,22 +131,6 @@ fn type_foldable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::Toke }) }); - let body_fold = s.each_variant(|vi| { - let bindings = vi.bindings(); - vi.construct(|_, index| { - let bind = &bindings[index]; - - // retain value of fields with #[type_foldable(identity)] - if has_ignore_attr(&bind.ast().attrs, "type_foldable", "identity") { - bind.to_token_stream() - } else { - quote! { - ::rustc_type_ir::TypeFoldable::fold_with(#bind, __folder) - } - } - }) - }); - // We filter fields which get ignored and don't require them to implement // `TypeFoldable`. We do so after generating `body_fold` as we still need // to generate code for them. @@ -164,13 +148,6 @@ fn type_foldable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::Toke ) -> Result { Ok(match self { #body_try_fold }) } - - fn fold_with<__F: ::rustc_type_ir::TypeFolder>( - self, - __folder: &mut __F - ) -> Self { - match self { #body_fold } - } }, ) }