@@ -65,7 +65,7 @@ use diagnostics::{extend_span_to_previous_binding, find_span_of_binding_until_ne
6565use diagnostics:: { ImportSuggestion , LabelSuggestion , Suggestion } ;
6666use imports:: { Import , ImportKind , ImportResolver , NameResolution } ;
6767use late:: { HasGenericParams , PathSource , Rib , RibKind :: * } ;
68- use macros:: { MacroRulesBinding , MacroRulesScope } ;
68+ use macros:: { MacroRulesBinding , MacroRulesScope , MacroRulesScopeRef } ;
6969
7070type Res = def:: Res < NodeId > ;
7171
@@ -101,7 +101,7 @@ impl Determinacy {
101101enum Scope < ' a > {
102102 DeriveHelpers ( ExpnId ) ,
103103 DeriveHelpersCompat ,
104- MacroRules ( MacroRulesScope < ' a > ) ,
104+ MacroRules ( MacroRulesScopeRef < ' a > ) ,
105105 CrateRoot ,
106106 Module ( Module < ' a > ) ,
107107 RegisteredAttrs ,
@@ -134,18 +134,18 @@ enum ScopeSet {
134134pub struct ParentScope < ' a > {
135135 module : Module < ' a > ,
136136 expansion : ExpnId ,
137- macro_rules : MacroRulesScope < ' a > ,
137+ macro_rules : MacroRulesScopeRef < ' a > ,
138138 derives : & ' a [ ast:: Path ] ,
139139}
140140
141141impl < ' a > ParentScope < ' a > {
142142 /// Creates a parent scope with the passed argument used as the module scope component,
143143 /// and other scope components set to default empty values.
144- pub fn module ( module : Module < ' a > ) -> ParentScope < ' a > {
144+ pub fn module ( module : Module < ' a > , resolver : & Resolver < ' a > ) -> ParentScope < ' a > {
145145 ParentScope {
146146 module,
147147 expansion : ExpnId :: root ( ) ,
148- macro_rules : MacroRulesScope :: Empty ,
148+ macro_rules : resolver . arenas . alloc_macro_rules_scope ( MacroRulesScope :: Empty ) ,
149149 derives : & [ ] ,
150150 }
151151 }
@@ -975,7 +975,10 @@ pub struct Resolver<'a> {
975975 invocation_parent_scopes : FxHashMap < ExpnId , ParentScope < ' a > > ,
976976 /// `macro_rules` scopes *produced* by expanding the macro invocations,
977977 /// include all the `macro_rules` items and other invocations generated by them.
978- output_macro_rules_scopes : FxHashMap < ExpnId , MacroRulesScope < ' a > > ,
978+ output_macro_rules_scopes : FxHashMap < ExpnId , MacroRulesScopeRef < ' a > > ,
979+ /// References to all `MacroRulesScope::Invocation(invoc_id)`s, used to update such scopes
980+ /// when their corresponding `invoc_id`s get expanded.
981+ invocation_macro_rules_scopes : FxHashMap < ExpnId , FxHashSet < MacroRulesScopeRef < ' a > > > ,
979982 /// Helper attributes that are in scope for the given expansion.
980983 helper_attrs : FxHashMap < ExpnId , Vec < Ident > > ,
981984
@@ -1044,6 +1047,9 @@ impl<'a> ResolverArenas<'a> {
10441047 fn alloc_name_resolution ( & ' a self ) -> & ' a RefCell < NameResolution < ' a > > {
10451048 self . name_resolutions . alloc ( Default :: default ( ) )
10461049 }
1050+ fn alloc_macro_rules_scope ( & ' a self , scope : MacroRulesScope < ' a > ) -> MacroRulesScopeRef < ' a > {
1051+ PtrKey ( self . dropless . alloc ( Cell :: new ( scope) ) )
1052+ }
10471053 fn alloc_macro_rules_binding (
10481054 & ' a self ,
10491055 binding : MacroRulesBinding < ' a > ,
@@ -1231,14 +1237,11 @@ impl<'a> Resolver<'a> {
12311237 let ( registered_attrs, registered_tools) =
12321238 macros:: registered_attrs_and_tools ( session, & krate. attrs ) ;
12331239
1234- let mut invocation_parent_scopes = FxHashMap :: default ( ) ;
1235- invocation_parent_scopes. insert ( ExpnId :: root ( ) , ParentScope :: module ( graph_root) ) ;
1236-
12371240 let features = session. features_untracked ( ) ;
12381241 let non_macro_attr =
12391242 |mark_used| Lrc :: new ( SyntaxExtension :: non_macro_attr ( mark_used, session. edition ( ) ) ) ;
12401243
1241- Resolver {
1244+ let mut resolver = Resolver {
12421245 session,
12431246
12441247 definitions,
@@ -1305,8 +1308,9 @@ impl<'a> Resolver<'a> {
13051308 dummy_ext_bang : Lrc :: new ( SyntaxExtension :: dummy_bang ( session. edition ( ) ) ) ,
13061309 dummy_ext_derive : Lrc :: new ( SyntaxExtension :: dummy_derive ( session. edition ( ) ) ) ,
13071310 non_macro_attrs : [ non_macro_attr ( false ) , non_macro_attr ( true ) ] ,
1308- invocation_parent_scopes,
1311+ invocation_parent_scopes : Default :: default ( ) ,
13091312 output_macro_rules_scopes : Default :: default ( ) ,
1313+ invocation_macro_rules_scopes : Default :: default ( ) ,
13101314 helper_attrs : Default :: default ( ) ,
13111315 local_macro_def_scopes : FxHashMap :: default ( ) ,
13121316 name_already_seen : FxHashMap :: default ( ) ,
@@ -1333,7 +1337,12 @@ impl<'a> Resolver<'a> {
13331337 invocation_parents,
13341338 next_disambiguator : Default :: default ( ) ,
13351339 trait_impl_items : Default :: default ( ) ,
1336- }
1340+ } ;
1341+
1342+ let root_parent_scope = ParentScope :: module ( graph_root, & resolver) ;
1343+ resolver. invocation_parent_scopes . insert ( ExpnId :: root ( ) , root_parent_scope) ;
1344+
1345+ resolver
13371346 }
13381347
13391348 pub fn next_node_id ( & mut self ) -> NodeId {
@@ -1703,7 +1712,7 @@ impl<'a> Resolver<'a> {
17031712 }
17041713 Scope :: DeriveHelpers ( ..) => Scope :: DeriveHelpersCompat ,
17051714 Scope :: DeriveHelpersCompat => Scope :: MacroRules ( parent_scope. macro_rules ) ,
1706- Scope :: MacroRules ( macro_rules_scope) => match macro_rules_scope {
1715+ Scope :: MacroRules ( macro_rules_scope) => match macro_rules_scope. get ( ) {
17071716 MacroRulesScope :: Binding ( binding) => {
17081717 Scope :: MacroRules ( binding. parent_macro_rules_scope )
17091718 }
@@ -3200,7 +3209,7 @@ impl<'a> Resolver<'a> {
32003209 }
32013210 } ;
32023211 let module = self . get_module ( module_id) ;
3203- let parent_scope = & ParentScope :: module ( module) ;
3212+ let parent_scope = & ParentScope :: module ( module, self ) ;
32043213 let res = self . resolve_ast_path ( & path, ns, parent_scope) . map_err ( |_| ( ) ) ?;
32053214 Ok ( ( path, res) )
32063215 }
0 commit comments