Skip to content

Commit 5b4589f

Browse files
Merge #9029
9029: minor: test that `ItemTree` makes `hir_def` queries syntax-independent r=jonas-schievink a=jonas-schievink bors r+ Co-authored-by: Jonas Schievink <[email protected]>
2 parents 01bfc5f + 55f3ca2 commit 5b4589f

File tree

3 files changed

+125
-45
lines changed

3 files changed

+125
-45
lines changed

crates/hir_def/src/nameres/tests/incremental.rs

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::sync::Arc;
22

3-
use base_db::SourceDatabaseExt;
3+
use base_db::{salsa::SweepStrategy, SourceDatabaseExt};
4+
5+
use crate::{AdtId, ModuleDefId};
46

57
use super::*;
68

@@ -163,3 +165,73 @@ m!(Z);
163165
assert_eq!(n_reparsed_macros, 0);
164166
}
165167
}
168+
169+
#[test]
170+
fn item_tree_prevents_reparsing() {
171+
// The `ItemTree` is used by both name resolution and the various queries in `adt.rs` and
172+
// `data.rs`. After computing the `ItemTree` and deleting the parse tree, we should be able to
173+
// run those other queries without triggering a reparse.
174+
175+
let (db, pos) = TestDB::with_position(
176+
r#"
177+
pub struct S;
178+
pub union U {}
179+
pub enum E {
180+
Variant,
181+
}
182+
pub fn f(_: S) { $0 }
183+
pub trait Tr {}
184+
impl Tr for () {}
185+
pub const C: u8 = 0;
186+
pub static ST: u8 = 0;
187+
pub type Ty = ();
188+
"#,
189+
);
190+
let krate = db.test_crate();
191+
{
192+
let events = db.log_executed(|| {
193+
db.file_item_tree(pos.file_id.into());
194+
});
195+
let n_calculated_item_trees = events.iter().filter(|it| it.contains("item_tree")).count();
196+
assert_eq!(n_calculated_item_trees, 1);
197+
let n_parsed_files = events.iter().filter(|it| it.contains("parse(")).count();
198+
assert_eq!(n_parsed_files, 1);
199+
}
200+
201+
// Delete the parse tree.
202+
let sweep = SweepStrategy::default().discard_values().sweep_all_revisions();
203+
base_db::ParseQuery.in_db(&db).sweep(sweep);
204+
205+
{
206+
let events = db.log_executed(|| {
207+
let crate_def_map = db.crate_def_map(krate);
208+
let (_, module_data) = crate_def_map.modules.iter().last().unwrap();
209+
assert_eq!(module_data.scope.resolutions().count(), 8);
210+
assert_eq!(module_data.scope.impls().count(), 1);
211+
212+
for imp in module_data.scope.impls() {
213+
db.impl_data(imp);
214+
}
215+
216+
for (_, res) in module_data.scope.resolutions() {
217+
match res.values.or(res.types).unwrap().0 {
218+
ModuleDefId::FunctionId(f) => drop(db.function_data(f)),
219+
ModuleDefId::AdtId(adt) => match adt {
220+
AdtId::StructId(it) => drop(db.struct_data(it)),
221+
AdtId::UnionId(it) => drop(db.union_data(it)),
222+
AdtId::EnumId(it) => drop(db.enum_data(it)),
223+
},
224+
ModuleDefId::ConstId(it) => drop(db.const_data(it)),
225+
ModuleDefId::StaticId(it) => drop(db.static_data(it)),
226+
ModuleDefId::TraitId(it) => drop(db.trait_data(it)),
227+
ModuleDefId::TypeAliasId(it) => drop(db.type_alias_data(it)),
228+
ModuleDefId::EnumVariantId(_)
229+
| ModuleDefId::ModuleId(_)
230+
| ModuleDefId::BuiltinType(_) => unreachable!(),
231+
}
232+
}
233+
});
234+
let n_reparsed_files = events.iter().filter(|it| it.contains("parse(")).count();
235+
assert_eq!(n_reparsed_files, 0);
236+
}
237+
}

crates/hir_ty/src/tests.rs

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod traits;
77
mod method_resolution;
88
mod macros;
99
mod display_source_code;
10+
mod incremental;
1011

1112
use std::{env, sync::Arc};
1213

@@ -317,50 +318,6 @@ fn ellipsize(mut text: String, max_len: usize) -> String {
317318
text
318319
}
319320

320-
#[test]
321-
fn typing_whitespace_inside_a_function_should_not_invalidate_types() {
322-
let (mut db, pos) = TestDB::with_position(
323-
"
324-
//- /lib.rs
325-
fn foo() -> i32 {
326-
$01 + 1
327-
}
328-
",
329-
);
330-
{
331-
let events = db.log_executed(|| {
332-
let module = db.module_for_file(pos.file_id);
333-
let crate_def_map = module.def_map(&db);
334-
visit_module(&db, &crate_def_map, module.local_id, &mut |def| {
335-
db.infer(def);
336-
});
337-
});
338-
assert!(format!("{:?}", events).contains("infer"))
339-
}
340-
341-
let new_text = "
342-
fn foo() -> i32 {
343-
1
344-
+
345-
1
346-
}
347-
"
348-
.to_string();
349-
350-
db.set_file_text(pos.file_id, Arc::new(new_text));
351-
352-
{
353-
let events = db.log_executed(|| {
354-
let module = db.module_for_file(pos.file_id);
355-
let crate_def_map = module.def_map(&db);
356-
visit_module(&db, &crate_def_map, module.local_id, &mut |def| {
357-
db.infer(def);
358-
});
359-
});
360-
assert!(!format!("{:?}", events).contains("infer"), "{:#?}", events)
361-
}
362-
}
363-
364321
fn check_infer(ra_fixture: &str, expect: Expect) {
365322
let mut actual = infer(ra_fixture);
366323
actual.push('\n');
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use std::sync::Arc;
2+
3+
use base_db::{fixture::WithFixture, SourceDatabaseExt};
4+
5+
use crate::{db::HirDatabase, test_db::TestDB};
6+
7+
use super::visit_module;
8+
9+
#[test]
10+
fn typing_whitespace_inside_a_function_should_not_invalidate_types() {
11+
let (mut db, pos) = TestDB::with_position(
12+
"
13+
//- /lib.rs
14+
fn foo() -> i32 {
15+
$01 + 1
16+
}
17+
",
18+
);
19+
{
20+
let events = db.log_executed(|| {
21+
let module = db.module_for_file(pos.file_id);
22+
let crate_def_map = module.def_map(&db);
23+
visit_module(&db, &crate_def_map, module.local_id, &mut |def| {
24+
db.infer(def);
25+
});
26+
});
27+
assert!(format!("{:?}", events).contains("infer"))
28+
}
29+
30+
let new_text = "
31+
fn foo() -> i32 {
32+
1
33+
+
34+
1
35+
}
36+
"
37+
.to_string();
38+
39+
db.set_file_text(pos.file_id, Arc::new(new_text));
40+
41+
{
42+
let events = db.log_executed(|| {
43+
let module = db.module_for_file(pos.file_id);
44+
let crate_def_map = module.def_map(&db);
45+
visit_module(&db, &crate_def_map, module.local_id, &mut |def| {
46+
db.infer(def);
47+
});
48+
});
49+
assert!(!format!("{:?}", events).contains("infer"), "{:#?}", events)
50+
}
51+
}

0 commit comments

Comments
 (0)