Skip to content

Commit 5b2c8bc

Browse files
Merge pull request #19975 from davidbarsky/davidbarsky/test-trait-solve-invalidation
hir-ty: test incremental trait solving
2 parents f7b7db8 + 210c71e commit 5b2c8bc

File tree

1 file changed

+87
-1
lines changed

1 file changed

+87
-1
lines changed

crates/hir-ty/src/tests/incremental.rs

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use base_db::SourceDatabase;
2-
use hir_def::ModuleDefId;
2+
use hir_def::{DefWithBodyId, ModuleDefId};
33
use test_fixture::WithFixture;
44

55
use crate::{db::HirDatabase, test_db::TestDB};
@@ -359,3 +359,89 @@ impl SomeStruct {
359359
assert_eq!(expected, actual);
360360
}
361361
}
362+
363+
#[test]
364+
fn add_struct_invalidates_trait_solve() {
365+
let (mut db, file_id) = TestDB::with_single_file(
366+
"
367+
//- /main.rs crate:main
368+
struct SomeStruct;
369+
370+
trait Trait<T> {
371+
fn method(&self) -> T;
372+
}
373+
impl Trait<u32> for SomeStruct {}
374+
375+
fn main() {
376+
let s = SomeStruct;
377+
s.method();
378+
s.$0
379+
}",
380+
);
381+
382+
{
383+
let events = db.log_executed(|| {
384+
let module = db.module_for_file(file_id.file_id(&db));
385+
let crate_def_map = module.def_map(&db);
386+
let mut defs: Vec<DefWithBodyId> = vec![];
387+
visit_module(&db, crate_def_map, module.local_id, &mut |it| {
388+
let def = match it {
389+
ModuleDefId::FunctionId(it) => it.into(),
390+
ModuleDefId::EnumVariantId(it) => it.into(),
391+
ModuleDefId::ConstId(it) => it.into(),
392+
ModuleDefId::StaticId(it) => it.into(),
393+
_ => return,
394+
};
395+
defs.push(def);
396+
});
397+
398+
for def in defs {
399+
let _inference_result = db.infer(def);
400+
}
401+
});
402+
assert!(format!("{events:?}").contains("trait_solve_shim"))
403+
}
404+
405+
let new_text = "
406+
//- /main.rs crate:main
407+
struct AnotherStruct;
408+
409+
struct SomeStruct;
410+
411+
trait Trait<T> {
412+
fn method(&self) -> T;
413+
}
414+
impl Trait<u32> for SomeStruct {}
415+
416+
fn main() {
417+
let s = SomeStruct;
418+
s.method();
419+
s.$0
420+
}";
421+
422+
db.set_file_text(file_id.file_id(&db), new_text);
423+
424+
{
425+
let events = db.log_executed(|| {
426+
let module = db.module_for_file(file_id.file_id(&db));
427+
let crate_def_map = module.def_map(&db);
428+
let mut defs: Vec<DefWithBodyId> = vec![];
429+
430+
visit_module(&db, crate_def_map, module.local_id, &mut |it| {
431+
let def = match it {
432+
ModuleDefId::FunctionId(it) => it.into(),
433+
ModuleDefId::EnumVariantId(it) => it.into(),
434+
ModuleDefId::ConstId(it) => it.into(),
435+
ModuleDefId::StaticId(it) => it.into(),
436+
_ => return,
437+
};
438+
defs.push(def);
439+
});
440+
441+
for def in defs {
442+
let _inference_result = db.infer(def);
443+
}
444+
});
445+
assert!(format!("{events:?}").contains("trait_solve_shim"))
446+
}
447+
}

0 commit comments

Comments
 (0)