From 8f36f768e1ca2654da4c22983fe3c8bc3ad059a9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 29 Dec 2019 14:46:24 +0100 Subject: Don't add non-impl/trait containers to scope --- crates/ra_hir_def/src/resolver.rs | 2 +- crates/ra_hir_ty/src/tests.rs | 80 +++++++++++++++++++++++--------- crates/ra_hir_ty/src/tests/regression.rs | 36 +++++++++++++- crates/ra_hir_ty/src/tests/simple.rs | 3 ++ 4 files changed, 96 insertions(+), 25 deletions(-) diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs index 43dc751d9..5d16dd087 100644 --- a/crates/ra_hir_def/src/resolver.rs +++ b/crates/ra_hir_def/src/resolver.rs @@ -644,7 +644,7 @@ impl HasResolver for ContainerId { fn resolver(self, db: &impl DefDatabase) -> Resolver { match self { ContainerId::ModuleId(it) => it.resolver(db), - ContainerId::DefWithBodyId(it) => it.resolver(db), + ContainerId::DefWithBodyId(it) => it.module(db).resolver(db), } } } diff --git a/crates/ra_hir_ty/src/tests.rs b/crates/ra_hir_ty/src/tests.rs index d447b4571..d1f10e675 100644 --- a/crates/ra_hir_ty/src/tests.rs +++ b/crates/ra_hir_ty/src/tests.rs @@ -11,8 +11,8 @@ use std::fmt::Write; use std::sync::Arc; use hir_def::{ - body::BodySourceMap, child_by_source::ChildBySource, db::DefDatabase, keys, - nameres::CrateDefMap, AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId, + body::BodySourceMap, child_by_source::ChildBySource, db::DefDatabase, item_scope::ItemScope, + keys, nameres::CrateDefMap, AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId, }; use hir_expand::InFile; use insta::assert_snapshot; @@ -163,35 +163,69 @@ fn visit_module( module_id: LocalModuleId, cb: &mut dyn FnMut(DefWithBodyId), ) { - for decl in crate_def_map[module_id].scope.declarations() { - match decl { - ModuleDefId::FunctionId(it) => cb(it.into()), - ModuleDefId::ConstId(it) => cb(it.into()), - ModuleDefId::StaticId(it) => cb(it.into()), - ModuleDefId::TraitId(it) => { - let trait_data = db.trait_data(it); - for &(_, item) in trait_data.items.iter() { - match item { - AssocItemId::FunctionId(it) => cb(it.into()), - AssocItemId::ConstId(it) => cb(it.into()), - AssocItemId::TypeAliasId(_) => (), - } - } - } - ModuleDefId::ModuleId(it) => visit_module(db, crate_def_map, it.local_id, cb), - _ => (), - } - } + visit_scope(db, crate_def_map, &crate_def_map[module_id].scope, cb); for impl_id in crate_def_map[module_id].scope.impls() { let impl_data = db.impl_data(impl_id); for &item in impl_data.items.iter() { match item { - AssocItemId::FunctionId(it) => cb(it.into()), - AssocItemId::ConstId(it) => cb(it.into()), + AssocItemId::FunctionId(it) => { + let def = it.into(); + cb(def); + let body = db.body(def); + visit_scope(db, crate_def_map, &body.item_scope, cb); + } + AssocItemId::ConstId(it) => { + let def = it.into(); + cb(def); + let body = db.body(def); + visit_scope(db, crate_def_map, &body.item_scope, cb); + } AssocItemId::TypeAliasId(_) => (), } } } + + fn visit_scope( + db: &TestDB, + crate_def_map: &CrateDefMap, + scope: &ItemScope, + cb: &mut dyn FnMut(DefWithBodyId), + ) { + for decl in scope.declarations() { + match decl { + ModuleDefId::FunctionId(it) => { + let def = it.into(); + cb(def); + let body = db.body(def); + visit_scope(db, crate_def_map, &body.item_scope, cb); + } + ModuleDefId::ConstId(it) => { + let def = it.into(); + cb(def); + let body = db.body(def); + visit_scope(db, crate_def_map, &body.item_scope, cb); + } + ModuleDefId::StaticId(it) => { + let def = it.into(); + cb(def); + let body = db.body(def); + visit_scope(db, crate_def_map, &body.item_scope, cb); + } + ModuleDefId::TraitId(it) => { + let trait_data = db.trait_data(it); + for &(_, item) in trait_data.items.iter() { + match item { + AssocItemId::FunctionId(it) => cb(it.into()), + AssocItemId::ConstId(it) => cb(it.into()), + AssocItemId::TypeAliasId(_) => (), + } + } + } + ModuleDefId::ModuleId(it) => visit_module(db, crate_def_map, it.local_id, cb), + _ => (), + } + } + } } fn ellipsize(mut text: String, max_len: usize) -> String { diff --git a/crates/ra_hir_ty/src/tests/regression.rs b/crates/ra_hir_ty/src/tests/regression.rs index 09d684ac2..8b3aa8564 100644 --- a/crates/ra_hir_ty/src/tests/regression.rs +++ b/crates/ra_hir_ty/src/tests/regression.rs @@ -1,7 +1,8 @@ -use super::infer; use insta::assert_snapshot; use test_utils::covers; +use super::infer; + #[test] fn bug_484() { assert_snapshot!( @@ -331,3 +332,36 @@ pub fn main_loop() { "### ); } + +#[test] +fn issue_2669() { + assert_snapshot!( + infer( + r#"trait A {} + trait Write {} + struct Response {} + + trait D { + fn foo(); + } + + impl D for Response { + fn foo() { + end(); + fn end() { + let _x: T = loop {}; + } + } + }"# + ), + @r###" + [147; 262) '{ ... }': () + [161; 164) 'end': fn end<{unknown}>() -> () + [161; 166) 'end()': () + [199; 252) '{ ... }': () + [221; 223) '_x': ! + [230; 237) 'loop {}': ! + [235; 237) '{}': () + "### + ) +} diff --git a/crates/ra_hir_ty/src/tests/simple.rs b/crates/ra_hir_ty/src/tests/simple.rs index 3e5e163e3..00134c99b 100644 --- a/crates/ra_hir_ty/src/tests/simple.rs +++ b/crates/ra_hir_ty/src/tests/simple.rs @@ -1518,6 +1518,7 @@ fn test() { [167; 179) 'GLOBAL_CONST': u32 [189; 191) 'id': u32 [194; 210) 'Foo::A..._CONST': u32 + [126; 128) '99': u32 "### ); } @@ -1549,6 +1550,8 @@ fn test() { [233; 246) 'GLOBAL_STATIC': u32 [256; 257) 'w': u32 [260; 277) 'GLOBAL...IC_MUT': u32 + [118; 120) '99': u32 + [161; 163) '99': u32 "### ); } -- cgit v1.2.3