From 552ba868afc8f72202ac834d07bbeb330aca007d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 11:14:10 +0300 Subject: Move attrs query to hir_def --- crates/ra_hir_def/src/nameres.rs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir_def/src/nameres.rs') diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index c01e020ef..6723465a5 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs @@ -58,7 +58,10 @@ mod tests; use std::sync::Arc; -use hir_expand::{ast_id_map::FileAstId, diagnostics::DiagnosticSink, name::Name, MacroDefId}; +use hir_expand::{ + ast_id_map::FileAstId, diagnostics::DiagnosticSink, either::Either, name::Name, MacroDefId, + Source, +}; use once_cell::sync::Lazy; use ra_arena::Arena; use ra_db::{CrateId, Edition, FileId}; @@ -116,12 +119,15 @@ pub struct ModuleData { pub parent: Option, pub children: FxHashMap, pub scope: ModuleScope, + + // FIXME: these can't be both null, we need a three-state enum here. /// None for root pub declaration: Option>, /// None for inline modules. /// /// Note that non-inline modules, by definition, live inside non-macro file. pub definition: Option, + pub impls: Vec, } @@ -285,6 +291,29 @@ impl CrateDefMap { } } +impl ModuleData { + /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. + pub fn definition_source( + &self, + db: &impl DefDatabase2, + ) -> Source> { + if let Some(file_id) = self.definition { + let sf = db.parse(file_id).tree(); + return Source::new(file_id.into(), Either::A(sf)); + } + let decl = self.declaration.unwrap(); + Source::new(decl.file_id(), Either::B(decl.to_node(db))) + } + + /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. + /// `None` for the crate root. + pub fn declaration_source(&self, db: &impl DefDatabase2) -> Option> { + let decl = self.declaration?; + let value = decl.to_node(db); + Some(Source { file_id: decl.file_id(), value }) + } +} + mod diagnostics { use hir_expand::diagnostics::DiagnosticSink; use ra_db::RelativePathBuf; -- cgit v1.2.3 From fc1e543f7abb69b8cab308410fa0a127950ee1c5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 14:44:43 +0300 Subject: Get rid of DefDatabase2 --- crates/ra_hir_def/src/nameres.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'crates/ra_hir_def/src/nameres.rs') diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index 6723465a5..101203b7b 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs @@ -71,7 +71,7 @@ use rustc_hash::{FxHashMap, FxHashSet}; use crate::{ builtin_type::BuiltinType, - db::DefDatabase2, + db::DefDatabase, nameres::{ diagnostics::DefDiagnostic, path_resolution::ResolveMode, per_ns::PerNs, raw::ImportId, }, @@ -220,7 +220,7 @@ impl CrateDefMap { pub(crate) fn crate_def_map_query( // Note that this doesn't have `+ AstDatabase`! // This gurantess that `CrateDefMap` is stable across reparses. - db: &impl DefDatabase2, + db: &impl DefDatabase, krate: CrateId, ) -> Arc { let _p = profile("crate_def_map_query"); @@ -262,7 +262,7 @@ impl CrateDefMap { pub fn add_diagnostics( &self, - db: &impl DefDatabase2, + db: &impl DefDatabase, module: CrateModuleId, sink: &mut DiagnosticSink, ) { @@ -271,7 +271,7 @@ impl CrateDefMap { pub fn resolve_path( &self, - db: &impl DefDatabase2, + db: &impl DefDatabase, original_module: CrateModuleId, path: &Path, ) -> (PerNs, Option) { @@ -295,7 +295,7 @@ impl ModuleData { /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. pub fn definition_source( &self, - db: &impl DefDatabase2, + db: &impl DefDatabase, ) -> Source> { if let Some(file_id) = self.definition { let sf = db.parse(file_id).tree(); @@ -307,7 +307,7 @@ impl ModuleData { /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. /// `None` for the crate root. - pub fn declaration_source(&self, db: &impl DefDatabase2) -> Option> { + pub fn declaration_source(&self, db: &impl DefDatabase) -> Option> { let decl = self.declaration?; let value = decl.to_node(db); Some(Source { file_id: decl.file_id(), value }) @@ -319,7 +319,7 @@ mod diagnostics { use ra_db::RelativePathBuf; use ra_syntax::{ast, AstPtr}; - use crate::{db::DefDatabase2, diagnostics::UnresolvedModule, nameres::CrateModuleId, AstId}; + use crate::{db::DefDatabase, diagnostics::UnresolvedModule, nameres::CrateModuleId, AstId}; #[derive(Debug, PartialEq, Eq)] pub(super) enum DefDiagnostic { @@ -333,7 +333,7 @@ mod diagnostics { impl DefDiagnostic { pub(super) fn add_to( &self, - db: &impl DefDatabase2, + db: &impl DefDatabase, target_module: CrateModuleId, sink: &mut DiagnosticSink, ) { -- cgit v1.2.3 From dd5c2dc5bf9e9dee863bd79105b1782b654221f7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 16:49:05 +0300 Subject: Move ImportId --- crates/ra_hir_def/src/nameres.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'crates/ra_hir_def/src/nameres.rs') diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index 101203b7b..0b3b60a37 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs @@ -72,11 +72,9 @@ use rustc_hash::{FxHashMap, FxHashSet}; use crate::{ builtin_type::BuiltinType, db::DefDatabase, - nameres::{ - diagnostics::DefDiagnostic, path_resolution::ResolveMode, per_ns::PerNs, raw::ImportId, - }, + nameres::{diagnostics::DefDiagnostic, path_resolution::ResolveMode, per_ns::PerNs}, path::Path, - AstId, CrateModuleId, FunctionId, ImplId, ModuleDefId, ModuleId, TraitId, + AstId, CrateModuleId, FunctionId, ImplId, LocalImportId, ModuleDefId, ModuleId, TraitId, }; /// Contains all top-level defs from a macro-expanded crate @@ -213,7 +211,7 @@ pub struct Resolution { /// None for unresolved pub def: PerNs, /// ident by which this is imported into local scope. - pub import: Option, + pub import: Option, } impl CrateDefMap { -- cgit v1.2.3 From 158b1cb524d8e07aa7a6ec2342bca2ce4667d316 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 16:49:53 +0300 Subject: Rename CrateModuleId --- crates/ra_hir_def/src/nameres.rs | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'crates/ra_hir_def/src/nameres.rs') diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index 0b3b60a37..d82356bbd 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs @@ -74,7 +74,7 @@ use crate::{ db::DefDatabase, nameres::{diagnostics::DefDiagnostic, path_resolution::ResolveMode, per_ns::PerNs}, path::Path, - AstId, CrateModuleId, FunctionId, ImplId, LocalImportId, ModuleDefId, ModuleId, TraitId, + AstId, FunctionId, ImplId, LocalImportId, LocalModuleId, ModuleDefId, ModuleId, TraitId, }; /// Contains all top-level defs from a macro-expanded crate @@ -87,8 +87,8 @@ pub struct CrateDefMap { /// a dependency (`std` or `core`). prelude: Option, extern_prelude: FxHashMap, - root: CrateModuleId, - modules: Arena, + root: LocalModuleId, + modules: Arena, /// Some macros are not well-behavior, which leads to infinite loop /// e.g. macro_rules! foo { ($ty:ty) => { foo!($ty); } } @@ -105,17 +105,17 @@ pub struct CrateDefMap { diagnostics: Vec, } -impl std::ops::Index for CrateDefMap { +impl std::ops::Index for CrateDefMap { type Output = ModuleData; - fn index(&self, id: CrateModuleId) -> &ModuleData { + fn index(&self, id: LocalModuleId) -> &ModuleData { &self.modules[id] } } #[derive(Default, Debug, PartialEq, Eq)] pub struct ModuleData { - pub parent: Option, - pub children: FxHashMap, + pub parent: Option, + pub children: FxHashMap, pub scope: ModuleScope, // FIXME: these can't be both null, we need a three-state enum here. @@ -225,7 +225,7 @@ impl CrateDefMap { let def_map = { let crate_graph = db.crate_graph(); let edition = crate_graph.edition(krate); - let mut modules: Arena = Arena::default(); + let mut modules: Arena = Arena::default(); let root = modules.alloc(ModuleData::default()); CrateDefMap { krate, @@ -246,7 +246,7 @@ impl CrateDefMap { self.krate } - pub fn root(&self) -> CrateModuleId { + pub fn root(&self) -> LocalModuleId { self.root } @@ -261,7 +261,7 @@ impl CrateDefMap { pub fn add_diagnostics( &self, db: &impl DefDatabase, - module: CrateModuleId, + module: LocalModuleId, sink: &mut DiagnosticSink, ) { self.diagnostics.iter().for_each(|it| it.add_to(db, module, sink)) @@ -270,18 +270,18 @@ impl CrateDefMap { pub fn resolve_path( &self, db: &impl DefDatabase, - original_module: CrateModuleId, + original_module: LocalModuleId, path: &Path, ) -> (PerNs, Option) { let res = self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path); (res.resolved_def, res.segment_index) } - pub fn modules(&self) -> impl Iterator + '_ { + pub fn modules(&self) -> impl Iterator + '_ { self.modules.iter().map(|(id, _data)| id) } - pub fn modules_for_file(&self, file_id: FileId) -> impl Iterator + '_ { + pub fn modules_for_file(&self, file_id: FileId) -> impl Iterator + '_ { self.modules .iter() .filter(move |(_id, data)| data.definition == Some(file_id)) @@ -317,12 +317,12 @@ mod diagnostics { use ra_db::RelativePathBuf; use ra_syntax::{ast, AstPtr}; - use crate::{db::DefDatabase, diagnostics::UnresolvedModule, nameres::CrateModuleId, AstId}; + use crate::{db::DefDatabase, diagnostics::UnresolvedModule, nameres::LocalModuleId, AstId}; #[derive(Debug, PartialEq, Eq)] pub(super) enum DefDiagnostic { UnresolvedModule { - module: CrateModuleId, + module: LocalModuleId, declaration: AstId, candidate: RelativePathBuf, }, @@ -332,7 +332,7 @@ mod diagnostics { pub(super) fn add_to( &self, db: &impl DefDatabase, - target_module: CrateModuleId, + target_module: LocalModuleId, sink: &mut DiagnosticSink, ) { match self { -- cgit v1.2.3 From 6bdd5fa461ba0f3f3697339ffb560c577e3b0cc6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 16:53:16 +0300 Subject: Privatise nameres --- crates/ra_hir_def/src/nameres.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir_def/src/nameres.rs') diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index d82356bbd..3b2e99647 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs @@ -47,8 +47,7 @@ //! path and, upon success, we run macro expansion and "collect module" phase on //! the result -pub mod raw; -pub mod per_ns; +pub(crate) mod raw; mod collector; mod mod_resolution; mod path_resolution; @@ -72,8 +71,9 @@ use rustc_hash::{FxHashMap, FxHashSet}; use crate::{ builtin_type::BuiltinType, db::DefDatabase, - nameres::{diagnostics::DefDiagnostic, path_resolution::ResolveMode, per_ns::PerNs}, + nameres::{diagnostics::DefDiagnostic, path_resolution::ResolveMode}, path::Path, + per_ns::PerNs, AstId, FunctionId, ImplId, LocalImportId, LocalModuleId, ModuleDefId, ModuleId, TraitId, }; -- cgit v1.2.3 From f0e0a40a6106e979843f97001bb3db59ceb29557 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 13:34:27 +0300 Subject: Reduce visbility --- crates/ra_hir_def/src/nameres.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'crates/ra_hir_def/src/nameres.rs') diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index 3b2e99647..68a0451a2 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs @@ -267,16 +267,6 @@ impl CrateDefMap { self.diagnostics.iter().for_each(|it| it.add_to(db, module, sink)) } - pub fn resolve_path( - &self, - db: &impl DefDatabase, - original_module: LocalModuleId, - path: &Path, - ) -> (PerNs, Option) { - let res = self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path); - (res.resolved_def, res.segment_index) - } - pub fn modules(&self) -> impl Iterator + '_ { self.modules.iter().map(|(id, _data)| id) } @@ -287,6 +277,16 @@ impl CrateDefMap { .filter(move |(_id, data)| data.definition == Some(file_id)) .map(|(id, _data)| id) } + + pub(crate) fn resolve_path( + &self, + db: &impl DefDatabase, + original_module: LocalModuleId, + path: &Path, + ) -> (PerNs, Option) { + let res = self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path); + (res.resolved_def, res.segment_index) + } } impl ModuleData { -- cgit v1.2.3 From cfffea6dc81f802e32d0312b958f20fa1b1d8425 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 13:57:45 +0300 Subject: Push poison_macros down --- crates/ra_hir_def/src/nameres.rs | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) (limited to 'crates/ra_hir_def/src/nameres.rs') diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index 68a0451a2..f6cf59c5f 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs @@ -66,7 +66,7 @@ use ra_arena::Arena; use ra_db::{CrateId, Edition, FileId}; use ra_prof::profile; use ra_syntax::ast; -use rustc_hash::{FxHashMap, FxHashSet}; +use rustc_hash::FxHashMap; use crate::{ builtin_type::BuiltinType, @@ -90,18 +90,6 @@ pub struct CrateDefMap { root: LocalModuleId, modules: Arena, - /// Some macros are not well-behavior, which leads to infinite loop - /// e.g. macro_rules! foo { ($ty:ty) => { foo!($ty); } } - /// We mark it down and skip it in collector - /// - /// FIXME: - /// Right now it only handle a poison macro in a single crate, - /// such that if other crate try to call that macro, - /// the whole process will do again until it became poisoned in that crate. - /// We should handle this macro set globally - /// However, do we want to put it as a global variable? - poison_macros: FxHashSet, - diagnostics: Vec, } @@ -234,7 +222,6 @@ impl CrateDefMap { prelude: None, root, modules, - poison_macros: FxHashSet::default(), diagnostics: Vec::new(), } }; -- cgit v1.2.3 From 99af523b68d3056c0ee355821b9b8f3c6fb5f504 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 17:00:10 +0300 Subject: Cleanup --- crates/ra_hir_def/src/nameres.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir_def/src/nameres.rs') diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index f6cf59c5f..5919771b0 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs @@ -169,7 +169,7 @@ impl ModuleScope { pub fn macros<'a>(&'a self) -> impl Iterator + 'a { self.items .iter() - .filter_map(|(name, res)| res.def.get_macros().map(|macro_| (name, macro_))) + .filter_map(|(name, res)| res.def.take_macros().map(|macro_| (name, macro_))) } /// Iterate over all legacy textual scoped macros visable at the end of the module -- cgit v1.2.3 From f5e0a31eaf9ddd7788e6261d49f4d18e8463a719 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 18:05:12 +0300 Subject: Cleanup nameres --- crates/ra_hir_def/src/nameres.rs | 32 ++++++-------------------------- 1 file changed, 6 insertions(+), 26 deletions(-) (limited to 'crates/ra_hir_def/src/nameres.rs') diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index 5919771b0..9476fb1ad 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs @@ -80,16 +80,16 @@ use crate::{ /// Contains all top-level defs from a macro-expanded crate #[derive(Debug, PartialEq, Eq)] pub struct CrateDefMap { - krate: CrateId, - edition: Edition, + pub root: LocalModuleId, + pub(crate) krate: CrateId, /// The prelude module for this crate. This either comes from an import /// marked with the `prelude_import` attribute, or (in the normal case) from /// a dependency (`std` or `core`). - prelude: Option, - extern_prelude: FxHashMap, - root: LocalModuleId, - modules: Arena, + pub(crate) prelude: Option, + pub(crate) extern_prelude: FxHashMap, + pub(crate) modules: Arena, + edition: Edition, diagnostics: Vec, } @@ -229,22 +229,6 @@ impl CrateDefMap { Arc::new(def_map) } - pub fn krate(&self) -> CrateId { - self.krate - } - - pub fn root(&self) -> LocalModuleId { - self.root - } - - pub fn prelude(&self) -> Option { - self.prelude - } - - pub fn extern_prelude(&self) -> &FxHashMap { - &self.extern_prelude - } - pub fn add_diagnostics( &self, db: &impl DefDatabase, @@ -254,10 +238,6 @@ impl CrateDefMap { self.diagnostics.iter().for_each(|it| it.add_to(db, module, sink)) } - pub fn modules(&self) -> impl Iterator + '_ { - self.modules.iter().map(|(id, _data)| id) - } - pub fn modules_for_file(&self, file_id: FileId) -> impl Iterator + '_ { self.modules .iter() -- cgit v1.2.3 From 434f108adad75b7c5e25db745a9f9fefa5cdaa31 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 18:48:29 +0300 Subject: Simplify --- crates/ra_hir_def/src/nameres.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir_def/src/nameres.rs') diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index 9476fb1ad..2359386c2 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs @@ -81,13 +81,13 @@ use crate::{ #[derive(Debug, PartialEq, Eq)] pub struct CrateDefMap { pub root: LocalModuleId, + pub modules: Arena, pub(crate) krate: CrateId, /// The prelude module for this crate. This either comes from an import /// marked with the `prelude_import` attribute, or (in the normal case) from /// a dependency (`std` or `core`). pub(crate) prelude: Option, pub(crate) extern_prelude: FxHashMap, - pub(crate) modules: Arena, edition: Edition, diagnostics: Vec, -- cgit v1.2.3