From 8f93e7b9a4f99a458bf03c7ef9871060fa8f2cf9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 9 Jan 2019 00:48:54 +0300 Subject: remove duplication in tests --- crates/ra_hir/src/nameres/tests.rs | 88 ++++++++++++++------------------------ 1 file changed, 33 insertions(+), 55 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs index c511c40b2..ac96bea37 100644 --- a/crates/ra_hir/src/nameres/tests.rs +++ b/crates/ra_hir/src/nameres/tests.rs @@ -322,9 +322,33 @@ fn reexport_across_crates() { ); } +fn check_item_map_is_not_recomputed(initial: &str, file_change: &str) { + let (mut db, pos) = MockDatabase::with_position(initial); + let source_root = db.file_source_root(pos.file_id); + { + let events = db.log_executed(|| { + db.item_map(source_root).unwrap(); + }); + assert!(format!("{:?}", events).contains("item_map")) + } + db.query_mut(ra_db::FileTextQuery) + .set(pos.file_id, Arc::new(file_change.to_string())); + + { + let events = db.log_executed(|| { + db.item_map(source_root).unwrap(); + }); + assert!( + !format!("{:?}", events).contains("item_map"), + "{:#?}", + events + ) + } +} + #[test] fn typing_inside_a_function_should_not_invalidate_item_map() { - let (mut db, pos) = MockDatabase::with_position( + check_item_map_is_not_recomputed( " //- /lib.rs mod foo; @@ -342,42 +366,19 @@ fn typing_inside_a_function_should_not_invalidate_item_map() { } } ", - ); - let source_root = db.file_source_root(pos.file_id); - { - let events = db.log_executed(|| { - db.item_map(source_root).unwrap(); - }); - assert!(format!("{:?}", events).contains("item_map")) - } - - let new_text = " + " salsa::query_group! { trait Baz { fn foo() -> i32 { 92 } } } - " - .to_string(); - - db.query_mut(ra_db::FileTextQuery) - .set(pos.file_id, Arc::new(new_text)); - - { - let events = db.log_executed(|| { - db.item_map(source_root).unwrap(); - }); - assert!( - !format!("{:?}", events).contains("item_map"), - "{:#?}", - events - ) - } + ", + ); } #[test] fn typing_inside_a_function_inside_a_macro_should_not_invalidate_item_map() { - let (mut db, pos) = MockDatabase::with_position( + check_item_map_is_not_recomputed( " //- /lib.rs mod foo;<|> @@ -392,36 +393,13 @@ fn typing_inside_a_function_inside_a_macro_should_not_invalidate_item_map() { //- /foo/bar.rs pub struct Baz; - ", - ); - let source_root = db.file_source_root(pos.file_id); - { - let events = db.log_executed(|| { - db.item_map(source_root).unwrap(); - }); - assert!(format!("{:?}", events).contains("item_map")) - } - - let new_text = " + ", + " mod foo; use crate::foo::bar::Baz; fn foo() -> i32 { 92 } - " - .to_string(); - - db.query_mut(ra_db::FileTextQuery) - .set(pos.file_id, Arc::new(new_text)); - - { - let events = db.log_executed(|| { - db.item_map(source_root).unwrap(); - }); - assert!( - !format!("{:?}", events).contains("item_map"), - "{:#?}", - events - ) - } + ", + ); } -- cgit v1.2.3 From bcb6fabd15d2767c21b6a4f9d00389341ec6731e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 9 Jan 2019 00:49:21 +0300 Subject: fix test naming --- crates/ra_hir/src/nameres/tests.rs | 44 +++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 22 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs index ac96bea37..0e9026a01 100644 --- a/crates/ra_hir/src/nameres/tests.rs +++ b/crates/ra_hir/src/nameres/tests.rs @@ -351,27 +351,25 @@ fn typing_inside_a_function_should_not_invalidate_item_map() { check_item_map_is_not_recomputed( " //- /lib.rs - mod foo; + mod foo;<|> use crate::foo::bar::Baz; + fn foo() -> i32 { + 1 + 1 + } //- /foo/mod.rs pub mod bar; //- /foo/bar.rs - <|> - salsa::query_group! { - trait Baz { - fn foo() -> i32 { 1 + 1 } - } - } + pub struct Baz; ", " - salsa::query_group! { - trait Baz { - fn foo() -> i32 { 92 } - } - } + mod foo; + + use crate::foo::bar::Baz; + + fn foo() -> i32 { 92 } ", ); } @@ -381,25 +379,27 @@ fn typing_inside_a_function_inside_a_macro_should_not_invalidate_item_map() { check_item_map_is_not_recomputed( " //- /lib.rs - mod foo;<|> + mod foo; use crate::foo::bar::Baz; - fn foo() -> i32 { - 1 + 1 - } //- /foo/mod.rs pub mod bar; //- /foo/bar.rs - pub struct Baz; + <|> + salsa::query_group! { + trait Baz { + fn foo() -> i32 { 1 + 1 } + } + } ", " - mod foo; - - use crate::foo::bar::Baz; - - fn foo() -> i32 { 92 } + salsa::query_group! { + trait Baz { + fn foo() -> i32 { 92 } + } + } ", ); } -- cgit v1.2.3 From 2dc85619beaa3cd9fc47bb8ac1791820691e1205 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 9 Jan 2019 00:54:06 +0300 Subject: one more invalidation test --- crates/ra_hir/src/nameres/tests.rs | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs index 0e9026a01..a19a6fe51 100644 --- a/crates/ra_hir/src/nameres/tests.rs +++ b/crates/ra_hir/src/nameres/tests.rs @@ -368,12 +368,43 @@ fn typing_inside_a_function_should_not_invalidate_item_map() { mod foo; use crate::foo::bar::Baz; - +{ fn foo() -> i32 { 92 } ", ); } +#[test] +fn adding_inner_items_should_not_invalidate_item_map() { + check_item_map_is_not_recomputed( + " + //- /lib.rs + struct S { a: i32} + mod foo;<|> + enum E { A } + use crate::foo::bar::Baz; + trait T { + fn a() {} + } + //- /foo/mod.rs + pub mod bar; + + //- /foo/bar.rs + pub struct Baz; + ", + " + struct S { a: i32, b: () } + mod foo;<|> + enum E { A, B } + use crate::foo::bar::Baz; + trait T { + fn a() {} + fn b() {} + } + ", + ); +} + #[test] fn typing_inside_a_function_inside_a_macro_should_not_invalidate_item_map() { check_item_map_is_not_recomputed( -- cgit v1.2.3 From 5609989368e5cd5137e8860b7a78859b98e89085 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 9 Jan 2019 01:57:40 +0300 Subject: more stable DefIds via bfs tree walking --- crates/ra_hir/src/ids.rs | 21 +++++++++++++++++++-- crates/ra_hir/src/nameres/tests.rs | 17 ++++++++++++----- 2 files changed, 31 insertions(+), 7 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index 3db757778..8ac49eba3 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs @@ -253,13 +253,17 @@ impl SourceFileItems { } fn init(&mut self, source_file: &SourceFile) { - source_file.syntax().descendants().for_each(|it| { + // By walking the tree in bread-first order we make sure that parents + // get lower ids then children. That is, addding a new child does not + // change parent's id. This means that, say, adding a new function to a + // trait does not chage ids of top-level items, which helps caching. + bfs(source_file.syntax(), |it| { if let Some(module_item) = ast::ModuleItem::cast(it) { self.alloc(module_item.syntax().to_owned()); } else if let Some(macro_call) = ast::MacroCall::cast(it) { self.alloc(macro_call.syntax().to_owned()); } - }); + }) } fn alloc(&mut self, item: TreePtr) -> SourceFileItemId { @@ -305,3 +309,16 @@ impl std::ops::Index for SourceFileItems { &self.arena[idx] } } + +/// Walks the subtree in bfs order, calling `f` for each node. +fn bfs(node: &SyntaxNode, mut f: impl FnMut(&SyntaxNode)) { + let mut curr_layer = vec![node]; + let mut next_layer = vec![]; + while !curr_layer.is_empty() { + curr_layer.drain(..).for_each(|node| { + next_layer.extend(node.children()); + f(node); + }); + std::mem::swap(&mut curr_layer, &mut next_layer); + } +} diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs index a19a6fe51..17de54b44 100644 --- a/crates/ra_hir/src/nameres/tests.rs +++ b/crates/ra_hir/src/nameres/tests.rs @@ -368,7 +368,7 @@ fn typing_inside_a_function_should_not_invalidate_item_map() { mod foo; use crate::foo::bar::Baz; -{ + fn foo() -> i32 { 92 } ", ); @@ -380,12 +380,15 @@ fn adding_inner_items_should_not_invalidate_item_map() { " //- /lib.rs struct S { a: i32} - mod foo;<|> enum E { A } - use crate::foo::bar::Baz; trait T { fn a() {} } + mod foo;<|> + impl S { + fn a() {} + } + use crate::foo::bar::Baz; //- /foo/mod.rs pub mod bar; @@ -394,13 +397,17 @@ fn adding_inner_items_should_not_invalidate_item_map() { ", " struct S { a: i32, b: () } - mod foo;<|> enum E { A, B } - use crate::foo::bar::Baz; trait T { fn a() {} fn b() {} } + mod foo;<|> + impl S { + fn a() {} + fn b() {} + } + use crate::foo::bar::Baz; ", ); } -- cgit v1.2.3