Skip to content

Commit 56ec8c1

Browse files
committed
Duplicate macros instead of always having both cases.
1 parent 5634083 commit 56ec8c1

File tree

2 files changed

+135
-122
lines changed

2 files changed

+135
-122
lines changed

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 105 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ use crate::ast::*;
2020
use crate::ptr::P;
2121
use crate::tokenstream::*;
2222
use crate::visit::{AssocCtxt, BoundKind, FnCtxt, LifetimeCtxt, VisitorResult, try_visit};
23-
use crate::{
24-
define_named_walk, impl_visitable, impl_visitable_calling, impl_visitable_calling_walkable,
25-
impl_visitable_direct, impl_visitable_list, impl_visitable_noop, impl_walkable,
26-
visit_visitable, visit_visitable_with, walk_walkable,
27-
};
2823

2924
mod sealed {
3025
use rustc_ast_ir::visit::VisitorResult;
@@ -160,6 +155,111 @@ pub trait MutWalkable<V: MutVisitor> {
160155
fn walk_mut(&mut self, visitor: &mut V);
161156
}
162157

158+
macro_rules! visit_visitable {
159+
(mut $visitor:expr, $($expr:expr),* $(,)?) => {{
160+
$(MutVisitable::visit_mut($expr, $visitor, ());)*
161+
}};
162+
}
163+
164+
macro_rules! visit_visitable_with {
165+
(mut $visitor:expr, $expr:expr, $extra:expr $(,)?) => {
166+
MutVisitable::visit_mut($expr, $visitor, $extra)
167+
};
168+
}
169+
170+
macro_rules! walk_walkable {
171+
($visitor:expr, $expr:expr, mut) => {
172+
MutWalkable::walk_mut($expr, $visitor)
173+
};
174+
}
175+
176+
macro_rules! impl_visitable {
177+
(|&mut $self:ident: $self_ty:ty,
178+
$vis:ident: &mut $vis_ty:ident,
179+
$extra:ident: $extra_ty:ty| $block:block) => {
180+
#[allow(unused_parens, non_local_definitions)]
181+
impl<$vis_ty: MutVisitor> MutVisitable<$vis_ty> for $self_ty {
182+
type Extra = $extra_ty;
183+
fn visit_mut(&mut $self, $vis: &mut $vis_ty, $extra: Self::Extra) -> V::Result {
184+
$block
185+
}
186+
}
187+
};
188+
}
189+
190+
macro_rules! impl_walkable {
191+
($(<$K:ident: $Kb:ident>)? |&mut $self:ident: $self_ty:ty,
192+
$vis:ident: &mut $vis_ty:ident| $block:block) => {
193+
#[allow(unused_parens, non_local_definitions)]
194+
impl<$($K: $Kb,)? $vis_ty: MutVisitor> MutWalkable<$vis_ty> for $self_ty {
195+
fn walk_mut(&mut $self, $vis: &mut $vis_ty) -> V::Result {
196+
$block
197+
}
198+
}
199+
};
200+
}
201+
202+
macro_rules! impl_visitable_noop {
203+
(<mut> $($ty:ty,)*) => {
204+
$(
205+
impl_visitable!(|&mut self: $ty, _vis: &mut V, _extra: ()| {});
206+
)*
207+
};
208+
}
209+
210+
macro_rules! impl_visitable_list {
211+
(<mut> $($ty:ty,)*) => {
212+
$(impl<V: MutVisitor, T> MutVisitable<V> for $ty
213+
where
214+
for<'a> &'a mut $ty: IntoIterator<Item = &'a mut T>,
215+
T: MutVisitable<V>,
216+
{
217+
type Extra = <T as MutVisitable<V>>::Extra;
218+
219+
#[inline]
220+
fn visit_mut(&mut self, visitor: &mut V, extra: Self::Extra) {
221+
for i in self {
222+
i.visit_mut(visitor, extra);
223+
}
224+
}
225+
})*
226+
}
227+
}
228+
229+
macro_rules! impl_visitable_direct {
230+
(<mut> $($ty:ty,)*) => {
231+
$(impl_visitable!(
232+
|&mut self: $ty, visitor: &mut V, _extra: ()| {
233+
MutWalkable::walk_mut(self, visitor)
234+
}
235+
);)*
236+
}
237+
}
238+
239+
macro_rules! impl_visitable_calling_walkable {
240+
(<mut>
241+
$( fn $method:ident($ty:ty $(, $extra_name:ident: $extra_ty:ty)?); )*
242+
) => {
243+
$(fn $method(&mut self, node: &mut $ty $(, $extra_name:$extra_ty)?) {
244+
impl_visitable!(|&mut self: $ty, visitor: &mut V, extra: ($($extra_ty)?)| {
245+
let ($($extra_name)?) = extra;
246+
visitor.$method(self $(, $extra_name)?);
247+
});
248+
walk_walkable!(self, node, mut)
249+
})*
250+
}
251+
}
252+
253+
macro_rules! define_named_walk {
254+
((mut) $Visitor:ident
255+
$( pub fn $method:ident($ty:ty); )*
256+
) => {
257+
$(pub fn $method<V: $Visitor>(visitor: &mut V, node: &mut $ty) {
258+
walk_walkable!(visitor, node, mut)
259+
})*
260+
};
261+
}
262+
163263
super::common_visitor_and_walkers!((mut) MutVisitor);
164264

165265
macro_rules! generate_flat_map_visitor_fns {

compiler/rustc_ast/src/visit.rs

Lines changed: 30 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,24 @@ pub(crate) trait Walkable<'a, V: Visitor<'a>> {
202202
fn walk_ref(&'a self, visitor: &mut V) -> V::Result;
203203
}
204204

205-
#[macro_export]
205+
macro_rules! visit_visitable {
206+
($visitor:expr, $($expr:expr),* $(,)?) => {{
207+
$(try_visit!(Visitable::visit($expr, $visitor, ()));)*
208+
}};
209+
}
210+
211+
macro_rules! visit_visitable_with {
212+
($visitor:expr, $expr:expr, $extra:expr $(,)?) => {
213+
try_visit!(Visitable::visit($expr, $visitor, $extra))
214+
};
215+
}
216+
217+
macro_rules! walk_walkable {
218+
($visitor:expr, $expr:expr, ) => {
219+
Walkable::walk_ref($expr, $visitor)
220+
};
221+
}
222+
206223
macro_rules! impl_visitable {
207224
(|&$lt:lifetime $self:ident: $self_ty:ty,
208225
$vis:ident: &mut $vis_ty:ident,
@@ -215,20 +232,8 @@ macro_rules! impl_visitable {
215232
}
216233
}
217234
};
218-
(|&mut $self:ident: $self_ty:ty,
219-
$vis:ident: &mut $vis_ty:ident,
220-
$extra:ident: $extra_ty:ty| $block:block) => {
221-
#[allow(unused_parens, non_local_definitions)]
222-
impl<$vis_ty: MutVisitor> MutVisitable<$vis_ty> for $self_ty {
223-
type Extra = $extra_ty;
224-
fn visit_mut(&mut $self, $vis: &mut $vis_ty, $extra: Self::Extra) {
225-
$block
226-
}
227-
}
228-
};
229235
}
230236

231-
#[macro_export]
232237
macro_rules! impl_walkable {
233238
($(<$K:ident: $Kb:ident>)? |&$lt:lifetime $self:ident: $self_ty:ty,
234239
$vis:ident: &mut $vis_ty:ident| $block:block) => {
@@ -239,18 +244,8 @@ macro_rules! impl_walkable {
239244
}
240245
}
241246
};
242-
($(<$K:ident: $Kb:ident>)? |&mut $self:ident: $self_ty:ty,
243-
$vis:ident: &mut $vis_ty:ident| $block:block) => {
244-
#[allow(unused_parens, non_local_definitions)]
245-
impl<$($K: $Kb,)? $vis_ty: MutVisitor> MutWalkable<$vis_ty> for $self_ty {
246-
fn walk_mut(&mut $self, $vis: &mut $vis_ty) {
247-
$block
248-
}
249-
}
250-
};
251247
}
252248

253-
#[macro_export]
254249
macro_rules! impl_visitable_noop {
255250
(<$lt:lifetime> $($ty:ty,)*) => {
256251
$(
@@ -259,16 +254,8 @@ macro_rules! impl_visitable_noop {
259254
});
260255
)*
261256
};
262-
(<mut> $($ty:ty,)*) => {
263-
$(
264-
impl_visitable!(|&mut self: $ty, _vis: &mut V, _extra: ()| {});
265-
)*
266-
}
267257
}
268258

269-
impl_visitable_noop!(<'ast> Span,);
270-
271-
#[macro_export]
272259
macro_rules! impl_visitable_list {
273260
(<$lt:lifetime> $($ty:ty,)*) => {
274261
$(impl<$lt, V: Visitor<$lt>, T> Visitable<$lt, V> for $ty
@@ -287,25 +274,8 @@ macro_rules! impl_visitable_list {
287274
}
288275
})*
289276
};
290-
(<mut> $($ty:ty,)*) => {
291-
$(impl<V: MutVisitor, T> MutVisitable<V> for $ty
292-
where
293-
for<'a> &'a mut $ty: IntoIterator<Item = &'a mut T>,
294-
T: MutVisitable<V>,
295-
{
296-
type Extra = <T as MutVisitable<V>>::Extra;
297-
298-
#[inline]
299-
fn visit_mut(&mut self, visitor: &mut V, extra: Self::Extra) {
300-
for i in self {
301-
i.visit_mut(visitor, extra);
302-
}
303-
}
304-
})*
305-
}
306277
}
307278

308-
#[macro_export]
309279
macro_rules! impl_visitable_direct {
310280
(<$lt:lifetime> $($ty:ty,)*) => {
311281
$(impl_visitable!(
@@ -314,78 +284,22 @@ macro_rules! impl_visitable_direct {
314284
}
315285
);)*
316286
};
317-
(<mut> $($ty:ty,)*) => {
318-
$(impl_visitable!(
319-
|&mut self: $ty, visitor: &mut V, _extra: ()| {
320-
MutWalkable::walk_mut(self, visitor)
321-
}
322-
);)*
323-
}
324-
}
325-
326-
#[macro_export]
327-
macro_rules! impl_visitable_calling {
328-
($method:ident<$($lt:lifetime)? $($mut:ident)?>($ty:ty $(, $extra:ident: $extra_ty:ty)?)) => {
329-
impl_visitable!(
330-
|&$($lt)? $($mut)? self: $ty, visitor: &mut V, extra: ($($extra_ty)?)| {
331-
let ($($extra)?) = extra;
332-
visitor.$method(self $(, $extra)?)
333-
}
334-
)
335-
};
336-
}
337-
338-
#[macro_export]
339-
macro_rules! visit_visitable {
340-
($visitor:expr, $($expr:expr),* $(,)?) => {{
341-
$(try_visit!(Visitable::visit($expr, $visitor, ()));)*
342-
}};
343-
(mut $visitor:expr, $($expr:expr),* $(,)?) => {{
344-
$(MutVisitable::visit_mut($expr, $visitor, ());)*
345-
}};
346287
}
347288

348-
#[macro_export]
349-
macro_rules! visit_visitable_with {
350-
($visitor:expr, $expr:expr, $extra:expr $(,)?) => {
351-
try_visit!(Visitable::visit($expr, $visitor, $extra))
352-
};
353-
(mut $visitor:expr, $expr:expr, $extra:expr $(,)?) => {
354-
MutVisitable::visit_mut($expr, $visitor, $extra)
355-
};
356-
}
357-
358-
#[macro_export]
359-
macro_rules! walk_walkable {
360-
($visitor:expr, $expr:expr, ) => {
361-
Walkable::walk_ref($expr, $visitor)
362-
};
363-
($visitor:expr, $expr:expr, mut) => {
364-
MutWalkable::walk_mut($expr, $visitor)
365-
};
366-
}
367-
368-
#[macro_export]
369289
macro_rules! impl_visitable_calling_walkable {
370290
(<$lt:lifetime>
371291
$( fn $method:ident($ty:ty $(, $extra_name:ident: $extra_ty:ty)?); )*
372292
) => {
373293
$(fn $method(&mut self, node: &$lt $ty $(, $extra_name:$extra_ty)?) -> Self::Result {
374-
impl_visitable_calling!($method<$lt>($ty $(, $extra_name:$extra_ty)?));
294+
impl_visitable!(|&$lt self: $ty, visitor: &mut V, extra: ($($extra_ty)?)| {
295+
let ($($extra_name)?) = extra;
296+
visitor.$method(self $(, $extra_name)?)
297+
});
375298
walk_walkable!(self, node, )
376299
})*
377300
};
378-
(<mut>
379-
$( fn $method:ident($ty:ty $(, $extra_name:ident: $extra_ty:ty)?); )*
380-
) => {
381-
$(fn $method(&mut self, node: &mut $ty $(, $extra_name:$extra_ty)?) {
382-
impl_visitable_calling!($method<mut>($ty $(, $extra_name:$extra_ty)?));
383-
walk_walkable!(self, node, mut)
384-
})*
385-
}
386301
}
387302

388-
#[macro_export]
389303
macro_rules! define_named_walk {
390304
($Visitor:ident<$lt:lifetime>
391305
$( pub fn $method:ident($ty:ty); )*
@@ -394,13 +308,6 @@ macro_rules! define_named_walk {
394308
walk_walkable!(visitor, node,)
395309
})*
396310
};
397-
((mut) $Visitor:ident
398-
$( pub fn $method:ident($ty:ty); )*
399-
) => {
400-
$(pub fn $method<V: $Visitor>(visitor: &mut V, node: &mut $ty) {
401-
walk_walkable!(visitor, node, mut)
402-
})*
403-
};
404311
}
405312

406313
#[macro_export]
@@ -470,6 +377,8 @@ macro_rules! common_visitor_and_walkers {
470377
u8,
471378
usize,
472379
);
380+
// `Span` is only a no-op for the non-mutable visitor.
381+
$(impl_visitable_noop!(<$lt> Span,);)?
473382

474383
// This macro generates `impl Visitable` and `impl MutVisitable` that simply iterate over
475384
// their contents. We do not use a generic impl for `ThinVec` because we want to allow
@@ -628,7 +537,9 @@ macro_rules! common_visitor_and_walkers {
628537
// field access version will continue working and it would be easy to
629538
// forget to add handling for it.
630539
fn visit_ident(&mut self, Ident { name: _, span }: &$($lt)? $($mut)? Ident) -> Self::Result {
631-
impl_visitable_calling!(visit_ident<$($lt)? $($mut)?>(Ident));
540+
impl_visitable!(|&$($lt)? $($mut)? self: Ident, visitor: &mut V, _extra: ()| {
541+
visitor.visit_ident(self)
542+
});
632543
visit_span(self, span)
633544
}
634545

@@ -759,7 +670,9 @@ macro_rules! common_visitor_and_walkers {
759670
// in case it's needed for something like #127241.
760671
#[inline]
761672
fn visit_span(&mut self, _sp: &$mut Span) {
762-
impl_visitable_calling!(visit_span<$mut>(Span));
673+
impl_visitable!(|&mut self: Span, visitor: &mut V, _extra: ()| {
674+
visitor.visit_span(self)
675+
});
763676
// Do nothing.
764677
}
765678

0 commit comments

Comments
 (0)