Skip to content

Commit

Permalink
Pass Ident by reference
Browse files Browse the repository at this point in the history
  • Loading branch information
maxcabrajac committed Oct 12, 2024
1 parent ba06226 commit 47a9602
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 53 deletions.
59 changes: 29 additions & 30 deletions compiler/rustc_ast/src/visitors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ macro_rules! make_ast_visitor {
make_visit!{Variant; visit_variant, walk_variant}
make_visit!{VariantData; visit_variant_data, walk_struct_def}

fn visit_ident(&mut self, _ident: Ident) -> Self::Result {
fn visit_ident(&mut self, _ident: &'ast Ident) -> Self::Result {
Self::Result::output()
}
/// This method is a hack to workaround unstable of `stmt_expr_attributes`.
Expand Down Expand Up @@ -427,7 +427,7 @@ pub mod visit {
#[derive(Copy, Clone, Debug)]
pub enum FnKind<'a> {
/// E.g., `fn foo()`, `fn foo(&self)`, or `extern "Abi" fn foo()`.
Fn(FnCtxt, Ident, &'a FnSig, &'a Visibility, &'a Generics, Option<&'a Block>),
Fn(FnCtxt, &'a Ident, &'a FnSig, &'a Visibility, &'a Generics, Option<&'a Block>),

/// E.g., `|x, y| body`.
Closure(&'a ClosureBinder, &'a Option<CoroutineKind>, &'a FnDecl, &'a Expr),
Expand Down Expand Up @@ -507,12 +507,12 @@ pub mod visit {
visitor: &mut V,
Label { ident }: &'a Label,
) -> V::Result {
visitor.visit_ident(*ident)
visitor.visit_ident(ident)
}

pub fn walk_lifetime<'a, V: Visitor<'a>>(visitor: &mut V, lifetime: &'a Lifetime) -> V::Result {
let Lifetime { id: _, ident } = lifetime;
visitor.visit_ident(*ident)
visitor.visit_ident(ident)
}

pub fn walk_poly_trait_ref<'a, V>(visitor: &mut V, trait_ref: &'a PolyTraitRef) -> V::Result
Expand Down Expand Up @@ -553,8 +553,7 @@ pub mod visit {
visit_opt!(visitor, visit_expr, expr);
}
ItemKind::Fn(box Fn { defaultness: _, generics, sig, body }) => {
let kind =
FnKind::Fn(FnCtxt::Free, *ident, sig, vis, generics, body.as_deref());
let kind = FnKind::Fn(FnCtxt::Free, ident, sig, vis, generics, body.as_deref());
try_visit!(visitor.visit_fn(kind, *span, *id));
}
ItemKind::Mod(_unsafety, mod_kind) => match mod_kind {
Expand Down Expand Up @@ -623,17 +622,17 @@ pub mod visit {
}) => {
try_visit!(visitor.visit_qself(qself));
try_visit!(visitor.visit_path(path, *id));
visit_opt!(visitor, visit_ident, *rename);
visit_opt!(visitor, visit_ident, rename);
visit_opt!(visitor, visit_block, body);
}
ItemKind::DelegationMac(box DelegationMac { qself, prefix, suffixes, body }) => {
try_visit!(visitor.visit_qself(qself));
try_visit!(visitor.visit_path(prefix, *id));
if let Some(suffixes) = suffixes {
for (ident, rename) in suffixes {
visitor.visit_ident(*ident);
visitor.visit_ident(ident);
if let Some(rename) = rename {
visitor.visit_ident(*rename);
visitor.visit_ident(rename);
}
}
}
Expand Down Expand Up @@ -667,7 +666,7 @@ pub mod visit {
variant;
walk_list!(visitor, visit_attribute, attrs);
try_visit!(visitor.visit_vis(vis));
try_visit!(visitor.visit_ident(*ident));
try_visit!(visitor.visit_ident(ident));
try_visit!(visitor.visit_variant_data(data));
visit_opt!(visitor, visit_variant_discr, disr_expr);
V::Result::output()
Expand All @@ -677,15 +676,15 @@ pub mod visit {
let ExprField { attrs, id: _, span: _, ident, expr, is_shorthand: _, is_placeholder: _ } =
f;
walk_list!(visitor, visit_attribute, attrs);
try_visit!(visitor.visit_ident(*ident));
try_visit!(visitor.visit_ident(ident));
try_visit!(visitor.visit_expr(expr));
V::Result::output()
}

pub fn walk_pat_field<'a, V: Visitor<'a>>(visitor: &mut V, fp: &'a PatField) -> V::Result {
let PatField { ident, pat, is_shorthand: _, attrs, id: _, span: _, is_placeholder: _ } = fp;
walk_list!(visitor, visit_attribute, attrs);
try_visit!(visitor.visit_ident(*ident));
try_visit!(visitor.visit_ident(ident));
try_visit!(visitor.visit_pat(pat));
V::Result::output()
}
Expand Down Expand Up @@ -759,7 +758,7 @@ pub mod visit {
match kind {
UseTreeKind::Simple(rename) => {
// The extra IDs are handled during AST lowering.
visit_opt!(visitor, visit_ident, *rename);
visit_opt!(visitor, visit_ident, rename);
}
UseTreeKind::Glob => {}
UseTreeKind::Nested { ref items, span: _ } => {
Expand All @@ -776,7 +775,7 @@ pub mod visit {
segment: &'a PathSegment,
) -> V::Result {
let PathSegment { ident, id: _, args } = segment;
try_visit!(visitor.visit_ident(*ident));
try_visit!(visitor.visit_ident(ident));
visit_opt!(visitor, visit_generic_args, args);
V::Result::output()
}
Expand Down Expand Up @@ -822,7 +821,7 @@ pub mod visit {
constraint: &'a AssocItemConstraint,
) -> V::Result {
let AssocItemConstraint { id: _, ident, gen_args, kind, span: _ } = constraint;
try_visit!(visitor.visit_ident(*ident));
try_visit!(visitor.visit_ident(ident));
visit_opt!(visitor, visit_generic_args, gen_args);
match kind {
AssocItemConstraintKind::Equality { term } => match term {
Expand Down Expand Up @@ -860,7 +859,7 @@ pub mod visit {
try_visit!(visitor.visit_pat(subpattern));
}
PatKind::Ident(_bmode, ident, optional_subpattern) => {
try_visit!(visitor.visit_ident(*ident));
try_visit!(visitor.visit_ident(ident));
visit_opt!(visitor, visit_pat, optional_subpattern);
}
PatKind::Lit(expression) => try_visit!(visitor.visit_expr(expression)),
Expand All @@ -885,7 +884,7 @@ pub mod visit {
_ctxt: AssocCtxt,
visitor: &mut V,
) -> V::Result {
let &Item { id, span, ident, ref vis, .. } = item;
let Item { id, span, ident, vis, .. } = item;
match self {
ForeignItemKind::Static(box StaticItem { ty, mutability: _, expr, safety: _ }) => {
try_visit!(visitor.visit_ty(ty));
Expand All @@ -894,7 +893,7 @@ pub mod visit {
ForeignItemKind::Fn(box Fn { defaultness: _, generics, sig, body }) => {
let kind =
FnKind::Fn(FnCtxt::Foreign, ident, sig, vis, generics, body.as_deref());
try_visit!(visitor.visit_fn(kind, span, id));
try_visit!(visitor.visit_fn(kind, *span, *id));
}
ForeignItemKind::TyAlias(box TyAlias {
generics,
Expand Down Expand Up @@ -952,7 +951,7 @@ pub mod visit {
let GenericParam { id: _, ident, attrs, bounds, is_placeholder: _, kind, colon_span: _ } =
param;
walk_list!(visitor, visit_attribute, attrs);
try_visit!(visitor.visit_ident(*ident));
try_visit!(visitor.visit_ident(ident));
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
match kind {
GenericParamKind::Lifetime => (),
Expand Down Expand Up @@ -1054,7 +1053,7 @@ pub mod visit {
ctxt: AssocCtxt,
visitor: &mut V,
) -> V::Result {
let &Item { id, span, ident, ref vis, .. } = item;
let Item { id, span, ident, vis, .. } = item;
match self {
AssocItemKind::Const(box ConstItem { defaultness: _, generics, ty, expr }) => {
try_visit!(visitor.visit_generics(generics));
Expand All @@ -1064,7 +1063,7 @@ pub mod visit {
AssocItemKind::Fn(box Fn { defaultness: _, generics, sig, body }) => {
let kind =
FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, vis, generics, body.as_deref());
try_visit!(visitor.visit_fn(kind, span, id));
try_visit!(visitor.visit_fn(kind, *span, *id));
}
AssocItemKind::Type(box TyAlias {
generics,
Expand All @@ -1090,7 +1089,7 @@ pub mod visit {
}) => {
try_visit!(visitor.visit_qself(qself));
try_visit!(visitor.visit_path(path, *id));
visit_opt!(visitor, visit_ident, *rename);
visit_opt!(visitor, visit_ident, rename);
visit_opt!(visitor, visit_block, body);
}
AssocItemKind::DelegationMac(box DelegationMac {
Expand All @@ -1100,12 +1099,12 @@ pub mod visit {
body,
}) => {
try_visit!(visitor.visit_qself(qself));
try_visit!(visitor.visit_path(prefix, id));
try_visit!(visitor.visit_path(prefix, *id));
if let Some(suffixes) = suffixes {
for (ident, rename) in suffixes {
visitor.visit_ident(*ident);
visitor.visit_ident(ident);
if let Some(rename) = rename {
visitor.visit_ident(*rename);
visitor.visit_ident(rename);
}
}
}
Expand All @@ -1121,7 +1120,7 @@ pub mod visit {
item: &'a Item<impl WalkItemKind>,
ctxt: AssocCtxt,
) -> V::Result {
let &Item { id: _, span: _, ident, ref vis, ref attrs, ref kind, tokens: _ } = item;
let Item { id: _, span: _, ident, vis, attrs, kind, tokens: _ } = item;
walk_list!(visitor, visit_attribute, attrs);
try_visit!(visitor.visit_vis(vis));
try_visit!(visitor.visit_ident(ident));
Expand All @@ -1141,7 +1140,7 @@ pub mod visit {
let FieldDef { attrs, id: _, span: _, vis, ident, ty, is_placeholder: _ } = field;
walk_list!(visitor, visit_attribute, attrs);
try_visit!(visitor.visit_vis(vis));
visit_opt!(visitor, visit_ident, *ident);
visit_opt!(visitor, visit_ident, ident);
try_visit!(visitor.visit_ty(ty));
V::Result::output()
}
Expand Down Expand Up @@ -1226,7 +1225,7 @@ pub mod visit {
for FormatArgument { kind, expr } in arguments.all_args() {
match kind {
FormatArgumentKind::Named(ident) | FormatArgumentKind::Captured(ident) => {
try_visit!(visitor.visit_ident(*ident))
try_visit!(visitor.visit_ident(ident))
}
FormatArgumentKind::Normal => {}
}
Expand Down Expand Up @@ -1346,7 +1345,7 @@ pub mod visit {
}
ExprKind::Field(subexpression, ident) => {
try_visit!(visitor.visit_expr(subexpression));
try_visit!(visitor.visit_ident(*ident));
try_visit!(visitor.visit_ident(ident));
}
ExprKind::Index(main_expression, index_expression, _span) => {
try_visit!(visitor.visit_expr(main_expression));
Expand Down Expand Up @@ -1381,7 +1380,7 @@ pub mod visit {
ExprKind::FormatArgs(f) => try_visit!(visitor.visit_format_args(f)),
ExprKind::OffsetOf(container, fields) => {
try_visit!(visitor.visit_ty(container));
walk_list!(visitor, visit_ident, fields.iter().copied());
walk_list!(visitor, visit_ident, fields.iter());
}
ExprKind::Yield(optional_expression) => {
visit_opt!(visitor, visit_expr, optional_expression);
Expand Down
28 changes: 17 additions & 11 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl<'a> AstValidator<'a> {
}

fn visit_struct_field_def(&mut self, field: &'a FieldDef) {
if let Some(ident) = field.ident
if let Some(ref ident) = field.ident
&& ident.name == kw::Underscore
{
self.visit_vis(&field.vis);
Expand Down Expand Up @@ -896,7 +896,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
}

this.visit_vis(&item.vis);
this.visit_ident(item.ident);
this.visit_ident(&item.ident);
let disallowed = matches!(constness, Const::No)
.then(|| TildeConstReason::TraitImpl { span: item.span });
this.with_tilde_const(disallowed, |this| this.visit_generics(generics));
Expand Down Expand Up @@ -950,7 +950,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
}

this.visit_vis(&item.vis);
this.visit_ident(item.ident);
this.visit_ident(&item.ident);
this.with_tilde_const(
Some(TildeConstReason::Impl { span: item.span }),
|this| this.visit_generics(generics),
Expand Down Expand Up @@ -988,9 +988,15 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
}

self.visit_vis(&item.vis);
self.visit_ident(item.ident);
let kind =
FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, generics, body.as_deref());
self.visit_ident(&item.ident);
let kind = FnKind::Fn(
FnCtxt::Free,
&item.ident,
sig,
&item.vis,
generics,
body.as_deref(),
);
self.visit_fn(kind, item.span, item.id);
walk_list!(self, visit_attribute, &item.attrs);
return; // Avoid visiting again.
Expand Down Expand Up @@ -1055,7 +1061,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
// Equivalent of `visit::walk_item` for `ItemKind::Trait` that inserts a bound
// context for the supertraits.
this.visit_vis(&item.vis);
this.visit_ident(item.ident);
this.visit_ident(&item.ident);
let disallowed = is_const_trait
.is_none()
.then(|| TildeConstReason::Trait { span: item.span });
Expand All @@ -1082,7 +1088,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
ItemKind::Struct(vdata, generics) => match vdata {
VariantData::Struct { fields, .. } => {
self.visit_vis(&item.vis);
self.visit_ident(item.ident);
self.visit_ident(&item.ident);
self.visit_generics(generics);
// Permit `Anon{Struct,Union}` as field type.
walk_list!(self, visit_struct_field_def, fields);
Expand All @@ -1098,7 +1104,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
match vdata {
VariantData::Struct { fields, .. } => {
self.visit_vis(&item.vis);
self.visit_ident(item.ident);
self.visit_ident(&item.ident);
self.visit_generics(generics);
// Permit `Anon{Struct,Union}` as field type.
walk_list!(self, visit_struct_field_def, fields);
Expand Down Expand Up @@ -1518,10 +1524,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
|| matches!(sig.header.constness, Const::Yes(_)) =>
{
self.visit_vis(&item.vis);
self.visit_ident(item.ident);
self.visit_ident(&item.ident);
let kind = FnKind::Fn(
FnCtxt::Assoc(ctxt),
item.ident,
&item.ident,
sig,
&item.vis,
generics,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_passes/src/node_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl NodeCounter {
}

impl<'ast> Visitor<'ast> for NodeCounter {
fn visit_ident(&mut self, _ident: Ident) {
fn visit_ident(&mut self, _ident: &Ident) {
self.count += 1;
}
fn visit_foreign_item(&mut self, i: &ForeignItem) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/deriving/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ impl<'a, 'b> rustc_ast::visit::Visitor<'a> for DetectNonVariantDefaultAttr<'a, '
rustc_ast::visit::walk_attribute(self, attr);
}
fn visit_variant(&mut self, v: &'a rustc_ast::Variant) {
self.visit_ident(v.ident);
self.visit_ident(&v.ident);
self.visit_vis(&v.vis);
self.visit_variant_data(&v.data);
visit_opt!(self, visit_anon_const, &v.disr_expr);
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_lint/src/early.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,9 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
ast_visit::walk_ty(self, t);
}

fn visit_ident(&mut self, ident: Ident) {
lint_callback!(self, check_ident, ident);
fn visit_ident(&mut self, ident: &Ident) {
// FIXME: Change check_ident so it receives a reference
lint_callback!(self, check_ident, *ident);
}

fn visit_local(&mut self, l: &'a ast::Local) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
// Visit attributes after items for backward compatibility.
// This way they can use `macro_rules` defined later.
self.visit_vis(&item.vis);
self.visit_ident(item.ident);
self.visit_ident(&item.ident);
item.kind.walk(item, AssocCtxt::Trait, self);
visit::walk_list!(self, visit_attribute, &item.attrs);
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ impl<'ra: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'_, 'ast, 'r
}

fn visit_assoc_item_constraint(&mut self, constraint: &'ast AssocItemConstraint) {
self.visit_ident(constraint.ident);
self.visit_ident(&constraint.ident);
if let Some(ref gen_args) = constraint.gen_args {
// Forbid anonymous lifetimes in GAT parameters until proper semantics are decided.
self.with_lifetime_rib(LifetimeRibKind::AnonymousReportError, |this| {
Expand Down Expand Up @@ -4581,7 +4581,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {

fn resolve_expr_field(&mut self, f: &'ast ExprField, e: &'ast Expr) {
self.resolve_expr(&f.expr, Some(e));
self.visit_ident(f.ident);
self.visit_ident(&f.ident);
walk_list!(self, visit_attribute, f.attrs.iter());
}

Expand Down
4 changes: 2 additions & 2 deletions src/tools/clippy/clippy_utils/src/ast_utils/ident_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl From<&Attribute> for IdentIter {
struct IdentCollector(Vec<Ident>);

impl Visitor<'_> for IdentCollector {
fn visit_ident(&mut self, ident: Ident) {
self.0.push(ident);
fn visit_ident(&mut self, ident: &Ident) {
self.0.push(*ident);
}
}
Loading

0 comments on commit 47a9602

Please sign in to comment.