From 12a3bf3c31d4c9a6d9ee110db174604f688ca0f0 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sun, 17 May 2020 23:37:30 +0800 Subject: Create LowerCtx on the fly --- crates/ra_hir_def/src/body/lower.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'crates/ra_hir_def/src') diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index 443b057ab..c69e0efea 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -60,13 +60,10 @@ pub(super) fn lower( params: Option, body: Option, ) -> (Body, BodySourceMap) { - let ctx = LowerCtx::new(db, expander.current_file_id.clone()); - ExprCollector { db, def, expander, - ctx, source_map: BodySourceMap::default(), body: Body { exprs: Arena::default(), @@ -83,7 +80,6 @@ struct ExprCollector<'a> { db: &'a dyn DefDatabase, def: DefWithBodyId, expander: Expander, - ctx: LowerCtx, body: Body, source_map: BodySourceMap, } @@ -122,6 +118,10 @@ impl ExprCollector<'_> { (self.body, self.source_map) } + fn ctx(&self) -> LowerCtx { + LowerCtx::new(self.db, self.expander.current_file_id) + } + fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr) -> ExprId { let src = self.expander.to_source(ptr); let id = self.make_expr(expr, Ok(src.clone())); @@ -268,7 +268,7 @@ impl ExprCollector<'_> { }; let method_name = e.name_ref().map(|nr| nr.as_name()).unwrap_or_else(Name::missing); let generic_args = - e.type_arg_list().and_then(|it| GenericArgs::from_ast(&self.ctx, it)); + e.type_arg_list().and_then(|it| GenericArgs::from_ast(&self.ctx(), it)); self.alloc_expr( Expr::MethodCall { receiver, method_name, args, generic_args }, syntax_ptr, @@ -373,7 +373,7 @@ impl ExprCollector<'_> { } ast::Expr::CastExpr(e) => { let expr = self.collect_expr_opt(e.expr()); - let type_ref = TypeRef::from_ast_opt(&self.ctx, e.type_ref()); + let type_ref = TypeRef::from_ast_opt(&self.ctx(), e.type_ref()); self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr) } ast::Expr::RefExpr(e) => { @@ -396,7 +396,7 @@ impl ExprCollector<'_> { for param in pl.params() { let pat = self.collect_pat_opt(param.pat()); let type_ref = - param.ascribed_type().map(|it| TypeRef::from_ast(&self.ctx, it)); + param.ascribed_type().map(|it| TypeRef::from_ast(&self.ctx(), it)); args.push(pat); arg_types.push(type_ref); } @@ -404,7 +404,7 @@ impl ExprCollector<'_> { let ret_type = e .ret_type() .and_then(|r| r.type_ref()) - .map(|it| TypeRef::from_ast(&self.ctx, it)); + .map(|it| TypeRef::from_ast(&self.ctx(), it)); let body = self.collect_expr_opt(e.body()); self.alloc_expr(Expr::Lambda { args, arg_types, ret_type, body }, syntax_ptr) } @@ -507,7 +507,8 @@ impl ExprCollector<'_> { .map(|s| match s { ast::Stmt::LetStmt(stmt) => { let pat = self.collect_pat_opt(stmt.pat()); - let type_ref = stmt.ascribed_type().map(|it| TypeRef::from_ast(&self.ctx, it)); + let type_ref = + stmt.ascribed_type().map(|it| TypeRef::from_ast(&self.ctx(), it)); let initializer = stmt.initializer().map(|e| self.collect_expr(e)); Statement::Let { pat, type_ref, initializer } } -- cgit v1.2.3 From 8f80df111722a1f1685a8aea02a2612f642ea8f6 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 18 May 2020 21:42:39 +0200 Subject: Querify `importable_locations_in_crate` This brings the time needed to compute the `add_missing_impl_members` assist down from ~5 minutes to 20 seconds --- crates/ra_hir_def/src/db.rs | 12 +++++++++++- crates/ra_hir_def/src/find_path.rs | 11 +++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) (limited to 'crates/ra_hir_def/src') diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs index e665ab45d..498a4c917 100644 --- a/crates/ra_hir_def/src/db.rs +++ b/crates/ra_hir_def/src/db.rs @@ -1,7 +1,7 @@ //! Defines database & queries for name resolution. use std::sync::Arc; -use hir_expand::{db::AstDatabase, HirFileId}; +use hir_expand::{db::AstDatabase, name::Name, HirFileId}; use ra_db::{salsa, CrateId, SourceDatabase, Upcast}; use ra_prof::profile; use ra_syntax::SmolStr; @@ -12,9 +12,12 @@ use crate::{ body::{scope::ExprScopes, Body, BodySourceMap}, data::{ConstData, FunctionData, ImplData, StaticData, TraitData, TypeAliasData}, docs::Documentation, + find_path, generics::GenericParams, + item_scope::ItemInNs, lang_item::{LangItemTarget, LangItems}, nameres::{raw::RawItems, CrateDefMap}, + visibility::Visibility, AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, FunctionId, FunctionLoc, GenericDefId, ImplId, ImplLoc, ModuleId, StaticId, StaticLoc, StructId, StructLoc, TraitId, TraitLoc, TypeAliasId, TypeAliasLoc, UnionId, UnionLoc, @@ -108,6 +111,13 @@ pub trait DefDatabase: InternDatabase + AstDatabase + Upcast { // Remove this query completely, in favor of `Attrs::docs` method #[salsa::invoke(Documentation::documentation_query)] fn documentation(&self, def: AttrDefId) -> Option; + + #[salsa::invoke(find_path::importable_locations_in_crate)] + fn importable_locations_of( + &self, + item: ItemInNs, + krate: CrateId, + ) -> Arc<[(ModuleId, Name, Visibility)]>; } fn crate_def_map_wait(db: &impl DefDatabase, krate: CrateId) -> Arc { diff --git a/crates/ra_hir_def/src/find_path.rs b/crates/ra_hir_def/src/find_path.rs index 70dcb03e6..1ca20fabd 100644 --- a/crates/ra_hir_def/src/find_path.rs +++ b/crates/ra_hir_def/src/find_path.rs @@ -8,6 +8,7 @@ use crate::{ CrateId, ModuleDefId, ModuleId, }; use hir_expand::name::{known, AsName, Name}; +use std::sync::Arc; use test_utils::tested_by; const MAX_PATH_LEN: usize = 15; @@ -45,6 +46,7 @@ impl ModPath { /// Find a path that can be used to refer to a certain item. This can depend on /// *from where* you're referring to the item, hence the `from` parameter. pub fn find_path(db: &dyn DefDatabase, item: ItemInNs, from: ModuleId) -> Option { + let _p = ra_prof::profile("find_path"); find_path_inner(db, item, from, MAX_PATH_LEN) } @@ -198,7 +200,7 @@ fn find_importable_locations( .chain(crate_graph[from.krate].dependencies.iter().map(|dep| dep.crate_id)) { result.extend( - importable_locations_in_crate(db, item, krate) + db.importable_locations_of(item, krate) .iter() .filter(|(_, _, vis)| vis.is_visible_from(db, from)) .map(|(m, n, _)| (*m, n.clone())), @@ -213,11 +215,11 @@ fn find_importable_locations( /// /// Note that the crate doesn't need to be the one in which the item is defined; /// it might be re-exported in other crates. -fn importable_locations_in_crate( +pub(crate) fn importable_locations_in_crate( db: &dyn DefDatabase, item: ItemInNs, krate: CrateId, -) -> Vec<(ModuleId, Name, Visibility)> { +) -> Arc<[(ModuleId, Name, Visibility)]> { let def_map = db.crate_def_map(krate); let mut result = Vec::new(); for (local_id, data) in def_map.modules.iter() { @@ -243,7 +245,8 @@ fn importable_locations_in_crate( result.push((ModuleId { krate, local_id }, name.clone(), vis)); } } - result + + Arc::from(result) } #[cfg(test)] -- cgit v1.2.3 From 5c9ebbeaa415a5ff811b3be5058d8cb374e9baac Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 19 May 2020 16:43:26 +0200 Subject: Cleanup imports --- crates/ra_hir_def/src/find_path.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir_def/src') diff --git a/crates/ra_hir_def/src/find_path.rs b/crates/ra_hir_def/src/find_path.rs index 1ca20fabd..c7976535c 100644 --- a/crates/ra_hir_def/src/find_path.rs +++ b/crates/ra_hir_def/src/find_path.rs @@ -1,5 +1,10 @@ //! An algorithm to find a path to refer to a certain item. +use std::sync::Arc; + +use hir_expand::name::{known, AsName, Name}; +use test_utils::tested_by; + use crate::{ db::DefDatabase, item_scope::ItemInNs, @@ -7,9 +12,6 @@ use crate::{ visibility::Visibility, CrateId, ModuleDefId, ModuleId, }; -use hir_expand::name::{known, AsName, Name}; -use std::sync::Arc; -use test_utils::tested_by; const MAX_PATH_LEN: usize = 15; -- cgit v1.2.3 From 908da9ac1b04a0eb27552ec9ca68a5f88c4f42c7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 19 May 2020 16:45:57 +0200 Subject: Simplify --- crates/ra_hir_def/src/find_path.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'crates/ra_hir_def/src') diff --git a/crates/ra_hir_def/src/find_path.rs b/crates/ra_hir_def/src/find_path.rs index c7976535c..aee7e7511 100644 --- a/crates/ra_hir_def/src/find_path.rs +++ b/crates/ra_hir_def/src/find_path.rs @@ -17,18 +17,14 @@ const MAX_PATH_LEN: usize = 15; impl ModPath { fn starts_with_std(&self) -> bool { - self.segments.first().filter(|&first_segment| first_segment == &known::std).is_some() + self.segments.first() == Some(&known::std) } // When std library is present, paths starting with `std::` // should be preferred over paths starting with `core::` and `alloc::` fn can_start_with_std(&self) -> bool { - self.segments - .first() - .filter(|&first_segment| { - first_segment == &known::alloc || first_segment == &known::core - }) - .is_some() + let first_segment = self.segments.first(); + first_segment == Some(&known::alloc) || first_segment == Some(&known::core) } fn len(&self) -> usize { -- cgit v1.2.3 From 01bd1e1296d31dbb102d198e9fd82e1e36f1193b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 19 May 2020 16:46:33 +0200 Subject: Move public API to the top --- crates/ra_hir_def/src/find_path.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'crates/ra_hir_def/src') diff --git a/crates/ra_hir_def/src/find_path.rs b/crates/ra_hir_def/src/find_path.rs index aee7e7511..15bc04c1a 100644 --- a/crates/ra_hir_def/src/find_path.rs +++ b/crates/ra_hir_def/src/find_path.rs @@ -13,6 +13,15 @@ use crate::{ CrateId, ModuleDefId, ModuleId, }; +// FIXME: handle local items + +/// Find a path that can be used to refer to a certain item. This can depend on +/// *from where* you're referring to the item, hence the `from` parameter. +pub fn find_path(db: &dyn DefDatabase, item: ItemInNs, from: ModuleId) -> Option { + let _p = ra_prof::profile("find_path"); + find_path_inner(db, item, from, MAX_PATH_LEN) +} + const MAX_PATH_LEN: usize = 15; impl ModPath { @@ -39,15 +48,6 @@ impl ModPath { } } -// FIXME: handle local items - -/// Find a path that can be used to refer to a certain item. This can depend on -/// *from where* you're referring to the item, hence the `from` parameter. -pub fn find_path(db: &dyn DefDatabase, item: ItemInNs, from: ModuleId) -> Option { - let _p = ra_prof::profile("find_path"); - find_path_inner(db, item, from, MAX_PATH_LEN) -} - fn find_path_inner( db: &dyn DefDatabase, item: ItemInNs, -- cgit v1.2.3 From dce31efdde9ca0311ed60f04b97049d42ed49ba8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 19 May 2020 16:54:45 +0200 Subject: Cleanup query fn naming --- crates/ra_hir_def/src/db.rs | 2 +- crates/ra_hir_def/src/find_path.rs | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir_def/src') diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs index 498a4c917..2f71511ba 100644 --- a/crates/ra_hir_def/src/db.rs +++ b/crates/ra_hir_def/src/db.rs @@ -112,7 +112,7 @@ pub trait DefDatabase: InternDatabase + AstDatabase + Upcast { #[salsa::invoke(Documentation::documentation_query)] fn documentation(&self, def: AttrDefId) -> Option; - #[salsa::invoke(find_path::importable_locations_in_crate)] + #[salsa::invoke(find_path::importable_locations_of_query)] fn importable_locations_of( &self, item: ItemInNs, diff --git a/crates/ra_hir_def/src/find_path.rs b/crates/ra_hir_def/src/find_path.rs index 15bc04c1a..2eb12ec8f 100644 --- a/crates/ra_hir_def/src/find_path.rs +++ b/crates/ra_hir_def/src/find_path.rs @@ -3,6 +3,7 @@ use std::sync::Arc; use hir_expand::name::{known, AsName, Name}; +use ra_prof::profile; use test_utils::tested_by; use crate::{ @@ -18,7 +19,7 @@ use crate::{ /// Find a path that can be used to refer to a certain item. This can depend on /// *from where* you're referring to the item, hence the `from` parameter. pub fn find_path(db: &dyn DefDatabase, item: ItemInNs, from: ModuleId) -> Option { - let _p = ra_prof::profile("find_path"); + let _p = profile("find_path"); find_path_inner(db, item, from, MAX_PATH_LEN) } @@ -213,11 +214,12 @@ fn find_importable_locations( /// /// Note that the crate doesn't need to be the one in which the item is defined; /// it might be re-exported in other crates. -pub(crate) fn importable_locations_in_crate( +pub(crate) fn importable_locations_of_query( db: &dyn DefDatabase, item: ItemInNs, krate: CrateId, ) -> Arc<[(ModuleId, Name, Visibility)]> { + let _p = profile("importable_locations_of_query"); let def_map = db.crate_def_map(krate); let mut result = Vec::new(); for (local_id, data) in def_map.modules.iter() { -- cgit v1.2.3 From ecac5d7de2192873c24b7b06d4964d188d8abe6a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 20 May 2020 12:59:20 +0200 Subject: Switch to new magic marks --- crates/ra_hir_def/src/body/lower.rs | 4 +- crates/ra_hir_def/src/body/scope.rs | 4 +- crates/ra_hir_def/src/find_path.rs | 57 ++++++++++++---------- crates/ra_hir_def/src/lib.rs | 2 - crates/ra_hir_def/src/marks.rs | 17 ------- crates/ra_hir_def/src/nameres/collector.rs | 14 +++--- crates/ra_hir_def/src/nameres/path_resolution.rs | 8 +-- crates/ra_hir_def/src/nameres/raw.rs | 4 +- crates/ra_hir_def/src/nameres/tests.rs | 8 +-- crates/ra_hir_def/src/nameres/tests/globs.rs | 7 ++- crates/ra_hir_def/src/nameres/tests/macros.rs | 9 ++-- .../ra_hir_def/src/nameres/tests/mod_resolution.rs | 2 +- crates/ra_hir_def/src/path/lower/lower_use.rs | 4 +- 13 files changed, 61 insertions(+), 79 deletions(-) delete mode 100644 crates/ra_hir_def/src/marks.rs (limited to 'crates/ra_hir_def/src') diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index c69e0efea..e08d62dd6 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -15,7 +15,7 @@ use ra_syntax::{ }, AstNode, AstPtr, }; -use test_utils::tested_by; +use test_utils::mark; use crate::{ adt::StructKind, @@ -226,7 +226,7 @@ impl ExprCollector<'_> { None => self.collect_expr_opt(condition.expr()), // if let -- desugar to match Some(pat) => { - tested_by!(infer_resolve_while_let); + mark::hit!(infer_resolve_while_let); let pat = self.collect_pat(pat); let match_expr = self.collect_expr_opt(condition.expr()); let placeholder_pat = self.missing_pat(); diff --git a/crates/ra_hir_def/src/body/scope.rs b/crates/ra_hir_def/src/body/scope.rs index 86f953c80..09e92b74e 100644 --- a/crates/ra_hir_def/src/body/scope.rs +++ b/crates/ra_hir_def/src/body/scope.rs @@ -174,7 +174,7 @@ mod tests { use hir_expand::{name::AsName, InFile}; use ra_db::{fixture::WithFixture, FileId, SourceDatabase}; use ra_syntax::{algo::find_node_at_offset, ast, AstNode}; - use test_utils::{assert_eq_text, covers, extract_offset}; + use test_utils::{assert_eq_text, extract_offset, mark}; use crate::{db::DefDatabase, test_db::TestDB, FunctionId, ModuleDefId}; @@ -388,7 +388,7 @@ mod tests { #[test] fn while_let_desugaring() { - covers!(infer_resolve_while_let); + mark::check!(infer_resolve_while_let); do_check_local_name( r#" fn test() { diff --git a/crates/ra_hir_def/src/find_path.rs b/crates/ra_hir_def/src/find_path.rs index 2eb12ec8f..68d3cde08 100644 --- a/crates/ra_hir_def/src/find_path.rs +++ b/crates/ra_hir_def/src/find_path.rs @@ -4,7 +4,7 @@ use std::sync::Arc; use hir_expand::name::{known, AsName, Name}; use ra_prof::profile; -use test_utils::tested_by; +use test_utils::mark; use crate::{ db::DefDatabase, @@ -164,17 +164,19 @@ fn find_path_inner( fn select_best_path(old_path: ModPath, new_path: ModPath, prefer_no_std: bool) -> ModPath { if old_path.starts_with_std() && new_path.can_start_with_std() { - tested_by!(prefer_std_paths); if prefer_no_std { + mark::hit!(prefer_no_std_paths); new_path } else { + mark::hit!(prefer_std_paths); old_path } } else if new_path.starts_with_std() && old_path.can_start_with_std() { - tested_by!(prefer_std_paths); if prefer_no_std { + mark::hit!(prefer_no_std_paths); old_path } else { + mark::hit!(prefer_std_paths); new_path } } else if new_path.len() < old_path.len() { @@ -251,12 +253,14 @@ pub(crate) fn importable_locations_of_query( #[cfg(test)] mod tests { - use super::*; - use crate::test_db::TestDB; use hir_expand::hygiene::Hygiene; use ra_db::fixture::WithFixture; use ra_syntax::ast::AstNode; - use test_utils::covers; + use test_utils::mark; + + use crate::test_db::TestDB; + + use super::*; /// `code` needs to contain a cursor marker; checks that `find_path` for the /// item the `path` refers to returns that same path when called from the @@ -511,7 +515,7 @@ mod tests { #[test] fn prefer_std_paths_over_alloc() { - covers!(prefer_std_paths); + mark::check!(prefer_std_paths); let code = r#" //- /main.rs crate:main deps:alloc,std <|> @@ -530,51 +534,50 @@ mod tests { } #[test] - fn prefer_alloc_paths_over_std() { - covers!(prefer_std_paths); + fn prefer_core_paths_over_std() { + mark::check!(prefer_no_std_paths); let code = r#" - //- /main.rs crate:main deps:alloc,std + //- /main.rs crate:main deps:core,std #![no_std] <|> - //- /std.rs crate:std deps:alloc + //- /std.rs crate:std deps:core - pub mod sync { - pub use alloc::sync::Arc; + pub mod fmt { + pub use core::fmt::Error; } - //- /zzz.rs crate:alloc + //- /zzz.rs crate:core - pub mod sync { - pub struct Arc; + pub mod fmt { + pub struct Error; } "#; - check_found_path(code, "alloc::sync::Arc"); + check_found_path(code, "core::fmt::Error"); } #[test] - fn prefer_core_paths_over_std() { - covers!(prefer_std_paths); + fn prefer_alloc_paths_over_std() { let code = r#" - //- /main.rs crate:main deps:core,std + //- /main.rs crate:main deps:alloc,std #![no_std] <|> - //- /std.rs crate:std deps:core + //- /std.rs crate:std deps:alloc - pub mod fmt { - pub use core::fmt::Error; + pub mod sync { + pub use alloc::sync::Arc; } - //- /zzz.rs crate:core + //- /zzz.rs crate:alloc - pub mod fmt { - pub struct Error; + pub mod sync { + pub struct Arc; } "#; - check_found_path(code, "core::fmt::Error"); + check_found_path(code, "alloc::sync::Arc"); } #[test] diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 518772e8a..5325a2760 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -46,8 +46,6 @@ pub mod find_path; #[cfg(test)] mod test_db; -#[cfg(test)] -mod marks; use std::hash::Hash; diff --git a/crates/ra_hir_def/src/marks.rs b/crates/ra_hir_def/src/marks.rs deleted file mode 100644 index daa49d5f1..000000000 --- a/crates/ra_hir_def/src/marks.rs +++ /dev/null @@ -1,17 +0,0 @@ -//! See test_utils/src/marks.rs - -test_utils::marks!( - bogus_paths - name_res_works_for_broken_modules - can_import_enum_variant - glob_enum - glob_enum_group - glob_across_crates - std_prelude - macro_rules_from_other_crates_are_visible_with_macro_use - prelude_is_macro_use - macro_dollar_crate_self - macro_dollar_crate_other - infer_resolve_while_let - prefer_std_paths -); diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index db994122a..353a31ad4 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -14,7 +14,7 @@ use ra_cfg::CfgOptions; use ra_db::{CrateId, FileId, ProcMacroId}; use ra_syntax::ast; use rustc_hash::FxHashMap; -use test_utils::tested_by; +use test_utils::mark; use crate::{ attr::Attrs, @@ -302,7 +302,7 @@ impl DefCollector<'_> { ); if let Some(ModuleDefId::ModuleId(m)) = res.take_types() { - tested_by!(macro_rules_from_other_crates_are_visible_with_macro_use); + mark::hit!(macro_rules_from_other_crates_are_visible_with_macro_use); self.import_all_macros_exported(current_module_id, m.krate); } } @@ -412,10 +412,10 @@ impl DefCollector<'_> { match def.take_types() { Some(ModuleDefId::ModuleId(m)) => { if import.is_prelude { - tested_by!(std_prelude); + mark::hit!(std_prelude); self.def_map.prelude = Some(m); } else if m.krate != self.def_map.krate { - tested_by!(glob_across_crates); + mark::hit!(glob_across_crates); // glob import from other crate => we can just import everything once let item_map = self.db.crate_def_map(m.krate); let scope = &item_map[m.local_id].scope; @@ -461,7 +461,7 @@ impl DefCollector<'_> { } } Some(ModuleDefId::AdtId(AdtId::EnumId(e))) => { - tested_by!(glob_enum); + mark::hit!(glob_enum); // glob import from enum => just import all the variants // XXX: urgh, so this works by accident! Here, we look at @@ -510,7 +510,7 @@ impl DefCollector<'_> { self.update(module_id, &[(name, def)], vis); } - None => tested_by!(bogus_paths), + None => mark::hit!(bogus_paths), } } } @@ -683,7 +683,7 @@ impl ModCollector<'_, '_> { // Prelude module is always considered to be `#[macro_use]`. if let Some(prelude_module) = self.def_collector.def_map.prelude { if prelude_module.krate != self.def_collector.def_map.krate { - tested_by!(prelude_is_macro_use); + mark::hit!(prelude_is_macro_use); self.def_collector.import_all_macros_exported(self.module_id, prelude_module.krate); } } diff --git a/crates/ra_hir_def/src/nameres/path_resolution.rs b/crates/ra_hir_def/src/nameres/path_resolution.rs index 35a0a0c98..19692e70c 100644 --- a/crates/ra_hir_def/src/nameres/path_resolution.rs +++ b/crates/ra_hir_def/src/nameres/path_resolution.rs @@ -14,7 +14,7 @@ use std::iter::successors; use hir_expand::name::Name; use ra_db::Edition; -use test_utils::tested_by; +use test_utils::mark; use crate::{ db::DefDatabase, @@ -108,7 +108,7 @@ impl CrateDefMap { let mut curr_per_ns: PerNs = match path.kind { PathKind::DollarCrate(krate) => { if krate == self.krate { - tested_by!(macro_dollar_crate_self); + mark::hit!(macro_dollar_crate_self); PerNs::types( ModuleId { krate: self.krate, local_id: self.root }.into(), Visibility::Public, @@ -116,7 +116,7 @@ impl CrateDefMap { } else { let def_map = db.crate_def_map(krate); let module = ModuleId { krate, local_id: def_map.root }; - tested_by!(macro_dollar_crate_other); + mark::hit!(macro_dollar_crate_other); PerNs::types(module.into(), Visibility::Public) } } @@ -221,7 +221,7 @@ impl CrateDefMap { } ModuleDefId::AdtId(AdtId::EnumId(e)) => { // enum variant - tested_by!(can_import_enum_variant); + mark::hit!(can_import_enum_variant); let enum_data = db.enum_data(e); match enum_data.variant(&segment) { Some(local_id) => { diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index f2716a295..4e628b14d 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -18,7 +18,7 @@ use ra_syntax::{ ast::{self, AttrsOwner, NameOwner, VisibilityOwner}, AstNode, }; -use test_utils::tested_by; +use test_utils::mark; use crate::{ attr::Attrs, @@ -346,7 +346,7 @@ impl RawItemsCollector { self.push_item(current_module, attrs, RawItemKind::Module(item)); return; } - tested_by!(name_res_works_for_broken_modules); + mark::hit!(name_res_works_for_broken_modules); } fn add_use_item(&mut self, current_module: Option>, use_item: ast::UseItem) { diff --git a/crates/ra_hir_def/src/nameres/tests.rs b/crates/ra_hir_def/src/nameres/tests.rs index 1b66c1aac..05cd0297d 100644 --- a/crates/ra_hir_def/src/nameres/tests.rs +++ b/crates/ra_hir_def/src/nameres/tests.rs @@ -8,7 +8,7 @@ use std::sync::Arc; use insta::assert_snapshot; use ra_db::{fixture::WithFixture, SourceDatabase}; -use test_utils::covers; +use test_utils::mark; use crate::{db::DefDatabase, nameres::*, test_db::TestDB}; @@ -132,7 +132,7 @@ fn crate_def_map_fn_mod_same_name() { #[test] fn bogus_paths() { - covers!(bogus_paths); + mark::check!(bogus_paths); let map = def_map( " //- /lib.rs @@ -247,7 +247,7 @@ fn re_exports() { #[test] fn std_prelude() { - covers!(std_prelude); + mark::check!(std_prelude); let map = def_map( " //- /main.rs crate:main deps:test_crate @@ -271,7 +271,7 @@ fn std_prelude() { #[test] fn can_import_enum_variant() { - covers!(can_import_enum_variant); + mark::check!(can_import_enum_variant); let map = def_map( " //- /lib.rs diff --git a/crates/ra_hir_def/src/nameres/tests/globs.rs b/crates/ra_hir_def/src/nameres/tests/globs.rs index ee8df3a26..2b12c0daa 100644 --- a/crates/ra_hir_def/src/nameres/tests/globs.rs +++ b/crates/ra_hir_def/src/nameres/tests/globs.rs @@ -152,7 +152,7 @@ fn glob_privacy_2() { #[test] fn glob_across_crates() { - covers!(glob_across_crates); + mark::check!(glob_across_crates); let map = def_map( r" //- /main.rs crate:main deps:test_crate @@ -171,7 +171,6 @@ fn glob_across_crates() { #[test] fn glob_privacy_across_crates() { - covers!(glob_across_crates); let map = def_map( r" //- /main.rs crate:main deps:test_crate @@ -191,7 +190,7 @@ fn glob_privacy_across_crates() { #[test] fn glob_enum() { - covers!(glob_enum); + mark::check!(glob_enum); let map = def_map( " //- /lib.rs @@ -212,7 +211,7 @@ fn glob_enum() { #[test] fn glob_enum_group() { - covers!(glob_enum_group); + mark::check!(glob_enum_group); let map = def_map( r" //- /lib.rs diff --git a/crates/ra_hir_def/src/nameres/tests/macros.rs b/crates/ra_hir_def/src/nameres/tests/macros.rs index 40289e3ca..84480d9f6 100644 --- a/crates/ra_hir_def/src/nameres/tests/macros.rs +++ b/crates/ra_hir_def/src/nameres/tests/macros.rs @@ -212,7 +212,7 @@ fn unexpanded_macro_should_expand_by_fixedpoint_loop() { #[test] fn macro_rules_from_other_crates_are_visible_with_macro_use() { - covers!(macro_rules_from_other_crates_are_visible_with_macro_use); + mark::check!(macro_rules_from_other_crates_are_visible_with_macro_use); let map = def_map( " //- /main.rs crate:main deps:foo @@ -262,7 +262,7 @@ fn macro_rules_from_other_crates_are_visible_with_macro_use() { #[test] fn prelude_is_macro_use() { - covers!(prelude_is_macro_use); + mark::check!(prelude_is_macro_use); let map = def_map( " //- /main.rs crate:main deps:foo @@ -544,8 +544,7 @@ fn path_qualified_macros() { #[test] fn macro_dollar_crate_is_correct_in_item() { - covers!(macro_dollar_crate_self); - covers!(macro_dollar_crate_other); + mark::check!(macro_dollar_crate_self); let map = def_map( " //- /main.rs crate:main deps:foo @@ -603,7 +602,7 @@ fn macro_dollar_crate_is_correct_in_item() { #[test] fn macro_dollar_crate_is_correct_in_indirect_deps() { - covers!(macro_dollar_crate_other); + mark::check!(macro_dollar_crate_other); // From std let map = def_map( r#" diff --git a/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs b/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs index 37fcdfb8c..b43b294ca 100644 --- a/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs +++ b/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs @@ -2,7 +2,7 @@ use super::*; #[test] fn name_res_works_for_broken_modules() { - covers!(name_res_works_for_broken_modules); + mark::check!(name_res_works_for_broken_modules); let map = def_map( r" //- /lib.rs diff --git a/crates/ra_hir_def/src/path/lower/lower_use.rs b/crates/ra_hir_def/src/path/lower/lower_use.rs index 5b6854b0f..7cc655487 100644 --- a/crates/ra_hir_def/src/path/lower/lower_use.rs +++ b/crates/ra_hir_def/src/path/lower/lower_use.rs @@ -6,7 +6,7 @@ use std::iter; use either::Either; use hir_expand::{hygiene::Hygiene, name::AsName}; use ra_syntax::ast::{self, NameOwner}; -use test_utils::tested_by; +use test_utils::mark; use crate::path::{ImportAlias, ModPath, PathKind}; @@ -54,7 +54,7 @@ pub(crate) fn lower_use_tree( // FIXME: report errors somewhere // We get here if we do } else if is_glob { - tested_by!(glob_enum_group); + mark::hit!(glob_enum_group); if let Some(prefix) = prefix { cb(prefix, &tree, is_glob, None) } -- cgit v1.2.3 From 6cdfd1c3cf3d58eee90b5034d4e2d702fdc0f8f5 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 19 May 2020 00:39:50 +0200 Subject: Make `find_path_inner` a query This eliminates any remaining performance problems in the "Implement default members" assist (at least that I've found). --- crates/ra_hir_def/src/db.rs | 4 ++++ crates/ra_hir_def/src/find_path.rs | 7 +++---- 2 files changed, 7 insertions(+), 4 deletions(-) (limited to 'crates/ra_hir_def/src') diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs index 2f71511ba..945a0025e 100644 --- a/crates/ra_hir_def/src/db.rs +++ b/crates/ra_hir_def/src/db.rs @@ -17,6 +17,7 @@ use crate::{ item_scope::ItemInNs, lang_item::{LangItemTarget, LangItems}, nameres::{raw::RawItems, CrateDefMap}, + path::ModPath, visibility::Visibility, AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, FunctionId, FunctionLoc, GenericDefId, ImplId, ImplLoc, ModuleId, StaticId, StaticLoc, StructId, StructLoc, TraitId, @@ -118,6 +119,9 @@ pub trait DefDatabase: InternDatabase + AstDatabase + Upcast { item: ItemInNs, krate: CrateId, ) -> Arc<[(ModuleId, Name, Visibility)]>; + + #[salsa::invoke(find_path::find_path_inner_query)] + fn find_path_inner(&self, item: ItemInNs, from: ModuleId, max_len: usize) -> Option; } fn crate_def_map_wait(db: &impl DefDatabase, krate: CrateId) -> Arc { diff --git a/crates/ra_hir_def/src/find_path.rs b/crates/ra_hir_def/src/find_path.rs index 68d3cde08..4db798473 100644 --- a/crates/ra_hir_def/src/find_path.rs +++ b/crates/ra_hir_def/src/find_path.rs @@ -20,7 +20,7 @@ use crate::{ /// *from where* you're referring to the item, hence the `from` parameter. pub fn find_path(db: &dyn DefDatabase, item: ItemInNs, from: ModuleId) -> Option { let _p = profile("find_path"); - find_path_inner(db, item, from, MAX_PATH_LEN) + db.find_path_inner(item, from, MAX_PATH_LEN) } const MAX_PATH_LEN: usize = 15; @@ -49,7 +49,7 @@ impl ModPath { } } -fn find_path_inner( +pub(crate) fn find_path_inner_query( db: &dyn DefDatabase, item: ItemInNs, from: ModuleId, @@ -140,8 +140,7 @@ fn find_path_inner( let mut best_path = None; let mut best_path_len = max_len; for (module_id, name) in importable_locations { - let mut path = match find_path_inner( - db, + let mut path = match db.find_path_inner( ItemInNs::Types(ModuleDefId::ModuleId(module_id)), from, best_path_len - 1, -- cgit v1.2.3