Skip to content

Commit

Permalink
Remove EnumTypeTraversalImpl.
Browse files Browse the repository at this point in the history
I suspect this macro was around before `TypeFoldable`/`TypeVisitable`
were derivable. But now it's only used for two types, `Result` and
`Option`. Removing the macro and implementing the traits for those types
by hand makes the code much simpler.
  • Loading branch information
nnethercote committed Apr 16, 2023
1 parent d558796 commit 32f6e7a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 165 deletions.
141 changes: 0 additions & 141 deletions compiler/rustc_type_ir/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,144 +33,3 @@ macro_rules! TrivialTypeTraversalImpls {
)+
};
}

macro_rules! EnumTypeTraversalImpl {
(impl<$($p:tt),*> TypeFoldable<$tcx:tt> for $s:path {
$($variants:tt)*
} $(where $($wc:tt)*)*) => {
impl<$($p),*> $crate::fold::TypeFoldable<$tcx> for $s
$(where $($wc)*)*
{
fn try_fold_with<V: $crate::fold::FallibleTypeFolder<$tcx>>(
self,
folder: &mut V,
) -> ::std::result::Result<Self, V::Error> {
EnumTypeTraversalImpl!(@FoldVariants(self, folder) input($($variants)*) output())
}
}
};

(impl<$($p:tt),*> TypeVisitable<$tcx:tt> for $s:path {
$($variants:tt)*
} $(where $($wc:tt)*)*) => {
impl<$($p),*> $crate::visit::TypeVisitable<$tcx> for $s
$(where $($wc)*)*
{
fn visit_with<V: $crate::visit::TypeVisitor<$tcx>>(
&self,
visitor: &mut V,
) -> ::std::ops::ControlFlow<V::BreakTy> {
EnumTypeTraversalImpl!(@VisitVariants(self, visitor) input($($variants)*) output())
}
}
};

(@FoldVariants($this:expr, $folder:expr) input() output($($output:tt)*)) => {
Ok(match $this {
$($output)*
})
};

(@FoldVariants($this:expr, $folder:expr)
input( ($variant:path) ( $($variant_arg:ident),* ) , $($input:tt)*)
output( $($output:tt)*) ) => {
EnumTypeTraversalImpl!(
@FoldVariants($this, $folder)
input($($input)*)
output(
$variant ( $($variant_arg),* ) => {
$variant (
$($crate::fold::TypeFoldable::try_fold_with($variant_arg, $folder)?),*
)
}
$($output)*
)
)
};

(@FoldVariants($this:expr, $folder:expr)
input( ($variant:path) { $($variant_arg:ident),* $(,)? } , $($input:tt)*)
output( $($output:tt)*) ) => {
EnumTypeTraversalImpl!(
@FoldVariants($this, $folder)
input($($input)*)
output(
$variant { $($variant_arg),* } => {
$variant {
$($variant_arg: $crate::fold::TypeFoldable::fold_with(
$variant_arg, $folder
)?),* }
}
$($output)*
)
)
};

(@FoldVariants($this:expr, $folder:expr)
input( ($variant:path), $($input:tt)*)
output( $($output:tt)*) ) => {
EnumTypeTraversalImpl!(
@FoldVariants($this, $folder)
input($($input)*)
output(
$variant => { $variant }
$($output)*
)
)
};

(@VisitVariants($this:expr, $visitor:expr) input() output($($output:tt)*)) => {
match $this {
$($output)*
}
};

(@VisitVariants($this:expr, $visitor:expr)
input( ($variant:path) ( $($variant_arg:ident),* ) , $($input:tt)*)
output( $($output:tt)*) ) => {
EnumTypeTraversalImpl!(
@VisitVariants($this, $visitor)
input($($input)*)
output(
$variant ( $($variant_arg),* ) => {
$($crate::visit::TypeVisitable::visit_with(
$variant_arg, $visitor
)?;)*
::std::ops::ControlFlow::Continue(())
}
$($output)*
)
)
};

(@VisitVariants($this:expr, $visitor:expr)
input( ($variant:path) { $($variant_arg:ident),* $(,)? } , $($input:tt)*)
output( $($output:tt)*) ) => {
EnumTypeTraversalImpl!(
@VisitVariants($this, $visitor)
input($($input)*)
output(
$variant { $($variant_arg),* } => {
$($crate::visit::TypeVisitable::visit_with(
$variant_arg, $visitor
)?;)*
::std::ops::ControlFlow::Continue(())
}
$($output)*
)
)
};

(@VisitVariants($this:expr, $visitor:expr)
input( ($variant:path), $($input:tt)*)
output( $($output:tt)*) ) => {
EnumTypeTraversalImpl!(
@VisitVariants($this, $visitor)
input($($input)*)
output(
$variant => { ::std::ops::ControlFlow::Continue(()) }
$($output)*
)
)
};
}
58 changes: 34 additions & 24 deletions compiler/rustc_type_ir/src/structural_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,40 @@ impl<I: Interner, A: TypeVisitable<I>, B: TypeVisitable<I>, C: TypeVisitable<I>>
}
}

EnumTypeTraversalImpl! {
impl<I, T> TypeFoldable<I> for Option<T> {
(Some)(a),
(None),
} where I: Interner, T: TypeFoldable<I>
}
EnumTypeTraversalImpl! {
impl<I, T> TypeVisitable<I> for Option<T> {
(Some)(a),
(None),
} where I: Interner, T: TypeVisitable<I>
}

EnumTypeTraversalImpl! {
impl<I, T, E> TypeFoldable<I> for Result<T, E> {
(Ok)(a),
(Err)(a),
} where I: Interner, T: TypeFoldable<I>, E: TypeFoldable<I>,
}
EnumTypeTraversalImpl! {
impl<I, T, E> TypeVisitable<I> for Result<T, E> {
(Ok)(a),
(Err)(a),
} where I: Interner, T: TypeVisitable<I>, E: TypeVisitable<I>,
impl<I: Interner, T: TypeFoldable<I>> TypeFoldable<I> for Option<T> {
fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
Ok(match self {
Some(v) => Some(v.try_fold_with(folder)?),
None => None,
})
}
}

impl<I: Interner, T: TypeVisitable<I>> TypeVisitable<I> for Option<T> {
fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
match self {
Some(v) => v.visit_with(visitor),
None => ControlFlow::Continue(()),
}
}
}

impl<I: Interner, T: TypeFoldable<I>, E: TypeFoldable<I>> TypeFoldable<I> for Result<T, E> {
fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
Ok(match self {
Ok(v) => Ok(v.try_fold_with(folder)?),
Err(e) => Err(e.try_fold_with(folder)?),
})
}
}

impl<I: Interner, T: TypeVisitable<I>, E: TypeVisitable<I>> TypeVisitable<I> for Result<T, E> {
fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
match self {
Ok(v) => v.visit_with(visitor),
Err(e) => e.visit_with(visitor),
}
}
}

impl<I: Interner, T: TypeFoldable<I>> TypeFoldable<I> for Rc<T> {
Expand Down

0 comments on commit 32f6e7a

Please sign in to comment.