]> git.lizzy.rs Git - rust.git/commitdiff
resolve: Rename some fields related to legacy macro scopes
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Wed, 29 Aug 2018 01:48:02 +0000 (04:48 +0300)
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Sat, 8 Sep 2018 11:15:11 +0000 (14:15 +0300)
src/librustc_resolve/build_reduced_graph.rs
src/librustc_resolve/macros.rs

index 37868a83e342168727f1623c8f2b0e7c5724cca4..423817d1be21d519b27d852188626c0aac05fd80 100644 (file)
@@ -950,7 +950,7 @@ fn legacy_macro_imports(&mut self, attrs: &[ast::Attribute]) -> LegacyMacroImpor
 
 pub struct BuildReducedGraphVisitor<'a, 'b: 'a, 'c: 'b> {
     pub resolver: &'a mut Resolver<'b, 'c>,
-    pub legacy_scope: LegacyScope<'b>,
+    pub current_legacy_scope: LegacyScope<'b>,
     pub expansion: Mark,
 }
 
@@ -960,7 +960,7 @@ fn visit_invoc(&mut self, id: ast::NodeId) -> &'b InvocationData<'b> {
         self.resolver.current_module.unresolved_invocations.borrow_mut().insert(mark);
         let invocation = self.resolver.invocations[&mark];
         invocation.module.set(self.resolver.current_module);
-        invocation.legacy_scope.set(self.legacy_scope);
+        invocation.parent_legacy_scope.set(self.current_legacy_scope);
         invocation
     }
 }
@@ -986,29 +986,30 @@ impl<'a, 'b, 'cl> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b, 'cl> {
     fn visit_item(&mut self, item: &'a Item) {
         let macro_use = match item.node {
             ItemKind::MacroDef(..) => {
-                self.resolver.define_macro(item, self.expansion, &mut self.legacy_scope);
+                self.resolver.define_macro(item, self.expansion, &mut self.current_legacy_scope);
                 return
             }
             ItemKind::Mac(..) => {
-                self.legacy_scope = LegacyScope::Expansion(self.visit_invoc(item.id));
+                self.current_legacy_scope = LegacyScope::Expansion(self.visit_invoc(item.id));
                 return
             }
             ItemKind::Mod(..) => self.resolver.contains_macro_use(&item.attrs),
             _ => false,
         };
 
-        let (parent, legacy_scope) = (self.resolver.current_module, self.legacy_scope);
+        let orig_current_module = self.resolver.current_module;
+        let orig_current_legacy_scope = self.current_legacy_scope;
         self.resolver.build_reduced_graph_for_item(item, self.expansion);
         visit::walk_item(self, item);
-        self.resolver.current_module = parent;
+        self.resolver.current_module = orig_current_module;
         if !macro_use {
-            self.legacy_scope = legacy_scope;
+            self.current_legacy_scope = orig_current_legacy_scope;
         }
     }
 
     fn visit_stmt(&mut self, stmt: &'a ast::Stmt) {
         if let ast::StmtKind::Mac(..) = stmt.node {
-            self.legacy_scope = LegacyScope::Expansion(self.visit_invoc(stmt.id));
+            self.current_legacy_scope = LegacyScope::Expansion(self.visit_invoc(stmt.id));
         } else {
             visit::walk_stmt(self, stmt);
         }
@@ -1025,11 +1026,12 @@ fn visit_foreign_item(&mut self, foreign_item: &'a ForeignItem) {
     }
 
     fn visit_block(&mut self, block: &'a Block) {
-        let (parent, legacy_scope) = (self.resolver.current_module, self.legacy_scope);
+        let orig_current_module = self.resolver.current_module;
+        let orig_current_legacy_scope = self.current_legacy_scope;
         self.resolver.build_reduced_graph_for_block(block, self.expansion);
         visit::walk_block(self, block);
-        self.resolver.current_module = parent;
-        self.legacy_scope = legacy_scope;
+        self.resolver.current_module = orig_current_module;
+        self.current_legacy_scope = orig_current_legacy_scope;
     }
 
     fn visit_trait_item(&mut self, item: &'a TraitItem) {
index af9959c808435ad8c167b97ca0b7e1c3d13fe553..baff58ccee3821a63b799e10448d6ef041b740d8 100644 (file)
 
 #[derive(Clone)]
 pub struct InvocationData<'a> {
-    pub module: Cell<Module<'a>>,
-    pub def_index: DefIndex,
-    // The scope in which the invocation path is resolved.
-    pub legacy_scope: Cell<LegacyScope<'a>>,
-    // The smallest scope that includes this invocation's expansion,
-    // or `Empty` if this invocation has not been expanded yet.
-    pub expansion: Cell<LegacyScope<'a>>,
+    crate module: Cell<Module<'a>>,
+    def_index: DefIndex,
+    // Legacy scope in which the macro was invoked.
+    // The invocation path is resolved in this scope.
+    crate parent_legacy_scope: Cell<LegacyScope<'a>>,
+    // Legacy scope *produced* by expanding this macro invocation,
+    // includes all the macro_rules items, other invocations, etc generated by it.
+    // `Empty` is used if for invocations that has not been expanded yet.
+    output_legacy_scope: Cell<LegacyScope<'a>>,
 }
 
 impl<'a> InvocationData<'a> {
@@ -64,12 +66,21 @@ pub fn root(graph_root: Module<'a>) -> Self {
         InvocationData {
             module: Cell::new(graph_root),
             def_index: CRATE_DEF_INDEX,
-            legacy_scope: Cell::new(LegacyScope::Empty),
-            expansion: Cell::new(LegacyScope::Empty),
+            parent_legacy_scope: Cell::new(LegacyScope::Empty),
+            output_legacy_scope: Cell::new(LegacyScope::Empty),
         }
     }
 }
 
+// Binding produced by a `macro_rules` item.
+// Not modularized, can shadow previous legacy bindings, etc.
+pub struct LegacyBinding<'a> {
+    binding: &'a NameBinding<'a>,
+    // Legacy scope into which the `macro_rules` item was planted.
+    parent_legacy_scope: Cell<LegacyScope<'a>>,
+    ident: Ident,
+}
+
 #[derive(Copy, Clone)]
 pub enum LegacyScope<'a> {
     Empty,
@@ -78,14 +89,6 @@ pub enum LegacyScope<'a> {
     Binding(&'a LegacyBinding<'a>),
 }
 
-// Binding produced by a `macro_rules` item.
-// Not modularized, can shadow previous legacy bindings, etc.
-pub struct LegacyBinding<'a> {
-    binding: &'a NameBinding<'a>,
-    parent: Cell<LegacyScope<'a>>,
-    ident: Ident,
-}
-
 pub struct ProcMacError {
     crate_name: Symbol,
     name: Symbol,
@@ -105,8 +108,8 @@ fn get_module_scope(&mut self, id: ast::NodeId) -> Mark {
         self.invocations.insert(mark, self.arenas.alloc_invocation_data(InvocationData {
             module: Cell::new(module),
             def_index: module.def_id().unwrap().index,
-            legacy_scope: Cell::new(LegacyScope::Empty),
-            expansion: Cell::new(LegacyScope::Empty),
+            parent_legacy_scope: Cell::new(LegacyScope::Empty),
+            output_legacy_scope: Cell::new(LegacyScope::Empty),
         }));
         mark
     }
@@ -178,11 +181,11 @@ fn visit_ast_fragment_with_placeholders(&mut self, mark: Mark, fragment: &AstFra
         }
         let mut visitor = BuildReducedGraphVisitor {
             resolver: self,
-            legacy_scope: LegacyScope::Invocation(invocation),
+            current_legacy_scope: LegacyScope::Invocation(invocation),
             expansion: mark,
         };
         fragment.visit_with(&mut visitor);
-        invocation.expansion.set(visitor.legacy_scope);
+        invocation.output_legacy_scope.set(visitor.current_legacy_scope);
     }
 
     fn add_builtin(&mut self, ident: ast::Ident, ext: Lrc<SyntaxExtension>) {
@@ -481,7 +484,7 @@ pub fn resolve_macro_to_def_inner(&mut self, path: &ast::Path, kind: MacroKind,
         }
 
         let legacy_resolution =
-            self.resolve_legacy_scope(path[0], invoc_id, &invocation.legacy_scope, false);
+            self.resolve_legacy_scope(path[0], invoc_id, &invocation.parent_legacy_scope, false);
         let result = if let Some(legacy_binding) = legacy_resolution {
             Ok(legacy_binding.def())
         } else {
@@ -814,18 +817,20 @@ fn resolve_legacy_scope(&mut self,
 
             macro_rules! continue_search { () => {
                 where_to_resolve = match where_to_resolve.get() {
-                    LegacyScope::Binding(binding) => &binding.parent,
-                    LegacyScope::Invocation(invocation) => &invocation.legacy_scope,
-                    LegacyScope::Expansion(invocation) => match invocation.expansion.get() {
-                        LegacyScope::Empty => &invocation.legacy_scope,
-                        LegacyScope::Binding(..) |
-                        LegacyScope::Expansion(..) => &invocation.expansion,
-                        LegacyScope::Invocation(..) => {
-                            where_to_resolve.set(invocation.legacy_scope.get());
-                            where_to_resolve
+                    LegacyScope::Empty => break, // nowhere else to search
+                    LegacyScope::Binding(binding) => &binding.parent_legacy_scope,
+                    LegacyScope::Invocation(invocation) => &invocation.parent_legacy_scope,
+                    LegacyScope::Expansion(invocation) => {
+                        match invocation.output_legacy_scope.get() {
+                            LegacyScope::Empty => &invocation.parent_legacy_scope,
+                            LegacyScope::Binding(..) |
+                            LegacyScope::Expansion(..) => &invocation.output_legacy_scope,
+                            LegacyScope::Invocation(..) => {
+                                where_to_resolve.set(invocation.parent_legacy_scope.get());
+                                where_to_resolve
+                            }
                         }
                     }
-                    LegacyScope::Empty => break, // nowhere else to search
                 };
 
                 continue;
@@ -880,8 +885,9 @@ pub fn finalize_current_module_macro_resolutions(&mut self) {
 
         for &(invoc_id, ident, kind, def) in module.legacy_macro_resolutions.borrow().iter() {
             let span = ident.span;
-            let legacy_scope = &self.invocations[&invoc_id].legacy_scope;
-            let legacy_resolution = self.resolve_legacy_scope(ident, invoc_id, legacy_scope, true);
+            let invoc_parent_legacy_scope = &self.invocations[&invoc_id].parent_legacy_scope;
+            let legacy_resolution =
+                self.resolve_legacy_scope(ident, invoc_id, invoc_parent_legacy_scope, true);
             let resolution = self.resolve_lexical_macro_path_segment(
                 ident, MacroNS, invoc_id, true, true, kind == MacroKind::Attr, span
             );
@@ -1007,8 +1013,8 @@ fn collect_def_ids(&mut self,
                 arenas.alloc_invocation_data(InvocationData {
                     def_index: invoc.def_index,
                     module: Cell::new(graph_root),
-                    expansion: Cell::new(LegacyScope::Empty),
-                    legacy_scope: Cell::new(LegacyScope::Empty),
+                    parent_legacy_scope: Cell::new(LegacyScope::Empty),
+                    output_legacy_scope: Cell::new(LegacyScope::Empty),
                 })
             });
         };
@@ -1023,7 +1029,7 @@ fn collect_def_ids(&mut self,
     pub fn define_macro(&mut self,
                         item: &ast::Item,
                         expansion: Mark,
-                        legacy_scope: &mut LegacyScope<'a>) {
+                        current_legacy_scope: &mut LegacyScope<'a>) {
         self.local_macro_def_scopes.insert(item.id, self.current_module);
         let ident = item.ident;
         if ident.name == "macro_rules" {
@@ -1043,9 +1049,10 @@ pub fn define_macro(&mut self,
             let def = Def::Macro(def_id, MacroKind::Bang);
             let vis = ty::Visibility::Invisible; // Doesn't matter for legacy bindings
             let binding = (def, vis, item.span, expansion).to_name_binding(self.arenas);
-            *legacy_scope = LegacyScope::Binding(self.arenas.alloc_legacy_binding(
-                LegacyBinding { parent: Cell::new(*legacy_scope), binding, ident }
-            ));
+            let legacy_binding = self.arenas.alloc_legacy_binding(LegacyBinding {
+                parent_legacy_scope: Cell::new(*current_legacy_scope), binding, ident
+            });
+            *current_legacy_scope = LegacyScope::Binding(legacy_binding);
             self.all_macros.insert(ident.name, def);
             if attr::contains_name(&item.attrs, "macro_export") {
                 let module = self.graph_root;