diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-06-13 14:06:27 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2021-06-13 14:06:27 +0100 |
commit | e6fa9b016fab4bf38f4e2a798fcabcc13b58e9ab (patch) | |
tree | 3e01ba8fb758b685abc4ea0fa162aa48aa81efd5 | |
parent | cc6d761a99ab3b7e28ed13ca3839358f3341da4d (diff) | |
parent | 39f190b72ccab9a166529f6ae1c8d5d562b15571 (diff) |
Merge #9247
9247: internal: refactor unresolved extern crate diagnostic r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
-rw-r--r-- | crates/hir/src/diagnostics.rs | 23 | ||||
-rw-r--r-- | crates/hir/src/lib.rs | 10 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/tests/diagnostics.rs | 29 | ||||
-rw-r--r-- | crates/ide/src/diagnostics.rs | 3 | ||||
-rw-r--r-- | crates/ide/src/diagnostics/unresolved_extern_crate.rs | 49 |
5 files changed, 60 insertions, 54 deletions
diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs index 158626dc0..ec0a8fe41 100644 --- a/crates/hir/src/diagnostics.rs +++ b/crates/hir/src/diagnostics.rs | |||
@@ -32,7 +32,7 @@ macro_rules! diagnostics { | |||
32 | }; | 32 | }; |
33 | } | 33 | } |
34 | 34 | ||
35 | diagnostics![UnresolvedModule, MissingFields]; | 35 | diagnostics![UnresolvedModule, UnresolvedExternCrate, MissingFields]; |
36 | 36 | ||
37 | #[derive(Debug)] | 37 | #[derive(Debug)] |
38 | pub struct UnresolvedModule { | 38 | pub struct UnresolvedModule { |
@@ -40,28 +40,9 @@ pub struct UnresolvedModule { | |||
40 | pub candidate: String, | 40 | pub candidate: String, |
41 | } | 41 | } |
42 | 42 | ||
43 | // Diagnostic: unresolved-extern-crate | ||
44 | // | ||
45 | // This diagnostic is triggered if rust-analyzer is unable to discover referred extern crate. | ||
46 | #[derive(Debug)] | 43 | #[derive(Debug)] |
47 | pub struct UnresolvedExternCrate { | 44 | pub struct UnresolvedExternCrate { |
48 | pub file: HirFileId, | 45 | pub decl: InFile<AstPtr<ast::ExternCrate>>, |
49 | pub item: AstPtr<ast::ExternCrate>, | ||
50 | } | ||
51 | |||
52 | impl Diagnostic for UnresolvedExternCrate { | ||
53 | fn code(&self) -> DiagnosticCode { | ||
54 | DiagnosticCode("unresolved-extern-crate") | ||
55 | } | ||
56 | fn message(&self) -> String { | ||
57 | "unresolved extern crate".to_string() | ||
58 | } | ||
59 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
60 | InFile::new(self.file, self.item.clone().into()) | ||
61 | } | ||
62 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
63 | self | ||
64 | } | ||
65 | } | 46 | } |
66 | 47 | ||
67 | #[derive(Debug)] | 48 | #[derive(Debug)] |
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 373134f62..f7883c469 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs | |||
@@ -484,10 +484,12 @@ impl Module { | |||
484 | } | 484 | } |
485 | DefDiagnosticKind::UnresolvedExternCrate { ast } => { | 485 | DefDiagnosticKind::UnresolvedExternCrate { ast } => { |
486 | let item = ast.to_node(db.upcast()); | 486 | let item = ast.to_node(db.upcast()); |
487 | sink.push(UnresolvedExternCrate { | 487 | acc.push( |
488 | file: ast.file_id, | 488 | UnresolvedExternCrate { |
489 | item: AstPtr::new(&item), | 489 | decl: InFile::new(ast.file_id, AstPtr::new(&item)), |
490 | }); | 490 | } |
491 | .into(), | ||
492 | ); | ||
491 | } | 493 | } |
492 | 494 | ||
493 | DefDiagnosticKind::UnresolvedImport { id, index } => { | 495 | DefDiagnosticKind::UnresolvedImport { id, index } => { |
diff --git a/crates/hir_def/src/nameres/tests/diagnostics.rs b/crates/hir_def/src/nameres/tests/diagnostics.rs index c82deca9c..84d6fdc93 100644 --- a/crates/hir_def/src/nameres/tests/diagnostics.rs +++ b/crates/hir_def/src/nameres/tests/diagnostics.rs | |||
@@ -26,35 +26,6 @@ fn unresolved_import() { | |||
26 | } | 26 | } |
27 | 27 | ||
28 | #[test] | 28 | #[test] |
29 | fn unresolved_extern_crate() { | ||
30 | check_diagnostics( | ||
31 | r" | ||
32 | //- /main.rs crate:main deps:core | ||
33 | extern crate core; | ||
34 | extern crate doesnotexist; | ||
35 | //^^^^^^^^^^^^^^^^^^^^^^^^^^ UnresolvedExternCrate | ||
36 | //- /lib.rs crate:core | ||
37 | ", | ||
38 | ); | ||
39 | } | ||
40 | |||
41 | #[test] | ||
42 | fn extern_crate_self_as() { | ||
43 | cov_mark::check!(extern_crate_self_as); | ||
44 | check_diagnostics( | ||
45 | r" | ||
46 | //- /lib.rs | ||
47 | extern crate doesnotexist; | ||
48 | //^^^^^^^^^^^^^^^^^^^^^^^^^^ UnresolvedExternCrate | ||
49 | // Should not error. | ||
50 | extern crate self as foo; | ||
51 | struct Foo; | ||
52 | use foo::Foo as Bar; | ||
53 | ", | ||
54 | ); | ||
55 | } | ||
56 | |||
57 | #[test] | ||
58 | fn dedup_unresolved_import_from_unresolved_crate() { | 29 | fn dedup_unresolved_import_from_unresolved_crate() { |
59 | check_diagnostics( | 30 | check_diagnostics( |
60 | r" | 31 | r" |
diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs index 3307e240b..1fbb7131d 100644 --- a/crates/ide/src/diagnostics.rs +++ b/crates/ide/src/diagnostics.rs | |||
@@ -5,6 +5,7 @@ | |||
5 | //! original files. So we need to map the ranges. | 5 | //! original files. So we need to map the ranges. |
6 | 6 | ||
7 | mod unresolved_module; | 7 | mod unresolved_module; |
8 | mod unresolved_extern_crate; | ||
8 | mod missing_fields; | 9 | mod missing_fields; |
9 | 10 | ||
10 | mod fixes; | 11 | mod fixes; |
@@ -229,8 +230,10 @@ pub(crate) fn diagnostics( | |||
229 | 230 | ||
230 | let ctx = DiagnosticsContext { config, sema, resolve }; | 231 | let ctx = DiagnosticsContext { config, sema, resolve }; |
231 | for diag in diags { | 232 | for diag in diags { |
233 | #[rustfmt::skip] | ||
232 | let d = match diag { | 234 | let d = match diag { |
233 | AnyDiagnostic::UnresolvedModule(d) => unresolved_module::unresolved_module(&ctx, &d), | 235 | AnyDiagnostic::UnresolvedModule(d) => unresolved_module::unresolved_module(&ctx, &d), |
236 | AnyDiagnostic::UnresolvedExternCrate(d) => unresolved_extern_crate::unresolved_extern_crate(&ctx, &d), | ||
234 | AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d), | 237 | AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d), |
235 | }; | 238 | }; |
236 | if let Some(code) = d.code { | 239 | if let Some(code) = d.code { |
diff --git a/crates/ide/src/diagnostics/unresolved_extern_crate.rs b/crates/ide/src/diagnostics/unresolved_extern_crate.rs new file mode 100644 index 000000000..2ea79c2ee --- /dev/null +++ b/crates/ide/src/diagnostics/unresolved_extern_crate.rs | |||
@@ -0,0 +1,49 @@ | |||
1 | use crate::diagnostics::{Diagnostic, DiagnosticsContext}; | ||
2 | |||
3 | // Diagnostic: unresolved-extern-crate | ||
4 | // | ||
5 | // This diagnostic is triggered if rust-analyzer is unable to discover referred extern crate. | ||
6 | pub(super) fn unresolved_extern_crate( | ||
7 | ctx: &DiagnosticsContext<'_>, | ||
8 | d: &hir::UnresolvedExternCrate, | ||
9 | ) -> Diagnostic { | ||
10 | Diagnostic::new( | ||
11 | "unresolved-extern-crate", | ||
12 | "unresolved extern crate", | ||
13 | ctx.sema.diagnostics_display_range(d.decl.clone().map(|it| it.into())).range, | ||
14 | ) | ||
15 | } | ||
16 | |||
17 | #[cfg(test)] | ||
18 | mod tests { | ||
19 | use crate::diagnostics::tests::check_diagnostics; | ||
20 | |||
21 | #[test] | ||
22 | fn unresolved_extern_crate() { | ||
23 | check_diagnostics( | ||
24 | r#" | ||
25 | //- /main.rs crate:main deps:core | ||
26 | extern crate core; | ||
27 | extern crate doesnotexist; | ||
28 | //^^^^^^^^^^^^^^^^^^^^^^^^^^ unresolved extern crate | ||
29 | //- /lib.rs crate:core | ||
30 | "#, | ||
31 | ); | ||
32 | } | ||
33 | |||
34 | #[test] | ||
35 | fn extern_crate_self_as() { | ||
36 | cov_mark::check!(extern_crate_self_as); | ||
37 | check_diagnostics( | ||
38 | r#" | ||
39 | //- /lib.rs | ||
40 | extern crate doesnotexist; | ||
41 | //^^^^^^^^^^^^^^^^^^^^^^^^^^ unresolved extern crate | ||
42 | // Should not error. | ||
43 | extern crate self as foo; | ||
44 | struct Foo; | ||
45 | use foo::Foo as Bar; | ||
46 | "#, | ||
47 | ); | ||
48 | } | ||
49 | } | ||