diff options
| author | Nicholas Nethercote <n.nethercote@gmail.com> | 2022-06-02 11:38:15 +1000 |
|---|---|---|
| committer | Nicholas Nethercote <n.nethercote@gmail.com> | 2022-06-08 09:24:03 +1000 |
| commit | 90db033955eaa000f8eea5691ea4297687a4dbef (patch) | |
| tree | 4f0dc754868ca453be66aa19fadb962e7ed8a5c3 /compiler/rustc_middle/src/macros.rs | |
| parent | 7480b501b4073cb38bacb64f22d7b09fb3f652d8 (diff) | |
| download | rust-90db033955eaa000f8eea5691ea4297687a4dbef.tar.gz rust-90db033955eaa000f8eea5691ea4297687a4dbef.zip | |
Folding revamp.
This commit makes type folding more like the way chalk does it. Currently, `TypeFoldable` has `fold_with` and `super_fold_with` methods. - `fold_with` is the standard entry point, and defaults to calling `super_fold_with`. - `super_fold_with` does the actual work of traversing a type. - For a few types of interest (`Ty`, `Region`, etc.) `fold_with` instead calls into a `TypeFolder`, which can then call back into `super_fold_with`. With the new approach, `TypeFoldable` has `fold_with` and `TypeSuperFoldable` has `super_fold_with`. - `fold_with` is still the standard entry point, *and* it does the actual work of traversing a type, for all types except types of interest. - `super_fold_with` is only implemented for the types of interest. Benefits of the new model. - I find it easier to understand. The distinction between types of interest and other types is clearer, and `super_fold_with` doesn't exist for most types. - With the current model is easy to get confused and implement a `super_fold_with` method that should be left defaulted. (Some of the precursor commits fixed such cases.) - With the current model it's easy to call `super_fold_with` within `TypeFolder` impls where `fold_with` should be called. The new approach makes this mistake impossible, and this commit fixes a number of such cases. - It's potentially faster, because it avoids the `fold_with` -> `super_fold_with` call in all cases except types of interest. A lot of the time the compile would inline those away, but not necessarily always.
Diffstat (limited to 'compiler/rustc_middle/src/macros.rs')
| -rw-r--r-- | compiler/rustc_middle/src/macros.rs | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/compiler/rustc_middle/src/macros.rs b/compiler/rustc_middle/src/macros.rs index 4e927f00acd..33b4dff977e 100644 --- a/compiler/rustc_middle/src/macros.rs +++ b/compiler/rustc_middle/src/macros.rs @@ -52,14 +52,14 @@ macro_rules! TrivialTypeFoldableImpls { (for <$tcx:lifetime> { $($ty:ty,)+ }) => { $( impl<$tcx> $crate::ty::fold::TypeFoldable<$tcx> for $ty { - fn try_super_fold_with<F: $crate::ty::fold::FallibleTypeFolder<$tcx>>( + fn try_fold_with<F: $crate::ty::fold::FallibleTypeFolder<$tcx>>( self, _: &mut F ) -> ::std::result::Result<$ty, F::Error> { Ok(self) } - fn super_visit_with<F: $crate::ty::fold::TypeVisitor<$tcx>>( + fn visit_with<F: $crate::ty::fold::TypeVisitor<$tcx>>( &self, _: &mut F) -> ::std::ops::ControlFlow<F::BreakTy> @@ -95,14 +95,14 @@ macro_rules! EnumTypeFoldableImpl { impl<$($p),*> $crate::ty::fold::TypeFoldable<$tcx> for $s $(where $($wc)*)* { - fn try_super_fold_with<V: $crate::ty::fold::FallibleTypeFolder<$tcx>>( + fn try_fold_with<V: $crate::ty::fold::FallibleTypeFolder<$tcx>>( self, folder: &mut V, ) -> ::std::result::Result<Self, V::Error> { EnumTypeFoldableImpl!(@FoldVariants(self, folder) input($($variants)*) output()) } - fn super_visit_with<V: $crate::ty::fold::TypeVisitor<$tcx>>( + fn visit_with<V: $crate::ty::fold::TypeVisitor<$tcx>>( &self, visitor: &mut V, ) -> ::std::ops::ControlFlow<V::BreakTy> { |
