Skip to content

Commit 32f6e7a

Browse files
committed
Remove EnumTypeTraversalImpl.
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.
1 parent d558796 commit 32f6e7a

File tree

2 files changed

+34
-165
lines changed

2 files changed

+34
-165
lines changed

compiler/rustc_type_ir/src/macros.rs

Lines changed: 0 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -33,144 +33,3 @@ macro_rules! TrivialTypeTraversalImpls {
3333
)+
3434
};
3535
}
36-
37-
macro_rules! EnumTypeTraversalImpl {
38-
(impl<$($p:tt),*> TypeFoldable<$tcx:tt> for $s:path {
39-
$($variants:tt)*
40-
} $(where $($wc:tt)*)*) => {
41-
impl<$($p),*> $crate::fold::TypeFoldable<$tcx> for $s
42-
$(where $($wc)*)*
43-
{
44-
fn try_fold_with<V: $crate::fold::FallibleTypeFolder<$tcx>>(
45-
self,
46-
folder: &mut V,
47-
) -> ::std::result::Result<Self, V::Error> {
48-
EnumTypeTraversalImpl!(@FoldVariants(self, folder) input($($variants)*) output())
49-
}
50-
}
51-
};
52-
53-
(impl<$($p:tt),*> TypeVisitable<$tcx:tt> for $s:path {
54-
$($variants:tt)*
55-
} $(where $($wc:tt)*)*) => {
56-
impl<$($p),*> $crate::visit::TypeVisitable<$tcx> for $s
57-
$(where $($wc)*)*
58-
{
59-
fn visit_with<V: $crate::visit::TypeVisitor<$tcx>>(
60-
&self,
61-
visitor: &mut V,
62-
) -> ::std::ops::ControlFlow<V::BreakTy> {
63-
EnumTypeTraversalImpl!(@VisitVariants(self, visitor) input($($variants)*) output())
64-
}
65-
}
66-
};
67-
68-
(@FoldVariants($this:expr, $folder:expr) input() output($($output:tt)*)) => {
69-
Ok(match $this {
70-
$($output)*
71-
})
72-
};
73-
74-
(@FoldVariants($this:expr, $folder:expr)
75-
input( ($variant:path) ( $($variant_arg:ident),* ) , $($input:tt)*)
76-
output( $($output:tt)*) ) => {
77-
EnumTypeTraversalImpl!(
78-
@FoldVariants($this, $folder)
79-
input($($input)*)
80-
output(
81-
$variant ( $($variant_arg),* ) => {
82-
$variant (
83-
$($crate::fold::TypeFoldable::try_fold_with($variant_arg, $folder)?),*
84-
)
85-
}
86-
$($output)*
87-
)
88-
)
89-
};
90-
91-
(@FoldVariants($this:expr, $folder:expr)
92-
input( ($variant:path) { $($variant_arg:ident),* $(,)? } , $($input:tt)*)
93-
output( $($output:tt)*) ) => {
94-
EnumTypeTraversalImpl!(
95-
@FoldVariants($this, $folder)
96-
input($($input)*)
97-
output(
98-
$variant { $($variant_arg),* } => {
99-
$variant {
100-
$($variant_arg: $crate::fold::TypeFoldable::fold_with(
101-
$variant_arg, $folder
102-
)?),* }
103-
}
104-
$($output)*
105-
)
106-
)
107-
};
108-
109-
(@FoldVariants($this:expr, $folder:expr)
110-
input( ($variant:path), $($input:tt)*)
111-
output( $($output:tt)*) ) => {
112-
EnumTypeTraversalImpl!(
113-
@FoldVariants($this, $folder)
114-
input($($input)*)
115-
output(
116-
$variant => { $variant }
117-
$($output)*
118-
)
119-
)
120-
};
121-
122-
(@VisitVariants($this:expr, $visitor:expr) input() output($($output:tt)*)) => {
123-
match $this {
124-
$($output)*
125-
}
126-
};
127-
128-
(@VisitVariants($this:expr, $visitor:expr)
129-
input( ($variant:path) ( $($variant_arg:ident),* ) , $($input:tt)*)
130-
output( $($output:tt)*) ) => {
131-
EnumTypeTraversalImpl!(
132-
@VisitVariants($this, $visitor)
133-
input($($input)*)
134-
output(
135-
$variant ( $($variant_arg),* ) => {
136-
$($crate::visit::TypeVisitable::visit_with(
137-
$variant_arg, $visitor
138-
)?;)*
139-
::std::ops::ControlFlow::Continue(())
140-
}
141-
$($output)*
142-
)
143-
)
144-
};
145-
146-
(@VisitVariants($this:expr, $visitor:expr)
147-
input( ($variant:path) { $($variant_arg:ident),* $(,)? } , $($input:tt)*)
148-
output( $($output:tt)*) ) => {
149-
EnumTypeTraversalImpl!(
150-
@VisitVariants($this, $visitor)
151-
input($($input)*)
152-
output(
153-
$variant { $($variant_arg),* } => {
154-
$($crate::visit::TypeVisitable::visit_with(
155-
$variant_arg, $visitor
156-
)?;)*
157-
::std::ops::ControlFlow::Continue(())
158-
}
159-
$($output)*
160-
)
161-
)
162-
};
163-
164-
(@VisitVariants($this:expr, $visitor:expr)
165-
input( ($variant:path), $($input:tt)*)
166-
output( $($output:tt)*) ) => {
167-
EnumTypeTraversalImpl!(
168-
@VisitVariants($this, $visitor)
169-
input($($input)*)
170-
output(
171-
$variant => { ::std::ops::ControlFlow::Continue(()) }
172-
$($output)*
173-
)
174-
)
175-
};
176-
}

compiler/rustc_type_ir/src/structural_impls.rs

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -70,30 +70,40 @@ impl<I: Interner, A: TypeVisitable<I>, B: TypeVisitable<I>, C: TypeVisitable<I>>
7070
}
7171
}
7272

73-
EnumTypeTraversalImpl! {
74-
impl<I, T> TypeFoldable<I> for Option<T> {
75-
(Some)(a),
76-
(None),
77-
} where I: Interner, T: TypeFoldable<I>
78-
}
79-
EnumTypeTraversalImpl! {
80-
impl<I, T> TypeVisitable<I> for Option<T> {
81-
(Some)(a),
82-
(None),
83-
} where I: Interner, T: TypeVisitable<I>
84-
}
85-
86-
EnumTypeTraversalImpl! {
87-
impl<I, T, E> TypeFoldable<I> for Result<T, E> {
88-
(Ok)(a),
89-
(Err)(a),
90-
} where I: Interner, T: TypeFoldable<I>, E: TypeFoldable<I>,
91-
}
92-
EnumTypeTraversalImpl! {
93-
impl<I, T, E> TypeVisitable<I> for Result<T, E> {
94-
(Ok)(a),
95-
(Err)(a),
96-
} where I: Interner, T: TypeVisitable<I>, E: TypeVisitable<I>,
73+
impl<I: Interner, T: TypeFoldable<I>> TypeFoldable<I> for Option<T> {
74+
fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
75+
Ok(match self {
76+
Some(v) => Some(v.try_fold_with(folder)?),
77+
None => None,
78+
})
79+
}
80+
}
81+
82+
impl<I: Interner, T: TypeVisitable<I>> TypeVisitable<I> for Option<T> {
83+
fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
84+
match self {
85+
Some(v) => v.visit_with(visitor),
86+
None => ControlFlow::Continue(()),
87+
}
88+
}
89+
}
90+
91+
impl<I: Interner, T: TypeFoldable<I>, E: TypeFoldable<I>> TypeFoldable<I> for Result<T, E> {
92+
fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
93+
Ok(match self {
94+
Ok(v) => Ok(v.try_fold_with(folder)?),
95+
Err(e) => Err(e.try_fold_with(folder)?),
96+
})
97+
}
98+
}
99+
100+
impl<I: Interner, T: TypeVisitable<I>, E: TypeVisitable<I>> TypeVisitable<I> for Result<T, E> {
101+
fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
102+
match self {
103+
Ok(v) => v.visit_with(visitor),
104+
Err(e) => e.visit_with(visitor),
105+
}
106+
}
97107
}
98108

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

0 commit comments

Comments
 (0)