Skip to content

Commit 12f6bdc

Browse files
author
Jonas Schievink
committed
Check ancestor maps when computing traits in scope
1 parent 6be4f30 commit 12f6bdc

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

crates/hir_def/src/resolver.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,16 @@ impl Resolver {
342342
traits.extend(prelude_def_map[prelude.local_id].scope.traits());
343343
}
344344
traits.extend(m.def_map[m.module_id].scope.traits());
345+
346+
// Add all traits that are in scope because of the containing DefMaps
347+
m.def_map.with_ancestor_maps(db, m.module_id, &mut |def_map, module| {
348+
if let Some(prelude) = def_map.prelude() {
349+
let prelude_def_map = prelude.def_map(db);
350+
traits.extend(prelude_def_map[prelude.local_id].scope.traits());
351+
}
352+
traits.extend(def_map[module].scope.traits());
353+
None::<()>
354+
});
345355
}
346356
}
347357
traits

crates/hir_ty/src/tests/traits.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3173,6 +3173,39 @@ fn f() {
31733173
);
31743174
}
31753175

3176+
#[test]
3177+
fn trait_in_scope_with_inner_item() {
3178+
check_infer(
3179+
r#"
3180+
mod m {
3181+
pub trait Tr {
3182+
fn method(&self) -> u8 { 0 }
3183+
}
3184+
3185+
impl Tr for () {}
3186+
}
3187+
3188+
use m::Tr;
3189+
3190+
fn f() {
3191+
fn inner() {
3192+
().method();
3193+
//^^^^^^^^^^^ u8
3194+
}
3195+
}
3196+
"#,
3197+
expect![[r#"
3198+
46..50 'self': &Self
3199+
58..63 '{ 0 }': u8
3200+
60..61 '0': u8
3201+
115..185 '{ ... } }': ()
3202+
132..183 '{ ... }': ()
3203+
142..144 '()': ()
3204+
142..153 '().method()': u8
3205+
"#]],
3206+
);
3207+
}
3208+
31763209
#[test]
31773210
fn inner_use_in_block() {
31783211
check_types(

0 commit comments

Comments
 (0)