From dfd79907b80e1a93b93e99aaf4d7af2a84c1c59f Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Wed, 24 Jun 2020 08:20:41 -0400 Subject: Use a macro in push_res to simplify code, simplify glob_shadowed_def assertion --- crates/ra_hir_def/src/item_scope.rs | 52 ++++----- crates/ra_hir_def/src/nameres/tests/globs.rs | 155 +++++---------------------- 2 files changed, 47 insertions(+), 160 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/item_scope.rs b/crates/ra_hir_def/src/item_scope.rs index 571ba3b8a..c81b966c3 100644 --- a/crates/ra_hir_def/src/item_scope.rs +++ b/crates/ra_hir_def/src/item_scope.rs @@ -5,6 +5,7 @@ use hir_expand::name::Name; use once_cell::sync::Lazy; use ra_db::CrateId; use rustc_hash::FxHashMap; +use test_utils::mark; use crate::{ db::DefDatabase, per_ns::PerNs, visibility::Visibility, AdtId, BuiltinType, HasModule, ImplId, @@ -126,41 +127,26 @@ impl ItemScope { let mut changed = false; let existing = self.visible.entry(name).or_default(); - match (existing.types, def.types) { - (None, Some(_)) => { - existing.types = def.types; - changed = true; - } - (Some(e), Some(d)) if e.0 != d.0 => { - existing.types = def.types; - changed = true; - } - _ => {} - } - - match (existing.values, def.values) { - (None, Some(_)) => { - existing.values = def.values; - changed = true; - } - (Some(e), Some(d)) if e.0 != d.0 => { - existing.values = def.values; - changed = true; - } - _ => {} + macro_rules! check_changed { + ($changed:ident, $existing:expr, $def:expr) => { + match ($existing, $def) { + (None, Some(_)) => { + $existing = $def; + $changed = true; + } + (Some(e), Some(d)) if e.0 != d.0 => { + mark::hit!(import_shadowed); + $existing = $def; + $changed = true; + } + _ => {} + } + }; } - match (existing.macros, def.macros) { - (None, Some(_)) => { - existing.macros = def.macros; - changed = true; - } - (Some(e), Some(d)) if e.0 != d.0 => { - existing.macros = def.macros; - changed = true; - } - _ => {} - } + check_changed!(changed, existing.types, def.types); + check_changed!(changed, existing.values, def.values); + check_changed!(changed, existing.macros, def.macros); changed } diff --git a/crates/ra_hir_def/src/nameres/tests/globs.rs b/crates/ra_hir_def/src/nameres/tests/globs.rs index f0d85d301..f117b0362 100644 --- a/crates/ra_hir_def/src/nameres/tests/globs.rs +++ b/crates/ra_hir_def/src/nameres/tests/globs.rs @@ -232,146 +232,47 @@ fn glob_enum_group() { #[test] fn glob_shadowed_def() { - let db = TestDB::with_files( + mark::check!(import_shadowed); + let map = def_map( r###" //- /lib.rs mod foo; mod bar; use foo::*; - use bar::Baz; + use bar::baz; + + use baz::Bar; //- /foo.rs - pub struct Baz; + pub mod baz { + pub struct Foo; + } //- /bar.rs - pub struct Baz; + pub mod baz { + pub struct Bar; + } "###, ); - let krate = db.test_crate(); + assert_snapshot!(map, @r###" + crate + Bar: t v + bar: t + baz: t + foo: t + + crate::bar + baz: t + + crate::bar::baz + Bar: t v + + crate::foo + baz: t - let crate_def_map = db.crate_def_map(krate); - let (_, root_module) = crate_def_map - .modules - .iter() - .find(|(_, module_data)| module_data.parent.is_none()) - .expect("Root module not found"); - let visible_entries = root_module.scope.entries().collect::>(); - insta::assert_debug_snapshot!( - visible_entries, - @r###" - [ - ( - Name( - Text( - "Baz", - ), - ), - PerNs { - types: Some( - ( - AdtId( - StructId( - StructId( - 1, - ), - ), - ), - Module( - ModuleId { - krate: CrateId( - 0, - ), - local_id: Idx::(0), - }, - ), - ), - ), - values: Some( - ( - AdtId( - StructId( - StructId( - 1, - ), - ), - ), - Module( - ModuleId { - krate: CrateId( - 0, - ), - local_id: Idx::(0), - }, - ), - ), - ), - macros: None, - }, - ), - ( - Name( - Text( - "bar", - ), - ), - PerNs { - types: Some( - ( - ModuleId( - ModuleId { - krate: CrateId( - 0, - ), - local_id: Idx::(2), - }, - ), - Module( - ModuleId { - krate: CrateId( - 0, - ), - local_id: Idx::(0), - }, - ), - ), - ), - values: None, - macros: None, - }, - ), - ( - Name( - Text( - "foo", - ), - ), - PerNs { - types: Some( - ( - ModuleId( - ModuleId { - krate: CrateId( - 0, - ), - local_id: Idx::(1), - }, - ), - Module( - ModuleId { - krate: CrateId( - 0, - ), - local_id: Idx::(0), - }, - ), - ), - ), - values: None, - macros: None, - }, - ), - ] + crate::foo::baz + Foo: t v "### ); } -- cgit v1.2.3