Skip to content

Commit 1b19284

Browse files
committed
Auto merge of #40220 - jseyfried:ast_macro_def, r=nrc
syntax: add `ast::ItemKind::MacroDef`, simplify hygiene info This PR - adds a new variant `MacroDef` to `ast::ItemKind` for `macro_rules!` and eventually `macro` items, - [breaking-change] forbids macro defs without a name (`macro_rules! { () => {} }` compiles today), - removes `ast::MacroDef`, and - no longer uses `Mark` and `Invocation` to identify and characterize macro definitions. - We used to apply (at least) two `Mark`s to an expanded identifier's `SyntaxContext` -- the definition mark(s) and the expansion mark(s). We now only apply the latter. r? @nrc
2 parents e4eb964 + 8c98996 commit 1b19284

File tree

25 files changed

+305
-316
lines changed

25 files changed

+305
-316
lines changed

src/librustc/hir/lowering.rs

+28-23
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ pub struct LoweringContext<'a> {
7979
trait_items: BTreeMap<hir::TraitItemId, hir::TraitItem>,
8080
impl_items: BTreeMap<hir::ImplItemId, hir::ImplItem>,
8181
bodies: BTreeMap<hir::BodyId, hir::Body>,
82+
exported_macros: Vec<hir::MacroDef>,
8283

8384
trait_impls: BTreeMap<DefId, Vec<NodeId>>,
8485
trait_default_impl: BTreeMap<DefId, NodeId>,
@@ -121,6 +122,7 @@ pub fn lower_crate(sess: &Session,
121122
bodies: BTreeMap::new(),
122123
trait_impls: BTreeMap::new(),
123124
trait_default_impl: BTreeMap::new(),
125+
exported_macros: Vec::new(),
124126
loop_scopes: Vec::new(),
125127
is_in_loop_condition: false,
126128
type_def_lifetime_params: DefIdMap(),
@@ -170,9 +172,10 @@ impl<'a> LoweringContext<'a> {
170172

171173
impl<'lcx, 'interner> Visitor<'lcx> for ItemLowerer<'lcx, 'interner> {
172174
fn visit_item(&mut self, item: &'lcx Item) {
173-
let hir_item = self.lctx.lower_item(item);
174-
self.lctx.items.insert(item.id, hir_item);
175-
visit::walk_item(self, item);
175+
if let Some(hir_item) = self.lctx.lower_item(item) {
176+
self.lctx.items.insert(item.id, hir_item);
177+
visit::walk_item(self, item);
178+
}
176179
}
177180

178181
fn visit_trait_item(&mut self, item: &'lcx TraitItem) {
@@ -195,14 +198,13 @@ impl<'a> LoweringContext<'a> {
195198

196199
let module = self.lower_mod(&c.module);
197200
let attrs = self.lower_attrs(&c.attrs);
198-
let exported_macros = c.exported_macros.iter().map(|m| self.lower_macro_def(m)).collect();
199201
let body_ids = body_ids(&self.bodies);
200202

201203
hir::Crate {
202204
module: module,
203205
attrs: attrs,
204206
span: c.span,
205-
exported_macros: exported_macros,
207+
exported_macros: hir::HirVec::from(self.exported_macros),
206208
items: self.items,
207209
trait_items: self.trait_items,
208210
impl_items: self.impl_items,
@@ -1134,7 +1136,7 @@ impl<'a> LoweringContext<'a> {
11341136
bounds,
11351137
items)
11361138
}
1137-
ItemKind::Mac(_) => panic!("Shouldn't still be around"),
1139+
ItemKind::MacroDef(..) | ItemKind::Mac(..) => panic!("Shouldn't still be around"),
11381140
}
11391141
}
11401142

@@ -1256,42 +1258,45 @@ impl<'a> LoweringContext<'a> {
12561258
}
12571259
}
12581260

1259-
fn lower_macro_def(&mut self, m: &MacroDef) -> hir::MacroDef {
1260-
hir::MacroDef {
1261-
name: m.ident.name,
1262-
attrs: self.lower_attrs(&m.attrs),
1263-
id: m.id,
1264-
span: m.span,
1265-
body: m.body.clone().into(),
1266-
}
1267-
}
1268-
12691261
fn lower_item_id(&mut self, i: &Item) -> SmallVector<hir::ItemId> {
1270-
if let ItemKind::Use(ref view_path) = i.node {
1271-
if let ViewPathList(_, ref imports) = view_path.node {
1272-
return iter::once(i.id).chain(imports.iter().map(|import| import.node.id))
1273-
.map(|id| hir::ItemId { id: id }).collect();
1262+
match i.node {
1263+
ItemKind::Use(ref view_path) => {
1264+
if let ViewPathList(_, ref imports) = view_path.node {
1265+
return iter::once(i.id).chain(imports.iter().map(|import| import.node.id))
1266+
.map(|id| hir::ItemId { id: id }).collect();
1267+
}
12741268
}
1269+
ItemKind::MacroDef(..) => return SmallVector::new(),
1270+
_ => {}
12751271
}
12761272
SmallVector::one(hir::ItemId { id: i.id })
12771273
}
12781274

1279-
pub fn lower_item(&mut self, i: &Item) -> hir::Item {
1275+
pub fn lower_item(&mut self, i: &Item) -> Option<hir::Item> {
12801276
let mut name = i.ident.name;
12811277
let attrs = self.lower_attrs(&i.attrs);
12821278
let mut vis = self.lower_visibility(&i.vis);
1279+
if let ItemKind::MacroDef(ref tts) = i.node {
1280+
if i.attrs.iter().any(|attr| attr.name() == "macro_export") {
1281+
self.exported_macros.push(hir::MacroDef {
1282+
name: name, attrs: attrs, id: i.id, span: i.span, body: tts.clone().into(),
1283+
});
1284+
}
1285+
return None;
1286+
}
1287+
12831288
let node = self.with_parent_def(i.id, |this| {
12841289
this.lower_item_kind(i.id, &mut name, &attrs, &mut vis, &i.node)
12851290
});
12861291

1287-
hir::Item {
1292+
Some(hir::Item {
12881293
id: i.id,
12891294
name: name,
12901295
attrs: attrs,
12911296
node: node,
12921297
vis: vis,
12931298
span: i.span,
1294-
}
1299+
})
12951300
}
12961301

12971302
fn lower_foreign_item(&mut self, i: &ForeignItem) -> hir::ForeignItem {

src/librustc/hir/map/def_collector.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
108108
ItemKind::Mod(..) => DefPathData::Module(i.ident.name.as_str()),
109109
ItemKind::Static(..) | ItemKind::Const(..) | ItemKind::Fn(..) =>
110110
DefPathData::ValueNs(i.ident.name.as_str()),
111-
ItemKind::Mac(..) if i.id == DUMMY_NODE_ID => return, // Scope placeholder
111+
ItemKind::MacroDef(..) => DefPathData::MacroDef(i.ident.name.as_str()),
112112
ItemKind::Mac(..) => return self.visit_macro_invoc(i.id, false),
113113
ItemKind::Use(ref view_path) => {
114114
match view_path.node {
@@ -269,10 +269,6 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
269269
self.create_def(def.lifetime.id, DefPathData::LifetimeDef(def.lifetime.name.as_str()));
270270
}
271271

272-
fn visit_macro_def(&mut self, macro_def: &'a MacroDef) {
273-
self.create_def(macro_def.id, DefPathData::MacroDef(macro_def.ident.name.as_str()));
274-
}
275-
276272
fn visit_stmt(&mut self, stmt: &'a Stmt) {
277273
match stmt.node {
278274
StmtKind::Mac(..) => self.visit_macro_invoc(stmt.id, false),

src/librustc/middle/cstore.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ pub struct NativeLibrary {
136136
}
137137

138138
pub enum LoadedMacro {
139-
MacroRules(ast::MacroDef),
139+
MacroDef(ast::Item),
140140
ProcMacro(Rc<SyntaxExtension>),
141141
}
142142

src/librustc_driver/driver.rs

-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ use super::Compilation;
4343
use serialize::json;
4444

4545
use std::env;
46-
use std::mem;
4746
use std::ffi::{OsString, OsStr};
4847
use std::fs;
4948
use std::io::{self, Write};
@@ -708,8 +707,6 @@ pub fn phase_2_configure_and_expand<F>(sess: &Session,
708707
krate
709708
});
710709

711-
krate.exported_macros = mem::replace(&mut resolver.exported_macros, Vec::new());
712-
713710
krate = time(time_passes, "maybe building test harness", || {
714711
syntax::test::modify_for_testing(&sess.parse_sess,
715712
&mut resolver,

src/librustc_metadata/cstore_impl.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -414,12 +414,13 @@ impl CrateStore for cstore::CStore {
414414
sess.imported_macro_spans.borrow_mut()
415415
.insert(local_span, (name.to_string(), data.get_span(id.index, sess)));
416416

417-
LoadedMacro::MacroRules(ast::MacroDef {
417+
LoadedMacro::MacroDef(ast::Item {
418418
ident: ast::Ident::with_empty_ctxt(name),
419419
id: ast::DUMMY_NODE_ID,
420420
span: local_span,
421421
attrs: attrs,
422-
body: body.into(),
422+
node: ast::ItemKind::MacroDef(body.into()),
423+
vis: ast::Visibility::Inherited,
423424
})
424425
}
425426

src/librustc_passes/hir_stats.rs

-5
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,4 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
375375
fn visit_attribute(&mut self, attr: &'v ast::Attribute) {
376376
self.record("Attribute", Id::None, attr);
377377
}
378-
379-
fn visit_macro_def(&mut self, macro_def: &'v ast::MacroDef) {
380-
self.record("MacroDef", Id::None, macro_def);
381-
ast_visit::walk_macro_def(self, macro_def)
382-
}
383378
}

src/librustc_resolve/build_reduced_graph.rs

+20-21
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ use syntax::ast::{Mutability, StmtKind, TraitItem, TraitItemKind};
3737
use syntax::ast::{Variant, ViewPathGlob, ViewPathList, ViewPathSimple};
3838
use syntax::ext::base::SyntaxExtension;
3939
use syntax::ext::base::Determinacy::Undetermined;
40-
use syntax::ext::expand::mark_tts;
4140
use syntax::ext::hygiene::Mark;
4241
use syntax::ext::tt::macro_rules;
4342
use syntax::parse::token;
@@ -373,7 +372,7 @@ impl<'a> Resolver<'a> {
373372
self.define(parent, ident, TypeNS, (module, vis, sp, expansion));
374373
self.current_module = module;
375374
}
376-
ItemKind::Mac(_) => panic!("unexpanded macro in resolve!"),
375+
ItemKind::MacroDef(..) | ItemKind::Mac(_) => unreachable!(),
377376
}
378377
}
379378

@@ -493,6 +492,16 @@ impl<'a> Resolver<'a> {
493492
})
494493
}
495494

495+
pub fn macro_def_scope(&mut self, expansion: Mark) -> Module<'a> {
496+
let def_id = self.macro_defs[&expansion];
497+
if let Some(id) = self.definitions.as_local_node_id(def_id) {
498+
self.local_macro_def_scopes[&id]
499+
} else {
500+
let module_def_id = ty::DefIdTree::parent(&*self, def_id).unwrap();
501+
self.get_extern_crate_root(module_def_id.krate)
502+
}
503+
}
504+
496505
pub fn get_macro(&mut self, def: Def) -> Rc<SyntaxExtension> {
497506
let def_id = match def {
498507
Def::Macro(def_id, ..) => def_id,
@@ -502,22 +511,12 @@ impl<'a> Resolver<'a> {
502511
return ext.clone();
503512
}
504513

505-
let mut macro_rules = match self.session.cstore.load_macro(def_id, &self.session) {
506-
LoadedMacro::MacroRules(macro_rules) => macro_rules,
514+
let macro_def = match self.session.cstore.load_macro(def_id, &self.session) {
515+
LoadedMacro::MacroDef(macro_def) => macro_def,
507516
LoadedMacro::ProcMacro(ext) => return ext,
508517
};
509518

510-
let mark = Mark::fresh();
511-
let invocation = self.arenas.alloc_invocation_data(InvocationData {
512-
module: Cell::new(self.get_extern_crate_root(def_id.krate)),
513-
def_index: CRATE_DEF_INDEX,
514-
const_expr: false,
515-
legacy_scope: Cell::new(LegacyScope::Empty),
516-
expansion: Cell::new(LegacyScope::Empty),
517-
});
518-
self.invocations.insert(mark, invocation);
519-
macro_rules.body = mark_tts(macro_rules.stream(), mark).into();
520-
let ext = Rc::new(macro_rules::compile(&self.session.parse_sess, &macro_rules));
519+
let ext = Rc::new(macro_rules::compile(&self.session.parse_sess, &macro_def));
521520
self.macro_map.insert(def_id, ext.clone());
522521
ext
523522
}
@@ -707,12 +706,12 @@ impl<'a, 'b> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b> {
707706

708707
fn visit_item(&mut self, item: &'a Item) {
709708
let macro_use = match item.node {
710-
ItemKind::Mac(ref mac) => {
711-
if mac.node.path.segments.is_empty() {
712-
self.legacy_scope = LegacyScope::Expansion(self.visit_invoc(item.id));
713-
} else {
714-
self.resolver.define_macro(item, &mut self.legacy_scope);
715-
}
709+
ItemKind::MacroDef(..) => {
710+
self.resolver.define_macro(item, &mut self.legacy_scope);
711+
return
712+
}
713+
ItemKind::Mac(..) => {
714+
self.legacy_scope = LegacyScope::Expansion(self.visit_invoc(item.id));
716715
return
717716
}
718717
ItemKind::Mod(..) => self.resolver.contains_macro_use(&item.attrs),

0 commit comments

Comments
 (0)