Skip to content

Commit 93d369b

Browse files
committed
resolve/expand: Rename some things for clarity and add comments
1 parent a83c356 commit 93d369b

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

src/librustc_resolve/macros.rs

+22-14
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,23 @@ impl<'a> base::Resolver for Resolver<'a> {
140140
ImportResolver { r: self }.resolve_imports()
141141
}
142142

143-
fn resolve_macro_invocation(&mut self, invoc: &Invocation, invoc_id: ExpnId, force: bool)
144-
-> Result<Option<Lrc<SyntaxExtension>>, Indeterminate> {
145-
let inherited_parent_scope = self.invocation_parent_scopes[&invoc_id];
146-
let parent_scope = *self.invocation_parent_scopes.entry(invoc.expansion_data.id)
147-
.or_insert(inherited_parent_scope);
143+
fn resolve_macro_invocation(
144+
&mut self, invoc: &Invocation, eager_expansion_root: ExpnId, force: bool
145+
) -> Result<Option<Lrc<SyntaxExtension>>, Indeterminate> {
146+
let invoc_id = invoc.expansion_data.id;
147+
let parent_scope = match self.invocation_parent_scopes.get(&invoc_id) {
148+
Some(parent_scope) => *parent_scope,
149+
None => {
150+
// If there's no entry in the table, then we are resolving an eagerly expanded
151+
// macro, which should inherit its parent scope from its eager expansion root -
152+
// the macro that requested this eager expansion.
153+
let parent_scope = *self.invocation_parent_scopes.get(&eager_expansion_root)
154+
.expect("non-eager expansion without a parent scope");
155+
self.invocation_parent_scopes.insert(invoc_id, parent_scope);
156+
parent_scope
157+
}
158+
};
159+
148160
let (path, kind, derives, after_derive) = match invoc.kind {
149161
InvocationKind::Attr { ref attr, ref derives, after_derive, .. } =>
150162
(&attr.path, MacroKind::Attr, self.arenas.alloc_ast_paths(derives), after_derive),
@@ -163,7 +175,7 @@ impl<'a> base::Resolver for Resolver<'a> {
163175
match self.resolve_macro_path(path, Some(MacroKind::Derive),
164176
&parent_scope, true, force) {
165177
Ok((Some(ref ext), _)) if ext.is_derive_copy => {
166-
self.add_derives(invoc.expansion_data.id, SpecialDerives::COPY);
178+
self.add_derives(invoc_id, SpecialDerives::COPY);
167179
return Ok(None);
168180
}
169181
Err(Determinacy::Undetermined) => result = Err(Indeterminate),
@@ -180,19 +192,15 @@ impl<'a> base::Resolver for Resolver<'a> {
180192
let (ext, res) = self.smart_resolve_macro_path(path, kind, parent_scope, force)?;
181193

182194
let span = invoc.span();
183-
invoc.expansion_data.id.set_expn_data(
184-
ext.expn_data(parent_scope.expansion, span, fast_print_path(path))
185-
);
195+
invoc_id.set_expn_data(ext.expn_data(parent_scope.expansion, span, fast_print_path(path)));
186196

187197
if let Res::Def(_, def_id) = res {
188198
if after_derive {
189199
self.session.span_err(span, "macro attributes must be placed before `#[derive]`");
190200
}
191-
self.macro_defs.insert(invoc.expansion_data.id, def_id);
192-
let normal_module_def_id =
193-
self.macro_def_scope(invoc.expansion_data.id).normal_ancestor_id;
194-
self.definitions.add_parent_module_of_macro_def(invoc.expansion_data.id,
195-
normal_module_def_id);
201+
self.macro_defs.insert(invoc_id, def_id);
202+
let normal_module_def_id = self.macro_def_scope(invoc_id).normal_ancestor_id;
203+
self.definitions.add_parent_module_of_macro_def(invoc_id, normal_module_def_id);
196204
}
197205

198206
Ok(Some(ext))

src/libsyntax/ext/base.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -682,8 +682,9 @@ pub trait Resolver {
682682

683683
fn resolve_imports(&mut self);
684684

685-
fn resolve_macro_invocation(&mut self, invoc: &Invocation, invoc_id: ExpnId, force: bool)
686-
-> Result<Option<Lrc<SyntaxExtension>>, Indeterminate>;
685+
fn resolve_macro_invocation(
686+
&mut self, invoc: &Invocation, eager_expansion_root: ExpnId, force: bool
687+
) -> Result<Option<Lrc<SyntaxExtension>>, Indeterminate>;
687688

688689
fn check_unused_macros(&self);
689690

src/libsyntax/ext/expand.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,11 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
305305
continue
306306
};
307307

308-
let scope =
308+
let eager_expansion_root =
309309
if self.monotonic { invoc.expansion_data.id } else { orig_expansion_data.id };
310-
let ext = match self.cx.resolver.resolve_macro_invocation(&invoc, scope, force) {
310+
let ext = match self.cx.resolver.resolve_macro_invocation(
311+
&invoc, eager_expansion_root, force
312+
) {
311313
Ok(ext) => ext,
312314
Err(Indeterminate) => {
313315
undetermined_invocations.push(invoc);

0 commit comments

Comments
 (0)