From 04af290d4e08e282dda0c0273f20a46f381224f4 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Mon, 4 Nov 2019 23:22:18 +0800 Subject: Add TokenId Shif in macro_rules --- crates/ra_mbe/src/lib.rs | 49 ++++++++++++++++++++++++++++++++++++++++++++-- crates/ra_mbe/src/tests.rs | 27 +++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 2 deletions(-) (limited to 'crates') diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index afdbee84e..7d4a5f307 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs @@ -42,6 +42,8 @@ pub use crate::syntax_bridge::{ #[derive(Clone, Debug, PartialEq, Eq)] pub struct MacroRules { pub(crate) rules: Vec, + /// Highest id of the token we have in TokenMap + pub(crate) shift: Option, } #[derive(Clone, Debug, PartialEq, Eq)] @@ -50,6 +52,42 @@ pub(crate) struct Rule { pub(crate) rhs: tt::Subtree, } +/// Find the "shift" (the highest id of the TokenId) inside a subtree +fn find_subtree_shift(tt: &tt::Subtree, mut cur: Option) -> Option { + use std::cmp::max; + + for t in &tt.token_trees { + cur = match t { + tt::TokenTree::Leaf(leaf) => match leaf { + tt::Leaf::Ident(ident) if ident.id != tt::TokenId::unspecified() => { + Some(max(cur.unwrap_or(0), ident.id.0)) + } + _ => cur, + }, + tt::TokenTree::Subtree(tt) => find_subtree_shift(tt, cur), + } + } + + cur +} + +/// Shift given TokenTree token id +fn shift_token_tree(tt: &mut tt::Subtree, shift: u32) { + for t in tt.token_trees.iter_mut() { + match t { + tt::TokenTree::Leaf(leaf) => match leaf { + tt::Leaf::Ident(ident) if ident.id != tt::TokenId::unspecified() => { + // Note that TokenId is started from zero, + // We have to add 1 to prevent duplication. + ident.id.0 += shift + 1; + } + _ => (), + }, + tt::TokenTree::Subtree(tt) => shift_token_tree(tt, shift), + } + } +} + impl MacroRules { pub fn parse(tt: &tt::Subtree) -> Result { // Note: this parsing can be implemented using mbe machinery itself, by @@ -72,10 +110,17 @@ impl MacroRules { validate(&rule.lhs)?; } - Ok(MacroRules { rules }) + Ok(MacroRules { rules, shift: find_subtree_shift(tt, None) }) } + pub fn expand(&self, tt: &tt::Subtree) -> Result { - mbe_expander::expand(self, tt) + // apply shift + let mut tt = tt.clone(); + if let Some(shift) = self.shift { + shift_token_tree(&mut tt, shift) + } + + mbe_expander::expand(self, &tt) } } diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs index f34ab52e1..a23e3afe3 100644 --- a/crates/ra_mbe/src/tests.rs +++ b/crates/ra_mbe/src/tests.rs @@ -58,6 +58,33 @@ mod rule_parsing { // * Port the test to rust and add it to this module // * Make it pass :-) +#[test] +fn test_token_id_shift() { + let macro_definition = r#" +macro_rules! foobar { + ($e:ident) => { foo bar $e } +} +"#; + let rules = create_rules(macro_definition); + let expansion = expand(&rules, "foobar!(baz);"); + + fn get_id(t: &tt::TokenTree) -> Option { + if let tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) = t { + return Some(ident.id.0); + } + None + } + + assert_eq!(expansion.token_trees.len(), 3); + // ($e:ident) => { foo bar $e } + // 0 1 2 3 4 + assert_eq!(get_id(&expansion.token_trees[0]), Some(2)); + assert_eq!(get_id(&expansion.token_trees[1]), Some(3)); + + // So baz should be 5 + assert_eq!(get_id(&expansion.token_trees[2]), Some(5)); +} + #[test] fn test_convert_tt() { let macro_definition = r#" -- cgit v1.2.3 From 63e42bb5bdd57b8b0155d9e38ebe78e0e864c4d5 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Tue, 5 Nov 2019 00:16:03 +0800 Subject: Change to better naming --- crates/ra_mbe/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'crates') diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index 7d4a5f307..f51149258 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs @@ -72,7 +72,7 @@ fn find_subtree_shift(tt: &tt::Subtree, mut cur: Option) -> Option { } /// Shift given TokenTree token id -fn shift_token_tree(tt: &mut tt::Subtree, shift: u32) { +fn shift_subtree(tt: &mut tt::Subtree, shift: u32) { for t in tt.token_trees.iter_mut() { match t { tt::TokenTree::Leaf(leaf) => match leaf { @@ -83,7 +83,7 @@ fn shift_token_tree(tt: &mut tt::Subtree, shift: u32) { } _ => (), }, - tt::TokenTree::Subtree(tt) => shift_token_tree(tt, shift), + tt::TokenTree::Subtree(tt) => shift_subtree(tt, shift), } } } @@ -117,7 +117,7 @@ impl MacroRules { // apply shift let mut tt = tt.clone(); if let Some(shift) = self.shift { - shift_token_tree(&mut tt, shift) + shift_subtree(&mut tt, shift) } mbe_expander::expand(self, &tt) -- cgit v1.2.3 From 42661a3b27f039f4eb11ef7c5f4733bb79b4a1ec Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Tue, 5 Nov 2019 00:33:19 +0800 Subject: Change Option to u32 for shift value --- crates/ra_mbe/src/lib.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'crates') diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index f51149258..1c076b36b 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs @@ -43,7 +43,7 @@ pub use crate::syntax_bridge::{ pub struct MacroRules { pub(crate) rules: Vec, /// Highest id of the token we have in TokenMap - pub(crate) shift: Option, + pub(crate) shift: u32, } #[derive(Clone, Debug, PartialEq, Eq)] @@ -53,14 +53,14 @@ pub(crate) struct Rule { } /// Find the "shift" (the highest id of the TokenId) inside a subtree -fn find_subtree_shift(tt: &tt::Subtree, mut cur: Option) -> Option { +fn find_subtree_shift(tt: &tt::Subtree, mut cur: u32) -> u32 { use std::cmp::max; for t in &tt.token_trees { cur = match t { tt::TokenTree::Leaf(leaf) => match leaf { tt::Leaf::Ident(ident) if ident.id != tt::TokenId::unspecified() => { - Some(max(cur.unwrap_or(0), ident.id.0)) + max(cur, ident.id.0) } _ => cur, }, @@ -110,16 +110,13 @@ impl MacroRules { validate(&rule.lhs)?; } - Ok(MacroRules { rules, shift: find_subtree_shift(tt, None) }) + Ok(MacroRules { rules, shift: find_subtree_shift(tt, 0) }) } pub fn expand(&self, tt: &tt::Subtree) -> Result { // apply shift let mut tt = tt.clone(); - if let Some(shift) = self.shift { - shift_subtree(&mut tt, shift) - } - + shift_subtree(&mut tt, self.shift); mbe_expander::expand(self, &tt) } } -- cgit v1.2.3 From 188a1412b9d21ead5d823ce3ca00d3b4f823221b Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Tue, 5 Nov 2019 01:01:05 +0800 Subject: Refactor and rename --- crates/ra_mbe/src/lib.rs | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) (limited to 'crates') diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index 1c076b36b..b92312d52 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs @@ -52,23 +52,21 @@ pub(crate) struct Rule { pub(crate) rhs: tt::Subtree, } -/// Find the "shift" (the highest id of the TokenId) inside a subtree -fn find_subtree_shift(tt: &tt::Subtree, mut cur: u32) -> u32 { - use std::cmp::max; - - for t in &tt.token_trees { - cur = match t { - tt::TokenTree::Leaf(leaf) => match leaf { - tt::Leaf::Ident(ident) if ident.id != tt::TokenId::unspecified() => { - max(cur, ident.id.0) - } - _ => cur, - }, - tt::TokenTree::Subtree(tt) => find_subtree_shift(tt, cur), - } - } - - cur +// Find the max token id inside a subtree +fn max_id(subtree: &tt::Subtree) -> Option { + subtree + .token_trees + .iter() + .filter_map(|tt| match tt { + tt::TokenTree::Subtree(subtree) => max_id(subtree), + tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) + if ident.id != tt::TokenId::unspecified() => + { + Some(ident.id.0) + } + _ => None, + }) + .max() } /// Shift given TokenTree token id @@ -77,9 +75,7 @@ fn shift_subtree(tt: &mut tt::Subtree, shift: u32) { match t { tt::TokenTree::Leaf(leaf) => match leaf { tt::Leaf::Ident(ident) if ident.id != tt::TokenId::unspecified() => { - // Note that TokenId is started from zero, - // We have to add 1 to prevent duplication. - ident.id.0 += shift + 1; + ident.id.0 += shift; } _ => (), }, @@ -110,7 +106,10 @@ impl MacroRules { validate(&rule.lhs)?; } - Ok(MacroRules { rules, shift: find_subtree_shift(tt, 0) }) + // Note that TokenId is started from zero, + // We have to add 1 to prevent duplication. + let shift = max_id(tt).unwrap_or(0) + 1; + Ok(MacroRules { rules, shift }) } pub fn expand(&self, tt: &tt::Subtree) -> Result { -- cgit v1.2.3 From a5839662f448602d6aa2d724432844fc2d08947e Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Tue, 5 Nov 2019 01:16:06 +0800 Subject: Change to add 1 if non zero shift --- crates/ra_mbe/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates') diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index b92312d52..15f000175 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs @@ -108,7 +108,7 @@ impl MacroRules { // Note that TokenId is started from zero, // We have to add 1 to prevent duplication. - let shift = max_id(tt).unwrap_or(0) + 1; + let shift = max_id(tt).map_or(0, |it| it + 1); Ok(MacroRules { rules, shift }) } -- cgit v1.2.3 From d04ecc841904401ff17c723702b3332b64235212 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 4 Nov 2019 21:42:25 +0300 Subject: Reduce visibility --- crates/ra_hir_def/src/nameres.rs | 4 ++-- crates/ra_hir_def/src/nameres/mod_resolution.rs | 12 ++++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index 433bdde48..55a747ee2 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs @@ -50,8 +50,8 @@ // FIXME: review privacy of submodules pub mod raw; pub mod per_ns; -pub mod collector; -pub mod mod_resolution; +mod collector; +mod mod_resolution; #[cfg(test)] mod tests; diff --git a/crates/ra_hir_def/src/nameres/mod_resolution.rs b/crates/ra_hir_def/src/nameres/mod_resolution.rs index f6b0b8fb1..b3b1379d0 100644 --- a/crates/ra_hir_def/src/nameres/mod_resolution.rs +++ b/crates/ra_hir_def/src/nameres/mod_resolution.rs @@ -6,7 +6,7 @@ use ra_syntax::SmolStr; use crate::{db::DefDatabase2, HirFileId}; #[derive(Clone, Debug)] -pub struct ModDir { +pub(super) struct ModDir { /// `.` for `mod.rs`, `lib.rs` /// `./foo` for `foo.rs` /// `./foo/bar` for `mod bar { mod x; }` nested in `foo.rs` @@ -16,11 +16,15 @@ pub struct ModDir { } impl ModDir { - pub fn root() -> ModDir { + pub(super) fn root() -> ModDir { ModDir { path: RelativePathBuf::default(), root_non_dir_owner: false } } - pub fn descend_into_definition(&self, name: &Name, attr_path: Option<&SmolStr>) -> ModDir { + pub(super) fn descend_into_definition( + &self, + name: &Name, + attr_path: Option<&SmolStr>, + ) -> ModDir { let mut path = self.path.clone(); match attr_to_path(attr_path) { None => path.push(&name.to_string()), @@ -34,7 +38,7 @@ impl ModDir { ModDir { path, root_non_dir_owner: false } } - pub fn resolve_declaration( + pub(super) fn resolve_declaration( &self, db: &impl DefDatabase2, file_id: HirFileId, -- cgit v1.2.3 From dcdcc9e4c8e24f8b643a0cc5c70e8e4dd624289f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 4 Nov 2019 22:04:51 +0300 Subject: Remove some duplicated test functions --- crates/ra_hir/src/mock.rs | 97 ++++++++--------------------------------------- 1 file changed, 16 insertions(+), 81 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs index ab97a09b9..48723fa02 100644 --- a/crates/ra_hir/src/mock.rs +++ b/crates/ra_hir/src/mock.rs @@ -2,15 +2,15 @@ use std::{panic, sync::Arc}; +use hir_def::{db::DefDatabase2, ModuleId}; use hir_expand::diagnostics::DiagnosticSink; use parking_lot::Mutex; use ra_cfg::CfgOptions; use ra_db::{ - salsa, CrateGraph, CrateId, Edition, FileId, FileLoader, FileLoaderDelegate, FilePosition, - RelativePath, RelativePathBuf, SourceDatabase, SourceDatabaseExt, SourceRoot, SourceRootId, + salsa, CrateGraph, CrateId, Edition, FileId, FileLoader, FileLoaderDelegate, RelativePath, + RelativePathBuf, SourceDatabase, SourceDatabaseExt, SourceRoot, SourceRootId, }; use rustc_hash::FxHashMap; -use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER}; use crate::{db, debug::HirDebugHelper}; @@ -63,12 +63,6 @@ impl HirDebugHelper for MockDatabase { } impl MockDatabase { - pub fn with_files(fixture: &str) -> MockDatabase { - let (db, position) = MockDatabase::from_fixture(fixture); - assert!(position.is_none()); - db - } - pub fn with_single_file(text: &str) -> (MockDatabase, SourceRoot, FileId) { let mut db = MockDatabase::default(); let mut source_root = SourceRoot::default(); @@ -86,67 +80,21 @@ impl MockDatabase { pub fn diagnostics(&self) -> String { let mut buf = String::new(); - let mut files: Vec = self.files.values().copied().collect(); - files.sort(); - for file in files { - let src = crate::Source { - file_id: file.into(), - ast: crate::ModuleSource::new(self, Some(file), None), - }; - let module = crate::Module::from_definition(self, src).unwrap(); - module.diagnostics( - self, - &mut DiagnosticSink::new(|d| { - buf += &format!("{:?}: {}\n", d.syntax_node(self).text(), d.message()); - }), - ) - } - buf - } - - fn from_fixture(fixture: &str) -> (MockDatabase, Option) { - let mut db = MockDatabase::default(); - - let pos = db.add_fixture(fixture); - - (db, pos) - } - - fn add_fixture(&mut self, fixture: &str) -> Option { - let mut position = None; - let mut source_root = SourceRoot::default(); - let mut source_root_id = WORKSPACE; - let mut source_root_prefix = "/".to_string(); - for entry in parse_fixture(fixture) { - if entry.meta.starts_with("root") { - self.set_source_root(source_root_id, Arc::new(source_root)); - source_root = SourceRoot::default(); - - source_root_id = SourceRootId(source_root_id.0 + 1); - source_root_prefix = entry.meta["root".len()..].trim().to_string(); - continue; - } - if entry.text.contains(CURSOR_MARKER) { - assert!(position.is_none(), "only one marker (<|>) per fixture is allowed"); - position = Some(self.add_file_with_position( - source_root_id, - &source_root_prefix, - &mut source_root, - &entry.meta, - &entry.text, - )); - } else { - self.add_file( - source_root_id, - &source_root_prefix, - &mut source_root, - &entry.meta, - &entry.text, - ); + let crate_graph = self.crate_graph(); + for krate in crate_graph.iter().next() { + let crate_def_map = self.crate_def_map(krate); + for (module_id, _) in crate_def_map.modules.iter() { + let module_id = ModuleId { krate, module_id }; + let module = crate::Module::from(module_id); + module.diagnostics( + self, + &mut DiagnosticSink::new(|d| { + buf += &format!("{:?}: {}\n", d.syntax_node(self).text(), d.message()); + }), + ) } } - self.set_source_root(source_root_id, Arc::new(source_root)); - position + buf } fn add_file( @@ -183,19 +131,6 @@ impl MockDatabase { } file_id } - - fn add_file_with_position( - &mut self, - source_root_id: SourceRootId, - source_root_prefix: &str, - source_root: &mut SourceRoot, - path: &str, - text: &str, - ) -> FilePosition { - let (offset, text) = extract_offset(text); - let file_id = self.add_file(source_root_id, source_root_prefix, source_root, path, &text); - FilePosition { file_id, offset } - } } impl salsa::Database for MockDatabase { -- cgit v1.2.3 From 24894aca3162d654ee051f591a920ef1525d821f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 4 Nov 2019 22:12:49 +0300 Subject: Remove more duplication in test fixtures --- crates/ra_assists/src/doc_tests.rs | 4 ++-- crates/ra_assists/src/lib.rs | 20 ++++++++-------- crates/ra_hir/src/expr/scope.rs | 6 ++--- crates/ra_hir/src/mock.rs | 48 ++------------------------------------ crates/ra_hir/src/ty/tests.rs | 2 +- 5 files changed, 18 insertions(+), 62 deletions(-) (limited to 'crates') diff --git a/crates/ra_assists/src/doc_tests.rs b/crates/ra_assists/src/doc_tests.rs index 6e1e3de84..43194037e 100644 --- a/crates/ra_assists/src/doc_tests.rs +++ b/crates/ra_assists/src/doc_tests.rs @@ -6,12 +6,12 @@ mod generated; use hir::mock::MockDatabase; -use ra_db::FileRange; +use ra_db::{fixture::WithFixture, FileRange}; use test_utils::{assert_eq_text, extract_range_or_offset}; fn check(assist_id: &str, before: &str, after: &str) { let (selection, before) = extract_range_or_offset(before); - let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = MockDatabase::with_single_file(&before); let frange = FileRange { file_id, range: selection.into() }; let (_assist_id, action) = crate::assists(&db, frange) diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 38599d4f1..13db37d29 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -147,7 +147,7 @@ mod assists { #[cfg(test)] mod helpers { use hir::mock::MockDatabase; - use ra_db::FileRange; + use ra_db::{fixture::WithFixture, FileRange}; use ra_syntax::TextRange; use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range}; @@ -159,7 +159,7 @@ mod helpers { after: &str, ) { let (before_cursor_pos, before) = extract_offset(before); - let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = MockDatabase::with_single_file(&before); let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; let assist = @@ -187,7 +187,7 @@ mod helpers { after: &str, ) { let (range, before) = extract_range(before); - let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = MockDatabase::with_single_file(&before); let frange = FileRange { file_id, range }; let assist = AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); @@ -209,7 +209,7 @@ mod helpers { target: &str, ) { let (before_cursor_pos, before) = extract_offset(before); - let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = MockDatabase::with_single_file(&before); let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; let assist = @@ -229,7 +229,7 @@ mod helpers { target: &str, ) { let (range, before) = extract_range(before); - let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = MockDatabase::with_single_file(&before); let frange = FileRange { file_id, range }; let assist = AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); @@ -247,7 +247,7 @@ mod helpers { before: &str, ) { let (before_cursor_pos, before) = extract_offset(before); - let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = MockDatabase::with_single_file(&before); let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; let assist = AssistCtx::with_ctx(&db, frange, true, assist); @@ -259,7 +259,7 @@ mod helpers { before: &str, ) { let (range, before) = extract_range(before); - let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = MockDatabase::with_single_file(&before); let frange = FileRange { file_id, range }; let assist = AssistCtx::with_ctx(&db, frange, true, assist); assert!(assist.is_none()); @@ -269,7 +269,7 @@ mod helpers { #[cfg(test)] mod tests { use hir::mock::MockDatabase; - use ra_db::FileRange; + use ra_db::{fixture::WithFixture, FileRange}; use ra_syntax::TextRange; use test_utils::{extract_offset, extract_range}; @@ -277,7 +277,7 @@ mod tests { fn assist_order_field_struct() { let before = "struct Foo { <|>bar: u32 }"; let (before_cursor_pos, before) = extract_offset(before); - let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = MockDatabase::with_single_file(&before); let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; let assists = super::assists(&db, frange); @@ -298,7 +298,7 @@ mod tests { } }"; let (range, before) = extract_range(before); - let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = MockDatabase::with_single_file(&before); let frange = FileRange { file_id, range }; let assists = super::assists(&db, frange); let mut assists = assists.iter(); diff --git a/crates/ra_hir/src/expr/scope.rs b/crates/ra_hir/src/expr/scope.rs index 5496822e7..f7196b740 100644 --- a/crates/ra_hir/src/expr/scope.rs +++ b/crates/ra_hir/src/expr/scope.rs @@ -174,7 +174,7 @@ fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope #[cfg(test)] mod tests { - use ra_db::SourceDatabase; + use ra_db::{fixture::WithFixture, SourceDatabase}; use ra_syntax::{algo::find_node_at_offset, ast, AstNode}; use test_utils::{assert_eq_text, extract_offset}; @@ -191,7 +191,7 @@ mod tests { buf }; - let (db, _source_root, file_id) = MockDatabase::with_single_file(&code); + let (db, file_id) = MockDatabase::with_single_file(&code); let file = db.parse(file_id).ok().unwrap(); let marker: ast::PathExpr = find_node_at_offset(file.syntax(), off).unwrap(); let analyzer = SourceAnalyzer::new(&db, file_id, marker.syntax(), None); @@ -288,7 +288,7 @@ mod tests { fn do_check_local_name(code: &str, expected_offset: u32) { let (off, code) = extract_offset(code); - let (db, _source_root, file_id) = MockDatabase::with_single_file(&code); + let (db, file_id) = MockDatabase::with_single_file(&code); let file = db.parse(file_id).ok().unwrap(); let expected_name = find_node_at_offset::(file.syntax(), expected_offset.into()) .expect("failed to find a name at the target offset"); diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs index 48723fa02..94aff93de 100644 --- a/crates/ra_hir/src/mock.rs +++ b/crates/ra_hir/src/mock.rs @@ -5,10 +5,9 @@ use std::{panic, sync::Arc}; use hir_def::{db::DefDatabase2, ModuleId}; use hir_expand::diagnostics::DiagnosticSink; use parking_lot::Mutex; -use ra_cfg::CfgOptions; use ra_db::{ - salsa, CrateGraph, CrateId, Edition, FileId, FileLoader, FileLoaderDelegate, RelativePath, - RelativePathBuf, SourceDatabase, SourceDatabaseExt, SourceRoot, SourceRootId, + salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath, SourceDatabase, + SourceRootId, }; use rustc_hash::FxHashMap; @@ -63,14 +62,6 @@ impl HirDebugHelper for MockDatabase { } impl MockDatabase { - pub fn with_single_file(text: &str) -> (MockDatabase, SourceRoot, FileId) { - let mut db = MockDatabase::default(); - let mut source_root = SourceRoot::default(); - let file_id = db.add_file(WORKSPACE, "/", &mut source_root, "/main.rs", text); - db.set_source_root(WORKSPACE, Arc::new(source_root.clone())); - (db, source_root, file_id) - } - pub fn file_id_of(&self, path: &str) -> FileId { match self.files.get(path) { Some(it) => *it, @@ -96,41 +87,6 @@ impl MockDatabase { } buf } - - fn add_file( - &mut self, - source_root_id: SourceRootId, - source_root_prefix: &str, - source_root: &mut SourceRoot, - path: &str, - text: &str, - ) -> FileId { - assert!(source_root_prefix.starts_with('/')); - assert!(source_root_prefix.ends_with('/')); - assert!(path.starts_with(source_root_prefix)); - let rel_path = RelativePathBuf::from_path(&path[source_root_prefix.len()..]).unwrap(); - - let is_crate_root = rel_path == "lib.rs" || rel_path == "/main.rs"; - - let file_id = FileId(self.files.len() as u32); - - let prev = self.files.insert(path.to_string(), file_id); - assert!(prev.is_none(), "duplicate files in the text fixture"); - Arc::make_mut(&mut self.file_paths).insert(file_id, path.to_string()); - - let text = Arc::new(text.to_string()); - self.set_file_text(file_id, text); - self.set_file_relative_path(file_id, rel_path.clone()); - self.set_file_source_root(file_id, source_root_id); - source_root.insert_file(rel_path, file_id); - - if is_crate_root { - let mut crate_graph = CrateGraph::default(); - crate_graph.add_crate_root(file_id, Edition::Edition2018, CfgOptions::default()); - self.set_crate_graph(Arc::new(crate_graph)); - } - file_id - } } impl salsa::Database for MockDatabase { diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 4b7e34878..4af2fe0b4 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -4665,7 +4665,7 @@ fn type_at(content: &str) -> String { } fn infer(content: &str) -> String { - let (db, _, file_id) = MockDatabase::with_single_file(content); + let (db, file_id) = MockDatabase::with_single_file(content); let source_file = db.parse(file_id).ok().unwrap(); let mut acc = String::new(); -- cgit v1.2.3 From 7649a8ebbe900f43cc9b8789c2942cfbadab8f1c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 4 Nov 2019 22:19:06 +0300 Subject: remove dead code --- crates/ra_hir/src/mock.rs | 33 +++++++-------------------------- 1 file changed, 7 insertions(+), 26 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs index 94aff93de..ff1f3fa75 100644 --- a/crates/ra_hir/src/mock.rs +++ b/crates/ra_hir/src/mock.rs @@ -9,7 +9,6 @@ use ra_db::{ salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath, SourceDatabase, SourceRootId, }; -use rustc_hash::FxHashMap; use crate::{db, debug::HirDebugHelper}; @@ -28,9 +27,6 @@ pub const WORKSPACE: SourceRootId = SourceRootId(0); pub struct MockDatabase { events: Mutex>>>, runtime: salsa::Runtime, - files: FxHashMap, - crate_names: Arc>, - file_paths: Arc>, } impl panic::RefUnwindSafe for MockDatabase {} @@ -51,24 +47,18 @@ impl FileLoader for MockDatabase { } } +// FIXME: improve `WithFixture` to bring useful hir debugging back impl HirDebugHelper for MockDatabase { - fn crate_name(&self, krate: CrateId) -> Option { - self.crate_names.get(&krate).cloned() + fn crate_name(&self, _krate: CrateId) -> Option { + None } - fn file_path(&self, file_id: FileId) -> Option { - self.file_paths.get(&file_id).cloned() + fn file_path(&self, _file_id: FileId) -> Option { + None } } impl MockDatabase { - pub fn file_id_of(&self, path: &str) -> FileId { - match self.files.get(path) { - Some(it) => *it, - None => panic!("unknown file: {:?}\nexisting files:\n{:#?}", path, self.files), - } - } - pub fn diagnostics(&self) -> String { let mut buf = String::new(); let crate_graph = self.crate_graph(); @@ -104,13 +94,8 @@ impl salsa::Database for MockDatabase { impl Default for MockDatabase { fn default() -> MockDatabase { - let mut db = MockDatabase { - events: Default::default(), - runtime: salsa::Runtime::default(), - files: FxHashMap::default(), - crate_names: Default::default(), - file_paths: Default::default(), - }; + let mut db = + MockDatabase { events: Default::default(), runtime: salsa::Runtime::default() }; db.set_crate_graph(Default::default()); db } @@ -121,10 +106,6 @@ impl salsa::ParallelDatabase for MockDatabase { salsa::Snapshot::new(MockDatabase { events: Default::default(), runtime: self.runtime.snapshot(self), - // only the root database can be used to get file_id by path. - files: FxHashMap::default(), - file_paths: Arc::clone(&self.file_paths), - crate_names: Arc::clone(&self.crate_names), }) } } -- cgit v1.2.3 From 74d827bb8001952f90c85fca3d064fe3096009bd Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 4 Nov 2019 22:21:15 +0300 Subject: Rename MockDatabase -> TestDB Actually working rename is sooo useful! --- crates/ra_assists/src/doc_tests.rs | 4 +- crates/ra_assists/src/lib.rs | 32 ++++----- crates/ra_hir/src/expr/scope.rs | 6 +- crates/ra_hir/src/lib.rs | 4 +- crates/ra_hir/src/mock.rs | 134 ------------------------------------- crates/ra_hir/src/test_db.rs | 131 ++++++++++++++++++++++++++++++++++++ crates/ra_hir/src/ty/tests.rs | 32 ++++----- 7 files changed, 170 insertions(+), 173 deletions(-) delete mode 100644 crates/ra_hir/src/mock.rs create mode 100644 crates/ra_hir/src/test_db.rs (limited to 'crates') diff --git a/crates/ra_assists/src/doc_tests.rs b/crates/ra_assists/src/doc_tests.rs index 43194037e..2500a5ab7 100644 --- a/crates/ra_assists/src/doc_tests.rs +++ b/crates/ra_assists/src/doc_tests.rs @@ -5,13 +5,13 @@ mod generated; -use hir::mock::MockDatabase; +use hir::mock::TestDB; use ra_db::{fixture::WithFixture, FileRange}; use test_utils::{assert_eq_text, extract_range_or_offset}; fn check(assist_id: &str, before: &str, after: &str) { let (selection, before) = extract_range_or_offset(before); - let (db, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = TestDB::with_single_file(&before); let frange = FileRange { file_id, range: selection.into() }; let (_assist_id, action) = crate::assists(&db, frange) diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 13db37d29..85fb72395 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -146,7 +146,7 @@ mod assists { #[cfg(test)] mod helpers { - use hir::mock::MockDatabase; + use hir::mock::TestDB; use ra_db::{fixture::WithFixture, FileRange}; use ra_syntax::TextRange; use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range}; @@ -154,12 +154,12 @@ mod helpers { use crate::{Assist, AssistCtx}; pub(crate) fn check_assist( - assist: fn(AssistCtx) -> Option, + assist: fn(AssistCtx) -> Option, before: &str, after: &str, ) { let (before_cursor_pos, before) = extract_offset(before); - let (db, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = TestDB::with_single_file(&before); let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; let assist = @@ -182,12 +182,12 @@ mod helpers { } pub(crate) fn check_assist_range( - assist: fn(AssistCtx) -> Option, + assist: fn(AssistCtx) -> Option, before: &str, after: &str, ) { let (range, before) = extract_range(before); - let (db, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = TestDB::with_single_file(&before); let frange = FileRange { file_id, range }; let assist = AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); @@ -204,12 +204,12 @@ mod helpers { } pub(crate) fn check_assist_target( - assist: fn(AssistCtx) -> Option, + assist: fn(AssistCtx) -> Option, before: &str, target: &str, ) { let (before_cursor_pos, before) = extract_offset(before); - let (db, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = TestDB::with_single_file(&before); let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; let assist = @@ -224,12 +224,12 @@ mod helpers { } pub(crate) fn check_assist_range_target( - assist: fn(AssistCtx) -> Option, + assist: fn(AssistCtx) -> Option, before: &str, target: &str, ) { let (range, before) = extract_range(before); - let (db, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = TestDB::with_single_file(&before); let frange = FileRange { file_id, range }; let assist = AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); @@ -243,11 +243,11 @@ mod helpers { } pub(crate) fn check_assist_not_applicable( - assist: fn(AssistCtx) -> Option, + assist: fn(AssistCtx) -> Option, before: &str, ) { let (before_cursor_pos, before) = extract_offset(before); - let (db, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = TestDB::with_single_file(&before); let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; let assist = AssistCtx::with_ctx(&db, frange, true, assist); @@ -255,11 +255,11 @@ mod helpers { } pub(crate) fn check_assist_range_not_applicable( - assist: fn(AssistCtx) -> Option, + assist: fn(AssistCtx) -> Option, before: &str, ) { let (range, before) = extract_range(before); - let (db, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = TestDB::with_single_file(&before); let frange = FileRange { file_id, range }; let assist = AssistCtx::with_ctx(&db, frange, true, assist); assert!(assist.is_none()); @@ -268,7 +268,7 @@ mod helpers { #[cfg(test)] mod tests { - use hir::mock::MockDatabase; + use hir::mock::TestDB; use ra_db::{fixture::WithFixture, FileRange}; use ra_syntax::TextRange; use test_utils::{extract_offset, extract_range}; @@ -277,7 +277,7 @@ mod tests { fn assist_order_field_struct() { let before = "struct Foo { <|>bar: u32 }"; let (before_cursor_pos, before) = extract_offset(before); - let (db, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = TestDB::with_single_file(&before); let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; let assists = super::assists(&db, frange); @@ -298,7 +298,7 @@ mod tests { } }"; let (range, before) = extract_range(before); - let (db, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = TestDB::with_single_file(&before); let frange = FileRange { file_id, range }; let assists = super::assists(&db, frange); let mut assists = assists.iter(); diff --git a/crates/ra_hir/src/expr/scope.rs b/crates/ra_hir/src/expr/scope.rs index f7196b740..c14c2ab66 100644 --- a/crates/ra_hir/src/expr/scope.rs +++ b/crates/ra_hir/src/expr/scope.rs @@ -178,7 +178,7 @@ mod tests { use ra_syntax::{algo::find_node_at_offset, ast, AstNode}; use test_utils::{assert_eq_text, extract_offset}; - use crate::{mock::MockDatabase, source_binder::SourceAnalyzer}; + use crate::{source_binder::SourceAnalyzer, test_db::TestDB}; fn do_check(code: &str, expected: &[&str]) { let (off, code) = extract_offset(code); @@ -191,7 +191,7 @@ mod tests { buf }; - let (db, file_id) = MockDatabase::with_single_file(&code); + let (db, file_id) = TestDB::with_single_file(&code); let file = db.parse(file_id).ok().unwrap(); let marker: ast::PathExpr = find_node_at_offset(file.syntax(), off).unwrap(); let analyzer = SourceAnalyzer::new(&db, file_id, marker.syntax(), None); @@ -288,7 +288,7 @@ mod tests { fn do_check_local_name(code: &str, expected_offset: u32) { let (off, code) = extract_offset(code); - let (db, file_id) = MockDatabase::with_single_file(&code); + let (db, file_id) = TestDB::with_single_file(&code); let file = db.parse(file_id).ok().unwrap(); let expected_name = find_node_at_offset::(file.syntax(), expected_offset.into()) .expect("failed to find a name at the target offset"); diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 3ba99d92d..da6aa2ed8 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -29,8 +29,6 @@ macro_rules! impl_froms { pub mod debug; pub mod db; -#[macro_use] -pub mod mock; pub mod source_binder; mod ids; @@ -51,6 +49,8 @@ mod code_model; pub mod from_source; +#[cfg(test)] +mod test_db; #[cfg(test)] mod marks; diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs deleted file mode 100644 index ff1f3fa75..000000000 --- a/crates/ra_hir/src/mock.rs +++ /dev/null @@ -1,134 +0,0 @@ -//! FIXME: write short doc here - -use std::{panic, sync::Arc}; - -use hir_def::{db::DefDatabase2, ModuleId}; -use hir_expand::diagnostics::DiagnosticSink; -use parking_lot::Mutex; -use ra_db::{ - salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath, SourceDatabase, - SourceRootId, -}; - -use crate::{db, debug::HirDebugHelper}; - -pub const WORKSPACE: SourceRootId = SourceRootId(0); - -#[salsa::database( - ra_db::SourceDatabaseExtStorage, - ra_db::SourceDatabaseStorage, - db::InternDatabaseStorage, - db::AstDatabaseStorage, - db::DefDatabaseStorage, - db::DefDatabase2Storage, - db::HirDatabaseStorage -)] -#[derive(Debug)] -pub struct MockDatabase { - events: Mutex>>>, - runtime: salsa::Runtime, -} - -impl panic::RefUnwindSafe for MockDatabase {} - -impl FileLoader for MockDatabase { - fn file_text(&self, file_id: FileId) -> Arc { - FileLoaderDelegate(self).file_text(file_id) - } - fn resolve_relative_path( - &self, - anchor: FileId, - relative_path: &RelativePath, - ) -> Option { - FileLoaderDelegate(self).resolve_relative_path(anchor, relative_path) - } - fn relevant_crates(&self, file_id: FileId) -> Arc> { - FileLoaderDelegate(self).relevant_crates(file_id) - } -} - -// FIXME: improve `WithFixture` to bring useful hir debugging back -impl HirDebugHelper for MockDatabase { - fn crate_name(&self, _krate: CrateId) -> Option { - None - } - - fn file_path(&self, _file_id: FileId) -> Option { - None - } -} - -impl MockDatabase { - pub fn diagnostics(&self) -> String { - let mut buf = String::new(); - let crate_graph = self.crate_graph(); - for krate in crate_graph.iter().next() { - let crate_def_map = self.crate_def_map(krate); - for (module_id, _) in crate_def_map.modules.iter() { - let module_id = ModuleId { krate, module_id }; - let module = crate::Module::from(module_id); - module.diagnostics( - self, - &mut DiagnosticSink::new(|d| { - buf += &format!("{:?}: {}\n", d.syntax_node(self).text(), d.message()); - }), - ) - } - } - buf - } -} - -impl salsa::Database for MockDatabase { - fn salsa_runtime(&self) -> &salsa::Runtime { - &self.runtime - } - - fn salsa_event(&self, event: impl Fn() -> salsa::Event) { - let mut events = self.events.lock(); - if let Some(events) = &mut *events { - events.push(event()); - } - } -} - -impl Default for MockDatabase { - fn default() -> MockDatabase { - let mut db = - MockDatabase { events: Default::default(), runtime: salsa::Runtime::default() }; - db.set_crate_graph(Default::default()); - db - } -} - -impl salsa::ParallelDatabase for MockDatabase { - fn snapshot(&self) -> salsa::Snapshot { - salsa::Snapshot::new(MockDatabase { - events: Default::default(), - runtime: self.runtime.snapshot(self), - }) - } -} - -impl MockDatabase { - pub fn log(&self, f: impl FnOnce()) -> Vec> { - *self.events.lock() = Some(Vec::new()); - f(); - self.events.lock().take().unwrap() - } - - pub fn log_executed(&self, f: impl FnOnce()) -> Vec { - let events = self.log(f); - events - .into_iter() - .filter_map(|e| match e.kind { - // This pretty horrible, but `Debug` is the only way to inspect - // QueryDescriptor at the moment. - salsa::EventKind::WillExecute { database_key } => { - Some(format!("{:?}", database_key)) - } - _ => None, - }) - .collect() - } -} diff --git a/crates/ra_hir/src/test_db.rs b/crates/ra_hir/src/test_db.rs new file mode 100644 index 000000000..047a27aaf --- /dev/null +++ b/crates/ra_hir/src/test_db.rs @@ -0,0 +1,131 @@ +//! FIXME: write short doc here + +use std::{panic, sync::Arc}; + +use hir_def::{db::DefDatabase2, ModuleId}; +use hir_expand::diagnostics::DiagnosticSink; +use parking_lot::Mutex; +use ra_db::{ + salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath, SourceDatabase, + SourceRootId, +}; + +use crate::{db, debug::HirDebugHelper}; + +#[salsa::database( + ra_db::SourceDatabaseExtStorage, + ra_db::SourceDatabaseStorage, + db::InternDatabaseStorage, + db::AstDatabaseStorage, + db::DefDatabaseStorage, + db::DefDatabase2Storage, + db::HirDatabaseStorage +)] +#[derive(Debug)] +pub struct TestDB { + events: Mutex>>>, + runtime: salsa::Runtime, +} + +impl panic::RefUnwindSafe for TestDB {} + +impl FileLoader for TestDB { + fn file_text(&self, file_id: FileId) -> Arc { + FileLoaderDelegate(self).file_text(file_id) + } + fn resolve_relative_path( + &self, + anchor: FileId, + relative_path: &RelativePath, + ) -> Option { + FileLoaderDelegate(self).resolve_relative_path(anchor, relative_path) + } + fn relevant_crates(&self, file_id: FileId) -> Arc> { + FileLoaderDelegate(self).relevant_crates(file_id) + } +} + +// FIXME: improve `WithFixture` to bring useful hir debugging back +impl HirDebugHelper for TestDB { + fn crate_name(&self, _krate: CrateId) -> Option { + None + } + + fn file_path(&self, _file_id: FileId) -> Option { + None + } +} + +impl TestDB { + pub fn diagnostics(&self) -> String { + let mut buf = String::new(); + let crate_graph = self.crate_graph(); + for krate in crate_graph.iter().next() { + let crate_def_map = self.crate_def_map(krate); + for (module_id, _) in crate_def_map.modules.iter() { + let module_id = ModuleId { krate, module_id }; + let module = crate::Module::from(module_id); + module.diagnostics( + self, + &mut DiagnosticSink::new(|d| { + buf += &format!("{:?}: {}\n", d.syntax_node(self).text(), d.message()); + }), + ) + } + } + buf + } +} + +impl salsa::Database for TestDB { + fn salsa_runtime(&self) -> &salsa::Runtime { + &self.runtime + } + + fn salsa_event(&self, event: impl Fn() -> salsa::Event) { + let mut events = self.events.lock(); + if let Some(events) = &mut *events { + events.push(event()); + } + } +} + +impl Default for TestDB { + fn default() -> TestDB { + let mut db = TestDB { events: Default::default(), runtime: salsa::Runtime::default() }; + db.set_crate_graph(Default::default()); + db + } +} + +impl salsa::ParallelDatabase for TestDB { + fn snapshot(&self) -> salsa::Snapshot { + salsa::Snapshot::new(TestDB { + events: Default::default(), + runtime: self.runtime.snapshot(self), + }) + } +} + +impl TestDB { + pub fn log(&self, f: impl FnOnce()) -> Vec> { + *self.events.lock() = Some(Vec::new()); + f(); + self.events.lock().take().unwrap() + } + + pub fn log_executed(&self, f: impl FnOnce()) -> Vec { + let events = self.log(f); + events + .into_iter() + .filter_map(|e| match e.kind { + // This pretty horrible, but `Debug` is the only way to inspect + // QueryDescriptor at the moment. + salsa::EventKind::WillExecute { database_key } => { + Some(format!("{:?}", database_key)) + } + _ => None, + }) + .collect() + } +} diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 4af2fe0b4..e56b9356e 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -11,7 +11,7 @@ use ra_syntax::{ use test_utils::covers; use crate::{ - expr::BodySourceMap, mock::MockDatabase, ty::display::HirDisplay, ty::InferenceResult, + expr::BodySourceMap, test_db::TestDB, ty::display::HirDisplay, ty::InferenceResult, SourceAnalyzer, }; @@ -24,7 +24,7 @@ mod coercion; #[test] fn cfg_impl_block() { - let (db, pos) = MockDatabase::with_position( + let (db, pos) = TestDB::with_position( r#" //- /main.rs crate:main deps:foo cfg:test use foo::S as T; @@ -64,7 +64,7 @@ impl S { #[test] fn infer_await() { - let (db, pos) = MockDatabase::with_position( + let (db, pos) = TestDB::with_position( r#" //- /main.rs crate:main deps:std @@ -95,7 +95,7 @@ mod future { #[test] fn infer_box() { - let (db, pos) = MockDatabase::with_position( + let (db, pos) = TestDB::with_position( r#" //- /main.rs crate:main deps:std @@ -122,7 +122,7 @@ mod boxed { #[test] fn infer_adt_self() { - let (db, pos) = MockDatabase::with_position( + let (db, pos) = TestDB::with_position( r#" //- /main.rs enum Nat { Succ(Self), Demo(Nat), Zero } @@ -141,7 +141,7 @@ fn test() { #[test] fn infer_try() { - let (db, pos) = MockDatabase::with_position( + let (db, pos) = TestDB::with_position( r#" //- /main.rs crate:main deps:std @@ -181,7 +181,7 @@ mod result { #[test] fn infer_for_loop() { - let (db, pos) = MockDatabase::with_position( + let (db, pos) = TestDB::with_position( r#" //- /main.rs crate:main deps:std @@ -223,7 +223,7 @@ mod collections { #[test] fn infer_while_let() { covers!(infer_while_let); - let (db, pos) = MockDatabase::with_position( + let (db, pos) = TestDB::with_position( r#" //- /main.rs enum Option { Some(T), None } @@ -2484,7 +2484,7 @@ pub fn main_loop() { #[test] fn cross_crate_associated_method_call() { - let (db, pos) = MockDatabase::with_position( + let (db, pos) = TestDB::with_position( r#" //- /main.rs crate:main deps:other_crate fn test() { @@ -3378,7 +3378,7 @@ fn test() { S.foo()<|>; } #[test] fn infer_macro_with_dollar_crate_is_correct_in_expr() { - let (db, pos) = MockDatabase::with_position( + let (db, pos) = TestDB::with_position( r#" //- /main.rs crate:main deps:foo fn test() { @@ -3482,7 +3482,7 @@ fn test() { (&S).foo()<|>; } #[test] fn method_resolution_trait_from_prelude() { - let (db, pos) = MockDatabase::with_position( + let (db, pos) = TestDB::with_position( r#" //- /main.rs crate:main deps:other_crate struct S; @@ -4651,7 +4651,7 @@ fn test() where T: Trait, U: Trait { assert_eq!(t, "{unknown}"); } -fn type_at_pos(db: &MockDatabase, pos: FilePosition) -> String { +fn type_at_pos(db: &TestDB, pos: FilePosition) -> String { let file = db.parse(pos.file_id).ok().unwrap(); let expr = algo::find_node_at_offset::(file.syntax(), pos.offset).unwrap(); let analyzer = SourceAnalyzer::new(db, pos.file_id, expr.syntax(), Some(pos.offset)); @@ -4660,12 +4660,12 @@ fn type_at_pos(db: &MockDatabase, pos: FilePosition) -> String { } fn type_at(content: &str) -> String { - let (db, file_pos) = MockDatabase::with_position(content); + let (db, file_pos) = TestDB::with_position(content); type_at_pos(&db, file_pos) } fn infer(content: &str) -> String { - let (db, file_id) = MockDatabase::with_single_file(content); + let (db, file_id) = TestDB::with_single_file(content); let source_file = db.parse(file_id).ok().unwrap(); let mut acc = String::new(); @@ -4748,7 +4748,7 @@ fn ellipsize(mut text: String, max_len: usize) -> String { #[test] fn typing_whitespace_inside_a_function_should_not_invalidate_types() { - let (mut db, pos) = MockDatabase::with_position( + let (mut db, pos) = TestDB::with_position( " //- /lib.rs fn foo() -> i32 { @@ -4788,7 +4788,7 @@ fn typing_whitespace_inside_a_function_should_not_invalidate_types() { #[test] fn no_such_field_diagnostics() { - let diagnostics = MockDatabase::with_files( + let diagnostics = TestDB::with_files( r" //- /lib.rs struct S { foo: i32, bar: () } -- cgit v1.2.3 From 42370610ce03c1bb5893d9e29c2c215482e8d8ca Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 4 Nov 2019 22:28:47 +0300 Subject: Restore assists tests --- crates/ra_assists/src/doc_tests.rs | 3 ++- crates/ra_assists/src/lib.rs | 8 ++++--- crates/ra_assists/src/test_db.rs | 43 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 crates/ra_assists/src/test_db.rs (limited to 'crates') diff --git a/crates/ra_assists/src/doc_tests.rs b/crates/ra_assists/src/doc_tests.rs index 2500a5ab7..a8f8446cb 100644 --- a/crates/ra_assists/src/doc_tests.rs +++ b/crates/ra_assists/src/doc_tests.rs @@ -5,10 +5,11 @@ mod generated; -use hir::mock::TestDB; use ra_db::{fixture::WithFixture, FileRange}; use test_utils::{assert_eq_text, extract_range_or_offset}; +use crate::test_db::TestDB; + fn check(assist_id: &str, before: &str, after: &str) { let (selection, before) = extract_range_or_offset(before); let (db, file_id) = TestDB::with_single_file(&before); diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 85fb72395..39c1c283f 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -9,6 +9,8 @@ mod assist_ctx; mod marks; #[cfg(test)] mod doc_tests; +#[cfg(test)] +mod test_db; use hir::db::HirDatabase; use ra_db::FileRange; @@ -146,12 +148,11 @@ mod assists { #[cfg(test)] mod helpers { - use hir::mock::TestDB; use ra_db::{fixture::WithFixture, FileRange}; use ra_syntax::TextRange; use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range}; - use crate::{Assist, AssistCtx}; + use crate::{test_db::TestDB, Assist, AssistCtx}; pub(crate) fn check_assist( assist: fn(AssistCtx) -> Option, @@ -268,11 +269,12 @@ mod helpers { #[cfg(test)] mod tests { - use hir::mock::TestDB; use ra_db::{fixture::WithFixture, FileRange}; use ra_syntax::TextRange; use test_utils::{extract_offset, extract_range}; + use crate::test_db::TestDB; + #[test] fn assist_order_field_struct() { let before = "struct Foo { <|>bar: u32 }"; diff --git a/crates/ra_assists/src/test_db.rs b/crates/ra_assists/src/test_db.rs new file mode 100644 index 000000000..7b196561a --- /dev/null +++ b/crates/ra_assists/src/test_db.rs @@ -0,0 +1,43 @@ +use std::sync::Arc; + +use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath}; + +#[salsa::database( + ra_db::SourceDatabaseExtStorage, + ra_db::SourceDatabaseStorage, + hir::db::InternDatabaseStorage, + hir::db::AstDatabaseStorage, + hir::db::DefDatabaseStorage, + hir::db::DefDatabase2Storage, + hir::db::HirDatabaseStorage +)] +#[derive(Debug, Default)] +pub struct TestDB { + runtime: salsa::Runtime, +} + +impl salsa::Database for TestDB { + fn salsa_runtime(&self) -> &salsa::Runtime { + &self.runtime + } +} + +impl std::panic::RefUnwindSafe for TestDB {} + +impl FileLoader for TestDB { + fn file_text(&self, file_id: FileId) -> Arc { + FileLoaderDelegate(self).file_text(file_id) + } + fn resolve_relative_path( + &self, + anchor: FileId, + relative_path: &RelativePath, + ) -> Option { + FileLoaderDelegate(self).resolve_relative_path(anchor, relative_path) + } + fn relevant_crates(&self, file_id: FileId) -> Arc> { + FileLoaderDelegate(self).relevant_crates(file_id) + } +} + +impl hir::debug::HirDebugHelper for TestDB {} -- cgit v1.2.3 From fd7819c3c09161986c3e1398d546e7503886760b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 4 Nov 2019 22:29:51 +0300 Subject: Simplify --- crates/ra_hir/src/test_db.rs | 59 ++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 35 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/test_db.rs b/crates/ra_hir/src/test_db.rs index 047a27aaf..84a2bf02b 100644 --- a/crates/ra_hir/src/test_db.rs +++ b/crates/ra_hir/src/test_db.rs @@ -5,10 +5,7 @@ use std::{panic, sync::Arc}; use hir_def::{db::DefDatabase2, ModuleId}; use hir_expand::diagnostics::DiagnosticSink; use parking_lot::Mutex; -use ra_db::{ - salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath, SourceDatabase, - SourceRootId, -}; +use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath, SourceDatabase}; use crate::{db, debug::HirDebugHelper}; @@ -21,12 +18,34 @@ use crate::{db, debug::HirDebugHelper}; db::DefDatabase2Storage, db::HirDatabaseStorage )] -#[derive(Debug)] +#[derive(Debug, Default)] pub struct TestDB { events: Mutex>>>, runtime: salsa::Runtime, } +impl salsa::Database for TestDB { + fn salsa_runtime(&self) -> &salsa::Runtime { + &self.runtime + } + + fn salsa_event(&self, event: impl Fn() -> salsa::Event) { + let mut events = self.events.lock(); + if let Some(events) = &mut *events { + events.push(event()); + } + } +} + +impl salsa::ParallelDatabase for TestDB { + fn snapshot(&self) -> salsa::Snapshot { + salsa::Snapshot::new(TestDB { + events: Default::default(), + runtime: self.runtime.snapshot(self), + }) + } +} + impl panic::RefUnwindSafe for TestDB {} impl FileLoader for TestDB { @@ -77,36 +96,6 @@ impl TestDB { } } -impl salsa::Database for TestDB { - fn salsa_runtime(&self) -> &salsa::Runtime { - &self.runtime - } - - fn salsa_event(&self, event: impl Fn() -> salsa::Event) { - let mut events = self.events.lock(); - if let Some(events) = &mut *events { - events.push(event()); - } - } -} - -impl Default for TestDB { - fn default() -> TestDB { - let mut db = TestDB { events: Default::default(), runtime: salsa::Runtime::default() }; - db.set_crate_graph(Default::default()); - db - } -} - -impl salsa::ParallelDatabase for TestDB { - fn snapshot(&self) -> salsa::Snapshot { - salsa::Snapshot::new(TestDB { - events: Default::default(), - runtime: self.runtime.snapshot(self), - }) - } -} - impl TestDB { pub fn log(&self, f: impl FnOnce()) -> Vec> { *self.events.lock() = Some(Vec::new()); -- cgit v1.2.3 From 50364bd47815f4f9174d46ae33f44f7f56545b2d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 4 Nov 2019 22:33:21 +0300 Subject: Appease the linter by dummy doc comments --- crates/ra_assists/src/test_db.rs | 2 ++ crates/ra_hir/src/test_db.rs | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) (limited to 'crates') diff --git a/crates/ra_assists/src/test_db.rs b/crates/ra_assists/src/test_db.rs index 7b196561a..5be7383ed 100644 --- a/crates/ra_assists/src/test_db.rs +++ b/crates/ra_assists/src/test_db.rs @@ -1,3 +1,5 @@ +//! Database used for testing `ra_assists`. + use std::sync::Arc; use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath}; diff --git a/crates/ra_hir/src/test_db.rs b/crates/ra_hir/src/test_db.rs index 84a2bf02b..5237b303a 100644 --- a/crates/ra_hir/src/test_db.rs +++ b/crates/ra_hir/src/test_db.rs @@ -1,4 +1,4 @@ -//! FIXME: write short doc here +//! Database used for testing `hir`. use std::{panic, sync::Arc}; -- cgit v1.2.3 From 739babc39188c93eda82ce16a24680bbf2463fa1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 4 Nov 2019 23:02:35 +0300 Subject: Move Namespace enum closer to usage --- crates/ra_hir/src/code_model.rs | 4 ++-- crates/ra_hir/src/db.rs | 4 ++-- crates/ra_hir/src/lib.rs | 5 +---- crates/ra_hir/src/ty.rs | 2 +- crates/ra_hir/src/ty/infer/expr.rs | 6 +++--- crates/ra_hir/src/ty/infer/path.rs | 4 ++-- crates/ra_hir/src/ty/lower.rs | 15 +++++++++++++-- crates/ra_hir/src/ty/traits/chalk.rs | 7 +++++-- crates/ra_hir_def/src/nameres/per_ns.rs | 8 -------- 9 files changed, 29 insertions(+), 26 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 181c5d47a..4e273d9e4 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -30,8 +30,8 @@ use crate::{ impl_block::ImplBlock, resolve::{Resolver, Scope, TypeNs}, traits::TraitData, - ty::{InferenceResult, TraitRef}, - Either, HasSource, Name, ScopeDef, Ty, {ImportId, Namespace}, + ty::{InferenceResult, Namespace, TraitRef}, + Either, HasSource, ImportId, Name, ScopeDef, Ty, }; /// hir::Crate describes a single crate. It's the main interface with which diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index eb66325f7..11b3f94ae 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -14,10 +14,10 @@ use crate::{ traits::TraitData, ty::{ method_resolution::CrateImplBlocks, traits::Impl, CallableDef, FnSig, GenericPredicate, - InferenceResult, Substs, Ty, TypableDef, TypeCtor, + InferenceResult, Namespace, Substs, Ty, TypableDef, TypeCtor, }, type_alias::TypeAliasData, - Const, ConstData, Crate, DefWithBody, ExprScopes, FnData, Function, Module, Namespace, Static, + Const, ConstData, Crate, DefWithBody, ExprScopes, FnData, Function, Module, Static, StructField, Trait, TypeAlias, }; diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index da6aa2ed8..5f2a05e76 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -81,10 +81,7 @@ pub use crate::{ pub use hir_def::{ builtin_type::BuiltinType, - nameres::{ - per_ns::{Namespace, PerNs}, - raw::ImportId, - }, + nameres::{per_ns::PerNs, raw::ImportId}, path::{Path, PathKind}, type_ref::Mutability, }; diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index d1a9d7411..cd2ac0e8b 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -27,7 +27,7 @@ pub(crate) use infer::{infer_query, InferTy, InferenceResult}; pub use lower::CallableDef; pub(crate) use lower::{ callable_item_sig, generic_defaults_query, generic_predicates_for_param_query, - generic_predicates_query, type_for_def, type_for_field, TypableDef, + generic_predicates_query, type_for_def, type_for_field, Namespace, TypableDef, }; pub(crate) use traits::{InEnvironment, Obligation, ProjectionPredicate, TraitEnvironment}; diff --git a/crates/ra_hir/src/ty/infer/expr.rs b/crates/ra_hir/src/ty/infer/expr.rs index a09ef5c5d..4af1d65ee 100644 --- a/crates/ra_hir/src/ty/infer/expr.rs +++ b/crates/ra_hir/src/ty/infer/expr.rs @@ -12,10 +12,10 @@ use crate::{ expr::{self, Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, generics::{GenericParams, HasGenericParams}, ty::{ - autoderef, method_resolution, op, primitive, CallableDef, InferTy, Mutability, Obligation, - ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk, + autoderef, method_resolution, op, primitive, CallableDef, InferTy, Mutability, Namespace, + Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk, }, - Adt, Name, Namespace, + Adt, Name, }; impl<'a, D: HirDatabase> InferenceContext<'a, D> { diff --git a/crates/ra_hir/src/ty/infer/path.rs b/crates/ra_hir/src/ty/infer/path.rs index 59b7f7eb6..865ced5a1 100644 --- a/crates/ra_hir/src/ty/infer/path.rs +++ b/crates/ra_hir/src/ty/infer/path.rs @@ -6,8 +6,8 @@ use super::{ExprOrPatId, InferenceContext, TraitRef}; use crate::{ db::HirDatabase, resolve::{ResolveValueResult, Resolver, TypeNs, ValueNs}, - ty::{method_resolution, Substs, Ty, TypableDef, TypeWalk}, - AssocItem, Container, HasGenericParams, Name, Namespace, Path, + ty::{method_resolution, Namespace, Substs, Ty, TypableDef, TypeWalk}, + AssocItem, Container, HasGenericParams, Name, Path, }; impl<'a, D: HirDatabase> InferenceContext<'a, D> { diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index e29ab8492..d26b16cb2 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -29,10 +29,21 @@ use crate::{ Adt, }, util::make_mut_slice, - Const, Enum, EnumVariant, Function, ModuleDef, Namespace, Path, Static, Struct, StructField, - Trait, TypeAlias, Union, + Const, Enum, EnumVariant, Function, ModuleDef, Path, Static, Struct, StructField, Trait, + TypeAlias, Union, }; +// FIXME: this is only really used in `type_for_def`, which contains a bunch of +// impossible cases. Perhaps we should recombine `TypeableDef` and `Namespace` +// into a `AsTypeDef`, `AsValueDef` enums? +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum Namespace { + Types, + Values, + // Note that only type inference uses this enum, and it doesn't care about macros. + // Macro, +} + impl Ty { pub(crate) fn from_hir(db: &impl HirDatabase, resolver: &Resolver, type_ref: &TypeRef) -> Self { match type_ref { diff --git a/crates/ra_hir/src/ty/traits/chalk.rs b/crates/ra_hir/src/ty/traits/chalk.rs index 39ef92182..c694952f3 100644 --- a/crates/ra_hir/src/ty/traits/chalk.rs +++ b/crates/ra_hir/src/ty/traits/chalk.rs @@ -18,7 +18,10 @@ use crate::{ db::HirDatabase, generics::GenericDef, ty::display::HirDisplay, - ty::{ApplicationTy, GenericPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk}, + ty::{ + ApplicationTy, GenericPredicate, Namespace, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, + TypeWalk, + }, AssocItem, Crate, HasGenericParams, ImplBlock, Trait, TypeAlias, }; @@ -652,7 +655,7 @@ fn impl_block_datum( }) .filter_map(|t| { let assoc_ty = trait_.associated_type_by_name(db, &t.name(db))?; - let ty = db.type_for_def(t.into(), crate::Namespace::Types).subst(&bound_vars); + let ty = db.type_for_def(t.into(), Namespace::Types).subst(&bound_vars); Some(chalk_rust_ir::AssociatedTyValue { impl_id, associated_ty_id: assoc_ty.to_chalk(db), diff --git a/crates/ra_hir_def/src/nameres/per_ns.rs b/crates/ra_hir_def/src/nameres/per_ns.rs index 298b0b0c7..717ed1ef9 100644 --- a/crates/ra_hir_def/src/nameres/per_ns.rs +++ b/crates/ra_hir_def/src/nameres/per_ns.rs @@ -4,14 +4,6 @@ use hir_expand::MacroDefId; use crate::ModuleDefId; -#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub enum Namespace { - Types, - Values, - // Note that only type inference uses this enum, and it doesn't care about macros. - // Macro, -} - #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub struct PerNs { pub types: Option, -- cgit v1.2.3 From 93d9e8a06f94bb74a7cde6fb5e21addcd16518cb Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 4 Nov 2019 23:37:35 +0300 Subject: Reduce visibility --- crates/ra_hir_def/src/nameres.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index 55a747ee2..e1959cdb9 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs @@ -196,7 +196,7 @@ pub struct Resolution { } impl Resolution { - pub(crate) fn from_macro(macro_: MacroDefId) -> Self { + fn from_macro(macro_: MacroDefId) -> Self { Resolution { def: PerNs::macros(macro_), import: None } } } @@ -460,7 +460,7 @@ impl CrateDefMap { from_crate_root.or(from_extern_prelude) } - pub(crate) fn resolve_name_in_module( + fn resolve_name_in_module( &self, db: &impl DefDatabase2, module: CrateModuleId, -- cgit v1.2.3