Skip to content

Commit 0539925

Browse files
bors[bot]montekki
andauthored
Merge #4421
4421: Find references to a function outside module r=flodiebold a=montekki Fixes #4188 Yet again, it looks like although the code in https://github.com/rust-analyzer/rust-analyzer/blob/da1f316b0246ce41d7cb8560181e294089f06ef3/crates/ra_ide_db/src/search.rs#L128-L132 may be wrong, it is not hit since the `vis` is `None` at this point. The fix is similar to the #4237 case: just add another special case to `Definition::visibility()`. Co-authored-by: Fedor Sakharov <fedor.sakharov@gmail.com>
2 parents eb892d7 + 3d66aa0 commit 0539925

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

crates/ra_hir/src/code_model.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,26 @@ impl ModuleDef {
148148
ModuleDef::BuiltinType(_) => None,
149149
}
150150
}
151+
152+
pub fn definition_visibility(&self, db: &dyn HirDatabase) -> Option<Visibility> {
153+
let module = match self {
154+
ModuleDef::Module(it) => it.parent(db)?,
155+
ModuleDef::Function(it) => return Some(it.visibility(db)),
156+
ModuleDef::Adt(it) => it.module(db),
157+
ModuleDef::EnumVariant(it) => {
158+
let parent = it.parent_enum(db);
159+
let module = it.module(db);
160+
return module.visibility_of(db, &ModuleDef::Adt(Adt::Enum(parent)));
161+
}
162+
ModuleDef::Const(it) => return Some(it.visibility(db)),
163+
ModuleDef::Static(it) => it.module(db),
164+
ModuleDef::Trait(it) => it.module(db),
165+
ModuleDef::TypeAlias(it) => return Some(it.visibility(db)),
166+
ModuleDef::BuiltinType(_) => return None,
167+
};
168+
169+
module.visibility_of(db, self)
170+
}
151171
}
152172

153173
pub use hir_def::{

crates/ra_ide/src/references.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,31 @@ mod tests {
593593
check_result(refs, "i BIND_PAT FileId(1) 36..37 Other", &["FileId(1) 51..52 Other Write"]);
594594
}
595595

596+
#[test]
597+
fn test_find_struct_function_refs_outside_module() {
598+
let code = r#"
599+
mod foo {
600+
pub struct Foo;
601+
602+
impl Foo {
603+
pub fn new<|>() -> Foo {
604+
Foo
605+
}
606+
}
607+
}
608+
609+
fn main() {
610+
let _f = foo::Foo::new();
611+
}"#;
612+
613+
let refs = get_all_refs(code);
614+
check_result(
615+
refs,
616+
"new FN_DEF FileId(1) 87..150 94..97 Other",
617+
&["FileId(1) 227..230 StructLiteral"],
618+
);
619+
}
620+
596621
fn get_all_refs(text: &str) -> ReferenceSearchResult {
597622
let (analysis, position) = single_file_with_position(text);
598623
analysis.find_all_refs(position, None).unwrap().unwrap()

crates/ra_ide_db/src/defs.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// FIXME: this badly needs rename/rewrite (matklad, 2020-02-06).
77

88
use hir::{
9-
Adt, Field, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, Name, PathResolution,
9+
Field, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, Name, PathResolution,
1010
Semantics, TypeParam, Visibility,
1111
};
1212
use ra_prof::profile;
@@ -42,18 +42,10 @@ impl Definition {
4242
}
4343

4444
pub fn visibility(&self, db: &RootDatabase) -> Option<Visibility> {
45-
let module = self.module(db);
46-
4745
match self {
4846
Definition::Macro(_) => None,
4947
Definition::Field(sf) => Some(sf.visibility(db)),
50-
Definition::ModuleDef(def) => match def {
51-
ModuleDef::EnumVariant(id) => {
52-
let parent = id.parent_enum(db);
53-
module?.visibility_of(db, &ModuleDef::Adt(Adt::Enum(parent)))
54-
}
55-
_ => module?.visibility_of(db, def),
56-
},
48+
Definition::ModuleDef(def) => def.definition_visibility(db),
5749
Definition::SelfType(_) => None,
5850
Definition::Local(_) => None,
5951
Definition::TypeParam(_) => None,

0 commit comments

Comments
 (0)