Rollup merge of #132106 - maxcabrajac:ident_ref, r=petrochenkov

Pass Ident by reference in ast Visitor

`MutVisitor`'s version of `visit_ident` passes around `&Ident`, but `Visitor` copies `Ident`. This PR changes that

r? `@petrochenkov`

related to #128974
This commit is contained in:
Matthias Krüger 2024-10-25 20:33:11 +02:00 committed by GitHub
commit 280790b9a1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 41 additions and 41 deletions

View File

@ -135,7 +135,7 @@ pub trait Visitor<'ast>: Sized {
/// or `ControlFlow<T>`.
type Result: VisitorResult = ();
fn visit_ident(&mut self, _ident: Ident) -> Self::Result {
fn visit_ident(&mut self, _ident: &'ast Ident) -> Self::Result {
Self::Result::output()
}
fn visit_foreign_item(&mut self, i: &'ast ForeignItem) -> Self::Result {
@ -314,12 +314,12 @@ pub fn walk_local<'a, V: Visitor<'a>>(visitor: &mut V, local: &'a Local) -> V::R
}
pub fn walk_label<'a, V: Visitor<'a>>(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
@ -426,7 +426,7 @@ impl WalkItemKind for ItemKind {
}) => {
try_visit!(walk_qself(visitor, 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 }) => {
@ -434,9 +434,9 @@ impl WalkItemKind for ItemKind {
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);
}
}
}
@ -469,7 +469,7 @@ where
let Variant { attrs, id: _, span: _, vis, ident, data, disr_expr, is_placeholder: _ } = 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()
@ -478,7 +478,7 @@ where
pub fn walk_expr_field<'a, V: Visitor<'a>>(visitor: &mut V, f: &'a ExprField) -> V::Result {
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()
}
@ -486,7 +486,7 @@ pub fn walk_expr_field<'a, V: Visitor<'a>>(visitor: &mut V, f: &'a ExprField) ->
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()
}
@ -561,7 +561,7 @@ pub fn walk_use_tree<'a, V: Visitor<'a>>(
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: _ } => {
@ -578,7 +578,7 @@ pub fn walk_path_segment<'a, V: Visitor<'a>>(
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()
}
@ -624,7 +624,7 @@ pub fn walk_assoc_item_constraint<'a, V: Visitor<'a>>(
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 {
@ -662,7 +662,7 @@ pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) -> V::Res
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)),
@ -748,7 +748,7 @@ pub fn walk_generic_param<'a, V: Visitor<'a>>(
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 => (),
@ -886,7 +886,7 @@ impl WalkItemKind for AssocItemKind {
}) => {
try_visit!(walk_qself(visitor, 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 { qself, prefix, suffixes, body }) => {
@ -894,9 +894,9 @@ impl WalkItemKind for AssocItemKind {
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);
}
}
}
@ -912,7 +912,7 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(
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));
@ -932,7 +932,7 @@ pub fn walk_field_def<'a, V: Visitor<'a>>(visitor: &mut V, field: &'a FieldDef)
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()
}
@ -1014,7 +1014,7 @@ pub fn walk_format_args<'a, V: Visitor<'a>>(visitor: &mut V, fmt: &'a FormatArgs
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 => {}
}
@ -1134,7 +1134,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) -> V
}
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));
@ -1169,7 +1169,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) -> V
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);

View File

@ -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);
@ -899,7 +899,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));
@ -953,7 +953,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),
@ -991,7 +991,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
}
self.visit_vis(&item.vis);
self.visit_ident(item.ident);
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);
@ -1058,7 +1058,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 });
@ -1085,7 +1085,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);
@ -1101,7 +1101,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);
@ -1521,7 +1521,7 @@ 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,

View File

@ -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) {

View File

@ -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);

View File

@ -1894,11 +1894,11 @@ impl EarlyLintPass for KeywordIdents {
fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &ast::MacCall) {
self.check_tokens(cx, &mac.args.tokens);
}
fn check_ident(&mut self, cx: &EarlyContext<'_>, ident: Ident) {
fn check_ident(&mut self, cx: &EarlyContext<'_>, ident: &Ident) {
if ident.name.as_str().starts_with('\'') {
self.check_ident_token(cx, UnderMacro(false), ident.without_first_quote(), "'");
} else {
self.check_ident_token(cx, UnderMacro(false), ident, "");
self.check_ident_token(cx, UnderMacro(false), *ident, "");
}
}
}

View File

@ -202,7 +202,7 @@ 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) {
fn visit_ident(&mut self, ident: &Ident) {
lint_callback!(self, check_ident, ident);
}

View File

@ -133,7 +133,7 @@ macro_rules! early_lint_methods {
($macro:path, $args:tt) => (
$macro!($args, [
fn check_param(a: &rustc_ast::Param);
fn check_ident(a: rustc_span::symbol::Ident);
fn check_ident(a: &rustc_span::symbol::Ident);
fn check_crate(a: &rustc_ast::Crate);
fn check_crate_post(a: &rustc_ast::Crate);
fn check_item(a: &rustc_ast::Item);

View File

@ -1321,7 +1321,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);
}

View File

@ -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| {
@ -4582,7 +4582,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());
}

View File

@ -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);
}
}