Check ancestor maps when computing traits in scope

This commit is contained in:
Jonas Schievink 2021-03-09 18:18:35 +01:00
parent 6be4f30cae
commit 12f6bdcfd9
2 changed files with 43 additions and 0 deletions

View File

@ -342,6 +342,16 @@ pub fn traits_in_scope(&self, db: &dyn DefDatabase) -> FxHashSet<TraitId> {
traits.extend(prelude_def_map[prelude.local_id].scope.traits());
}
traits.extend(m.def_map[m.module_id].scope.traits());
// Add all traits that are in scope because of the containing DefMaps
m.def_map.with_ancestor_maps(db, m.module_id, &mut |def_map, module| {
if let Some(prelude) = def_map.prelude() {
let prelude_def_map = prelude.def_map(db);
traits.extend(prelude_def_map[prelude.local_id].scope.traits());
}
traits.extend(def_map[module].scope.traits());
None::<()>
});
}
}
traits

View File

@ -3173,6 +3173,39 @@ fn f() {
);
}
#[test]
fn trait_in_scope_with_inner_item() {
check_infer(
r#"
mod m {
pub trait Tr {
fn method(&self) -> u8 { 0 }
}
impl Tr for () {}
}
use m::Tr;
fn f() {
fn inner() {
().method();
//^^^^^^^^^^^ u8
}
}
"#,
expect![[r#"
46..50 'self': &Self
58..63 '{ 0 }': u8
60..61 '0': u8
115..185 '{ ... } }': ()
132..183 '{ ... }': ()
142..144 '()': ()
142..153 '().method()': u8
"#]],
);
}
#[test]
fn inner_use_in_block() {
check_types(