@@ -140,9 +140,23 @@ impl<'a> base::Resolver for Resolver<'a> {
140
140
ImportResolver { r : self } . resolve_imports ( )
141
141
}
142
142
143
- fn resolve_macro_invocation ( & mut self , invoc : & Invocation , invoc_id : ExpnId , force : bool )
144
- -> Result < Option < Lrc < SyntaxExtension > > , Indeterminate > {
145
- let parent_scope = self . invocation_parent_scopes [ & invoc_id] ;
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
+
146
160
let ( path, kind, derives, after_derive) = match invoc. kind {
147
161
InvocationKind :: Attr { ref attr, ref derives, after_derive, .. } =>
148
162
( & attr. path , MacroKind :: Attr , self . arenas . alloc_ast_paths ( derives) , after_derive) ,
@@ -161,7 +175,7 @@ impl<'a> base::Resolver for Resolver<'a> {
161
175
match self . resolve_macro_path ( path, Some ( MacroKind :: Derive ) ,
162
176
& parent_scope, true , force) {
163
177
Ok ( ( Some ( ref ext) , _) ) if ext. is_derive_copy => {
164
- self . add_derives ( invoc . expansion_data . id , SpecialDerives :: COPY ) ;
178
+ self . add_derives ( invoc_id , SpecialDerives :: COPY ) ;
165
179
return Ok ( None ) ;
166
180
}
167
181
Err ( Determinacy :: Undetermined ) => result = Err ( Indeterminate ) ,
@@ -178,19 +192,15 @@ impl<'a> base::Resolver for Resolver<'a> {
178
192
let ( ext, res) = self . smart_resolve_macro_path ( path, kind, parent_scope, force) ?;
179
193
180
194
let span = invoc. span ( ) ;
181
- invoc. expansion_data . id . set_expn_data (
182
- ext. expn_data ( parent_scope. expansion , span, fast_print_path ( path) )
183
- ) ;
195
+ invoc_id. set_expn_data ( ext. expn_data ( parent_scope. expansion , span, fast_print_path ( path) ) ) ;
184
196
185
197
if let Res :: Def ( _, def_id) = res {
186
198
if after_derive {
187
199
self . session . span_err ( span, "macro attributes must be placed before `#[derive]`" ) ;
188
200
}
189
- self . macro_defs . insert ( invoc. expansion_data . id , def_id) ;
190
- let normal_module_def_id =
191
- self . macro_def_scope ( invoc. expansion_data . id ) . normal_ancestor_id ;
192
- self . definitions . add_parent_module_of_macro_def ( invoc. expansion_data . id ,
193
- 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) ;
194
204
}
195
205
196
206
Ok ( Some ( ext) )
0 commit comments