Skip to content

Commit 8c98996

Browse files
jseyfriedalexcrichton
authored andcommitted
Avoid using Mark and Invocation for macro defs.
1 parent e839486 commit 8c98996

File tree

13 files changed

+100
-93
lines changed

13 files changed

+100
-93
lines changed

src/librustc/hir/lowering.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,7 @@ impl<'a> LoweringContext<'a> {
12761276
let mut name = i.ident.name;
12771277
let attrs = self.lower_attrs(&i.attrs);
12781278
let mut vis = self.lower_visibility(&i.vis);
1279-
if let ItemKind::MacroDef(ref tts, _) = i.node {
1279+
if let ItemKind::MacroDef(ref tts) = i.node {
12801280
if i.attrs.iter().any(|attr| attr.name() == "macro_export") {
12811281
self.exported_macros.push(hir::MacroDef {
12821282
name: name, attrs: attrs, id: i.id, span: i.span, body: tts.clone().into(),

src/librustc_metadata/cstore_impl.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use std::rc::Rc;
3434

3535
use syntax::ast;
3636
use syntax::attr;
37-
use syntax::ext::hygiene::Mark;
3837
use syntax::parse::filemap_to_stream;
3938
use syntax::symbol::Symbol;
4039
use syntax_pos::{mk_sp, Span};
@@ -420,7 +419,7 @@ impl CrateStore for cstore::CStore {
420419
id: ast::DUMMY_NODE_ID,
421420
span: local_span,
422421
attrs: attrs,
423-
node: ast::ItemKind::MacroDef(body.into(), Mark::fresh()),
422+
node: ast::ItemKind::MacroDef(body.into()),
424423
vis: ast::Visibility::Inherited,
425424
})
426425
}

src/librustc_resolve/build_reduced_graph.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,16 @@ impl<'a> Resolver<'a> {
492492
})
493493
}
494494

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+
495505
pub fn get_macro(&mut self, def: Def) -> Rc<SyntaxExtension> {
496506
let def_id = match def {
497507
Def::Macro(def_id, ..) => def_id,
@@ -506,15 +516,6 @@ impl<'a> Resolver<'a> {
506516
LoadedMacro::ProcMacro(ext) => return ext,
507517
};
508518

509-
let invocation = self.arenas.alloc_invocation_data(InvocationData {
510-
module: Cell::new(self.get_extern_crate_root(def_id.krate)),
511-
// FIXME(jseyfried) the following are irrelevant
512-
def_index: CRATE_DEF_INDEX, const_expr: false,
513-
legacy_scope: Cell::new(LegacyScope::Empty), expansion: Cell::new(LegacyScope::Empty),
514-
});
515-
if let ast::ItemKind::MacroDef(_, mark) = macro_def.node {
516-
self.invocations.insert(mark, invocation);
517-
}
518519
let ext = Rc::new(macro_rules::compile(&self.session.parse_sess, &macro_def));
519520
self.macro_map.insert(def_id, ext.clone());
520521
ext

src/librustc_resolve/lib.rs

+34-21
Original file line numberDiff line numberDiff line change
@@ -779,8 +779,8 @@ enum RibKind<'a> {
779779
// We passed through a module.
780780
ModuleRibKind(Module<'a>),
781781

782-
// We passed through a `macro_rules!` statement with the given expansion
783-
MacroDefinition(Mark),
782+
// We passed through a `macro_rules!` statement
783+
MacroDefinition(DefId),
784784

785785
// All bindings in this rib are type parameters that can't be used
786786
// from the default of a type parameter because they're not declared
@@ -997,14 +997,18 @@ impl<'a> NameBinding<'a> {
997997
}
998998
}
999999

1000-
fn get_macro(&self, resolver: &mut Resolver<'a>) -> Rc<SyntaxExtension> {
1000+
fn def_ignoring_ambiguity(&self) -> Def {
10011001
match self.kind {
1002-
NameBindingKind::Import { binding, .. } => binding.get_macro(resolver),
1003-
NameBindingKind::Ambiguity { b1, .. } => b1.get_macro(resolver),
1004-
_ => resolver.get_macro(self.def()),
1002+
NameBindingKind::Import { binding, .. } => binding.def_ignoring_ambiguity(),
1003+
NameBindingKind::Ambiguity { b1, .. } => b1.def_ignoring_ambiguity(),
1004+
_ => self.def(),
10051005
}
10061006
}
10071007

1008+
fn get_macro(&self, resolver: &mut Resolver<'a>) -> Rc<SyntaxExtension> {
1009+
resolver.get_macro(self.def_ignoring_ambiguity())
1010+
}
1011+
10081012
// We sometimes need to treat variants as `pub` for backwards compatibility
10091013
fn pseudo_vis(&self) -> ty::Visibility {
10101014
if self.is_variant() { ty::Visibility::Public } else { self.vis }
@@ -1172,6 +1176,8 @@ pub struct Resolver<'a> {
11721176
builtin_macros: FxHashMap<Name, &'a NameBinding<'a>>,
11731177
lexical_macro_resolutions: Vec<(Name, &'a Cell<LegacyScope<'a>>)>,
11741178
macro_map: FxHashMap<DefId, Rc<SyntaxExtension>>,
1179+
macro_defs: FxHashMap<Mark, DefId>,
1180+
local_macro_def_scopes: FxHashMap<NodeId, Module<'a>>,
11751181
macro_exports: Vec<Export>,
11761182
pub whitelisted_legacy_custom_derives: Vec<Name>,
11771183
pub found_unresolved_macro: bool,
@@ -1300,6 +1306,9 @@ impl<'a> Resolver<'a> {
13001306

13011307
let features = session.features.borrow();
13021308

1309+
let mut macro_defs = FxHashMap();
1310+
macro_defs.insert(Mark::root(), root_def_id);
1311+
13031312
Resolver {
13041313
session: session,
13051314

@@ -1366,6 +1375,8 @@ impl<'a> Resolver<'a> {
13661375
macro_map: FxHashMap(),
13671376
macro_exports: Vec::new(),
13681377
invocations: invocations,
1378+
macro_defs: macro_defs,
1379+
local_macro_def_scopes: FxHashMap(),
13691380
name_already_seen: FxHashMap(),
13701381
whitelisted_legacy_custom_derives: Vec::new(),
13711382
proc_macro_enabled: features.proc_macro,
@@ -1503,24 +1514,25 @@ impl<'a> Resolver<'a> {
15031514
}
15041515
}
15051516

1506-
if let MacroDefinition(mac) = self.ribs[ns][i].kind {
1517+
if let MacroDefinition(def) = self.ribs[ns][i].kind {
15071518
// If an invocation of this macro created `ident`, give up on `ident`
15081519
// and switch to `ident`'s source from the macro definition.
1509-
let (source_ctxt, source_macro) = ident.ctxt.source();
1510-
if source_macro == mac {
1511-
ident.ctxt = source_ctxt;
1520+
let ctxt_data = ident.ctxt.data();
1521+
if def == self.macro_defs[&ctxt_data.outer_mark] {
1522+
ident.ctxt = ctxt_data.prev_ctxt;
15121523
}
15131524
}
15141525
}
15151526

15161527
None
15171528
}
15181529

1519-
fn resolve_crate_var(&mut self, mut crate_var_ctxt: SyntaxContext) -> Module<'a> {
1520-
while crate_var_ctxt.source().0 != SyntaxContext::empty() {
1521-
crate_var_ctxt = crate_var_ctxt.source().0;
1530+
fn resolve_crate_var(&mut self, crate_var_ctxt: SyntaxContext) -> Module<'a> {
1531+
let mut ctxt_data = crate_var_ctxt.data();
1532+
while ctxt_data.prev_ctxt != SyntaxContext::empty() {
1533+
ctxt_data = ctxt_data.prev_ctxt.data();
15221534
}
1523-
let module = self.invocations[&crate_var_ctxt.source().1].module.get();
1535+
let module = self.macro_def_scope(ctxt_data.outer_mark);
15241536
if module.is_local() { self.graph_root } else { module }
15251537
}
15261538

@@ -1572,12 +1584,12 @@ impl<'a> Resolver<'a> {
15721584
NormalRibKind => {
15731585
// Continue
15741586
}
1575-
MacroDefinition(mac) => {
1587+
MacroDefinition(def) => {
15761588
// If an invocation of this macro created `ident`, give up on `ident`
15771589
// and switch to `ident`'s source from the macro definition.
1578-
let (source_ctxt, source_macro) = ident.ctxt.source();
1579-
if source_macro == mac {
1580-
ident.ctxt = source_ctxt;
1590+
let ctxt_data = ident.ctxt.data();
1591+
if def == self.macro_defs[&ctxt_data.outer_mark] {
1592+
ident.ctxt = ctxt_data.prev_ctxt;
15811593
}
15821594
}
15831595
_ => {
@@ -2025,10 +2037,11 @@ impl<'a> Resolver<'a> {
20252037
// Descend into the block.
20262038
for stmt in &block.stmts {
20272039
if let ast::StmtKind::Item(ref item) = stmt.node {
2028-
if let ast::ItemKind::MacroDef(_, mark) = item.node {
2040+
if let ast::ItemKind::MacroDef(..) = item.node {
20292041
num_macro_definition_ribs += 1;
2030-
self.ribs[ValueNS].push(Rib::new(MacroDefinition(mark)));
2031-
self.label_ribs.push(Rib::new(MacroDefinition(mark)));
2042+
let def = self.definitions.local_def_id(item.id);
2043+
self.ribs[ValueNS].push(Rib::new(MacroDefinition(def)));
2044+
self.label_ribs.push(Rib::new(MacroDefinition(def)));
20322045
}
20332046
}
20342047

src/librustc_resolve/macros.rs

+43-36
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub enum LegacyScope<'a> {
7474
pub struct LegacyBinding<'a> {
7575
pub parent: Cell<LegacyScope<'a>>,
7676
pub name: ast::Name,
77-
ext: Rc<SyntaxExtension>,
77+
def_id: DefId,
7878
pub span: Span,
7979
}
8080

@@ -239,15 +239,34 @@ impl<'a> base::Resolver for Resolver<'a> {
239239

240240
fn resolve_invoc(&mut self, invoc: &mut Invocation, scope: Mark, force: bool)
241241
-> Result<Option<Rc<SyntaxExtension>>, Determinacy> {
242-
let (attr, traits, item) = match invoc.kind {
242+
let def = match invoc.kind {
243243
InvocationKind::Attr { attr: None, .. } => return Ok(None),
244+
_ => match self.resolve_invoc_to_def(invoc, scope, force) {
245+
Ok(def) => def,
246+
Err(determinacy) => return Err(determinacy),
247+
},
248+
};
249+
self.macro_defs.insert(invoc.expansion_data.mark, def.def_id());
250+
Ok(Some(self.get_macro(def)))
251+
}
252+
253+
fn resolve_macro(&mut self, scope: Mark, path: &ast::Path, kind: MacroKind, force: bool)
254+
-> Result<Rc<SyntaxExtension>, Determinacy> {
255+
self.resolve_macro_to_def(scope, path, kind, force).map(|def| self.get_macro(def))
256+
}
257+
}
258+
259+
impl<'a> Resolver<'a> {
260+
fn resolve_invoc_to_def(&mut self, invoc: &mut Invocation, scope: Mark, force: bool)
261+
-> Result<Def, Determinacy> {
262+
let (attr, traits, item) = match invoc.kind {
244263
InvocationKind::Attr { ref mut attr, ref traits, ref mut item } => (attr, traits, item),
245264
InvocationKind::Bang { ref mac, .. } => {
246-
return self.resolve_macro(scope, &mac.node.path, MacroKind::Bang, force).map(Some);
265+
return self.resolve_macro_to_def(scope, &mac.node.path, MacroKind::Bang, force);
247266
}
248267
InvocationKind::Derive { name, span, .. } => {
249268
let path = ast::Path::from_ident(span, Ident::with_empty_ctxt(name));
250-
return self.resolve_macro(scope, &path, MacroKind::Derive, force).map(Some);
269+
return self.resolve_macro_to_def(scope, &path, MacroKind::Derive, force);
251270
}
252271
};
253272

@@ -257,8 +276,8 @@ impl<'a> base::Resolver for Resolver<'a> {
257276
};
258277

259278
let mut determined = true;
260-
match self.resolve_macro(scope, &path, MacroKind::Attr, force) {
261-
Ok(ext) => return Ok(Some(ext)),
279+
match self.resolve_macro_to_def(scope, &path, MacroKind::Attr, force) {
280+
Ok(def) => return Ok(def),
262281
Err(Determinacy::Undetermined) => determined = false,
263282
Err(Determinacy::Determined) if force => return Err(Determinacy::Determined),
264283
Err(Determinacy::Determined) => {}
@@ -293,8 +312,8 @@ impl<'a> base::Resolver for Resolver<'a> {
293312
Err(if determined { Determinacy::Determined } else { Determinacy::Undetermined })
294313
}
295314

296-
fn resolve_macro(&mut self, scope: Mark, path: &ast::Path, kind: MacroKind, force: bool)
297-
-> Result<Rc<SyntaxExtension>, Determinacy> {
315+
fn resolve_macro_to_def(&mut self, scope: Mark, path: &ast::Path, kind: MacroKind, force: bool)
316+
-> Result<Def, Determinacy> {
298317
let ast::Path { ref segments, span } = *path;
299318
if segments.iter().any(|segment| segment.parameters.is_some()) {
300319
let kind =
@@ -317,10 +336,10 @@ impl<'a> base::Resolver for Resolver<'a> {
317336
return Err(Determinacy::Determined);
318337
}
319338

320-
let ext = match self.resolve_path(&path, Some(MacroNS), None) {
339+
let def = match self.resolve_path(&path, Some(MacroNS), None) {
321340
PathResult::NonModule(path_res) => match path_res.base_def() {
322341
Def::Err => Err(Determinacy::Determined),
323-
def @ _ => Ok(self.get_macro(def)),
342+
def @ _ => Ok(def),
324343
},
325344
PathResult::Module(..) => unreachable!(),
326345
PathResult::Indeterminate if !force => return Err(Determinacy::Undetermined),
@@ -331,15 +350,15 @@ impl<'a> base::Resolver for Resolver<'a> {
331350
};
332351
self.current_module.macro_resolutions.borrow_mut()
333352
.push((path.into_boxed_slice(), span));
334-
return ext;
353+
return def;
335354
}
336355

337356
let name = path[0].name;
338357
let result = match self.resolve_legacy_scope(&invocation.legacy_scope, name, false) {
339-
Some(MacroBinding::Legacy(binding)) => Ok(binding.ext.clone()),
340-
Some(MacroBinding::Modern(binding)) => Ok(binding.get_macro(self)),
358+
Some(MacroBinding::Legacy(binding)) => Ok(Def::Macro(binding.def_id, MacroKind::Bang)),
359+
Some(MacroBinding::Modern(binding)) => Ok(binding.def_ignoring_ambiguity()),
341360
None => match self.resolve_lexical_macro_path_segment(path[0], MacroNS, None) {
342-
Ok(binding) => Ok(binding.get_macro(self)),
361+
Ok(binding) => Ok(binding.def_ignoring_ambiguity()),
343362
Err(Determinacy::Undetermined) if !force =>
344363
return Err(Determinacy::Undetermined),
345364
Err(_) => {
@@ -354,9 +373,7 @@ impl<'a> base::Resolver for Resolver<'a> {
354373

355374
result
356375
}
357-
}
358376

359-
impl<'a> Resolver<'a> {
360377
// Resolve the initial segment of a non-global macro path (e.g. `foo` in `foo::bar!();`)
361378
pub fn resolve_lexical_macro_path_segment(&mut self,
362379
ident: Ident,
@@ -597,33 +614,23 @@ impl<'a> Resolver<'a> {
597614
}
598615

599616
pub fn define_macro(&mut self, item: &ast::Item, legacy_scope: &mut LegacyScope<'a>) {
600-
if item.ident.name == "macro_rules" {
617+
self.local_macro_def_scopes.insert(item.id, self.current_module);
618+
let ident = item.ident;
619+
if ident.name == "macro_rules" {
601620
self.session.span_err(item.span, "user-defined macros may not be named `macro_rules`");
602621
}
603622

604-
let invocation = self.arenas.alloc_invocation_data(InvocationData {
605-
module: Cell::new(self.current_module),
606-
// FIXME(jseyfried) the following are irrelevant
607-
def_index: CRATE_DEF_INDEX, const_integer: false,
608-
legacy_scope: Cell::new(LegacyScope::Empty), expansion: Cell::new(LegacyScope::Empty),
609-
});
610-
if let ast::ItemKind::MacroDef(_, mark) = item.node {
611-
self.invocations.insert(mark, invocation);
612-
}
613-
623+
let def_id = self.definitions.local_def_id(item.id);
624+
let ext = Rc::new(macro_rules::compile(&self.session.parse_sess, item));
625+
self.macro_map.insert(def_id, ext);
614626
*legacy_scope = LegacyScope::Binding(self.arenas.alloc_legacy_binding(LegacyBinding {
615-
parent: Cell::new(*legacy_scope),
616-
name: item.ident.name,
617-
ext: Rc::new(macro_rules::compile(&self.session.parse_sess, item)),
618-
span: item.span,
627+
parent: Cell::new(*legacy_scope), name: ident.name, def_id: def_id, span: item.span,
619628
}));
620-
self.macro_names.insert(item.ident.name);
629+
self.macro_names.insert(ident.name);
621630

622631
if attr::contains_name(&item.attrs, "macro_export") {
623-
self.macro_exports.push(Export {
624-
name: item.ident.name,
625-
def: Def::Macro(self.definitions.local_def_id(item.id), MacroKind::Bang),
626-
});
632+
let def = Def::Macro(def_id, MacroKind::Bang);
633+
self.macro_exports.push(Export { name: ident.name, def: def });
627634
}
628635
}
629636

src/librustdoc/visit_ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
211211
LoadedMacro::ProcMacro(..) => continue,
212212
};
213213

214-
let matchers = if let ast::ItemKind::MacroDef(ref tokens, _) = def.node {
214+
let matchers = if let ast::ItemKind::MacroDef(ref tokens) = def.node {
215215
let tts: Vec<_> = TokenStream::from(tokens.clone()).into_trees().collect();
216216
tts.chunks(4).map(|arm| arm[0].span()).collect()
217217
} else {

src/libsyntax/ast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub use util::ThinVec;
2020
use syntax_pos::{mk_sp, Span, DUMMY_SP, ExpnId};
2121
use codemap::{respan, Spanned};
2222
use abi::Abi;
23-
use ext::hygiene::{Mark, SyntaxContext};
23+
use ext::hygiene::SyntaxContext;
2424
use print::pprust;
2525
use ptr::P;
2626
use symbol::{Symbol, keywords};
@@ -1860,7 +1860,7 @@ pub enum ItemKind {
18601860
Mac(Mac),
18611861

18621862
/// A macro definition.
1863-
MacroDef(ThinTokenStream, Mark /* FIXME(jseyfried) remove this */),
1863+
MacroDef(ThinTokenStream),
18641864
}
18651865

18661866
impl ItemKind {

src/libsyntax/ext/expand.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl ExpansionKind {
154154
pub struct Invocation {
155155
pub kind: InvocationKind,
156156
expansion_kind: ExpansionKind,
157-
expansion_data: ExpansionData,
157+
pub expansion_data: ExpansionData,
158158
}
159159

160160
pub enum InvocationKind {
@@ -432,7 +432,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
432432

433433
let extname = path.segments.last().unwrap().identifier.name;
434434
let ident = ident.unwrap_or(keywords::Invalid.ident());
435-
let marked_tts = mark_tts(mac.node.stream(), mark);
435+
let marked_tts =
436+
noop_fold_tts(mac.node.stream(), &mut Marker { mark: mark, expn_id: None });
436437
let opt_expanded = match *ext {
437438
NormalTT(ref expandfun, exp_span, allow_internal_unstable) => {
438439
if ident.name != keywords::Invalid.name() {
@@ -1094,8 +1095,3 @@ impl Folder for Marker {
10941095
span
10951096
}
10961097
}
1097-
1098-
// apply a given mark to the given token trees. Used prior to expansion of a macro.
1099-
pub fn mark_tts(tts: TokenStream, m: Mark) -> TokenStream {
1100-
noop_fold_tts(tts, &mut Marker{mark:m, expn_id: None})
1101-
}

0 commit comments

Comments
 (0)