Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify visitors #100392

Merged
merged 4 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 9 additions & 23 deletions compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,23 +168,17 @@ pub trait Visitor<'ast>: Sized {
fn visit_param_bound(&mut self, bounds: &'ast GenericBound, _ctxt: BoundKind) {
walk_param_bound(self, bounds)
}
fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef, m: &'ast TraitBoundModifier) {
walk_poly_trait_ref(self, t, m)
fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef) {
walk_poly_trait_ref(self, t)
}
fn visit_variant_data(&mut self, s: &'ast VariantData) {
walk_struct_def(self, s)
}
fn visit_field_def(&mut self, s: &'ast FieldDef) {
walk_field_def(self, s)
}
fn visit_enum_def(
&mut self,
enum_definition: &'ast EnumDef,
generics: &'ast Generics,
item_id: NodeId,
_: Span,
) {
walk_enum_def(self, enum_definition, generics, item_id)
fn visit_enum_def(&mut self, enum_definition: &'ast EnumDef) {
walk_enum_def(self, enum_definition)
}
fn visit_variant(&mut self, v: &'ast Variant) {
walk_variant(self, v)
Expand Down Expand Up @@ -287,11 +281,8 @@ pub fn walk_lifetime<'a, V: Visitor<'a>>(visitor: &mut V, lifetime: &'a Lifetime
visitor.visit_ident(lifetime.ident);
}

pub fn walk_poly_trait_ref<'a, V>(
visitor: &mut V,
trait_ref: &'a PolyTraitRef,
_: &TraitBoundModifier,
) where
pub fn walk_poly_trait_ref<'a, V>(visitor: &mut V, trait_ref: &'a PolyTraitRef)
where
V: Visitor<'a>,
{
walk_list!(visitor, visit_generic_param, &trait_ref.bound_generic_params);
Expand Down Expand Up @@ -334,7 +325,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
}
ItemKind::Enum(ref enum_definition, ref generics) => {
visitor.visit_generics(generics);
visitor.visit_enum_def(enum_definition, generics, item.id, item.span)
visitor.visit_enum_def(enum_definition)
}
ItemKind::Impl(box Impl {
defaultness: _,
Expand Down Expand Up @@ -377,12 +368,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
walk_list!(visitor, visit_attribute, &item.attrs);
}

pub fn walk_enum_def<'a, V: Visitor<'a>>(
visitor: &mut V,
enum_definition: &'a EnumDef,
_: &'a Generics,
_: NodeId,
) {
pub fn walk_enum_def<'a, V: Visitor<'a>>(visitor: &mut V, enum_definition: &'a EnumDef) {
walk_list!(visitor, visit_variant, &enum_definition.variants);
}

Expand Down Expand Up @@ -598,7 +584,7 @@ pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignI

pub fn walk_param_bound<'a, V: Visitor<'a>>(visitor: &mut V, bound: &'a GenericBound) {
match *bound {
GenericBound::Trait(ref typ, ref modifier) => visitor.visit_poly_trait_ref(typ, modifier),
GenericBound::Trait(ref typ, ref _modifier) => visitor.visit_poly_trait_ref(typ),
GenericBound::Outlives(ref lifetime) => {
visitor.visit_lifetime(lifetime, LifetimeCtxt::Bound)
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ast_lowering/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,14 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
self.insert(lifetime.span, lifetime.hir_id, Node::Lifetime(lifetime));
}

fn visit_variant(&mut self, v: &'hir Variant<'hir>, g: &'hir Generics<'hir>, item_id: HirId) {
fn visit_variant(&mut self, v: &'hir Variant<'hir>) {
self.insert(v.span, v.id, Node::Variant(v));
self.with_parent(v.id, |this| {
// Register the constructor of this variant.
if let Some(ctor_hir_id) = v.data.ctor_hir_id() {
this.insert(v.span, ctor_hir_id, Node::Ctor(&v.data));
}
intravisit::walk_variant(this, v, g, item_id);
intravisit::walk_variant(this, v);
});
}

Expand Down
9 changes: 3 additions & 6 deletions compiler/rustc_ast_lowering/src/lifetime_collector.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use super::ResolverAstLoweringExt;
use rustc_ast::visit::{self, BoundKind, LifetimeCtxt, Visitor};
use rustc_ast::{
FnRetTy, GenericBounds, Lifetime, NodeId, PathSegment, PolyTraitRef, TraitBoundModifier, Ty,
TyKind,
};
use rustc_ast::{FnRetTy, GenericBounds, Lifetime, NodeId, PathSegment, PolyTraitRef, Ty, TyKind};
use rustc_hir::def::LifetimeRes;
use rustc_middle::span_bug;
use rustc_middle::ty::ResolverAstLowering;
Expand Down Expand Up @@ -71,10 +68,10 @@ impl<'ast> Visitor<'ast> for LifetimeCollectVisitor<'ast> {
visit::walk_path_segment(self, path_span, path_segment);
}

fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef, m: &'ast TraitBoundModifier) {
fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef) {
self.current_binders.push(t.trait_ref.ref_id);

visit::walk_poly_trait_ref(self, t, m);
visit::walk_poly_trait_ref(self, t);

self.current_binders.pop();
}
Expand Down
16 changes: 4 additions & 12 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1538,25 +1538,17 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
visit::walk_param_bound(self, bound)
}

fn visit_poly_trait_ref(&mut self, t: &'a PolyTraitRef, m: &'a TraitBoundModifier) {
fn visit_poly_trait_ref(&mut self, t: &'a PolyTraitRef) {
self.check_late_bound_lifetime_defs(&t.bound_generic_params);
visit::walk_poly_trait_ref(self, t, m);
visit::walk_poly_trait_ref(self, t);
}

fn visit_variant_data(&mut self, s: &'a VariantData) {
self.with_banned_assoc_ty_bound(|this| visit::walk_struct_def(this, s))
}

fn visit_enum_def(
&mut self,
enum_definition: &'a EnumDef,
generics: &'a Generics,
item_id: NodeId,
_: Span,
) {
self.with_banned_assoc_ty_bound(|this| {
visit::walk_enum_def(this, enum_definition, generics, item_id)
})
fn visit_enum_def(&mut self, enum_definition: &'a EnumDef) {
self.with_banned_assoc_ty_bound(|this| visit::walk_enum_def(this, enum_definition))
}

fn visit_fn(&mut self, fk: FnKind<'a>, span: Span, id: NodeId) {
Expand Down
14 changes: 4 additions & 10 deletions compiler/rustc_ast_passes/src/node_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ impl<'ast> Visitor<'ast> for NodeCounter {
self.count += 1;
walk_param_bound(self, bounds)
}
fn visit_poly_trait_ref(&mut self, t: &PolyTraitRef, m: &TraitBoundModifier) {
fn visit_poly_trait_ref(&mut self, t: &PolyTraitRef) {
self.count += 1;
walk_poly_trait_ref(self, t, m)
walk_poly_trait_ref(self, t)
}
fn visit_variant_data(&mut self, s: &VariantData) {
self.count += 1;
Expand All @@ -91,15 +91,9 @@ impl<'ast> Visitor<'ast> for NodeCounter {
self.count += 1;
walk_field_def(self, s)
}
fn visit_enum_def(
&mut self,
enum_definition: &EnumDef,
generics: &Generics,
item_id: NodeId,
_: Span,
) {
fn visit_enum_def(&mut self, enum_definition: &EnumDef) {
self.count += 1;
walk_enum_def(self, enum_definition, generics, item_id)
walk_enum_def(self, enum_definition)
}
fn visit_variant(&mut self, v: &Variant) {
self.count += 1;
Expand Down
8 changes: 2 additions & 6 deletions compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,16 +383,12 @@ fn find_type_parameters(
}

// Place bound generic params on a stack, to extract them when a type is encountered.
fn visit_poly_trait_ref(
&mut self,
trait_ref: &'a ast::PolyTraitRef,
modifier: &'a ast::TraitBoundModifier,
) {
fn visit_poly_trait_ref(&mut self, trait_ref: &'a ast::PolyTraitRef) {
let stack_len = self.bound_generic_params_stack.len();
self.bound_generic_params_stack
.extend(trait_ref.bound_generic_params.clone().into_iter());

visit::walk_poly_trait_ref(self, trait_ref, modifier);
visit::walk_poly_trait_ref(self, trait_ref);

self.bound_generic_params_stack.truncate(stack_len);
}
Expand Down
51 changes: 10 additions & 41 deletions compiler/rustc_hir/src/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,30 +385,17 @@ pub trait Visitor<'v>: Sized {
fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef<'v>, m: TraitBoundModifier) {
walk_poly_trait_ref(self, t, m)
}
fn visit_variant_data(
&mut self,
s: &'v VariantData<'v>,
_: Symbol,
_: &'v Generics<'v>,
_parent_id: HirId,
_: Span,
) {
fn visit_variant_data(&mut self, s: &'v VariantData<'v>) {
walk_struct_def(self, s)
}
fn visit_field_def(&mut self, s: &'v FieldDef<'v>) {
walk_field_def(self, s)
}
fn visit_enum_def(
&mut self,
enum_definition: &'v EnumDef<'v>,
generics: &'v Generics<'v>,
item_id: HirId,
_: Span,
) {
walk_enum_def(self, enum_definition, generics, item_id)
fn visit_enum_def(&mut self, enum_definition: &'v EnumDef<'v>, item_id: HirId) {
walk_enum_def(self, enum_definition, item_id)
}
fn visit_variant(&mut self, v: &'v Variant<'v>, g: &'v Generics<'v>, item_id: HirId) {
walk_variant(self, v, g, item_id)
fn visit_variant(&mut self, v: &'v Variant<'v>) {
walk_variant(self, v)
}
fn visit_label(&mut self, label: &'v Label) {
walk_label(self, label)
Expand Down Expand Up @@ -572,7 +559,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
ItemKind::Enum(ref enum_definition, ref generics) => {
visitor.visit_generics(generics);
// `visit_enum_def()` takes care of visiting the `Item`'s `HirId`.
visitor.visit_enum_def(enum_definition, generics, item.hir_id(), item.span)
visitor.visit_enum_def(enum_definition, item.hir_id())
}
ItemKind::Impl(Impl {
unsafety: _,
Expand All @@ -595,13 +582,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
| ItemKind::Union(ref struct_definition, ref generics) => {
visitor.visit_generics(generics);
visitor.visit_id(item.hir_id());
visitor.visit_variant_data(
struct_definition,
item.ident.name,
generics,
item.hir_id(),
item.span,
);
visitor.visit_variant_data(struct_definition);
}
ItemKind::Trait(.., ref generics, bounds, trait_item_refs) => {
visitor.visit_id(item.hir_id());
Expand Down Expand Up @@ -649,28 +630,16 @@ pub fn walk_use<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path<'v>, hir_id:
pub fn walk_enum_def<'v, V: Visitor<'v>>(
visitor: &mut V,
enum_definition: &'v EnumDef<'v>,
generics: &'v Generics<'v>,
item_id: HirId,
) {
visitor.visit_id(item_id);
walk_list!(visitor, visit_variant, enum_definition.variants, generics, item_id);
walk_list!(visitor, visit_variant, enum_definition.variants);
}

pub fn walk_variant<'v, V: Visitor<'v>>(
visitor: &mut V,
variant: &'v Variant<'v>,
generics: &'v Generics<'v>,
parent_item_id: HirId,
) {
pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V, variant: &'v Variant<'v>) {
visitor.visit_ident(variant.ident);
visitor.visit_id(variant.id);
visitor.visit_variant_data(
&variant.data,
variant.ident.name,
generics,
parent_item_id,
variant.span,
);
visitor.visit_variant_data(&variant.data);
walk_list!(visitor, visit_anon_const, &variant.disr_expr);
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_lint/src/early.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
ast_visit::walk_where_predicate(self, p);
}

fn visit_poly_trait_ref(&mut self, t: &'a ast::PolyTraitRef, m: &'a ast::TraitBoundModifier) {
run_early_pass!(self, check_poly_trait_ref, t, m);
ast_visit::walk_poly_trait_ref(self, t, m);
fn visit_poly_trait_ref(&mut self, t: &'a ast::PolyTraitRef) {
run_early_pass!(self, check_poly_trait_ref, t);
ast_visit::walk_poly_trait_ref(self, t);
}

fn visit_assoc_item(&mut self, item: &'a ast::AssocItem, ctxt: ast_visit::AssocCtxt) {
Expand Down
19 changes: 3 additions & 16 deletions compiler/rustc_lint/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use rustc_hir::intravisit::Visitor;
use rustc_middle::hir::nested_filter;
use rustc_middle::ty::{self, TyCtxt};
use rustc_session::lint::LintPass;
use rustc_span::symbol::Symbol;
use rustc_span::Span;

use std::any::Any;
Expand Down Expand Up @@ -194,14 +193,7 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas
self.context.cached_typeck_results.set(old_cached_typeck_results);
}

fn visit_variant_data(
&mut self,
s: &'tcx hir::VariantData<'tcx>,
_: Symbol,
_: &'tcx hir::Generics<'tcx>,
_: hir::HirId,
_: Span,
) {
fn visit_variant_data(&mut self, s: &'tcx hir::VariantData<'tcx>) {
lint_callback!(self, check_struct_def, s);
hir_visit::walk_struct_def(self, s);
}
Expand All @@ -213,15 +205,10 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas
})
}

fn visit_variant(
&mut self,
v: &'tcx hir::Variant<'tcx>,
g: &'tcx hir::Generics<'tcx>,
item_id: hir::HirId,
) {
fn visit_variant(&mut self, v: &'tcx hir::Variant<'tcx>) {
self.with_lint_attrs(v.id, |cx| {
lint_callback!(cx, check_variant, v);
hir_visit::walk_variant(cx, v, g, item_id);
hir_visit::walk_variant(cx, v);
})
}

Expand Down
9 changes: 2 additions & 7 deletions compiler/rustc_lint/src/levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -772,14 +772,9 @@ impl<'tcx> intravisit::Visitor<'tcx> for LintLevelMapBuilder<'tcx> {
})
}

fn visit_variant(
&mut self,
v: &'tcx hir::Variant<'tcx>,
g: &'tcx hir::Generics<'tcx>,
item_id: hir::HirId,
) {
fn visit_variant(&mut self, v: &'tcx hir::Variant<'tcx>) {
self.with_lint_attrs(v.id, |builder| {
intravisit::walk_variant(builder, v, g, item_id);
intravisit::walk_variant(builder, v);
})
}

Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_lint/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ macro_rules! early_lint_methods {
fn check_generic_arg(a: &ast::GenericArg);
fn check_generic_param(a: &ast::GenericParam);
fn check_generics(a: &ast::Generics);
fn check_poly_trait_ref(a: &ast::PolyTraitRef,
b: &ast::TraitBoundModifier);
fn check_poly_trait_ref(a: &ast::PolyTraitRef);
fn check_fn(a: rustc_ast::visit::FnKind<'_>, c: Span, d_: ast::NodeId);
fn check_trait_item(a: &ast::AssocItem);
fn check_impl_item(a: &ast::AssocItem);
Expand Down
10 changes: 1 addition & 9 deletions compiler/rustc_mir_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use rustc_middle::mir::visit::Visitor as _;
use rustc_middle::mir::{traversal, Body, ConstQualifs, MirPass, MirPhase, Promoted};
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, TyCtxt, TypeVisitable};
use rustc_span::{Span, Symbol};

#[macro_use]
mod pass_manager;
Expand Down Expand Up @@ -159,14 +158,7 @@ fn mir_keys(tcx: TyCtxt<'_>, (): ()) -> FxIndexSet<LocalDefId> {
set: &'a mut FxIndexSet<LocalDefId>,
}
impl<'tcx> Visitor<'tcx> for GatherCtors<'_, 'tcx> {
fn visit_variant_data(
&mut self,
v: &'tcx hir::VariantData<'tcx>,
_: Symbol,
_: &'tcx hir::Generics<'tcx>,
_: hir::HirId,
_: Span,
) {
fn visit_variant_data(&mut self, v: &'tcx hir::VariantData<'tcx>) {
if let hir::VariantData::Tuple(_, hir_id) = *v {
self.set.insert(self.tcx.hir().local_def_id(hir_id));
}
Expand Down
Loading