From 1e4aaee7bbc1d56698e70158aa35f578422623d9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 13 Jun 2021 17:51:44 +0300 Subject: internal: refactor unresolved proc macro diagnostic --- crates/hir/src/diagnostics.rs | 34 ++-------------------------------- crates/hir/src/lib.rs | 33 +++++++++++++++------------------ 2 files changed, 17 insertions(+), 50 deletions(-) (limited to 'crates/hir') diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs index 03e7f5e84..2039d2b43 100644 --- a/crates/hir/src/diagnostics.rs +++ b/crates/hir/src/diagnostics.rs @@ -36,6 +36,7 @@ diagnostics![ UnresolvedExternCrate, UnresolvedImport, UnresolvedMacroCall, + UnresolvedProcMacro, MissingFields, InactiveCode, ]; @@ -69,46 +70,15 @@ pub struct InactiveCode { pub opts: CfgOptions, } -// Diagnostic: unresolved-proc-macro -// -// This diagnostic is shown when a procedural macro can not be found. This usually means that -// procedural macro support is simply disabled (and hence is only a weak hint instead of an error), -// but can also indicate project setup problems. -// -// If you are seeing a lot of "proc macro not expanded" warnings, you can add this option to the -// `rust-analyzer.diagnostics.disabled` list to prevent them from showing. Alternatively you can -// enable support for procedural macros (see `rust-analyzer.procMacro.enable`). #[derive(Debug, Clone, Eq, PartialEq)] pub struct UnresolvedProcMacro { - pub file: HirFileId, - pub node: SyntaxNodePtr, + pub node: InFile, /// If the diagnostic can be pinpointed more accurately than via `node`, this is the `TextRange` /// to use instead. pub precise_location: Option, pub macro_name: Option, } -impl Diagnostic for UnresolvedProcMacro { - fn code(&self) -> DiagnosticCode { - DiagnosticCode("unresolved-proc-macro") - } - - fn message(&self) -> String { - match &self.macro_name { - Some(name) => format!("proc macro `{}` not expanded", name), - None => "proc macro not expanded".to_string(), - } - } - - fn display_source(&self) -> InFile { - InFile::new(self.file, self.node.clone()) - } - - fn as_any(&self) -> &(dyn Any + Send + 'static) { - self - } -} - // Diagnostic: macro-error // // This diagnostic is shown for macro expansion errors. diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index d59b52b25..87a3db946 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -518,10 +518,10 @@ impl Module { DefDiagnosticKind::UnresolvedProcMacro { ast } => { let mut precise_location = None; - let (file, ast, name) = match ast { + let (node, name) = match ast { MacroCallKind::FnLike { ast_id, .. } => { let node = ast_id.to_node(db.upcast()); - (ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node)), None) + (ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node))), None) } MacroCallKind::Derive { ast_id, derive_name, .. } => { let node = ast_id.to_node(db.upcast()); @@ -554,8 +554,7 @@ impl Module { } ( - ast_id.file_id, - SyntaxNodePtr::from(AstPtr::new(&node)), + ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node))), Some(derive_name.clone()), ) } @@ -566,18 +565,14 @@ impl Module { || panic!("cannot find attribute #{}", invoc_attr_index), ); ( - ast_id.file_id, - SyntaxNodePtr::from(AstPtr::new(&attr)), + ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&attr))), Some(attr_name.clone()), ) } }; - sink.push(UnresolvedProcMacro { - file, - node: ast, - precise_location, - macro_name: name, - }); + acc.push( + UnresolvedProcMacro { node, precise_location, macro_name: name }.into(), + ); } DefDiagnosticKind::UnresolvedMacroCall { ast, path } => { @@ -1056,12 +1051,14 @@ impl Function { node: node.value.clone().into(), message: message.to_string(), }), - BodyDiagnostic::UnresolvedProcMacro { node } => sink.push(UnresolvedProcMacro { - file: node.file_id, - node: node.value.clone().into(), - precise_location: None, - macro_name: None, - }), + BodyDiagnostic::UnresolvedProcMacro { node } => acc.push( + UnresolvedProcMacro { + node: node.clone().map(|it| it.into()), + precise_location: None, + macro_name: None, + } + .into(), + ), BodyDiagnostic::UnresolvedMacroCall { node, path } => acc.push( UnresolvedMacroCall { macro_call: node.clone(), path: path.clone() }.into(), ), -- cgit v1.2.3