From 55f3ca2b7442761593e172e094a69660f4993b74 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Fri, 28 May 2021 00:46:05 +0200 Subject: Test that `ItemTree` works as intended --- crates/hir_def/src/nameres/tests/incremental.rs | 74 ++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) (limited to 'crates/hir_def') diff --git a/crates/hir_def/src/nameres/tests/incremental.rs b/crates/hir_def/src/nameres/tests/incremental.rs index d884a6eb4..7bf152e26 100644 --- a/crates/hir_def/src/nameres/tests/incremental.rs +++ b/crates/hir_def/src/nameres/tests/incremental.rs @@ -1,6 +1,8 @@ use std::sync::Arc; -use base_db::SourceDatabaseExt; +use base_db::{salsa::SweepStrategy, SourceDatabaseExt}; + +use crate::{AdtId, ModuleDefId}; use super::*; @@ -163,3 +165,73 @@ m!(Z); assert_eq!(n_reparsed_macros, 0); } } + +#[test] +fn item_tree_prevents_reparsing() { + // The `ItemTree` is used by both name resolution and the various queries in `adt.rs` and + // `data.rs`. After computing the `ItemTree` and deleting the parse tree, we should be able to + // run those other queries without triggering a reparse. + + let (db, pos) = TestDB::with_position( + r#" +pub struct S; +pub union U {} +pub enum E { + Variant, +} +pub fn f(_: S) { $0 } +pub trait Tr {} +impl Tr for () {} +pub const C: u8 = 0; +pub static ST: u8 = 0; +pub type Ty = (); +"#, + ); + let krate = db.test_crate(); + { + let events = db.log_executed(|| { + db.file_item_tree(pos.file_id.into()); + }); + let n_calculated_item_trees = events.iter().filter(|it| it.contains("item_tree")).count(); + assert_eq!(n_calculated_item_trees, 1); + let n_parsed_files = events.iter().filter(|it| it.contains("parse(")).count(); + assert_eq!(n_parsed_files, 1); + } + + // Delete the parse tree. + let sweep = SweepStrategy::default().discard_values().sweep_all_revisions(); + base_db::ParseQuery.in_db(&db).sweep(sweep); + + { + let events = db.log_executed(|| { + let crate_def_map = db.crate_def_map(krate); + let (_, module_data) = crate_def_map.modules.iter().last().unwrap(); + assert_eq!(module_data.scope.resolutions().count(), 8); + assert_eq!(module_data.scope.impls().count(), 1); + + for imp in module_data.scope.impls() { + db.impl_data(imp); + } + + for (_, res) in module_data.scope.resolutions() { + match res.values.or(res.types).unwrap().0 { + ModuleDefId::FunctionId(f) => drop(db.function_data(f)), + ModuleDefId::AdtId(adt) => match adt { + AdtId::StructId(it) => drop(db.struct_data(it)), + AdtId::UnionId(it) => drop(db.union_data(it)), + AdtId::EnumId(it) => drop(db.enum_data(it)), + }, + ModuleDefId::ConstId(it) => drop(db.const_data(it)), + ModuleDefId::StaticId(it) => drop(db.static_data(it)), + ModuleDefId::TraitId(it) => drop(db.trait_data(it)), + ModuleDefId::TypeAliasId(it) => drop(db.type_alias_data(it)), + ModuleDefId::EnumVariantId(_) + | ModuleDefId::ModuleId(_) + | ModuleDefId::BuiltinType(_) => unreachable!(), + } + } + }); + let n_reparsed_files = events.iter().filter(|it| it.contains("parse(")).count(); + assert_eq!(n_reparsed_files, 0); + } +} -- cgit v1.2.3