From 39f190b72ccab9a166529f6ae1c8d5d562b15571 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 13 Jun 2021 16:05:43 +0300 Subject: internal: refactor unresolved extern crate diagnostic --- crates/hir/src/diagnostics.rs | 23 +--------- crates/hir/src/lib.rs | 10 +++-- crates/hir_def/src/nameres/tests/diagnostics.rs | 29 ------------- crates/ide/src/diagnostics.rs | 3 ++ .../ide/src/diagnostics/unresolved_extern_crate.rs | 49 ++++++++++++++++++++++ 5 files changed, 60 insertions(+), 54 deletions(-) create mode 100644 crates/ide/src/diagnostics/unresolved_extern_crate.rs 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 { }; } -diagnostics![UnresolvedModule, MissingFields]; +diagnostics![UnresolvedModule, UnresolvedExternCrate, MissingFields]; #[derive(Debug)] pub struct UnresolvedModule { @@ -40,28 +40,9 @@ pub struct UnresolvedModule { pub candidate: String, } -// Diagnostic: unresolved-extern-crate -// -// This diagnostic is triggered if rust-analyzer is unable to discover referred extern crate. #[derive(Debug)] pub struct UnresolvedExternCrate { - pub file: HirFileId, - pub item: AstPtr, -} - -impl Diagnostic for UnresolvedExternCrate { - fn code(&self) -> DiagnosticCode { - DiagnosticCode("unresolved-extern-crate") - } - fn message(&self) -> String { - "unresolved extern crate".to_string() - } - fn display_source(&self) -> InFile { - InFile::new(self.file, self.item.clone().into()) - } - fn as_any(&self) -> &(dyn Any + Send + 'static) { - self - } + pub decl: InFile>, } #[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 { } DefDiagnosticKind::UnresolvedExternCrate { ast } => { let item = ast.to_node(db.upcast()); - sink.push(UnresolvedExternCrate { - file: ast.file_id, - item: AstPtr::new(&item), - }); + acc.push( + UnresolvedExternCrate { + decl: InFile::new(ast.file_id, AstPtr::new(&item)), + } + .into(), + ); } 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 @@ -25,35 +25,6 @@ fn unresolved_import() { ); } -#[test] -fn unresolved_extern_crate() { - check_diagnostics( - r" - //- /main.rs crate:main deps:core - extern crate core; - extern crate doesnotexist; - //^^^^^^^^^^^^^^^^^^^^^^^^^^ UnresolvedExternCrate - //- /lib.rs crate:core - ", - ); -} - -#[test] -fn extern_crate_self_as() { - cov_mark::check!(extern_crate_self_as); - check_diagnostics( - r" - //- /lib.rs - extern crate doesnotexist; - //^^^^^^^^^^^^^^^^^^^^^^^^^^ UnresolvedExternCrate - // Should not error. - extern crate self as foo; - struct Foo; - use foo::Foo as Bar; - ", - ); -} - #[test] fn dedup_unresolved_import_from_unresolved_crate() { check_diagnostics( 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 @@ //! original files. So we need to map the ranges. mod unresolved_module; +mod unresolved_extern_crate; mod missing_fields; mod fixes; @@ -229,8 +230,10 @@ pub(crate) fn diagnostics( let ctx = DiagnosticsContext { config, sema, resolve }; for diag in diags { + #[rustfmt::skip] let d = match diag { AnyDiagnostic::UnresolvedModule(d) => unresolved_module::unresolved_module(&ctx, &d), + AnyDiagnostic::UnresolvedExternCrate(d) => unresolved_extern_crate::unresolved_extern_crate(&ctx, &d), AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d), }; 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 @@ +use crate::diagnostics::{Diagnostic, DiagnosticsContext}; + +// Diagnostic: unresolved-extern-crate +// +// This diagnostic is triggered if rust-analyzer is unable to discover referred extern crate. +pub(super) fn unresolved_extern_crate( + ctx: &DiagnosticsContext<'_>, + d: &hir::UnresolvedExternCrate, +) -> Diagnostic { + Diagnostic::new( + "unresolved-extern-crate", + "unresolved extern crate", + ctx.sema.diagnostics_display_range(d.decl.clone().map(|it| it.into())).range, + ) +} + +#[cfg(test)] +mod tests { + use crate::diagnostics::tests::check_diagnostics; + + #[test] + fn unresolved_extern_crate() { + check_diagnostics( + r#" +//- /main.rs crate:main deps:core +extern crate core; + extern crate doesnotexist; +//^^^^^^^^^^^^^^^^^^^^^^^^^^ unresolved extern crate +//- /lib.rs crate:core +"#, + ); + } + + #[test] + fn extern_crate_self_as() { + cov_mark::check!(extern_crate_self_as); + check_diagnostics( + r#" +//- /lib.rs + extern crate doesnotexist; +//^^^^^^^^^^^^^^^^^^^^^^^^^^ unresolved extern crate +// Should not error. +extern crate self as foo; +struct Foo; +use foo::Foo as Bar; +"#, + ); + } +} -- cgit v1.2.3