From bbbdbb0e44bb4cea653584017acce4bcda158939 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 23 Nov 2019 18:19:57 +0300 Subject: [PATCH] Move def collector from `rustc` to `rustc_resolve` --- src/librustc/hir/map/definitions.rs | 11 +++++++- src/librustc/hir/map/mod.rs | 2 -- src/librustc_resolve/build_reduced_graph.rs | 5 ++-- .../map => librustc_resolve}/def_collector.rs | 27 +++++++++++-------- src/librustc_resolve/lib.rs | 1 + 5 files changed, 29 insertions(+), 17 deletions(-) rename src/{librustc/hir/map => librustc_resolve}/def_collector.rs (95%) diff --git a/src/librustc/hir/map/definitions.rs b/src/librustc/hir/map/definitions.rs index 2b3bc37c87ccb..91b4971cd92be 100644 --- a/src/librustc/hir/map/definitions.rs +++ b/src/librustc/hir/map/definitions.rs @@ -105,7 +105,7 @@ pub struct Definitions { /// we know what parent node that fragment should be attached to thanks to this table. invocation_parents: FxHashMap, /// Indices of unnamed struct or variant fields with unresolved attributes. - pub(super) placeholder_field_indices: NodeMap, + placeholder_field_indices: NodeMap, } /// A unique identifier that we can use to lookup a definition @@ -535,6 +535,15 @@ impl Definitions { let old_parent = self.invocation_parents.insert(invoc_id, parent); assert!(old_parent.is_none(), "parent `DefIndex` is reset for an invocation"); } + + pub fn placeholder_field_index(&self, node_id: ast::NodeId) -> usize { + self.placeholder_field_indices[&node_id] + } + + pub fn set_placeholder_field_index(&mut self, node_id: ast::NodeId, index: usize) { + let old_index = self.placeholder_field_indices.insert(node_id, index); + assert!(old_index.is_none(), "placeholder field index is reset for a node ID"); + } } impl DefPathData { diff --git a/src/librustc/hir/map/mod.rs b/src/librustc/hir/map/mod.rs index 83372dd8adefd..fc754c5e675e6 100644 --- a/src/librustc/hir/map/mod.rs +++ b/src/librustc/hir/map/mod.rs @@ -1,5 +1,4 @@ use self::collector::NodeCollector; -pub use self::def_collector::DefCollector; pub use self::definitions::{ Definitions, DefKey, DefPath, DefPathData, DisambiguatedDefPathData, DefPathHash }; @@ -25,7 +24,6 @@ use syntax_pos::{Span, DUMMY_SP}; pub mod blocks; mod collector; -mod def_collector; pub mod definitions; mod hir_id_validator; diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs index fd401fde20454..a178c603a462b 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -5,6 +5,7 @@ //! unexpanded macros in the fragment are visited and registered. //! Imports are also considered items and placed into modules here, but not resolved yet. +use crate::def_collector::collect_definitions; use crate::macros::{LegacyBinding, LegacyScope}; use crate::resolve_imports::ImportDirective; use crate::resolve_imports::ImportDirectiveSubclass::{self, GlobImport, SingleImport}; @@ -16,7 +17,6 @@ use crate::{ResolutionError, Determinacy, PathResult, CrateLint}; use rustc::bug; use rustc::hir::def::{self, *}; use rustc::hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE, DefId}; -use rustc::hir::map::DefCollector; use rustc::ty; use rustc::middle::cstore::CrateStore; use rustc_metadata::cstore::LoadedMacro; @@ -167,8 +167,7 @@ impl<'a> Resolver<'a> { fragment: &AstFragment, parent_scope: ParentScope<'a>, ) -> LegacyScope<'a> { - let mut def_collector = DefCollector::new(&mut self.definitions, parent_scope.expansion); - fragment.visit_with(&mut def_collector); + collect_definitions(&mut self.definitions, fragment, parent_scope.expansion); let mut visitor = BuildReducedGraphVisitor { r: self, parent_scope }; fragment.visit_with(&mut visitor); visitor.parent_scope.legacy diff --git a/src/librustc/hir/map/def_collector.rs b/src/librustc_resolve/def_collector.rs similarity index 95% rename from src/librustc/hir/map/def_collector.rs rename to src/librustc_resolve/def_collector.rs index cfd90f50b1b04..414ea6e9aa1b5 100644 --- a/src/librustc/hir/map/def_collector.rs +++ b/src/librustc_resolve/def_collector.rs @@ -1,26 +1,31 @@ -use crate::hir::map::definitions::*; -use crate::hir::def_id::DefIndex; - +use log::debug; +use rustc::hir::map::definitions::*; +use rustc::hir::def_id::DefIndex; use syntax::ast::*; use syntax::visit; use syntax::symbol::{kw, sym}; use syntax::token::{self, Token}; +use syntax_expand::expand::AstFragment; use syntax_pos::hygiene::ExpnId; use syntax_pos::Span; +crate fn collect_definitions( + definitions: &mut Definitions, + fragment: &AstFragment, + expansion: ExpnId, +) { + let parent_def = definitions.invocation_parent(expansion); + fragment.visit_with(&mut DefCollector { definitions, parent_def, expansion }); +} + /// Creates `DefId`s for nodes in the AST. -pub struct DefCollector<'a> { +struct DefCollector<'a> { definitions: &'a mut Definitions, parent_def: DefIndex, expansion: ExpnId, } impl<'a> DefCollector<'a> { - pub fn new(definitions: &'a mut Definitions, expansion: ExpnId) -> Self { - let parent_def = definitions.invocation_parent(expansion); - DefCollector { definitions, parent_def, expansion } - } - fn create_def(&mut self, node_id: NodeId, data: DefPathData, @@ -82,7 +87,7 @@ impl<'a> DefCollector<'a> { .or_else(|| index.map(sym::integer)) .unwrap_or_else(|| { let node_id = NodeId::placeholder_from_expn_id(self.expansion); - sym::integer(self.definitions.placeholder_field_indices[&node_id]) + sym::integer(self.definitions.placeholder_field_index(node_id)) }); let def = self.create_def(field.id, DefPathData::ValueNs(name), field.span); self.with_parent(def, |this| visit::walk_struct_field(this, field)); @@ -186,7 +191,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { for (index, field) in data.fields().iter().enumerate() { self.collect_field(field, Some(index)); if field.is_placeholder && field.ident.is_none() { - self.definitions.placeholder_field_indices.insert(field.id, index); + self.definitions.set_placeholder_field_index(field.id, index); } } } diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index c49db39643bc7..347b72885657a 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -68,6 +68,7 @@ use rustc_error_codes::*; type Res = def::Res; +mod def_collector; mod diagnostics; mod late; mod macros;