From 98f32c1de1fd0e7eada304bc039b143b6cd6db58 Mon Sep 17 00:00:00 2001 From: David Barsky Date: Tue, 3 Jun 2025 08:11:49 -0400 Subject: [PATCH] hir-ty: add incremental tests checking for `infer` invalidation --- crates/hir-ty/src/tests/incremental.rs | 253 +++++++++++++++++++++++++ 1 file changed, 253 insertions(+) diff --git a/crates/hir-ty/src/tests/incremental.rs b/crates/hir-ty/src/tests/incremental.rs index 48474d2d26..e8e3812c69 100644 --- a/crates/hir-ty/src/tests/incremental.rs +++ b/crates/hir-ty/src/tests/incremental.rs @@ -106,3 +106,256 @@ fn baz() -> i32 { assert_eq!(format!("{events:?}").matches("infer_shim").count(), 1, "{events:#?}") } } + +#[test] +fn adding_struct_invalidates_infer() { + let (mut db, pos) = TestDB::with_position( + " +//- /lib.rs +fn foo() -> i32 { + 1 + 1 +} + +fn bar() -> f32 { + 2.0 * 3.0 +} +$0", + ); + { + let events = db.log_executed(|| { + let module = db.module_for_file(pos.file_id.file_id(&db)); + let _crate_def_map = module.def_map(&db); + db.trait_impls_in_crate(module.krate()); + }); + assert!(format!("{events:?}").contains("trait_impls_in_crate_shim")) + } + + let new_text = " +fn foo() -> i32 { + 1 + 1 +} + +fn bar() -> f32 { + 2.0 * 3.0 +} + +pub struct NewStruct { + field: i32, +} +"; + + db.set_file_text(pos.file_id.file_id(&db), new_text); + + { + let actual = db.log_executed(|| { + let module = db.module_for_file(pos.file_id.file_id(&db)); + let _crate_def_map = module.def_map(&db); + db.trait_impls_in_crate(module.krate()); + }); + + let expected = vec![ + "parse_shim".to_owned(), + "ast_id_map_shim".to_owned(), + "file_item_tree_shim".to_owned(), + "real_span_map_shim".to_owned(), + "crate_local_def_map".to_owned(), + "trait_impls_in_crate_shim".to_owned(), + ]; + + assert_eq!(expected, actual); + } +} + +#[test] +fn adding_enum_query_log() { + let (mut db, pos) = TestDB::with_position( + " +//- /lib.rs +fn foo() -> i32 { + 1 + 1 +} + +fn bar() -> f32 { + 2.0 * 3.0 +} +$0", + ); + { + let events = db.log_executed(|| { + let module = db.module_for_file(pos.file_id.file_id(&db)); + let _crate_def_map = module.def_map(&db); + db.trait_impls_in_crate(module.krate()); + }); + assert!(format!("{events:?}").contains("trait_impls_in_crate_shim")) + } + + let new_text = " +fn foo() -> i32 { + 1 + 1 +} + +fn bar() -> f32 { + 2.0 * 3.0 +} + +pub enum SomeEnum { + A, + B +} +"; + + db.set_file_text(pos.file_id.file_id(&db), new_text); + + { + let actual = db.log_executed(|| { + let module = db.module_for_file(pos.file_id.file_id(&db)); + let _crate_def_map = module.def_map(&db); + db.trait_impls_in_crate(module.krate()); + }); + + let expected = vec![ + "parse_shim".to_owned(), + "ast_id_map_shim".to_owned(), + "file_item_tree_shim".to_owned(), + "real_span_map_shim".to_owned(), + "crate_local_def_map".to_owned(), + "trait_impls_in_crate_shim".to_owned(), + ]; + + assert_eq!(expected, actual); + } +} + +#[test] +fn adding_use_query_log() { + let (mut db, pos) = TestDB::with_position( + " +//- /lib.rs +fn foo() -> i32 { + 1 + 1 +} + +fn bar() -> f32 { + 2.0 * 3.0 +} +$0", + ); + { + let events = db.log_executed(|| { + let module = db.module_for_file(pos.file_id.file_id(&db)); + let _crate_def_map = module.def_map(&db); + db.trait_impls_in_crate(module.krate()); + }); + assert!(format!("{events:?}").contains("trait_impls_in_crate_shim")) + } + + let new_text = " +use std::collections::HashMap; + +fn foo() -> i32 { + 1 + 1 +} + +fn bar() -> f32 { + 2.0 * 3.0 +} +"; + + db.set_file_text(pos.file_id.file_id(&db), new_text); + + { + let actual = db.log_executed(|| { + let module = db.module_for_file(pos.file_id.file_id(&db)); + let _crate_def_map = module.def_map(&db); + db.trait_impls_in_crate(module.krate()); + }); + + let expected = vec![ + "parse_shim".to_owned(), + "ast_id_map_shim".to_owned(), + "file_item_tree_shim".to_owned(), + "real_span_map_shim".to_owned(), + "crate_local_def_map".to_owned(), + "trait_impls_in_crate_shim".to_owned(), + ]; + + assert_eq!(expected, actual); + } +} + +#[test] +fn adding_impl_query_log() { + let (mut db, pos) = TestDB::with_position( + " +//- /lib.rs +fn foo() -> i32 { + 1 + 1 +} + +fn bar() -> f32 { + 2.0 * 3.0 +} + +pub struct SomeStruct { + field: i32, +} +$0", + ); + { + let events = db.log_executed(|| { + let module = db.module_for_file(pos.file_id.file_id(&db)); + let _crate_def_map = module.def_map(&db); + db.trait_impls_in_crate(module.krate()); + }); + assert!(format!("{events:?}").contains("trait_impls_in_crate_shim")) + } + + let new_text = " +fn foo() -> i32 { + 1 + 1 +} + +fn bar() -> f32 { + 2.0 * 3.0 +} + +pub struct SomeStruct { + field: i32, +} + +impl SomeStruct { + pub fn new(value: i32) -> Self { + Self { field: value } + } +} +"; + + db.set_file_text(pos.file_id.file_id(&db), new_text); + + { + let actual = db.log_executed(|| { + let module = db.module_for_file(pos.file_id.file_id(&db)); + let _crate_def_map = module.def_map(&db); + db.trait_impls_in_crate(module.krate()); + }); + + let expected = vec![ + "parse_shim".to_owned(), + "ast_id_map_shim".to_owned(), + "file_item_tree_shim".to_owned(), + "real_span_map_shim".to_owned(), + "crate_local_def_map".to_owned(), + "trait_impls_in_crate_shim".to_owned(), + "attrs_shim".to_owned(), + "impl_trait_with_diagnostics_shim".to_owned(), + "impl_signature_shim".to_owned(), + "impl_signature_with_source_map_shim".to_owned(), + "impl_self_ty_with_diagnostics_shim".to_owned(), + "struct_signature_shim".to_owned(), + "struct_signature_with_source_map_shim".to_owned(), + "type_for_adt_tracked".to_owned(), + ]; + + assert_eq!(expected, actual); + } +}