Skip to content

Commit

Permalink
Rollup merge of rust-lang#87779 - Aaron1011:stmt-ast-id, r=petrochenkov
Browse files Browse the repository at this point in the history
Remove special case for statement `NodeId` assignment

We now let `noop_flat_map_stmt` assign `NodeId`s (via `visit_id`),
just as we do for other AST nodes.
  • Loading branch information
JohnTitor authored Aug 6, 2021
2 parents 5f37944 + 1a7e56c commit 7353e2f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 28 deletions.
9 changes: 2 additions & 7 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
self.cx.force_mode = orig_force_mode;

// Finally incorporate all the expanded macros into the input AST fragment.
let mut placeholder_expander = PlaceholderExpander::new(self.cx, self.monotonic);
let mut placeholder_expander = PlaceholderExpander::default();
while let Some(expanded_fragments) = expanded_fragments.pop() {
for (expn_id, expanded_fragment) in expanded_fragments.into_iter().rev() {
placeholder_expander
Expand Down Expand Up @@ -1341,14 +1341,9 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
}
}

// The placeholder expander gives ids to statements, so we avoid folding the id here.
// We don't use `assign_id!` - it will be called when we visit statement's contents
// (e.g. an expression, item, or local)
let ast::Stmt { id, kind, span } = stmt;
let res = noop_flat_map_stmt_kind(kind, self)
.into_iter()
.map(|kind| ast::Stmt { id, kind, span })
.collect();
let res = noop_flat_map_stmt(stmt, self);

self.cx.current_expansion.is_trailing_mac = false;
res
Expand Down
25 changes: 4 additions & 21 deletions compiler/rustc_expand/src/placeholders.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::base::ExtCtxt;
use crate::expand::{AstFragment, AstFragmentKind};

use rustc_ast as ast;
Expand Down Expand Up @@ -175,17 +174,12 @@ pub fn placeholder(
}
}

pub struct PlaceholderExpander<'a, 'b> {
#[derive(Default)]
pub struct PlaceholderExpander {
expanded_fragments: FxHashMap<ast::NodeId, AstFragment>,
cx: &'a mut ExtCtxt<'b>,
monotonic: bool,
}

impl<'a, 'b> PlaceholderExpander<'a, 'b> {
pub fn new(cx: &'a mut ExtCtxt<'b>, monotonic: bool) -> Self {
PlaceholderExpander { cx, expanded_fragments: FxHashMap::default(), monotonic }
}

impl PlaceholderExpander {
pub fn add(&mut self, id: ast::NodeId, mut fragment: AstFragment) {
fragment.mut_visit_with(self);
self.expanded_fragments.insert(id, fragment);
Expand All @@ -196,7 +190,7 @@ impl<'a, 'b> PlaceholderExpander<'a, 'b> {
}
}

impl<'a, 'b> MutVisitor for PlaceholderExpander<'a, 'b> {
impl MutVisitor for PlaceholderExpander {
fn flat_map_arm(&mut self, arm: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
if arm.is_placeholder {
self.remove(arm.id).make_arms()
Expand Down Expand Up @@ -360,15 +354,4 @@ impl<'a, 'b> MutVisitor for PlaceholderExpander<'a, 'b> {
_ => noop_visit_ty(ty, self),
}
}

fn visit_block(&mut self, block: &mut P<ast::Block>) {
noop_visit_block(block, self);

for stmt in block.stmts.iter_mut() {
if self.monotonic {
assert_eq!(stmt.id, ast::DUMMY_NODE_ID);
stmt.id = self.cx.resolver.next_node_id();
}
}
}
}

0 comments on commit 7353e2f

Please sign in to comment.