4346: Fix rename of enum variant visible from module r=matklad a=montekki

Probably fixes #4237 

It looks like the ref is found correctly in this case but it's visibility is not correctly determined. I took a stab at fixing that by adding an implementation of `HasVisibility` for `EnumVariant` so it works more or less the same way it does for struct fields.

In other words, the `search_range` here does not contain the ref since it's not considered visible:

efd8e34c39/crates/ra_ide_db/src/search.rs (L209-L214)


Before that I tried to populate `ItemScope` with visible enum variants but that ended up with breaking tests all over the place and also it looked illogical in the end: `ItemScope` is not populated with, say, public struct fields and the same should be true for `enum` variants.

I've added two more or less identical tests: one for the case with a struct field rename and one for enum variant rename; the test for struct should probably be removed and the names should be changed.


Co-authored-by: Fedor Sakharov <fedor.sakharov@gmail.com>
This commit is contained in:
bors[bot] 2020-05-07 16:29:01 +00:00 committed by GitHub
commit fd84c31ff7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 70 additions and 2 deletions

View file

@ -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();

View file

@ -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;
@ -47,7 +47,13 @@ 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) => {
let parent = id.parent_enum(db);
module?.visibility_of(db, &ModuleDef::Adt(Adt::Enum(parent)))
}
_ => module?.visibility_of(db, def),
},
Definition::SelfType(_) => None,
Definition::Local(_) => None,
Definition::TypeParam(_) => None,