Skip to content

Commit 9a1ce4a

Browse files
authored
Unrolled build for rust-lang#141249
Rollup merge of rust-lang#141249 - fee1-dead-contrib:push-mwxxsvrsotvs, r=oli-obk introduce common macro for `MutVisitor` and `Visitor` to dedup code helps with rust-lang#127615. I can do everything in one go but I figured it might be worth it to open a PR first for vibeck. r? oli-obk
2 parents e5a2a6a + c5bab6e commit 9a1ce4a

File tree

3 files changed

+73
-49
lines changed

3 files changed

+73
-49
lines changed

compiler/rustc_ast/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#![feature(associated_type_defaults)]
1616
#![feature(box_patterns)]
1717
#![feature(if_let_guard)]
18+
#![feature(macro_metavar_expr)]
1819
#![feature(negative_impls)]
1920
#![feature(never_type)]
2021
#![feature(rustdoc_internals)]

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use thin_vec::ThinVec;
2020
use crate::ast::*;
2121
use crate::ptr::P;
2222
use crate::tokenstream::*;
23-
use crate::visit::{AssocCtxt, BoundKind, FnCtxt};
23+
use crate::visit::{AssocCtxt, BoundKind, FnCtxt, try_visit};
2424

2525
pub trait ExpectOne<A: Array> {
2626
fn expect_one(self, err: &'static str) -> A::Item;
@@ -388,6 +388,8 @@ pub trait MutVisitor: Sized {
388388
}
389389
}
390390

391+
super::common_visitor_and_walkers!((mut) MutVisitor);
392+
391393
/// Use a map-style function (`FnOnce(T) -> T`) to overwrite a `&mut T`. Useful
392394
/// when using a `flat_map_*` or `filter_map_*` method within a `visit_`
393395
/// method.
@@ -777,15 +779,6 @@ fn visit_defaultness<T: MutVisitor>(vis: &mut T, defaultness: &mut Defaultness)
777779
}
778780
}
779781

780-
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
781-
fn visit_safety<T: MutVisitor>(vis: &mut T, safety: &mut Safety) {
782-
match safety {
783-
Safety::Unsafe(span) => vis.visit_span(span),
784-
Safety::Safe(span) => vis.visit_span(span),
785-
Safety::Default => {}
786-
}
787-
}
788-
789782
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
790783
fn visit_polarity<T: MutVisitor>(vis: &mut T, polarity: &mut ImplPolarity) {
791784
match polarity {
@@ -794,14 +787,6 @@ fn visit_polarity<T: MutVisitor>(vis: &mut T, polarity: &mut ImplPolarity) {
794787
}
795788
}
796789

797-
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
798-
fn visit_constness<T: MutVisitor>(vis: &mut T, constness: &mut Const) {
799-
match constness {
800-
Const::Yes(span) => vis.visit_span(span),
801-
Const::No => {}
802-
}
803-
}
804-
805790
fn walk_closure_binder<T: MutVisitor>(vis: &mut T, binder: &mut ClosureBinder) {
806791
match binder {
807792
ClosureBinder::NotPresent => {}
@@ -940,15 +925,6 @@ pub fn walk_flat_map_generic_param<T: MutVisitor>(
940925
smallvec![param]
941926
}
942927

943-
fn walk_label<T: MutVisitor>(vis: &mut T, Label { ident }: &mut Label) {
944-
vis.visit_ident(ident);
945-
}
946-
947-
fn walk_lifetime<T: MutVisitor>(vis: &mut T, Lifetime { id, ident }: &mut Lifetime) {
948-
vis.visit_id(id);
949-
vis.visit_ident(ident);
950-
}
951-
952928
fn walk_generics<T: MutVisitor>(vis: &mut T, generics: &mut Generics) {
953929
let Generics { params, where_clause, span } = generics;
954930
params.flat_map_in_place(|param| vis.flat_map_generic_param(param));
@@ -1340,13 +1316,6 @@ fn walk_const_item<T: MutVisitor>(vis: &mut T, item: &mut ConstItem) {
13401316
walk_define_opaques(vis, define_opaque);
13411317
}
13421318

1343-
fn walk_fn_header<T: MutVisitor>(vis: &mut T, header: &mut FnHeader) {
1344-
let FnHeader { safety, coroutine_kind, constness, ext: _ } = header;
1345-
visit_constness(vis, constness);
1346-
coroutine_kind.as_mut().map(|coroutine_kind| vis.visit_coroutine_kind(coroutine_kind));
1347-
visit_safety(vis, safety);
1348-
}
1349-
13501319
pub fn walk_crate<T: MutVisitor>(vis: &mut T, krate: &mut Crate) {
13511320
let Crate { attrs, items, spans, id, is_placeholder: _ } = krate;
13521321
vis.visit_id(id);

compiler/rustc_ast/src/visit.rs

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,75 @@ pub trait Visitor<'ast>: Sized {
315315
}
316316
}
317317

318+
#[macro_export]
319+
macro_rules! common_visitor_and_walkers {
320+
($(($mut: ident))? $Visitor:ident$(<$lt:lifetime>)?) => {
321+
// this is only used by the MutVisitor. We include this symmetry here to make writing other functions easier
322+
$(${ignore($lt)}
323+
#[expect(unused, rustc::pass_by_value)]
324+
#[inline]
325+
)?
326+
fn visit_span<$($lt,)? V: $Visitor$(<$lt>)?>(visitor: &mut V, span: &$($lt)? $($mut)? Span) $(-> <V as Visitor<$lt>>::Result)? {
327+
$(
328+
let _ = stringify!($mut);
329+
visitor.visit_span(span);
330+
)?
331+
$(${ignore($lt)}V::Result::output())?
332+
}
333+
334+
// this is only used by the MutVisitor. We include this symmetry here to make writing other functions easier
335+
$(${ignore($lt)}
336+
#[expect(unused, rustc::pass_by_value)]
337+
#[inline]
338+
)?
339+
fn visit_id<$($lt,)? V: $Visitor$(<$lt>)?>(visitor: &mut V, id: &$($lt)? $($mut)? NodeId) $(-> <V as Visitor<$lt>>::Result)? {
340+
$(
341+
let _ = stringify!($mut);
342+
visitor.visit_id(id);
343+
)?
344+
$(${ignore($lt)}V::Result::output())?
345+
}
346+
347+
// this is only used by the MutVisitor. We include this symmetry here to make writing other functions easier
348+
fn visit_safety<$($lt,)? V: $Visitor$(<$lt>)?>(vis: &mut V, safety: &$($lt)? $($mut)? Safety) $(-> <V as Visitor<$lt>>::Result)? {
349+
match safety {
350+
Safety::Unsafe(span) => visit_span(vis, span),
351+
Safety::Safe(span) => visit_span(vis, span),
352+
Safety::Default => { $(${ignore($lt)}V::Result::output())? }
353+
}
354+
}
355+
356+
fn visit_constness<$($lt,)? V: $Visitor$(<$lt>)?>(vis: &mut V, constness: &$($lt)? $($mut)? Const) $(-> <V as Visitor<$lt>>::Result)? {
357+
match constness {
358+
Const::Yes(span) => visit_span(vis, span),
359+
Const::No => {
360+
$(<V as Visitor<$lt>>::Result::output())?
361+
}
362+
}
363+
}
364+
365+
pub fn walk_label<$($lt,)? V: $Visitor$(<$lt>)?>(visitor: &mut V, Label { ident }: &$($lt)? $($mut)? Label) $(-> <V as Visitor<$lt>>::Result)? {
366+
visitor.visit_ident(ident)
367+
}
368+
369+
pub fn walk_fn_header<$($lt,)? V: $Visitor$(<$lt>)?>(visitor: &mut V, header: &$($lt)? $($mut)? FnHeader) $(-> <V as Visitor<$lt>>::Result)? {
370+
let FnHeader { safety, coroutine_kind, constness, ext: _ } = header;
371+
try_visit!(visit_constness(visitor, constness));
372+
if let Some(coroutine_kind) = coroutine_kind {
373+
try_visit!(visitor.visit_coroutine_kind(coroutine_kind));
374+
}
375+
visit_safety(visitor, safety)
376+
}
377+
378+
pub fn walk_lifetime<$($lt,)? V: $Visitor$(<$lt>)?>(visitor: &mut V, Lifetime { id, ident }: &$($lt)? $($mut)? Lifetime) $(-> <V as Visitor<$lt>>::Result)? {
379+
try_visit!(visit_id(visitor, id));
380+
visitor.visit_ident(ident)
381+
}
382+
};
383+
}
384+
385+
common_visitor_and_walkers!(Visitor<'a>);
386+
318387
pub fn walk_crate<'a, V: Visitor<'a>>(visitor: &mut V, krate: &'a Crate) -> V::Result {
319388
let Crate { attrs, items, spans: _, id: _, is_placeholder: _ } = krate;
320389
walk_list!(visitor, visit_attribute, attrs);
@@ -334,15 +403,6 @@ pub fn walk_local<'a, V: Visitor<'a>>(visitor: &mut V, local: &'a Local) -> V::R
334403
V::Result::output()
335404
}
336405

337-
pub fn walk_label<'a, V: Visitor<'a>>(visitor: &mut V, Label { ident }: &'a Label) -> V::Result {
338-
visitor.visit_ident(ident)
339-
}
340-
341-
pub fn walk_lifetime<'a, V: Visitor<'a>>(visitor: &mut V, lifetime: &'a Lifetime) -> V::Result {
342-
let Lifetime { id: _, ident } = lifetime;
343-
visitor.visit_ident(ident)
344-
}
345-
346406
pub fn walk_poly_trait_ref<'a, V>(visitor: &mut V, trait_ref: &'a PolyTraitRef) -> V::Result
347407
where
348408
V: Visitor<'a>,
@@ -926,12 +986,6 @@ pub fn walk_fn_ret_ty<'a, V: Visitor<'a>>(visitor: &mut V, ret_ty: &'a FnRetTy)
926986
V::Result::output()
927987
}
928988

929-
pub fn walk_fn_header<'a, V: Visitor<'a>>(visitor: &mut V, fn_header: &'a FnHeader) -> V::Result {
930-
let FnHeader { safety: _, coroutine_kind, constness: _, ext: _ } = fn_header;
931-
visit_opt!(visitor, visit_coroutine_kind, coroutine_kind.as_ref());
932-
V::Result::output()
933-
}
934-
935989
pub fn walk_fn_decl<'a, V: Visitor<'a>>(
936990
visitor: &mut V,
937991
FnDecl { inputs, output }: &'a FnDecl,

0 commit comments

Comments
 (0)