Skip to content

Commit

Permalink
Auto merge of rust-lang#127524 - oli-obk:feed_item_attrs2, r=petroche…
Browse files Browse the repository at this point in the history
…nkov

Make ast `MutVisitor` have the same method name and style as `Visitor`

It doesn't map 100% because some `MutVisitor` methods can filter or even expand to multiple items, but consistency seems nicer.

tracking issue: rust-lang#127615
  • Loading branch information
bors committed Jul 24, 2024
2 parents 08a9ca7 + e9f32d0 commit d24930c
Show file tree
Hide file tree
Showing 12 changed files with 478 additions and 445 deletions.
571 changes: 298 additions & 273 deletions compiler/rustc_ast/src/mut_visit.rs

Large diffs are not rendered by default.

71 changes: 40 additions & 31 deletions compiler/rustc_builtin_macros/src/cfg_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::ops::ControlFlow;
use rustc_ast as ast;
use rustc_ast::mut_visit::MutVisitor;
use rustc_ast::ptr::P;
use rustc_ast::visit::Visitor;
use rustc_ast::visit::{AssocCtxt, Visitor};
use rustc_ast::NodeId;
use rustc_ast::{mut_visit, visit};
use rustc_ast::{Attribute, HasAttrs, HasTokens};
Expand Down Expand Up @@ -53,11 +53,8 @@ fn flat_map_annotatable(
) -> Option<Annotatable> {
match annotatable {
Annotatable::Item(item) => vis.flat_map_item(item).pop().map(Annotatable::Item),
Annotatable::TraitItem(item) => {
vis.flat_map_trait_item(item).pop().map(Annotatable::TraitItem)
}
Annotatable::ImplItem(item) => {
vis.flat_map_impl_item(item).pop().map(Annotatable::ImplItem)
Annotatable::AssocItem(item, ctxt) => {
Some(Annotatable::AssocItem(vis.flat_map_assoc_item(item, ctxt).pop()?, ctxt))
}
Annotatable::ForeignItem(item) => {
vis.flat_map_foreign_item(item).pop().map(Annotatable::ForeignItem)
Expand Down Expand Up @@ -106,8 +103,7 @@ fn has_cfg_or_cfg_attr(annotatable: &Annotatable) -> bool {

let res = match annotatable {
Annotatable::Item(item) => CfgFinder.visit_item(item),
Annotatable::TraitItem(item) => CfgFinder.visit_assoc_item(item, visit::AssocCtxt::Trait),
Annotatable::ImplItem(item) => CfgFinder.visit_assoc_item(item, visit::AssocCtxt::Impl),
Annotatable::AssocItem(item, ctxt) => CfgFinder.visit_assoc_item(item, *ctxt),
Annotatable::ForeignItem(item) => CfgFinder.visit_foreign_item(item),
Annotatable::Stmt(stmt) => CfgFinder.visit_stmt(stmt),
Annotatable::Expr(expr) => CfgFinder.visit_expr(expr),
Expand Down Expand Up @@ -150,14 +146,16 @@ impl CfgEval<'_> {
Annotatable::Item(_) => {
|parser| Ok(Annotatable::Item(parser.parse_item(ForceCollect::Yes)?.unwrap()))
}
Annotatable::TraitItem(_) => |parser| {
Ok(Annotatable::TraitItem(
Annotatable::AssocItem(_, AssocCtxt::Trait) => |parser| {
Ok(Annotatable::AssocItem(
parser.parse_trait_item(ForceCollect::Yes)?.unwrap().unwrap(),
AssocCtxt::Trait,
))
},
Annotatable::ImplItem(_) => |parser| {
Ok(Annotatable::ImplItem(
Annotatable::AssocItem(_, AssocCtxt::Impl) => |parser| {
Ok(Annotatable::AssocItem(
parser.parse_impl_item(ForceCollect::Yes)?.unwrap().unwrap(),
AssocCtxt::Impl,
))
},
Annotatable::ForeignItem(_) => |parser| {
Expand Down Expand Up @@ -214,72 +212,83 @@ impl MutVisitor for CfgEval<'_> {
#[instrument(level = "trace", skip(self))]
fn visit_expr(&mut self, expr: &mut P<ast::Expr>) {
self.0.configure_expr(expr, false);
mut_visit::noop_visit_expr(expr, self);
mut_visit::walk_expr(self, expr);
}

#[instrument(level = "trace", skip(self))]
fn visit_method_receiver_expr(&mut self, expr: &mut P<ast::Expr>) {
self.0.configure_expr(expr, true);
mut_visit::noop_visit_expr(expr, self);
mut_visit::walk_expr(self, expr);
}

fn filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
let mut expr = configure!(self, expr);
mut_visit::noop_visit_expr(&mut expr, self);
mut_visit::walk_expr(self, &mut expr);
Some(expr)
}

fn flat_map_generic_param(
&mut self,
param: ast::GenericParam,
) -> SmallVec<[ast::GenericParam; 1]> {
mut_visit::noop_flat_map_generic_param(configure!(self, param), self)
let param = configure!(self, param);
mut_visit::walk_flat_map_generic_param(self, param)
}

fn flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
mut_visit::noop_flat_map_stmt(configure!(self, stmt), self)
let stmt = configure!(self, stmt);
mut_visit::walk_flat_map_stmt(self, stmt)
}

fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
mut_visit::noop_flat_map_item(configure!(self, item), self)
}

fn flat_map_impl_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
mut_visit::noop_flat_map_item(configure!(self, item), self)
let item = configure!(self, item);
mut_visit::walk_flat_map_item(self, item)
}

fn flat_map_trait_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
mut_visit::noop_flat_map_item(configure!(self, item), self)
fn flat_map_assoc_item(
&mut self,
item: P<ast::AssocItem>,
_ctxt: AssocCtxt,
) -> SmallVec<[P<ast::AssocItem>; 1]> {
let item = configure!(self, item);
mut_visit::walk_flat_map_item(self, item)
}

fn flat_map_foreign_item(
&mut self,
foreign_item: P<ast::ForeignItem>,
) -> SmallVec<[P<ast::ForeignItem>; 1]> {
mut_visit::noop_flat_map_item(configure!(self, foreign_item), self)
let foreign_item = configure!(self, foreign_item);
mut_visit::walk_flat_map_item(self, foreign_item)
}

fn flat_map_arm(&mut self, arm: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
mut_visit::noop_flat_map_arm(configure!(self, arm), self)
let arm = configure!(self, arm);
mut_visit::walk_flat_map_arm(self, arm)
}

fn flat_map_expr_field(&mut self, field: ast::ExprField) -> SmallVec<[ast::ExprField; 1]> {
mut_visit::noop_flat_map_expr_field(configure!(self, field), self)
let field = configure!(self, field);
mut_visit::walk_flat_map_expr_field(self, field)
}

fn flat_map_pat_field(&mut self, fp: ast::PatField) -> SmallVec<[ast::PatField; 1]> {
mut_visit::noop_flat_map_pat_field(configure!(self, fp), self)
let fp = configure!(self, fp);
mut_visit::walk_flat_map_pat_field(self, fp)
}

fn flat_map_param(&mut self, p: ast::Param) -> SmallVec<[ast::Param; 1]> {
mut_visit::noop_flat_map_param(configure!(self, p), self)
let p = configure!(self, p);
mut_visit::walk_flat_map_param(self, p)
}

fn flat_map_field_def(&mut self, sf: ast::FieldDef) -> SmallVec<[ast::FieldDef; 1]> {
mut_visit::noop_flat_map_field_def(configure!(self, sf), self)
let sf = configure!(self, sf);
mut_visit::walk_flat_map_field_def(self, sf)
}

fn flat_map_variant(&mut self, variant: ast::Variant) -> SmallVec<[ast::Variant; 1]> {
mut_visit::noop_flat_map_variant(configure!(self, variant), self)
let variant = configure!(self, variant);
mut_visit::walk_flat_map_variant(self, variant)
}
}
12 changes: 6 additions & 6 deletions compiler/rustc_builtin_macros/src/test_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ impl TestHarnessGenerator<'_> {
impl<'a> MutVisitor for TestHarnessGenerator<'a> {
fn visit_crate(&mut self, c: &mut ast::Crate) {
let prev_tests = mem::take(&mut self.tests);
noop_visit_crate(c, self);
walk_crate(self, c);
self.add_test_cases(ast::CRATE_NODE_ID, c.spans.inner_span, prev_tests);

// Create a main function to run our tests
c.items.push(mk_main(&mut self.cx));
}

fn flat_map_item(&mut self, i: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
let mut item = i.into_inner();
fn flat_map_item(&mut self, mut i: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
let item = &mut *i;
if let Some(name) = get_test_name(&item) {
debug!("this is a test item");

Expand All @@ -144,13 +144,13 @@ impl<'a> MutVisitor for TestHarnessGenerator<'a> {
item.kind
{
let prev_tests = mem::take(&mut self.tests);
noop_visit_item_kind(&mut item.kind, self);
walk_item_kind(&mut item.kind, item.span, item.id, self);
self.add_test_cases(item.id, span, prev_tests);
} else {
// But in those cases, we emit a lint to warn the user of these missing tests.
walk_item(&mut InnerItemLinter { sess: self.cx.ext_cx.sess }, &item);
}
smallvec![P(item)]
smallvec![i]
}
}

Expand Down Expand Up @@ -192,7 +192,7 @@ struct EntryPointCleaner<'a> {
impl<'a> MutVisitor for EntryPointCleaner<'a> {
fn flat_map_item(&mut self, i: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
self.depth += 1;
let item = noop_flat_map_item(i, self).expect_one("noop did something");
let item = walk_flat_map_item(self, i).expect_one("noop did something");
self.depth -= 1;

// Remove any #[rustc_main] or #[start] from the AST so it doesn't
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_builtin_macros/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ pub(crate) fn check_builtin_macro_attribute(ecx: &ExtCtxt<'_>, meta_item: &MetaI
pub(crate) fn warn_on_duplicate_attribute(ecx: &ExtCtxt<'_>, item: &Annotatable, name: Symbol) {
let attrs: Option<&[Attribute]> = match item {
Annotatable::Item(item) => Some(&item.attrs),
Annotatable::TraitItem(item) => Some(&item.attrs),
Annotatable::ImplItem(item) => Some(&item.attrs),
Annotatable::AssocItem(item, _) => Some(&item.attrs),
Annotatable::ForeignItem(item) => Some(&item.attrs),
Annotatable::Expr(expr) => Some(&expr.attrs),
Annotatable::Arm(arm) => Some(&arm.attrs),
Expand Down
20 changes: 7 additions & 13 deletions compiler/rustc_expand/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ use thin_vec::ThinVec;
#[derive(Debug, Clone)]
pub enum Annotatable {
Item(P<ast::Item>),
TraitItem(P<ast::AssocItem>),
ImplItem(P<ast::AssocItem>),
AssocItem(P<ast::AssocItem>, AssocCtxt),
ForeignItem(P<ast::ForeignItem>),
Stmt(P<ast::Stmt>),
Expr(P<ast::Expr>),
Expand All @@ -56,8 +55,7 @@ impl Annotatable {
pub fn span(&self) -> Span {
match self {
Annotatable::Item(item) => item.span,
Annotatable::TraitItem(trait_item) => trait_item.span,
Annotatable::ImplItem(impl_item) => impl_item.span,
Annotatable::AssocItem(assoc_item, _) => assoc_item.span,
Annotatable::ForeignItem(foreign_item) => foreign_item.span,
Annotatable::Stmt(stmt) => stmt.span,
Annotatable::Expr(expr) => expr.span,
Expand All @@ -75,8 +73,7 @@ impl Annotatable {
pub fn visit_attrs(&mut self, f: impl FnOnce(&mut AttrVec)) {
match self {
Annotatable::Item(item) => item.visit_attrs(f),
Annotatable::TraitItem(trait_item) => trait_item.visit_attrs(f),
Annotatable::ImplItem(impl_item) => impl_item.visit_attrs(f),
Annotatable::AssocItem(assoc_item, _) => assoc_item.visit_attrs(f),
Annotatable::ForeignItem(foreign_item) => foreign_item.visit_attrs(f),
Annotatable::Stmt(stmt) => stmt.visit_attrs(f),
Annotatable::Expr(expr) => expr.visit_attrs(f),
Expand All @@ -94,8 +91,7 @@ impl Annotatable {
pub fn visit_with<'a, V: Visitor<'a>>(&'a self, visitor: &mut V) -> V::Result {
match self {
Annotatable::Item(item) => visitor.visit_item(item),
Annotatable::TraitItem(item) => visitor.visit_assoc_item(item, AssocCtxt::Trait),
Annotatable::ImplItem(item) => visitor.visit_assoc_item(item, AssocCtxt::Impl),
Annotatable::AssocItem(item, ctxt) => visitor.visit_assoc_item(item, *ctxt),
Annotatable::ForeignItem(foreign_item) => visitor.visit_foreign_item(foreign_item),
Annotatable::Stmt(stmt) => visitor.visit_stmt(stmt),
Annotatable::Expr(expr) => visitor.visit_expr(expr),
Expand All @@ -113,9 +109,7 @@ impl Annotatable {
pub fn to_tokens(&self) -> TokenStream {
match self {
Annotatable::Item(node) => TokenStream::from_ast(node),
Annotatable::TraitItem(node) | Annotatable::ImplItem(node) => {
TokenStream::from_ast(node)
}
Annotatable::AssocItem(node, _) => TokenStream::from_ast(node),
Annotatable::ForeignItem(node) => TokenStream::from_ast(node),
Annotatable::Stmt(node) => {
assert!(!matches!(node.kind, ast::StmtKind::Empty));
Expand All @@ -142,14 +136,14 @@ impl Annotatable {

pub fn expect_trait_item(self) -> P<ast::AssocItem> {
match self {
Annotatable::TraitItem(i) => i,
Annotatable::AssocItem(i, AssocCtxt::Trait) => i,
_ => panic!("expected Item"),
}
}

pub fn expect_impl_item(self) -> P<ast::AssocItem> {
match self {
Annotatable::ImplItem(i) => i,
Annotatable::AssocItem(i, AssocCtxt::Impl) => i,
_ => panic!("expected Item"),
}
}
Expand Down
Loading

0 comments on commit d24930c

Please sign in to comment.