aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2021-06-13 14:05:43 +0100
committerAleksey Kladov <[email protected]>2021-06-13 14:05:43 +0100
commit39f190b72ccab9a166529f6ae1c8d5d562b15571 (patch)
treeb9035504de80a31b9071da6102a983e7ee260d18 /crates
parent6383252cc2770545505d40217732f14e93a396c4 (diff)
internal: refactor unresolved extern crate diagnostic
Diffstat (limited to 'crates')
-rw-r--r--crates/hir/src/diagnostics.rs23
-rw-r--r--crates/hir/src/lib.rs10
-rw-r--r--crates/hir_def/src/nameres/tests/diagnostics.rs29
-rw-r--r--crates/ide/src/diagnostics.rs3
-rw-r--r--crates/ide/src/diagnostics/unresolved_extern_crate.rs49
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
35diagnostics![UnresolvedModule, MissingFields]; 35diagnostics![UnresolvedModule, UnresolvedExternCrate, MissingFields];
36 36
37#[derive(Debug)] 37#[derive(Debug)]
38pub struct UnresolvedModule { 38pub 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)]
47pub struct UnresolvedExternCrate { 44pub struct UnresolvedExternCrate {
48 pub file: HirFileId, 45 pub decl: InFile<AstPtr<ast::ExternCrate>>,
49 pub item: AstPtr<ast::ExternCrate>,
50}
51
52impl 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]
29fn 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]
42fn 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]
58fn dedup_unresolved_import_from_unresolved_crate() { 29fn 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
7mod unresolved_module; 7mod unresolved_module;
8mod unresolved_extern_crate;
8mod missing_fields; 9mod missing_fields;
9 10
10mod fixes; 11mod 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 @@
1use 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.
6pub(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)]
18mod 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
26extern 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.
43extern crate self as foo;
44struct Foo;
45use foo::Foo as Bar;
46"#,
47 );
48 }
49}