From bd9f1f7eb78843ddd91d259a04e988b0681a5db4 Mon Sep 17 00:00:00 2001 From: Fedor Sakharov Date: Wed, 6 May 2020 17:17:35 +0300 Subject: Fix rename of enum variant visible from module --- crates/ra_hir/src/code_model.rs | 7 ++++ crates/ra_hir_def/src/adt.rs | 4 ++- crates/ra_ide/src/references/rename.rs | 62 ++++++++++++++++++++++++++++++++++ crates/ra_ide_db/src/defs.rs | 5 ++- 4 files changed, 76 insertions(+), 2 deletions(-) diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 5f480c304..7eba0b233 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -474,6 +474,13 @@ impl EnumVariant { } } +impl HasVisibility for EnumVariant { + fn visibility(&self, db: &dyn HirDatabase) -> Visibility { + let visibility = &db.enum_data(self.parent.id).visibility; + visibility.resolve(db.upcast(), &self.parent.id.resolver(db.upcast())) + } +} + /// A Data Type #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum Adt { diff --git a/crates/ra_hir_def/src/adt.rs b/crates/ra_hir_def/src/adt.rs index 2bc34d449..0fda4d6c9 100644 --- a/crates/ra_hir_def/src/adt.rs +++ b/crates/ra_hir_def/src/adt.rs @@ -33,6 +33,7 @@ pub struct StructData { #[derive(Debug, Clone, PartialEq, Eq)] pub struct EnumData { pub name: Name, + pub visibility: RawVisibility, pub variants: Arena, } @@ -91,7 +92,8 @@ impl EnumData { let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); let mut trace = Trace::new_for_arena(); lower_enum(db, &mut trace, &src, e.lookup(db).container.module(db)); - Arc::new(EnumData { name, variants: trace.into_arena() }) + let visibility = RawVisibility::from_ast(db, src.with_value(src.value.visibility())); + Arc::new(EnumData { name, visibility, variants: trace.into_arena() }) } pub fn variant(&self, name: &Name) -> Option { diff --git a/crates/ra_ide/src/references/rename.rs b/crates/ra_ide/src/references/rename.rs index 0398d53bc..2cbb82c1a 100644 --- a/crates/ra_ide/src/references/rename.rs +++ b/crates/ra_ide/src/references/rename.rs @@ -712,6 +712,68 @@ mod tests { "###); } + #[test] + fn test_enum_variant_from_module_1() { + test_rename( + r#" + mod foo { + pub enum Foo { + Bar<|>, + } + } + + fn func(f: foo::Foo) { + match f { + foo::Foo::Bar => {} + } + } + "#, + "Baz", + r#" + mod foo { + pub enum Foo { + Baz, + } + } + + fn func(f: foo::Foo) { + match f { + foo::Foo::Baz => {} + } + } + "#, + ); + } + + #[test] + fn test_enum_variant_from_module_2() { + test_rename( + r#" + mod foo { + pub struct Foo { + pub bar<|>: uint, + } + } + + fn foo(f: foo::Foo) { + let _ = f.bar; + } + "#, + "baz", + r#" + mod foo { + pub struct Foo { + pub baz: uint, + } + } + + fn foo(f: foo::Foo) { + let _ = f.baz; + } + "#, + ); + } + fn test_rename(text: &str, new_name: &str, expected: &str) { let (analysis, position) = single_file_with_position(text); let source_change = analysis.rename(position, new_name).unwrap(); diff --git a/crates/ra_ide_db/src/defs.rs b/crates/ra_ide_db/src/defs.rs index 40d0e77b5..2e2850efb 100644 --- a/crates/ra_ide_db/src/defs.rs +++ b/crates/ra_ide_db/src/defs.rs @@ -47,7 +47,10 @@ impl Definition { match self { Definition::Macro(_) => None, Definition::Field(sf) => Some(sf.visibility(db)), - Definition::ModuleDef(def) => module?.visibility_of(db, def), + Definition::ModuleDef(def) => match def { + ModuleDef::EnumVariant(id) => Some(id.visibility(db)), + _ => module?.visibility_of(db, def), + }, Definition::SelfType(_) => None, Definition::Local(_) => None, Definition::TypeParam(_) => None, -- cgit v1.2.3 From 210f0cbd27b0621c47e15c74bbb16ab47a642999 Mon Sep 17 00:00:00 2001 From: Fedor Sakharov Date: Thu, 7 May 2020 16:30:20 +0300 Subject: Remove HasVisibility implementation --- crates/ra_hir/src/code_model.rs | 7 ------- crates/ra_hir_def/src/adt.rs | 4 +--- crates/ra_ide_db/src/defs.rs | 7 +++++-- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 7eba0b233..5f480c304 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -474,13 +474,6 @@ impl EnumVariant { } } -impl HasVisibility for EnumVariant { - fn visibility(&self, db: &dyn HirDatabase) -> Visibility { - let visibility = &db.enum_data(self.parent.id).visibility; - visibility.resolve(db.upcast(), &self.parent.id.resolver(db.upcast())) - } -} - /// A Data Type #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum Adt { diff --git a/crates/ra_hir_def/src/adt.rs b/crates/ra_hir_def/src/adt.rs index 0fda4d6c9..2bc34d449 100644 --- a/crates/ra_hir_def/src/adt.rs +++ b/crates/ra_hir_def/src/adt.rs @@ -33,7 +33,6 @@ pub struct StructData { #[derive(Debug, Clone, PartialEq, Eq)] pub struct EnumData { pub name: Name, - pub visibility: RawVisibility, pub variants: Arena, } @@ -92,8 +91,7 @@ impl EnumData { let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); let mut trace = Trace::new_for_arena(); lower_enum(db, &mut trace, &src, e.lookup(db).container.module(db)); - let visibility = RawVisibility::from_ast(db, src.with_value(src.value.visibility())); - Arc::new(EnumData { name, visibility, variants: trace.into_arena() }) + Arc::new(EnumData { name, variants: trace.into_arena() }) } pub fn variant(&self, name: &Name) -> Option { diff --git a/crates/ra_ide_db/src/defs.rs b/crates/ra_ide_db/src/defs.rs index 2e2850efb..f990e3bb9 100644 --- a/crates/ra_ide_db/src/defs.rs +++ b/crates/ra_ide_db/src/defs.rs @@ -6,7 +6,7 @@ // FIXME: this badly needs rename/rewrite (matklad, 2020-02-06). use hir::{ - Field, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, Name, PathResolution, + Adt, Field, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, Name, PathResolution, Semantics, TypeParam, Visibility, }; use ra_prof::profile; @@ -48,7 +48,10 @@ impl Definition { Definition::Macro(_) => None, Definition::Field(sf) => Some(sf.visibility(db)), Definition::ModuleDef(def) => match def { - ModuleDef::EnumVariant(id) => Some(id.visibility(db)), + ModuleDef::EnumVariant(id) => { + let parent = id.parent_enum(db); + module?.visibility_of(db, &ModuleDef::Adt(Adt::Enum(parent))) + } _ => module?.visibility_of(db, def), }, Definition::SelfType(_) => None, -- cgit v1.2.3