Skip to content

Commit 90009f3

Browse files
authored
Unrolled build for rust-lang#124382
Rollup merge of rust-lang#124382 - petrochenkov:itemvisit, r=lcnr ast: Generalize item kind visiting And avoid duplicating logic for visiting `Item`s with different kinds (regular, associated, foreign). The diff is better viewed with whitespace ignored.
2 parents 9ac33d9 + 7517a4f commit 90009f3

File tree

18 files changed

+410
-382
lines changed

18 files changed

+410
-382
lines changed

Diff for: compiler/rustc_ast/src/mut_visit.rs

+176-184
Large diffs are not rendered by default.

Diff for: compiler/rustc_ast/src/visit.rs

+168-130
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ pub enum LifetimeCtxt {
102102
GenericArg,
103103
}
104104

105+
pub trait WalkItemKind: Sized {
106+
fn walk<'a, V: Visitor<'a>>(
107+
&'a self,
108+
item: &'a Item<Self>,
109+
ctxt: AssocCtxt,
110+
visitor: &mut V,
111+
) -> V::Result;
112+
}
113+
105114
/// Each method of the `Visitor` trait is a hook to be potentially
106115
/// overridden. Each method's default implementation recursively visits
107116
/// the substructure of the input via the corresponding `walk` method;
@@ -120,7 +129,7 @@ pub trait Visitor<'ast>: Sized {
120129
Self::Result::output()
121130
}
122131
fn visit_foreign_item(&mut self, i: &'ast ForeignItem) -> Self::Result {
123-
walk_foreign_item(self, i)
132+
walk_item(self, i)
124133
}
125134
fn visit_item(&mut self, i: &'ast Item) -> Self::Result {
126135
walk_item(self, i)
@@ -312,87 +321,98 @@ pub fn walk_trait_ref<'a, V: Visitor<'a>>(visitor: &mut V, trait_ref: &'a TraitR
312321
visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
313322
}
314323

315-
pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) -> V::Result {
316-
try_visit!(visitor.visit_vis(&item.vis));
317-
try_visit!(visitor.visit_ident(item.ident));
318-
match &item.kind {
319-
ItemKind::ExternCrate(_) => {}
320-
ItemKind::Use(use_tree) => try_visit!(visitor.visit_use_tree(use_tree, item.id, false)),
321-
ItemKind::Static(box StaticItem { ty, mutability: _, expr }) => {
322-
try_visit!(visitor.visit_ty(ty));
323-
visit_opt!(visitor, visit_expr, expr);
324-
}
325-
ItemKind::Const(box ConstItem { defaultness: _, generics, ty, expr }) => {
326-
try_visit!(visitor.visit_generics(generics));
327-
try_visit!(visitor.visit_ty(ty));
328-
visit_opt!(visitor, visit_expr, expr);
329-
}
330-
ItemKind::Fn(box Fn { defaultness: _, generics, sig, body }) => {
331-
let kind =
332-
FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, generics, body.as_deref());
333-
try_visit!(visitor.visit_fn(kind, item.span, item.id));
334-
}
335-
ItemKind::Mod(_unsafety, mod_kind) => match mod_kind {
336-
ModKind::Loaded(items, _inline, _inner_span) => {
337-
walk_list!(visitor, visit_item, items);
324+
impl WalkItemKind for ItemKind {
325+
fn walk<'a, V: Visitor<'a>>(
326+
&'a self,
327+
item: &'a Item<Self>,
328+
_ctxt: AssocCtxt,
329+
visitor: &mut V,
330+
) -> V::Result {
331+
match self {
332+
ItemKind::ExternCrate(_) => {}
333+
ItemKind::Use(use_tree) => try_visit!(visitor.visit_use_tree(use_tree, item.id, false)),
334+
ItemKind::Static(box StaticItem { ty, mutability: _, expr }) => {
335+
try_visit!(visitor.visit_ty(ty));
336+
visit_opt!(visitor, visit_expr, expr);
338337
}
339-
ModKind::Unloaded => {}
340-
},
341-
ItemKind::ForeignMod(foreign_module) => {
342-
walk_list!(visitor, visit_foreign_item, &foreign_module.items);
343-
}
344-
ItemKind::GlobalAsm(asm) => try_visit!(visitor.visit_inline_asm(asm)),
345-
ItemKind::TyAlias(box TyAlias { generics, bounds, ty, .. }) => {
346-
try_visit!(visitor.visit_generics(generics));
347-
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
348-
visit_opt!(visitor, visit_ty, ty);
349-
}
350-
ItemKind::Enum(enum_definition, generics) => {
351-
try_visit!(visitor.visit_generics(generics));
352-
try_visit!(visitor.visit_enum_def(enum_definition));
353-
}
354-
ItemKind::Impl(box Impl {
355-
defaultness: _,
356-
unsafety: _,
357-
generics,
358-
constness: _,
359-
polarity: _,
360-
of_trait,
361-
self_ty,
362-
items,
363-
}) => {
364-
try_visit!(visitor.visit_generics(generics));
365-
visit_opt!(visitor, visit_trait_ref, of_trait);
366-
try_visit!(visitor.visit_ty(self_ty));
367-
walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Impl);
368-
}
369-
ItemKind::Struct(struct_definition, generics)
370-
| ItemKind::Union(struct_definition, generics) => {
371-
try_visit!(visitor.visit_generics(generics));
372-
try_visit!(visitor.visit_variant_data(struct_definition));
373-
}
374-
ItemKind::Trait(box Trait { unsafety: _, is_auto: _, generics, bounds, items }) => {
375-
try_visit!(visitor.visit_generics(generics));
376-
walk_list!(visitor, visit_param_bound, bounds, BoundKind::SuperTraits);
377-
walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Trait);
378-
}
379-
ItemKind::TraitAlias(generics, bounds) => {
380-
try_visit!(visitor.visit_generics(generics));
381-
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
382-
}
383-
ItemKind::MacCall(mac) => try_visit!(visitor.visit_mac_call(mac)),
384-
ItemKind::MacroDef(ts) => try_visit!(visitor.visit_mac_def(ts, item.id)),
385-
ItemKind::Delegation(box Delegation { id, qself, path, rename, body }) => {
386-
if let Some(qself) = qself {
387-
try_visit!(visitor.visit_ty(&qself.ty));
338+
ItemKind::Const(box ConstItem { defaultness: _, generics, ty, expr }) => {
339+
try_visit!(visitor.visit_generics(generics));
340+
try_visit!(visitor.visit_ty(ty));
341+
visit_opt!(visitor, visit_expr, expr);
342+
}
343+
ItemKind::Fn(box Fn { defaultness: _, generics, sig, body }) => {
344+
let kind =
345+
FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, generics, body.as_deref());
346+
try_visit!(visitor.visit_fn(kind, item.span, item.id));
347+
}
348+
ItemKind::Mod(_unsafety, mod_kind) => match mod_kind {
349+
ModKind::Loaded(items, _inline, _inner_span) => {
350+
walk_list!(visitor, visit_item, items);
351+
}
352+
ModKind::Unloaded => {}
353+
},
354+
ItemKind::ForeignMod(foreign_module) => {
355+
walk_list!(visitor, visit_foreign_item, &foreign_module.items);
356+
}
357+
ItemKind::GlobalAsm(asm) => try_visit!(visitor.visit_inline_asm(asm)),
358+
ItemKind::TyAlias(box TyAlias { generics, bounds, ty, .. }) => {
359+
try_visit!(visitor.visit_generics(generics));
360+
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
361+
visit_opt!(visitor, visit_ty, ty);
362+
}
363+
ItemKind::Enum(enum_definition, generics) => {
364+
try_visit!(visitor.visit_generics(generics));
365+
try_visit!(visitor.visit_enum_def(enum_definition));
366+
}
367+
ItemKind::Impl(box Impl {
368+
defaultness: _,
369+
unsafety: _,
370+
generics,
371+
constness: _,
372+
polarity: _,
373+
of_trait,
374+
self_ty,
375+
items,
376+
}) => {
377+
try_visit!(visitor.visit_generics(generics));
378+
visit_opt!(visitor, visit_trait_ref, of_trait);
379+
try_visit!(visitor.visit_ty(self_ty));
380+
walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Impl);
381+
}
382+
ItemKind::Struct(struct_definition, generics)
383+
| ItemKind::Union(struct_definition, generics) => {
384+
try_visit!(visitor.visit_generics(generics));
385+
try_visit!(visitor.visit_variant_data(struct_definition));
386+
}
387+
ItemKind::Trait(box Trait { unsafety: _, is_auto: _, generics, bounds, items }) => {
388+
try_visit!(visitor.visit_generics(generics));
389+
walk_list!(visitor, visit_param_bound, bounds, BoundKind::SuperTraits);
390+
walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Trait);
391+
}
392+
ItemKind::TraitAlias(generics, bounds) => {
393+
try_visit!(visitor.visit_generics(generics));
394+
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
395+
}
396+
ItemKind::MacCall(mac) => try_visit!(visitor.visit_mac_call(mac)),
397+
ItemKind::MacroDef(ts) => try_visit!(visitor.visit_mac_def(ts, item.id)),
398+
ItemKind::Delegation(box Delegation { id, qself, path, rename, body }) => {
399+
if let Some(qself) = qself {
400+
try_visit!(visitor.visit_ty(&qself.ty));
401+
}
402+
try_visit!(visitor.visit_path(path, *id));
403+
visit_opt!(visitor, visit_ident, *rename);
404+
visit_opt!(visitor, visit_block, body);
388405
}
389-
try_visit!(visitor.visit_path(path, *id));
390-
visit_opt!(visitor, visit_ident, *rename);
391-
visit_opt!(visitor, visit_block, body);
392406
}
407+
V::Result::output()
393408
}
394-
walk_list!(visitor, visit_attribute, &item.attrs);
395-
V::Result::output()
409+
}
410+
411+
pub fn walk_item<'a, V: Visitor<'a>>(
412+
visitor: &mut V,
413+
item: &'a Item<impl WalkItemKind>,
414+
) -> V::Result {
415+
walk_assoc_item(visitor, item, AssocCtxt::Trait /*ignored*/)
396416
}
397417

398418
pub fn walk_enum_def<'a, V: Visitor<'a>>(
@@ -613,30 +633,34 @@ pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) -> V::Res
613633
V::Result::output()
614634
}
615635

616-
pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignItem) -> V::Result {
617-
let &Item { id, span, ident, ref vis, ref attrs, ref kind, tokens: _ } = item;
618-
try_visit!(visitor.visit_vis(vis));
619-
try_visit!(visitor.visit_ident(ident));
620-
walk_list!(visitor, visit_attribute, attrs);
621-
match kind {
622-
ForeignItemKind::Static(ty, _, expr) => {
623-
try_visit!(visitor.visit_ty(ty));
624-
visit_opt!(visitor, visit_expr, expr);
625-
}
626-
ForeignItemKind::Fn(box Fn { defaultness: _, generics, sig, body }) => {
627-
let kind = FnKind::Fn(FnCtxt::Foreign, ident, sig, vis, generics, body.as_deref());
628-
try_visit!(visitor.visit_fn(kind, span, id));
629-
}
630-
ForeignItemKind::TyAlias(box TyAlias { generics, bounds, ty, .. }) => {
631-
try_visit!(visitor.visit_generics(generics));
632-
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
633-
visit_opt!(visitor, visit_ty, ty);
634-
}
635-
ForeignItemKind::MacCall(mac) => {
636-
try_visit!(visitor.visit_mac_call(mac));
636+
impl WalkItemKind for ForeignItemKind {
637+
fn walk<'a, V: Visitor<'a>>(
638+
&'a self,
639+
item: &'a Item<Self>,
640+
_ctxt: AssocCtxt,
641+
visitor: &mut V,
642+
) -> V::Result {
643+
let &Item { id, span, ident, ref vis, .. } = item;
644+
match self {
645+
ForeignItemKind::Static(ty, _, expr) => {
646+
try_visit!(visitor.visit_ty(ty));
647+
visit_opt!(visitor, visit_expr, expr);
648+
}
649+
ForeignItemKind::Fn(box Fn { defaultness: _, generics, sig, body }) => {
650+
let kind = FnKind::Fn(FnCtxt::Foreign, ident, sig, vis, generics, body.as_deref());
651+
try_visit!(visitor.visit_fn(kind, span, id));
652+
}
653+
ForeignItemKind::TyAlias(box TyAlias { generics, bounds, ty, .. }) => {
654+
try_visit!(visitor.visit_generics(generics));
655+
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
656+
visit_opt!(visitor, visit_ty, ty);
657+
}
658+
ForeignItemKind::MacCall(mac) => {
659+
try_visit!(visitor.visit_mac_call(mac));
660+
}
637661
}
662+
V::Result::output()
638663
}
639-
V::Result::output()
640664
}
641665

642666
pub fn walk_param_bound<'a, V: Visitor<'a>>(visitor: &mut V, bound: &'a GenericBound) -> V::Result {
@@ -756,42 +780,56 @@ pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>) -> V::Resu
756780
V::Result::output()
757781
}
758782

783+
impl WalkItemKind for AssocItemKind {
784+
fn walk<'a, V: Visitor<'a>>(
785+
&'a self,
786+
item: &'a Item<Self>,
787+
ctxt: AssocCtxt,
788+
visitor: &mut V,
789+
) -> V::Result {
790+
let &Item { id, span, ident, ref vis, .. } = item;
791+
match self {
792+
AssocItemKind::Const(box ConstItem { defaultness: _, generics, ty, expr }) => {
793+
try_visit!(visitor.visit_generics(generics));
794+
try_visit!(visitor.visit_ty(ty));
795+
visit_opt!(visitor, visit_expr, expr);
796+
}
797+
AssocItemKind::Fn(box Fn { defaultness: _, generics, sig, body }) => {
798+
let kind =
799+
FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, vis, generics, body.as_deref());
800+
try_visit!(visitor.visit_fn(kind, span, id));
801+
}
802+
AssocItemKind::Type(box TyAlias { generics, bounds, ty, .. }) => {
803+
try_visit!(visitor.visit_generics(generics));
804+
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
805+
visit_opt!(visitor, visit_ty, ty);
806+
}
807+
AssocItemKind::MacCall(mac) => {
808+
try_visit!(visitor.visit_mac_call(mac));
809+
}
810+
AssocItemKind::Delegation(box Delegation { id, qself, path, rename, body }) => {
811+
if let Some(qself) = qself {
812+
try_visit!(visitor.visit_ty(&qself.ty));
813+
}
814+
try_visit!(visitor.visit_path(path, *id));
815+
visit_opt!(visitor, visit_ident, *rename);
816+
visit_opt!(visitor, visit_block, body);
817+
}
818+
}
819+
V::Result::output()
820+
}
821+
}
822+
759823
pub fn walk_assoc_item<'a, V: Visitor<'a>>(
760824
visitor: &mut V,
761-
item: &'a AssocItem,
825+
item: &'a Item<impl WalkItemKind>,
762826
ctxt: AssocCtxt,
763827
) -> V::Result {
764-
let &Item { id, span, ident, ref vis, ref attrs, ref kind, tokens: _ } = item;
828+
let &Item { id: _, span: _, ident, ref vis, ref attrs, ref kind, tokens: _ } = item;
829+
walk_list!(visitor, visit_attribute, attrs);
765830
try_visit!(visitor.visit_vis(vis));
766831
try_visit!(visitor.visit_ident(ident));
767-
walk_list!(visitor, visit_attribute, attrs);
768-
match kind {
769-
AssocItemKind::Const(box ConstItem { defaultness: _, generics, ty, expr }) => {
770-
try_visit!(visitor.visit_generics(generics));
771-
try_visit!(visitor.visit_ty(ty));
772-
visit_opt!(visitor, visit_expr, expr);
773-
}
774-
AssocItemKind::Fn(box Fn { defaultness: _, generics, sig, body }) => {
775-
let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, vis, generics, body.as_deref());
776-
try_visit!(visitor.visit_fn(kind, span, id));
777-
}
778-
AssocItemKind::Type(box TyAlias { generics, bounds, ty, .. }) => {
779-
try_visit!(visitor.visit_generics(generics));
780-
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
781-
visit_opt!(visitor, visit_ty, ty);
782-
}
783-
AssocItemKind::MacCall(mac) => {
784-
try_visit!(visitor.visit_mac_call(mac));
785-
}
786-
AssocItemKind::Delegation(box Delegation { id, qself, path, rename, body }) => {
787-
if let Some(qself) = qself {
788-
try_visit!(visitor.visit_ty(&qself.ty));
789-
}
790-
try_visit!(visitor.visit_path(path, *id));
791-
visit_opt!(visitor, visit_ident, *rename);
792-
visit_opt!(visitor, visit_block, body);
793-
}
794-
}
832+
try_visit!(kind.walk(item, ctxt, visitor));
795833
V::Result::output()
796834
}
797835

Diff for: compiler/rustc_ast_lowering/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ fn index_crate<'a>(
394394
let def_id = self.node_id_to_def_id[&item.id];
395395
*self.index.ensure_contains_elem(def_id, || AstOwner::NonOwner) =
396396
AstOwner::ForeignItem(item);
397-
visit::walk_foreign_item(self, item);
397+
visit::walk_item(self, item);
398398
}
399399
}
400400
}

Diff for: compiler/rustc_ast_passes/src/ast_validation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11921192
ForeignItemKind::MacCall(..) => {}
11931193
}
11941194

1195-
visit::walk_foreign_item(self, fi)
1195+
visit::walk_item(self, fi)
11961196
}
11971197

11981198
// Mirrors `visit::walk_generic_args`, but tracks relevant state.

Diff for: compiler/rustc_ast_passes/src/feature_gate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
319319
ast::ForeignItemKind::MacCall(..) => {}
320320
}
321321

322-
visit::walk_foreign_item(self, i)
322+
visit::walk_item(self, i)
323323
}
324324

325325
fn visit_ty(&mut self, ty: &'a ast::Ty) {

Diff for: compiler/rustc_ast_passes/src/node_count.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl<'ast> Visitor<'ast> for NodeCounter {
2121
}
2222
fn visit_foreign_item(&mut self, i: &ForeignItem) {
2323
self.count += 1;
24-
walk_foreign_item(self, i)
24+
walk_item(self, i)
2525
}
2626
fn visit_item(&mut self, i: &Item) {
2727
self.count += 1;

0 commit comments

Comments
 (0)