From 38af81f183504145bd3bf6ba5bc40b3e41e04593 Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Tue, 23 Jun 2020 22:10:01 -0400 Subject: Account for updated module ids when determining whether a resolution is "changed" --- crates/ra_hir_def/src/item_scope.rs | 41 ++++++-- crates/ra_hir_def/src/nameres/tests/globs.rs | 146 +++++++++++++++++++++++++++ 2 files changed, 178 insertions(+), 9 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir_def/src/item_scope.rs b/crates/ra_hir_def/src/item_scope.rs index b03ba939a..571ba3b8a 100644 --- a/crates/ra_hir_def/src/item_scope.rs +++ b/crates/ra_hir_def/src/item_scope.rs @@ -126,17 +126,40 @@ impl ItemScope { let mut changed = false; let existing = self.visible.entry(name).or_default(); - if existing.types.is_none() && def.types.is_some() { - existing.types = def.types; - changed = true; + 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; + } + _ => {} } - if existing.values.is_none() && def.values.is_some() { - existing.values = def.values; - 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; + } + _ => {} } - if existing.macros.is_none() && def.macros.is_some() { - existing.macros = def.macros; - 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; + } + _ => {} } changed diff --git a/crates/ra_hir_def/src/nameres/tests/globs.rs b/crates/ra_hir_def/src/nameres/tests/globs.rs index 2b12c0daa..f0d85d301 100644 --- a/crates/ra_hir_def/src/nameres/tests/globs.rs +++ b/crates/ra_hir_def/src/nameres/tests/globs.rs @@ -229,3 +229,149 @@ fn glob_enum_group() { "### ); } + +#[test] +fn glob_shadowed_def() { + let db = TestDB::with_files( + r###" + //- /lib.rs + mod foo; + mod bar; + + use foo::*; + use bar::Baz; + + //- /foo.rs + pub struct Baz; + + //- /bar.rs + pub struct Baz; + "###, + ); + let krate = db.test_crate(); + + 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, + }, + ), + ] + "### + ); +} -- cgit v1.2.3 From c15c22139faacd41cc7e098a15aa6b820e0a20ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Wed, 24 Jun 2020 14:07:03 +0300 Subject: Make Debug less verbose for VfsPath and use Display in analysis-stats --- crates/rust-analyzer/src/cli/analysis_stats.rs | 2 +- crates/vfs/src/vfs_path.rs | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) (limited to 'crates') diff --git a/crates/rust-analyzer/src/cli/analysis_stats.rs b/crates/rust-analyzer/src/cli/analysis_stats.rs index 90868760b..2f4c29e06 100644 --- a/crates/rust-analyzer/src/cli/analysis_stats.rs +++ b/crates/rust-analyzer/src/cli/analysis_stats.rs @@ -121,7 +121,7 @@ pub fn analysis_stats( let original_file = src.file_id.original_file(db); let path = vfs.file_path(original_file); let syntax_range = src.value.syntax().text_range(); - format_to!(msg, " ({:?} {:?})", path, syntax_range); + format_to!(msg, " ({} {:?})", path, syntax_range); } if verbosity.is_spammy() { bar.println(msg.to_string()); diff --git a/crates/vfs/src/vfs_path.rs b/crates/vfs/src/vfs_path.rs index 0a8a86c62..940f91d0e 100644 --- a/crates/vfs/src/vfs_path.rs +++ b/crates/vfs/src/vfs_path.rs @@ -5,7 +5,7 @@ use paths::{AbsPath, AbsPathBuf}; /// Long-term, we want to support files which do not reside in the file-system, /// so we treat VfsPaths as opaque identifiers. -#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)] +#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)] pub struct VfsPath(VfsPathRepr); impl VfsPath { @@ -50,7 +50,7 @@ impl VfsPath { } } -#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)] +#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)] enum VfsPathRepr { PathBuf(AbsPathBuf), VirtualPath(VirtualPath), @@ -71,6 +71,21 @@ impl fmt::Display for VfsPath { } } +impl fmt::Debug for VfsPath { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Debug::fmt(&self.0, f) + } +} + +impl fmt::Debug for VfsPathRepr { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match &self { + VfsPathRepr::PathBuf(it) => fmt::Debug::fmt(&it.display(), f), + VfsPathRepr::VirtualPath(VirtualPath(it)) => fmt::Debug::fmt(&it, f), + } + } +} + #[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)] struct VirtualPath(String); -- cgit v1.2.3 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') 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 From 43c6f6c039985923c3039808834892206c81684d Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Wed, 24 Jun 2020 08:50:59 -0400 Subject: Fix formatting issue with glob shadow test --- crates/ra_hir_def/src/nameres/tests/globs.rs | 34 ++++++++++++++-------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir_def/src/nameres/tests/globs.rs b/crates/ra_hir_def/src/nameres/tests/globs.rs index f117b0362..2f440975a 100644 --- a/crates/ra_hir_def/src/nameres/tests/globs.rs +++ b/crates/ra_hir_def/src/nameres/tests/globs.rs @@ -256,23 +256,23 @@ fn glob_shadowed_def() { "###, ); 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 - - crate::foo::baz - Foo: t v + ⋮crate + ⋮Bar: t v + ⋮bar: t + ⋮baz: t + ⋮foo: t + ⋮ + ⋮crate::bar + ⋮baz: t + ⋮ + ⋮crate::bar::baz + ⋮Bar: t v + ⋮ + ⋮crate::foo + ⋮baz: t + ⋮ + ⋮crate::foo::baz + ⋮Foo: t v "### ); } -- cgit v1.2.3