From 6d104de15aee6a24a442871c59528c39d410c161 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 13 Jun 2021 16:42:34 +0300 Subject: internal: refactor unresolved import diagnostic --- crates/hir/src/diagnostics.rs | 27 ++------------------------- crates/hir/src/lib.rs | 5 ++++- 2 files changed, 6 insertions(+), 26 deletions(-) (limited to 'crates/hir') diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs index ec0a8fe41..70a4d000d 100644 --- a/crates/hir/src/diagnostics.rs +++ b/crates/hir/src/diagnostics.rs @@ -32,7 +32,7 @@ macro_rules! diagnostics { }; } -diagnostics![UnresolvedModule, UnresolvedExternCrate, MissingFields]; +diagnostics![UnresolvedModule, UnresolvedExternCrate, UnresolvedImport, MissingFields]; #[derive(Debug)] pub struct UnresolvedModule { @@ -47,30 +47,7 @@ pub struct UnresolvedExternCrate { #[derive(Debug)] pub struct UnresolvedImport { - pub file: HirFileId, - pub node: AstPtr, -} - -impl Diagnostic for UnresolvedImport { - fn code(&self) -> DiagnosticCode { - DiagnosticCode("unresolved-import") - } - fn message(&self) -> String { - "unresolved import".to_string() - } - fn display_source(&self) -> InFile { - InFile::new(self.file, self.node.clone().into()) - } - fn as_any(&self) -> &(dyn Any + Send + 'static) { - self - } - fn is_experimental(&self) -> bool { - // This currently results in false positives in the following cases: - // - `cfg_if!`-generated code in libstd (we don't load the sysroot correctly) - // - `core::arch` (we don't handle `#[path = "../"]` correctly) - // - proc macros and/or proc macro generated code - true - } + pub decl: InFile>, } // Diagnostic: unresolved-macro-call diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index f7883c469..d32246709 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -498,7 +498,10 @@ impl Module { let import = &item_tree[id.value]; let use_tree = import.use_tree_to_ast(db.upcast(), file_id, *index); - sink.push(UnresolvedImport { file: file_id, node: AstPtr::new(&use_tree) }); + acc.push( + UnresolvedImport { decl: InFile::new(file_id, AstPtr::new(&use_tree)) } + .into(), + ); } DefDiagnosticKind::UnconfiguredCode { ast, cfg, opts } => { -- cgit v1.2.3 From fa9ed4e0ce633e51d1411951bf044719e6837457 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 13 Jun 2021 17:06:36 +0300 Subject: internal: refactor unresolved macro call diagnostic --- crates/hir/src/diagnostics.rs | 35 +++++++++-------------------------- crates/hir/src/lib.rs | 22 ++++++++++------------ 2 files changed, 19 insertions(+), 38 deletions(-) (limited to 'crates/hir') diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs index 70a4d000d..718c86b3a 100644 --- a/crates/hir/src/diagnostics.rs +++ b/crates/hir/src/diagnostics.rs @@ -17,7 +17,7 @@ pub use crate::diagnostics_sink::{ }; macro_rules! diagnostics { - ($($diag:ident),*) => { + ($($diag:ident,)*) => { pub enum AnyDiagnostic {$( $diag(Box<$diag>), )*} @@ -32,7 +32,13 @@ macro_rules! diagnostics { }; } -diagnostics![UnresolvedModule, UnresolvedExternCrate, UnresolvedImport, MissingFields]; +diagnostics![ + UnresolvedModule, + UnresolvedExternCrate, + UnresolvedImport, + UnresolvedMacroCall, + MissingFields, +]; #[derive(Debug)] pub struct UnresolvedModule { @@ -50,35 +56,12 @@ pub struct UnresolvedImport { pub decl: InFile>, } -// Diagnostic: unresolved-macro-call -// -// This diagnostic is triggered if rust-analyzer is unable to resolve the path to a -// macro in a macro invocation. #[derive(Debug, Clone, Eq, PartialEq)] pub struct UnresolvedMacroCall { - pub file: HirFileId, - pub node: AstPtr, + pub macro_call: InFile>, pub path: ModPath, } -impl Diagnostic for UnresolvedMacroCall { - fn code(&self) -> DiagnosticCode { - DiagnosticCode("unresolved-macro-call") - } - fn message(&self) -> String { - format!("unresolved macro `{}!`", self.path) - } - fn display_source(&self) -> InFile { - InFile::new(self.file, self.node.clone().into()) - } - fn as_any(&self) -> &(dyn Any + Send + 'static) { - self - } - fn is_experimental(&self) -> bool { - true - } -} - // Diagnostic: inactive-code // // This diagnostic is shown for code with inactive `#[cfg]` attributes. diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index d32246709..0a9414013 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -580,11 +580,13 @@ impl Module { DefDiagnosticKind::UnresolvedMacroCall { ast, path } => { let node = ast.to_node(db.upcast()); - sink.push(UnresolvedMacroCall { - file: ast.file_id, - node: AstPtr::new(&node), - path: path.clone(), - }); + acc.push( + UnresolvedMacroCall { + macro_call: InFile::new(ast.file_id, AstPtr::new(&node)), + path: path.clone(), + } + .into(), + ); } DefDiagnosticKind::MacroError { ast, message } => { @@ -1060,13 +1062,9 @@ impl Function { precise_location: None, macro_name: None, }), - BodyDiagnostic::UnresolvedMacroCall { node, path } => { - sink.push(UnresolvedMacroCall { - file: node.file_id, - node: node.value.clone(), - path: path.clone(), - }) - } + BodyDiagnostic::UnresolvedMacroCall { node, path } => acc.push( + UnresolvedMacroCall { macro_call: node.clone(), path: path.clone() }.into(), + ), } } -- cgit v1.2.3