aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-05-11 13:21:08 +0100
committerGitHub <[email protected]>2020-05-11 13:21:08 +0100
commit05399250d47cdceffbf1ded08983b13a9dcc87c1 (patch)
treec3165d3c3236893c23deb881a792c2c34be2e2bf
parenteb892d707c379eff514df9c2a6b2203f38874b14 (diff)
parent3d66aa054230ad788162ce49f0d334e900458cac (diff)
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 <[email protected]>
-rw-r--r--crates/ra_hir/src/code_model.rs20
-rw-r--r--crates/ra_ide/src/references.rs25
-rw-r--r--crates/ra_ide_db/src/defs.rs12
3 files changed, 47 insertions, 10 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 3fc2eccdd..e8e3211fc 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -148,6 +148,26 @@ impl ModuleDef {
148 ModuleDef::BuiltinType(_) => None, 148 ModuleDef::BuiltinType(_) => None,
149 } 149 }
150 } 150 }
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 }
151} 171}
152 172
153pub use hir_def::{ 173pub use hir_def::{
diff --git a/crates/ra_ide/src/references.rs b/crates/ra_ide/src/references.rs
index 555ccf295..074284b42 100644
--- a/crates/ra_ide/src/references.rs
+++ b/crates/ra_ide/src/references.rs
@@ -593,6 +593,31 @@ mod tests {
593 check_result(refs, "i BIND_PAT FileId(1) 36..37 Other", &["FileId(1) 51..52 Other Write"]); 593 check_result(refs, "i BIND_PAT FileId(1) 36..37 Other", &["FileId(1) 51..52 Other Write"]);
594 } 594 }
595 595
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
596 fn get_all_refs(text: &str) -> ReferenceSearchResult { 621 fn get_all_refs(text: &str) -> ReferenceSearchResult {
597 let (analysis, position) = single_file_with_position(text); 622 let (analysis, position) = single_file_with_position(text);
598 analysis.find_all_refs(position, None).unwrap().unwrap() 623 analysis.find_all_refs(position, None).unwrap().unwrap()
diff --git a/crates/ra_ide_db/src/defs.rs b/crates/ra_ide_db/src/defs.rs
index f990e3bb9..60c11178e 100644
--- a/crates/ra_ide_db/src/defs.rs
+++ b/crates/ra_ide_db/src/defs.rs
@@ -6,7 +6,7 @@
6// FIXME: this badly needs rename/rewrite (matklad, 2020-02-06). 6// FIXME: this badly needs rename/rewrite (matklad, 2020-02-06).
7 7
8use hir::{ 8use hir::{
9 Adt, Field, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, Name, PathResolution, 9 Field, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, Name, PathResolution,
10 Semantics, TypeParam, Visibility, 10 Semantics, TypeParam, Visibility,
11}; 11};
12use ra_prof::profile; 12use ra_prof::profile;
@@ -42,18 +42,10 @@ impl Definition {
42 } 42 }
43 43
44 pub fn visibility(&self, db: &RootDatabase) -> Option<Visibility> { 44 pub fn visibility(&self, db: &RootDatabase) -> Option<Visibility> {
45 let module = self.module(db);
46
47 match self { 45 match self {
48 Definition::Macro(_) => None, 46 Definition::Macro(_) => None,
49 Definition::Field(sf) => Some(sf.visibility(db)), 47 Definition::Field(sf) => Some(sf.visibility(db)),
50 Definition::ModuleDef(def) => match def { 48 Definition::ModuleDef(def) => def.definition_visibility(db),
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 },
57 Definition::SelfType(_) => None, 49 Definition::SelfType(_) => None,
58 Definition::Local(_) => None, 50 Definition::Local(_) => None,
59 Definition::TypeParam(_) => None, 51 Definition::TypeParam(_) => None,