From 00303284b5cc3a82e32dc3ecbbcfeb2f99de6818 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 13 Jun 2021 18:41:04 +0300 Subject: internal: refactor macro error --- crates/hir/src/diagnostics.rs | 26 ++------------------------ crates/hir/src/lib.rs | 20 +++++++++++--------- 2 files changed, 13 insertions(+), 33 deletions(-) (limited to 'crates/hir/src') diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs index 2039d2b43..28580eeb4 100644 --- a/crates/hir/src/diagnostics.rs +++ b/crates/hir/src/diagnostics.rs @@ -37,6 +37,7 @@ diagnostics![ UnresolvedImport, UnresolvedMacroCall, UnresolvedProcMacro, + MacroError, MissingFields, InactiveCode, ]; @@ -79,35 +80,12 @@ pub struct UnresolvedProcMacro { pub macro_name: Option, } -// Diagnostic: macro-error -// -// This diagnostic is shown for macro expansion errors. #[derive(Debug, Clone, Eq, PartialEq)] pub struct MacroError { - pub file: HirFileId, - pub node: SyntaxNodePtr, + pub node: InFile, pub message: String, } -impl Diagnostic for MacroError { - fn code(&self) -> DiagnosticCode { - DiagnosticCode("macro-error") - } - fn message(&self) -> String { - self.message.clone() - } - fn display_source(&self) -> InFile { - InFile::new(self.file, self.node.clone()) - } - fn as_any(&self) -> &(dyn Any + Send + 'static) { - self - } - fn is_experimental(&self) -> bool { - // Newly added and not very well-tested, might contain false positives. - true - } -} - #[derive(Debug)] pub struct UnimplementedBuiltinMacro { pub file: HirFileId, diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 87a3db946..d891d0ec1 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -587,19 +587,19 @@ impl Module { } DefDiagnosticKind::MacroError { ast, message } => { - let (file, ast) = match ast { + let node = match ast { MacroCallKind::FnLike { ast_id, .. } => { let node = ast_id.to_node(db.upcast()); - (ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node))) + ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node))) } MacroCallKind::Derive { ast_id, .. } | MacroCallKind::Attr { ast_id, .. } => { // FIXME: point to the attribute instead, this creates very large diagnostics let node = ast_id.to_node(db.upcast()); - (ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node))) + ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node))) } }; - sink.push(MacroError { file, node: ast, message: message.clone() }); + acc.push(MacroError { node, message: message.clone() }.into()); } DefDiagnosticKind::UnimplementedBuiltinMacro { ast } => { @@ -1046,11 +1046,13 @@ impl Function { InactiveCode { node: node.clone(), cfg: cfg.clone(), opts: opts.clone() } .into(), ), - BodyDiagnostic::MacroError { node, message } => sink.push(MacroError { - file: node.file_id, - node: node.value.clone().into(), - message: message.to_string(), - }), + BodyDiagnostic::MacroError { node, message } => acc.push( + MacroError { + node: node.clone().map(|it| it.into()), + message: message.to_string(), + } + .into(), + ), BodyDiagnostic::UnresolvedProcMacro { node } => acc.push( UnresolvedProcMacro { node: node.clone().map(|it| it.into()), -- cgit v1.2.3