From 12f6bdcfd9fe1393887b3be0d0329fcf11492e75 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 9 Mar 2021 18:18:35 +0100 Subject: [PATCH] Check ancestor maps when computing traits in scope --- crates/hir_def/src/resolver.rs | 10 ++++++++++ crates/hir_ty/src/tests/traits.rs | 33 +++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/crates/hir_def/src/resolver.rs b/crates/hir_def/src/resolver.rs index 77ff21739d7..28b184f7cf6 100644 --- a/crates/hir_def/src/resolver.rs +++ b/crates/hir_def/src/resolver.rs @@ -342,6 +342,16 @@ impl Resolver { 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 diff --git a/crates/hir_ty/src/tests/traits.rs b/crates/hir_ty/src/tests/traits.rs index 52809208259..e185b1c0a16 100644 --- a/crates/hir_ty/src/tests/traits.rs +++ b/crates/hir_ty/src/tests/traits.rs @@ -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(