Skip to content

Commit 1ee64e4

Browse files
committed
Use Resolver::visit_expansion only with monotonic expansions.
1 parent 272cf4e commit 1ee64e4

File tree

4 files changed

+24
-19
lines changed

4 files changed

+24
-19
lines changed

src/librustc_resolve/lib.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,7 @@ pub struct Resolver<'a> {
10701070
macro_names: FnvHashSet<Name>,
10711071

10721072
// Maps the `Mark` of an expansion to its containing module or block.
1073-
expansion_data: Vec<macros::ExpansionData>,
1073+
expansion_data: FnvHashMap<u32, macros::ExpansionData>,
10741074
}
10751075

10761076
pub struct ResolverArenas<'a> {
@@ -1184,6 +1184,9 @@ impl<'a> Resolver<'a> {
11841184
let mut module_map = NodeMap();
11851185
module_map.insert(CRATE_NODE_ID, graph_root);
11861186

1187+
let mut expansion_data = FnvHashMap();
1188+
expansion_data.insert(0, macros::ExpansionData::default()); // Crate root expansion
1189+
11871190
Resolver {
11881191
session: session,
11891192

@@ -1239,7 +1242,7 @@ impl<'a> Resolver<'a> {
12391242

12401243
macro_loader: macro_loader,
12411244
macro_names: FnvHashSet(),
1242-
expansion_data: vec![macros::ExpansionData::default()],
1245+
expansion_data: expansion_data,
12431246
}
12441247
}
12451248

src/librustc_resolve/macros.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl<'a> base::Resolver for Resolver<'a> {
4747

4848
fn visit_expansion(&mut self, mark: Mark, expansion: &Expansion) {
4949
expansion.visit_with(&mut ExpansionVisitor {
50-
current_module: self.expansion_data[mark.as_u32() as usize].module.clone(),
50+
current_module: self.expansion_data[&mark.as_u32()].module.clone(),
5151
resolver: self,
5252
});
5353
}
@@ -57,7 +57,7 @@ impl<'a> base::Resolver for Resolver<'a> {
5757
self.macro_names.insert(ident.name);
5858
}
5959

60-
let mut module = self.expansion_data[scope.as_u32() as usize].module.clone();
60+
let mut module = self.expansion_data[&scope.as_u32()].module.clone();
6161
while module.macros_escape {
6262
module = module.parent.clone().unwrap();
6363
}
@@ -71,7 +71,7 @@ impl<'a> base::Resolver for Resolver<'a> {
7171
fn find_attr_invoc(&mut self, attrs: &mut Vec<ast::Attribute>) -> Option<ast::Attribute> {
7272
for i in 0..attrs.len() {
7373
let name = intern(&attrs[i].name());
74-
match self.expansion_data[0].module.macros.borrow().get(&name) {
74+
match self.expansion_data[&0].module.macros.borrow().get(&name) {
7575
Some(ext) => match **ext {
7676
MultiModifier(..) | MultiDecorator(..) => return Some(attrs.remove(i)),
7777
_ => {}
@@ -82,7 +82,7 @@ impl<'a> base::Resolver for Resolver<'a> {
8282
None
8383
}
8484

85-
fn resolve_invoc(&mut self, invoc: &Invocation) -> Option<Rc<SyntaxExtension>> {
85+
fn resolve_invoc(&mut self, scope: Mark, invoc: &Invocation) -> Option<Rc<SyntaxExtension>> {
8686
let (name, span) = match invoc.kind {
8787
InvocationKind::Bang { ref mac, .. } => {
8888
let path = &mac.node.path;
@@ -97,7 +97,7 @@ impl<'a> base::Resolver for Resolver<'a> {
9797
InvocationKind::Attr { ref attr, .. } => (intern(&*attr.name()), attr.span),
9898
};
9999

100-
let mut module = self.expansion_data[invoc.mark().as_u32() as usize].module.clone();
100+
let mut module = self.expansion_data[&scope.as_u32()].module.clone();
101101
loop {
102102
if let Some(ext) = module.macros.borrow().get(&name) {
103103
return Some(ext.clone());
@@ -135,8 +135,7 @@ struct ExpansionVisitor<'b, 'a: 'b> {
135135

136136
impl<'a, 'b> ExpansionVisitor<'a, 'b> {
137137
fn visit_invoc(&mut self, id: ast::NodeId) {
138-
assert_eq!(id.as_u32(), self.resolver.expansion_data.len() as u32);
139-
self.resolver.expansion_data.push(ExpansionData {
138+
self.resolver.expansion_data.insert(id.as_u32(), ExpansionData {
140139
module: self.current_module.clone(),
141140
});
142141
}

src/libsyntax/ext/base.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ pub trait Resolver {
470470
fn add_expansions_at_stmt(&mut self, id: ast::NodeId, macros: Vec<Mark>);
471471

472472
fn find_attr_invoc(&mut self, attrs: &mut Vec<Attribute>) -> Option<Attribute>;
473-
fn resolve_invoc(&mut self, invoc: &Invocation) -> Option<Rc<SyntaxExtension>>;
473+
fn resolve_invoc(&mut self, scope: Mark, invoc: &Invocation) -> Option<Rc<SyntaxExtension>>;
474474
}
475475

476476
pub enum LoadedMacro {
@@ -491,7 +491,9 @@ impl Resolver for DummyResolver {
491491
fn add_expansions_at_stmt(&mut self, _id: ast::NodeId, _macros: Vec<Mark>) {}
492492

493493
fn find_attr_invoc(&mut self, _attrs: &mut Vec<Attribute>) -> Option<Attribute> { None }
494-
fn resolve_invoc(&mut self, _invoc: &Invocation) -> Option<Rc<SyntaxExtension>> { None }
494+
fn resolve_invoc(&mut self, _scope: Mark, _invoc: &Invocation) -> Option<Rc<SyntaxExtension>> {
495+
None
496+
}
495497
}
496498

497499
#[derive(Clone)]

src/libsyntax/ext/expand.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,6 @@ impl Invocation {
165165
InvocationKind::Attr { ref attr, .. } => attr.span,
166166
}
167167
}
168-
169-
pub fn mark(&self) -> Mark {
170-
self.expansion_data.mark
171-
}
172168
}
173169

174170
pub struct MacroExpander<'a, 'b:'a> {
@@ -219,7 +215,9 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
219215
let ExpansionData { depth, mark, .. } = invoc.expansion_data;
220216
self.cx.current_expansion = invoc.expansion_data.clone();
221217

222-
let expansion = match self.cx.resolver.resolve_invoc(&invoc) {
218+
let scope = if self.monotonic { mark } else { orig_expansion_data.mark };
219+
self.cx.current_expansion.mark = scope;
220+
let expansion = match self.cx.resolver.resolve_invoc(scope, &invoc) {
223221
Some(ext) => self.expand_invoc(invoc, ext),
224222
None => invoc.expansion_kind.dummy(invoc.span()),
225223
};
@@ -267,8 +265,11 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
267265
};
268266
self.cx.cfg = crate_config;
269267

270-
let mark = self.cx.current_expansion.mark;
271-
self.cx.resolver.visit_expansion(mark, &result.0);
268+
if self.monotonic {
269+
let mark = self.cx.current_expansion.mark;
270+
self.cx.resolver.visit_expansion(mark, &result.0);
271+
}
272+
272273
result
273274
}
274275

@@ -314,7 +315,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
314315

315316
/// Expand a macro invocation. Returns the result of expansion.
316317
fn expand_bang_invoc(&mut self, invoc: Invocation, ext: Rc<SyntaxExtension>) -> Expansion {
317-
let (mark, kind) = (invoc.mark(), invoc.expansion_kind);
318+
let (mark, kind) = (invoc.expansion_data.mark, invoc.expansion_kind);
318319
let (attrs, mac, ident, span) = match invoc.kind {
319320
InvocationKind::Bang { attrs, mac, ident, span } => (attrs, mac, ident, span),
320321
_ => unreachable!(),

0 commit comments

Comments
 (0)