Skip to content

Commit 3d95d17

Browse files
petrochenkovpietroalbini
authored andcommitted
expand: Keep the correct current expansion ID for eager expansions
Solve the problem of `ParentScope` entries for eager expansions not exising in the resolver map by creating them on demand.
1 parent 1e77ef8 commit 3d95d17

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

src/librustc_resolve/macros.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ impl<'a> base::Resolver for Resolver<'a> {
182182

183183
fn resolve_macro_invocation(&mut self, invoc: &Invocation, invoc_id: ExpnId, force: bool)
184184
-> Result<Option<Lrc<SyntaxExtension>>, Indeterminate> {
185+
if !self.invocations.contains_key(&invoc.expansion_data.id) {
186+
self.invocations.insert(invoc.expansion_data.id, self.invocations[&invoc_id]);
187+
}
188+
let invoc_id = invoc.expansion_data.id;
185189
let (path, kind, derives_in_scope, after_derive) = match invoc.kind {
186190
InvocationKind::Attr { ref attr, ref derives, after_derive, .. } =>
187191
(&attr.path, MacroKind::Attr, derives.clone(), after_derive),
@@ -201,7 +205,7 @@ impl<'a> base::Resolver for Resolver<'a> {
201205
match self.resolve_macro_path(path, Some(MacroKind::Derive),
202206
parent_scope, true, force) {
203207
Ok((Some(ref ext), _)) if ext.is_derive_copy => {
204-
self.add_derives(invoc.expansion_data.id, SpecialDerives::COPY);
208+
self.add_derives(invoc_id, SpecialDerives::COPY);
205209
return Ok(None);
206210
}
207211
Err(Determinacy::Undetermined) => result = Err(Indeterminate),
@@ -217,17 +221,15 @@ impl<'a> base::Resolver for Resolver<'a> {
217221
let (ext, res) = self.smart_resolve_macro_path(path, kind, parent_scope, force)?;
218222

219223
let span = invoc.span();
220-
invoc.expansion_data.id.set_expn_info(ext.expn_info(span, fast_print_path(path)));
224+
invoc_id.set_expn_info(ext.expn_info(span, fast_print_path(path)));
221225

222226
if let Res::Def(_, def_id) = res {
223227
if after_derive {
224228
self.session.span_err(span, "macro attributes must be placed before `#[derive]`");
225229
}
226-
self.macro_defs.insert(invoc.expansion_data.id, def_id);
227-
let normal_module_def_id =
228-
self.macro_def_scope(invoc.expansion_data.id).normal_ancestor_id;
229-
self.definitions.add_parent_module_of_macro_def(invoc.expansion_data.id,
230-
normal_module_def_id);
230+
self.macro_defs.insert(invoc_id, def_id);
231+
let normal_module_def_id = self.macro_def_scope(invoc_id).normal_ancestor_id;
232+
self.definitions.add_parent_module_of_macro_def(invoc_id, normal_module_def_id);
231233
}
232234

233235
Ok(Some(ext))

src/libsyntax/ext/expand.rs

-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
331331
progress = true;
332332
let ExpansionData { depth, id: expn_id, .. } = invoc.expansion_data;
333333
self.cx.current_expansion = invoc.expansion_data.clone();
334-
self.cx.current_expansion.id = scope;
335334

336335
// FIXME(jseyfried): Refactor out the following logic
337336
let (expanded_fragment, new_invocations) = if let Some(ext) = ext {

src/test/ui/hygiene/eager-from-opaque.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ error: cannot find macro `foo!` in this scope
33
|
44
LL | foo!()
55
| ^^^
6+
...
7+
LL | format_args!(bar!());
8+
| ------ in this macro invocation
69

710
error: aborting due to previous error
811

src/test/ui/macros/derive-in-eager-expansion-hang.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ LL | |
88
LL | | ""
99
LL | | }
1010
| |_____^
11+
...
12+
LL | format_args!(hang!());
13+
| ------- in this macro invocation
1114
help: you might be missing a string literal to format with
1215
|
1316
LL | format_args!("{}", hang!());

0 commit comments

Comments
 (0)