Skip to content

Commit c86c8d4

Browse files
committed
Perform node id assignment and macros_at_scope construction during
the `InvocationCollector` and `PlaceholderExpander` folds.
1 parent 72a6369 commit c86c8d4

File tree

7 files changed

+83
-104
lines changed

7 files changed

+83
-104
lines changed

src/librustc_driver/driver.rs

-2
Original file line numberDiff line numberDiff line change
@@ -707,8 +707,6 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
707707
&sess.features.borrow())
708708
});
709709

710-
let krate = time(sess.time_passes(), "assigning node ids", || resolver.assign_node_ids(krate));
711-
712710
if sess.opts.debugging_opts.input_stats {
713711
println!("Post-expansion node count: {}", count_nodes(&krate));
714712
}

src/librustc_resolve/assign_ids.rs

-92
This file was deleted.

src/librustc_resolve/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ mod macros;
8484
mod check_unused;
8585
mod build_reduced_graph;
8686
mod resolve_imports;
87-
mod assign_ids;
8887

8988
enum SuggestionType {
9089
Macro(String),

src/librustc_resolve/macros.rs

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ impl<'a> base::Resolver for Resolver<'a> {
4141
self.macro_loader.load_crate(extern_crate, allows_macros)
4242
}
4343

44+
fn next_node_id(&mut self) -> ast::NodeId {
45+
self.session.next_node_id()
46+
}
47+
4448
fn visit_expansion(&mut self, mark: Mark, expansion: &Expansion) {
4549
expansion.visit_with(&mut ExpansionVisitor {
4650
current_module: self.expansion_data[mark.as_u32() as usize].module.clone(),

src/libsyntax/ext/base.rs

+3
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ pub type NamedSyntaxExtension = (Name, SyntaxExtension);
464464

465465
pub trait Resolver {
466466
fn load_crate(&mut self, extern_crate: &ast::Item, allows_macros: bool) -> Vec<LoadedMacro>;
467+
fn next_node_id(&mut self) -> ast::NodeId;
467468

468469
fn visit_expansion(&mut self, mark: Mark, expansion: &Expansion);
469470
fn add_macro(&mut self, scope: Mark, ident: ast::Ident, ext: Rc<SyntaxExtension>);
@@ -479,10 +480,12 @@ pub enum LoadedMacro {
479480
}
480481

481482
pub struct DummyResolver;
483+
482484
impl Resolver for DummyResolver {
483485
fn load_crate(&mut self, _extern_crate: &ast::Item, _allows_macros: bool) -> Vec<LoadedMacro> {
484486
Vec::new()
485487
}
488+
fn next_node_id(&mut self) -> ast::NodeId { ast::DUMMY_NODE_ID }
486489

487490
fn visit_expansion(&mut self, _invoc: Mark, _expansion: &Expansion) {}
488491
fn add_macro(&mut self, _scope: Mark, _ident: ast::Ident, _ext: Rc<SyntaxExtension>) {}

src/libsyntax/ext/expand.rs

+19-5
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
195195
krate.module.items = self.expand(items).make_items().into();
196196
krate.exported_macros = mem::replace(&mut self.cx.exported_macros, Vec::new());
197197

198+
for def in &mut krate.exported_macros {
199+
def.id = self.cx.resolver.next_node_id()
200+
}
201+
198202
if self.cx.parse_sess.span_diagnostic.err_count() > err_count {
199203
self.cx.parse_sess.span_diagnostic.abort_if_errors();
200204
}
@@ -234,7 +238,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
234238

235239
self.cx.current_expansion = orig_expansion_data;
236240

237-
let mut placeholder_expander = PlaceholderExpander::new();
241+
let mut placeholder_expander = PlaceholderExpander::new(self.cx);
238242
while let Some(expansions) = expansions.pop() {
239243
for (mark, expansion) in expansions.into_iter().rev() {
240244
let expansion = expansion.fold_with(&mut placeholder_expander);
@@ -530,9 +534,14 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
530534
None => return SmallVector::zero(),
531535
};
532536

533-
let (mac, style, attrs) = match stmt.node {
534-
StmtKind::Mac(mac) => mac.unwrap(),
535-
_ => return noop_fold_stmt(stmt, self),
537+
let (mac, style, attrs) = if let StmtKind::Mac(mac) = stmt.node {
538+
mac.unwrap()
539+
} else {
540+
// The placeholder expander gives ids to statements, so we avoid folding the id here.
541+
let ast::Stmt { id, node, span } = stmt;
542+
return noop_fold_stmt_kind(node, self).into_iter().map(|node| {
543+
ast::Stmt { id: id, node: node, span: span }
544+
}).collect()
536545
};
537546

538547
let mut placeholder =
@@ -624,7 +633,7 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
624633
}
625634
}
626635
}
627-
SmallVector::one(item)
636+
noop_fold_item(item, self)
628637
},
629638
_ => noop_fold_item(item, self),
630639
}
@@ -687,6 +696,11 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
687696
fn fold_item_kind(&mut self, item: ast::ItemKind) -> ast::ItemKind {
688697
noop_fold_item_kind(self.cfg.configure_item_kind(item), self)
689698
}
699+
700+
fn new_id(&mut self, id: ast::NodeId) -> ast::NodeId {
701+
assert_eq!(id, ast::DUMMY_NODE_ID);
702+
self.cx.resolver.next_node_id()
703+
}
690704
}
691705

692706
pub struct ExpansionConfig<'feat> {

src/libsyntax/ext/placeholders.rs

+57-4
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010

1111
use ast;
1212
use codemap::{DUMMY_SP, dummy_spanned};
13+
use ext::base::ExtCtxt;
1314
use ext::expand::{Expansion, ExpansionKind};
1415
use fold::*;
1516
use parse::token::keywords;
1617
use ptr::P;
18+
use util::move_map::MoveMap;
1719
use util::small_vector::SmallVector;
1820

1921
use std::collections::HashMap;
22+
use std::mem;
2023

2124
pub fn placeholder(kind: ExpansionKind, id: ast::NodeId) -> Expansion {
2225
fn mac_placeholder() -> ast::Mac {
@@ -69,13 +72,15 @@ pub fn macro_scope_placeholder() -> Expansion {
6972
placeholder(ExpansionKind::Items, ast::DUMMY_NODE_ID)
7073
}
7174

72-
pub struct PlaceholderExpander {
75+
pub struct PlaceholderExpander<'a, 'b: 'a> {
7376
expansions: HashMap<ast::NodeId, Expansion>,
77+
cx: &'a mut ExtCtxt<'b>,
7478
}
7579

76-
impl PlaceholderExpander {
77-
pub fn new() -> Self {
80+
impl<'a, 'b> PlaceholderExpander<'a, 'b> {
81+
pub fn new(cx: &'a mut ExtCtxt<'b>) -> Self {
7882
PlaceholderExpander {
83+
cx: cx,
7984
expansions: HashMap::new(),
8085
}
8186
}
@@ -89,7 +94,7 @@ impl PlaceholderExpander {
8994
}
9095
}
9196

92-
impl Folder for PlaceholderExpander {
97+
impl<'a, 'b> Folder for PlaceholderExpander<'a, 'b> {
9398
fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> {
9499
match item.node {
95100
// Scope placeholder
@@ -155,6 +160,54 @@ impl Folder for PlaceholderExpander {
155160
_ => noop_fold_ty(ty, self),
156161
}
157162
}
163+
164+
fn fold_block(&mut self, block: P<ast::Block>) -> P<ast::Block> {
165+
noop_fold_block(block, self).map(|mut block| {
166+
let mut macros = Vec::new();
167+
let mut remaining_stmts = block.stmts.len();
168+
169+
block.stmts = block.stmts.move_flat_map(|mut stmt| {
170+
remaining_stmts -= 1;
171+
172+
// Scope placeholder
173+
if let ast::StmtKind::Item(ref item) = stmt.node {
174+
if let ast::ItemKind::Mac(..) = item.node {
175+
macros.push(item.ident.ctxt.data().outer_mark);
176+
return None;
177+
}
178+
}
179+
180+
match stmt.node {
181+
// Avoid wasting a node id on a trailing expression statement,
182+
// which shares a HIR node with the expression itself.
183+
ast::StmtKind::Expr(ref expr) if remaining_stmts == 0 => stmt.id = expr.id,
184+
185+
_ => {
186+
assert_eq!(stmt.id, ast::DUMMY_NODE_ID);
187+
stmt.id = self.cx.resolver.next_node_id();
188+
}
189+
}
190+
191+
if !macros.is_empty() {
192+
let macros = mem::replace(&mut macros, Vec::new());
193+
self.cx.resolver.add_expansions_at_stmt(stmt.id, macros);
194+
}
195+
196+
Some(stmt)
197+
});
198+
199+
block
200+
})
201+
}
202+
203+
fn fold_mod(&mut self, module: ast::Mod) -> ast::Mod {
204+
let mut module = noop_fold_mod(module, self);
205+
module.items = module.items.move_flat_map(|item| match item.node {
206+
ast::ItemKind::Mac(_) => None, // remove scope placeholders from modules
207+
_ => Some(item),
208+
});
209+
module
210+
}
158211
}
159212

160213
pub fn reconstructed_macro_rules(def: &ast::MacroDef, path: &ast::Path) -> Expansion {

0 commit comments

Comments
 (0)