mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 13:48:50 +00:00
Fix rename of enum variant visible from module
This commit is contained in:
parent
efd8e34c39
commit
bd9f1f7eb7
4 changed files with 76 additions and 2 deletions
|
@ -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 {
|
||||
|
|
|
@ -33,6 +33,7 @@ pub struct StructData {
|
|||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct EnumData {
|
||||
pub name: Name,
|
||||
pub visibility: RawVisibility,
|
||||
pub variants: Arena<EnumVariantData>,
|
||||
}
|
||||
|
||||
|
@ -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<LocalEnumVariantId> {
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue