Skip to content

Commit 9057c8d

Browse files
committed
Avoid using Mark and Invocation for macro defs.
1 parent a519659 commit 9057c8d

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
@@ -752,8 +752,8 @@ enum RibKind<'a> {
752752
// We passed through a module.
753753
ModuleRibKind(Module<'a>),
754754

755-
// We passed through a `macro_rules!` statement with the given expansion
756-
MacroDefinition(Mark),
755+
// We passed through a `macro_rules!` statement
756+
MacroDefinition(DefId),
757757

758758
// All bindings in this rib are type parameters that can't be used
759759
// from the default of a type parameter because they're not declared
@@ -970,14 +970,18 @@ impl<'a> NameBinding<'a> {
970970
}
971971
}
972972

973-
fn get_macro(&self, resolver: &mut Resolver<'a>) -> Rc<SyntaxExtension> {
973+
fn def_ignoring_ambiguity(&self) -> Def {
974974
match self.kind {
975-
NameBindingKind::Import { binding, .. } => binding.get_macro(resolver),
976-
NameBindingKind::Ambiguity { b1, .. } => b1.get_macro(resolver),
977-
_ => resolver.get_macro(self.def()),
975+
NameBindingKind::Import { binding, .. } => binding.def_ignoring_ambiguity(),
976+
NameBindingKind::Ambiguity { b1, .. } => b1.def_ignoring_ambiguity(),
977+
_ => self.def(),
978978
}
979979
}
980980

981+
fn get_macro(&self, resolver: &mut Resolver<'a>) -> Rc<SyntaxExtension> {
982+
resolver.get_macro(self.def_ignoring_ambiguity())
983+
}
984+
981985
// We sometimes need to treat variants as `pub` for backwards compatibility
982986
fn pseudo_vis(&self) -> ty::Visibility {
983987
if self.is_variant() { ty::Visibility::Public } else { self.vis }
@@ -1145,6 +1149,8 @@ pub struct Resolver<'a> {
11451149
builtin_macros: FxHashMap<Name, &'a NameBinding<'a>>,
11461150
lexical_macro_resolutions: Vec<(Name, &'a Cell<LegacyScope<'a>>)>,
11471151
macro_map: FxHashMap<DefId, Rc<SyntaxExtension>>,
1152+
macro_defs: FxHashMap<Mark, DefId>,
1153+
local_macro_def_scopes: FxHashMap<NodeId, Module<'a>>,
11481154
macro_exports: Vec<Export>,
11491155
pub whitelisted_legacy_custom_derives: Vec<Name>,
11501156
pub found_unresolved_macro: bool,
@@ -1273,6 +1279,9 @@ impl<'a> Resolver<'a> {
12731279

12741280
let features = session.features.borrow();
12751281

1282+
let mut macro_defs = FxHashMap();
1283+
macro_defs.insert(Mark::root(), root_def_id);
1284+
12761285
Resolver {
12771286
session: session,
12781287

@@ -1339,6 +1348,8 @@ impl<'a> Resolver<'a> {
13391348
macro_map: FxHashMap(),
13401349
macro_exports: Vec::new(),
13411350
invocations: invocations,
1351+
macro_defs: macro_defs,
1352+
local_macro_def_scopes: FxHashMap(),
13421353
name_already_seen: FxHashMap(),
13431354
whitelisted_legacy_custom_derives: Vec::new(),
13441355
proc_macro_enabled: features.proc_macro,
@@ -1476,24 +1487,25 @@ impl<'a> Resolver<'a> {
14761487
}
14771488
}
14781489

1479-
if let MacroDefinition(mac) = self.ribs[ns][i].kind {
1490+
if let MacroDefinition(def) = self.ribs[ns][i].kind {
14801491
// If an invocation of this macro created `ident`, give up on `ident`
14811492
// and switch to `ident`'s source from the macro definition.
1482-
let (source_ctxt, source_macro) = ident.ctxt.source();
1483-
if source_macro == mac {
1484-
ident.ctxt = source_ctxt;
1493+
let ctxt_data = ident.ctxt.data();
1494+
if def == self.macro_defs[&ctxt_data.outer_mark] {
1495+
ident.ctxt = ctxt_data.prev_ctxt;
14851496
}
14861497
}
14871498
}
14881499

14891500
None
14901501
}
14911502

1492-
fn resolve_crate_var(&mut self, mut crate_var_ctxt: SyntaxContext) -> Module<'a> {
1493-
while crate_var_ctxt.source().0 != SyntaxContext::empty() {
1494-
crate_var_ctxt = crate_var_ctxt.source().0;
1503+
fn resolve_crate_var(&mut self, crate_var_ctxt: SyntaxContext) -> Module<'a> {
1504+
let mut ctxt_data = crate_var_ctxt.data();
1505+
while ctxt_data.prev_ctxt != SyntaxContext::empty() {
1506+
ctxt_data = ctxt_data.prev_ctxt.data();
14951507
}
1496-
let module = self.invocations[&crate_var_ctxt.source().1].module.get();
1508+
let module = self.macro_def_scope(ctxt_data.outer_mark);
14971509
if module.is_local() { self.graph_root } else { module }
14981510
}
14991511

@@ -1545,12 +1557,12 @@ impl<'a> Resolver<'a> {
15451557
NormalRibKind => {
15461558
// Continue
15471559
}
1548-
MacroDefinition(mac) => {
1560+
MacroDefinition(def) => {
15491561
// If an invocation of this macro created `ident`, give up on `ident`
15501562
// and switch to `ident`'s source from the macro definition.
1551-
let (source_ctxt, source_macro) = ident.ctxt.source();
1552-
if source_macro == mac {
1553-
ident.ctxt = source_ctxt;
1563+
let ctxt_data = ident.ctxt.data();
1564+
if def == self.macro_defs[&ctxt_data.outer_mark] {
1565+
ident.ctxt = ctxt_data.prev_ctxt;
15541566
}
15551567
}
15561568
_ => {
@@ -1968,10 +1980,11 @@ impl<'a> Resolver<'a> {
19681980
// Descend into the block.
19691981
for stmt in &block.stmts {
19701982
if let ast::StmtKind::Item(ref item) = stmt.node {
1971-
if let ast::ItemKind::MacroDef(_, mark) = item.node {
1983+
if let ast::ItemKind::MacroDef(..) = item.node {
19721984
num_macro_definition_ribs += 1;
1973-
self.ribs[ValueNS].push(Rib::new(MacroDefinition(mark)));
1974-
self.label_ribs.push(Rib::new(MacroDefinition(mark)));
1985+
let def = self.definitions.local_def_id(item.id);
1986+
self.ribs[ValueNS].push(Rib::new(MacroDefinition(def)));
1987+
self.label_ribs.push(Rib::new(MacroDefinition(def)));
19751988
}
19761989
}
19771990

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)