Skip to content

Commit

Permalink
Auto merge of #101709 - nnethercote:simplify-visitors-more, r=cjgillot
Browse files Browse the repository at this point in the history
Simplify visitors more

A successor to #100392.

r? `@cjgillot`
  • Loading branch information
bors committed Sep 14, 2022
2 parents a5b58ad + b8ed1c5 commit a0d1df4
Show file tree
Hide file tree
Showing 26 changed files with 115 additions and 156 deletions.
30 changes: 13 additions & 17 deletions compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ pub trait Visitor<'ast>: Sized {
fn visit_where_predicate(&mut self, p: &'ast WherePredicate) {
walk_where_predicate(self, p)
}
fn visit_fn(&mut self, fk: FnKind<'ast>, s: Span, _: NodeId) {
walk_fn(self, fk, s)
fn visit_fn(&mut self, fk: FnKind<'ast>, _: Span, _: NodeId) {
walk_fn(self, fk)
}
fn visit_assoc_item(&mut self, i: &'ast AssocItem, ctxt: AssocCtxt) {
walk_assoc_item(self, i, ctxt)
Expand Down Expand Up @@ -201,11 +201,11 @@ pub trait Visitor<'ast>: Sized {
fn visit_use_tree(&mut self, use_tree: &'ast UseTree, id: NodeId, _nested: bool) {
walk_use_tree(self, use_tree, id)
}
fn visit_path_segment(&mut self, path_span: Span, path_segment: &'ast PathSegment) {
walk_path_segment(self, path_span, path_segment)
fn visit_path_segment(&mut self, path_segment: &'ast PathSegment) {
walk_path_segment(self, path_segment)
}
fn visit_generic_args(&mut self, path_span: Span, generic_args: &'ast GenericArgs) {
walk_generic_args(self, path_span, generic_args)
fn visit_generic_args(&mut self, generic_args: &'ast GenericArgs) {
walk_generic_args(self, generic_args)
}
fn visit_generic_arg(&mut self, generic_arg: &'ast GenericArg) {
walk_generic_arg(self, generic_arg)
Expand Down Expand Up @@ -435,7 +435,7 @@ pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {

pub fn walk_path<'a, V: Visitor<'a>>(visitor: &mut V, path: &'a Path) {
for segment in &path.segments {
visitor.visit_path_segment(path.span, segment);
visitor.visit_path_segment(segment);
}
}

Expand All @@ -457,18 +457,14 @@ pub fn walk_use_tree<'a, V: Visitor<'a>>(visitor: &mut V, use_tree: &'a UseTree,
}
}

pub fn walk_path_segment<'a, V: Visitor<'a>>(
visitor: &mut V,
path_span: Span,
segment: &'a PathSegment,
) {
pub fn walk_path_segment<'a, V: Visitor<'a>>(visitor: &mut V, segment: &'a PathSegment) {
visitor.visit_ident(segment.ident);
if let Some(ref args) = segment.args {
visitor.visit_generic_args(path_span, args);
visitor.visit_generic_args(args);
}
}

pub fn walk_generic_args<'a, V>(visitor: &mut V, _path_span: Span, generic_args: &'a GenericArgs)
pub fn walk_generic_args<'a, V>(visitor: &mut V, generic_args: &'a GenericArgs)
where
V: Visitor<'a>,
{
Expand Down Expand Up @@ -502,7 +498,7 @@ where
pub fn walk_assoc_constraint<'a, V: Visitor<'a>>(visitor: &mut V, constraint: &'a AssocConstraint) {
visitor.visit_ident(constraint.ident);
if let Some(ref gen_args) = constraint.gen_args {
visitor.visit_generic_args(gen_args.span(), gen_args);
visitor.visit_generic_args(gen_args);
}
match constraint.kind {
AssocConstraintKind::Equality { ref term } => match term {
Expand Down Expand Up @@ -659,7 +655,7 @@ pub fn walk_fn_decl<'a, V: Visitor<'a>>(visitor: &mut V, function_declaration: &
visitor.visit_fn_ret_ty(&function_declaration.output);
}

pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>, _span: Span) {
pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>) {
match kind {
FnKind::Fn(_, _, sig, _, generics, body) => {
visitor.visit_generics(generics);
Expand Down Expand Up @@ -800,7 +796,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
walk_list!(visitor, visit_expr, arguments);
}
ExprKind::MethodCall(ref segment, ref receiver, ref arguments, _span) => {
visitor.visit_path_segment(expression.span, segment);
visitor.visit_path_segment(segment);
visitor.visit_expr(receiver);
walk_list!(visitor, visit_expr, arguments);
}
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_ast_lowering/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,9 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
});
}

fn visit_path_segment(&mut self, path_span: Span, path_segment: &'hir PathSegment<'hir>) {
self.insert(path_span, path_segment.hir_id, Node::PathSegment(path_segment));
intravisit::walk_path_segment(self, path_span, path_segment);
fn visit_path_segment(&mut self, path_segment: &'hir PathSegment<'hir>) {
self.insert(path_segment.ident.span, path_segment.hir_id, Node::PathSegment(path_segment));
intravisit::walk_path_segment(self, path_segment);
}

fn visit_ty(&mut self, ty: &'hir Ty<'hir>) {
Expand Down Expand Up @@ -280,12 +280,12 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
fk: intravisit::FnKind<'hir>,
fd: &'hir FnDecl<'hir>,
b: BodyId,
s: Span,
_: Span,
id: HirId,
) {
assert_eq!(self.owner, id.owner);
assert_eq!(self.parent_node, id.local_id);
intravisit::walk_fn(self, fk, fd, b, s, id);
intravisit::walk_fn(self, fk, fd, b, id);
}

fn visit_block(&mut self, block: &'hir Block<'hir>) {
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_ast_lowering/src/lifetime_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ impl<'ast> Visitor<'ast> for LifetimeCollectVisitor<'ast> {
self.record_lifetime_use(*lifetime);
}

fn visit_path_segment(&mut self, path_span: Span, path_segment: &'ast PathSegment) {
self.record_elided_anchor(path_segment.id, path_span);
visit::walk_path_segment(self, path_span, path_segment);
fn visit_path_segment(&mut self, path_segment: &'ast PathSegment) {
self.record_elided_anchor(path_segment.id, path_segment.ident.span);
visit::walk_path_segment(self, path_segment);
}

fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef) {
Expand Down
10 changes: 4 additions & 6 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,9 @@ impl<'a> AstValidator<'a> {
for (i, segment) in path.segments.iter().enumerate() {
// Allow `impl Trait` iff we're on the final path segment
if i == path.segments.len() - 1 {
self.visit_path_segment(path.span, segment);
self.visit_path_segment(segment);
} else {
self.with_banned_impl_trait(|this| {
this.visit_path_segment(path.span, segment)
});
self.with_banned_impl_trait(|this| this.visit_path_segment(segment));
}
}
}
Expand Down Expand Up @@ -1293,7 +1291,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
}

// Mirrors `visit::walk_generic_args`, but tracks relevant state.
fn visit_generic_args(&mut self, _: Span, generic_args: &'a GenericArgs) {
fn visit_generic_args(&mut self, generic_args: &'a GenericArgs) {
match *generic_args {
GenericArgs::AngleBracketed(ref data) => {
self.check_generic_args_before_constraints(data);
Expand Down Expand Up @@ -1529,7 +1527,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
matches!(fk.header(), Some(FnHeader { constness: Const::Yes(_), .. }))
|| matches!(fk.ctxt(), Some(FnCtxt::Assoc(_)));

self.with_tilde_const(tilde_const_allowed, |this| visit::walk_fn(this, fk, span));
self.with_tilde_const(tilde_const_allowed, |this| visit::walk_fn(this, fk));
}

fn visit_assoc_item(&mut self, item: &'a AssocItem, ctxt: AssocCtxt) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
gate_feature_post!(&self, c_variadic, span, "C-variadic functions are unstable");
}

visit::walk_fn(self, fn_kind, span)
visit::walk_fn(self, fn_kind)
}

fn visit_assoc_constraint(&mut self, constraint: &'a AssocConstraint) {
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_ast_passes/src/node_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ impl<'ast> Visitor<'ast> for NodeCounter {
self.count += 1;
walk_generics(self, g)
}
fn visit_fn(&mut self, fk: visit::FnKind<'_>, s: Span, _: NodeId) {
fn visit_fn(&mut self, fk: visit::FnKind<'_>, _: Span, _: NodeId) {
self.count += 1;
walk_fn(self, fk, s)
walk_fn(self, fk)
}
fn visit_assoc_item(&mut self, ti: &AssocItem, ctxt: AssocCtxt) {
self.count += 1;
Expand Down Expand Up @@ -115,9 +115,9 @@ impl<'ast> Visitor<'ast> for NodeCounter {
self.count += 1;
walk_use_tree(self, use_tree, id)
}
fn visit_generic_args(&mut self, path_span: Span, generic_args: &GenericArgs) {
fn visit_generic_args(&mut self, generic_args: &GenericArgs) {
self.count += 1;
walk_generic_args(self, path_span, generic_args)
walk_generic_args(self, generic_args)
}
fn visit_assoc_constraint(&mut self, constraint: &AssocConstraint) {
self.count += 1;
Expand Down
73 changes: 28 additions & 45 deletions compiler/rustc_hir/src/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ pub trait Visitor<'v>: Sized {
fn visit_id(&mut self, _hir_id: HirId) {
// Nothing to do.
}
fn visit_name(&mut self, _span: Span, _name: Symbol) {
fn visit_name(&mut self, _name: Symbol) {
// Nothing to do.
}
fn visit_ident(&mut self, ident: Ident) {
Expand Down Expand Up @@ -361,8 +361,8 @@ pub trait Visitor<'v>: Sized {
fn visit_fn_decl(&mut self, fd: &'v FnDecl<'v>) {
walk_fn_decl(self, fd)
}
fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl<'v>, b: BodyId, s: Span, id: HirId) {
walk_fn(self, fk, fd, b, s, id)
fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl<'v>, b: BodyId, _: Span, id: HirId) {
walk_fn(self, fk, fd, b, id)
}
fn visit_use(&mut self, path: &'v Path<'v>, hir_id: HirId) {
walk_use(self, path, hir_id)
Expand All @@ -388,8 +388,8 @@ pub trait Visitor<'v>: Sized {
fn visit_param_bound(&mut self, bounds: &'v GenericBound<'v>) {
walk_param_bound(self, bounds)
}
fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef<'v>, m: TraitBoundModifier) {
walk_poly_trait_ref(self, t, m)
fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef<'v>) {
walk_poly_trait_ref(self, t)
}
fn visit_variant_data(&mut self, s: &'v VariantData<'v>) {
walk_struct_def(self, s)
Expand Down Expand Up @@ -420,17 +420,18 @@ pub trait Visitor<'v>: Sized {
fn visit_lifetime(&mut self, lifetime: &'v Lifetime) {
walk_lifetime(self, lifetime)
}
fn visit_qpath(&mut self, qpath: &'v QPath<'v>, id: HirId, span: Span) {
walk_qpath(self, qpath, id, span)
// The span is that of the surrounding type/pattern/expr/whatever.
fn visit_qpath(&mut self, qpath: &'v QPath<'v>, id: HirId, _span: Span) {
walk_qpath(self, qpath, id)
}
fn visit_path(&mut self, path: &'v Path<'v>, _id: HirId) {
walk_path(self, path)
}
fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment<'v>) {
walk_path_segment(self, path_span, path_segment)
fn visit_path_segment(&mut self, path_segment: &'v PathSegment<'v>) {
walk_path_segment(self, path_segment)
}
fn visit_generic_args(&mut self, path_span: Span, generic_args: &'v GenericArgs<'v>) {
walk_generic_args(self, path_span, generic_args)
fn visit_generic_args(&mut self, generic_args: &'v GenericArgs<'v>) {
walk_generic_args(self, generic_args)
}
fn visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding<'v>) {
walk_assoc_type_binding(self, type_binding)
Expand Down Expand Up @@ -472,7 +473,7 @@ pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local<'v>) {
}

pub fn walk_ident<'v, V: Visitor<'v>>(visitor: &mut V, ident: Ident) {
visitor.visit_name(ident.span, ident.name);
visitor.visit_name(ident.name);
}

pub fn walk_label<'v, V: Visitor<'v>>(visitor: &mut V, label: &'v Label) {
Expand All @@ -494,11 +495,7 @@ pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime
}
}

pub fn walk_poly_trait_ref<'v, V: Visitor<'v>>(
visitor: &mut V,
trait_ref: &'v PolyTraitRef<'v>,
_modifier: TraitBoundModifier,
) {
pub fn walk_poly_trait_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_ref: &'v PolyTraitRef<'v>) {
walk_list!(visitor, visit_generic_param, trait_ref.bound_generic_params);
visitor.visit_trait_ref(&trait_ref.trait_ref);
}
Expand All @@ -519,7 +516,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
ItemKind::ExternCrate(orig_name) => {
visitor.visit_id(item.hir_id());
if let Some(orig_name) = orig_name {
visitor.visit_name(item.span, orig_name);
visitor.visit_name(orig_name);
}
}
ItemKind::Use(ref path, _) => {
Expand Down Expand Up @@ -680,7 +677,7 @@ pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v>) {
}
TyKind::TraitObject(bounds, ref lifetime, _syntax) => {
for bound in bounds {
visitor.visit_poly_trait_ref(bound, TraitBoundModifier::None);
visitor.visit_poly_trait_ref(bound);
}
visitor.visit_lifetime(lifetime);
}
Expand All @@ -693,48 +690,35 @@ pub fn walk_inf<'v, V: Visitor<'v>>(visitor: &mut V, inf: &'v InferArg) {
visitor.visit_id(inf.hir_id);
}

pub fn walk_qpath<'v, V: Visitor<'v>>(
visitor: &mut V,
qpath: &'v QPath<'v>,
id: HirId,
span: Span,
) {
pub fn walk_qpath<'v, V: Visitor<'v>>(visitor: &mut V, qpath: &'v QPath<'v>, id: HirId) {
match *qpath {
QPath::Resolved(ref maybe_qself, ref path) => {
walk_list!(visitor, visit_ty, maybe_qself);
visitor.visit_path(path, id)
}
QPath::TypeRelative(ref qself, ref segment) => {
visitor.visit_ty(qself);
visitor.visit_path_segment(span, segment);
visitor.visit_path_segment(segment);
}
QPath::LangItem(..) => {}
}
}

pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path<'v>) {
for segment in path.segments {
visitor.visit_path_segment(path.span, segment);
visitor.visit_path_segment(segment);
}
}

pub fn walk_path_segment<'v, V: Visitor<'v>>(
visitor: &mut V,
path_span: Span,
segment: &'v PathSegment<'v>,
) {
pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V, segment: &'v PathSegment<'v>) {
visitor.visit_ident(segment.ident);
visitor.visit_id(segment.hir_id);
if let Some(ref args) = segment.args {
visitor.visit_generic_args(path_span, args);
visitor.visit_generic_args(args);
}
}

pub fn walk_generic_args<'v, V: Visitor<'v>>(
visitor: &mut V,
_path_span: Span,
generic_args: &'v GenericArgs<'v>,
) {
pub fn walk_generic_args<'v, V: Visitor<'v>>(visitor: &mut V, generic_args: &'v GenericArgs<'v>) {
walk_list!(visitor, visit_generic_arg, generic_args.args);
walk_list!(visitor, visit_assoc_type_binding, generic_args.bindings);
}
Expand All @@ -745,7 +729,7 @@ pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(
) {
visitor.visit_id(type_binding.hir_id);
visitor.visit_ident(type_binding.ident);
visitor.visit_generic_args(type_binding.span, type_binding.gen_args);
visitor.visit_generic_args(type_binding.gen_args);
match type_binding.kind {
TypeBindingKind::Equality { ref term } => match term {
Term::Ty(ref ty) => visitor.visit_ty(ty),
Expand Down Expand Up @@ -819,12 +803,12 @@ pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V, foreign_item: &'v

pub fn walk_param_bound<'v, V: Visitor<'v>>(visitor: &mut V, bound: &'v GenericBound<'v>) {
match *bound {
GenericBound::Trait(ref typ, modifier) => {
visitor.visit_poly_trait_ref(typ, modifier);
GenericBound::Trait(ref typ, _modifier) => {
visitor.visit_poly_trait_ref(typ);
}
GenericBound::LangItemTrait(_, span, hir_id, args) => {
GenericBound::LangItemTrait(_, _span, hir_id, args) => {
visitor.visit_id(hir_id);
visitor.visit_generic_args(span, args);
visitor.visit_generic_args(args);
}
GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
}
Expand Down Expand Up @@ -910,7 +894,6 @@ pub fn walk_fn<'v, V: Visitor<'v>>(
function_kind: FnKind<'v>,
function_declaration: &'v FnDecl<'v>,
body_id: BodyId,
_span: Span,
id: HirId,
) {
visitor.visit_id(id);
Expand Down Expand Up @@ -1095,7 +1078,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
walk_list!(visitor, visit_expr, arguments);
}
ExprKind::MethodCall(ref segment, receiver, arguments, _) => {
visitor.visit_path_segment(expression.span, segment);
visitor.visit_path_segment(segment);
visitor.visit_expr(receiver);
walk_list!(visitor, visit_expr, arguments);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl<'tcx> Visitor<'tcx> for FindNestedTypeVisitor<'tcx> {
hir::TyKind::TraitObject(bounds, ..) => {
for bound in bounds {
self.current_index.shift_in(1);
self.visit_poly_trait_ref(bound, hir::TraitBoundModifier::None);
self.visit_poly_trait_ref(bound);
self.current_index.shift_out(1);
}
}
Expand Down
Loading

0 comments on commit a0d1df4

Please sign in to comment.