From 0432aa0ed7be3f41d41928499abc688a956214cf Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 26 Nov 2020 20:09:54 +0100 Subject: Publish diagnostics for macro expansion errors --- crates/hir_def/src/nameres.rs | 52 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) (limited to 'crates/hir_def/src/nameres.rs') diff --git a/crates/hir_def/src/nameres.rs b/crates/hir_def/src/nameres.rs index 202a7dcb6..3d65a46bf 100644 --- a/crates/hir_def/src/nameres.rs +++ b/crates/hir_def/src/nameres.rs @@ -286,8 +286,8 @@ mod diagnostics { use cfg::{CfgExpr, CfgOptions}; use hir_expand::diagnostics::DiagnosticSink; use hir_expand::hygiene::Hygiene; - use hir_expand::InFile; - use syntax::{ast, AstPtr}; + use hir_expand::{InFile, MacroCallKind}; + use syntax::{ast, AstPtr, SyntaxNodePtr}; use crate::path::ModPath; use crate::{db::DefDatabase, diagnostics::*, nameres::LocalModuleId, AstId}; @@ -301,6 +301,10 @@ mod diagnostics { UnresolvedImport { ast: AstId, index: usize }, UnconfiguredCode { ast: AstId, cfg: CfgExpr, opts: CfgOptions }, + + UnresolvedProcMacro { ast: MacroCallKind }, + + MacroError { ast: MacroCallKind, message: String }, } #[derive(Debug, PartialEq, Eq)] @@ -348,6 +352,18 @@ mod diagnostics { Self { in_module: container, kind: DiagnosticKind::UnconfiguredCode { ast, cfg, opts } } } + pub(super) fn unresolved_proc_macro(container: LocalModuleId, ast: MacroCallKind) -> Self { + Self { in_module: container, kind: DiagnosticKind::UnresolvedProcMacro { ast } } + } + + pub(super) fn macro_error( + container: LocalModuleId, + ast: MacroCallKind, + message: String, + ) -> Self { + Self { in_module: container, kind: DiagnosticKind::MacroError { ast, message } } + } + pub(super) fn add_to( &self, db: &dyn DefDatabase, @@ -407,6 +423,38 @@ mod diagnostics { opts: opts.clone(), }); } + + DiagnosticKind::UnresolvedProcMacro { ast } => { + let (file, ast, name) = match ast { + MacroCallKind::FnLike(ast) => { + let node = ast.to_node(db.upcast()); + (ast.file_id, SyntaxNodePtr::from(AstPtr::new(&node)), None) + } + MacroCallKind::Attr(ast, name) => { + let node = ast.to_node(db.upcast()); + ( + ast.file_id, + SyntaxNodePtr::from(AstPtr::new(&node)), + Some(name.to_string()), + ) + } + }; + sink.push(UnresolvedProcMacro { file, node: ast, macro_name: name }); + } + + DiagnosticKind::MacroError { ast, message } => { + let (file, ast) = match ast { + MacroCallKind::FnLike(ast) => { + let node = ast.to_node(db.upcast()); + (ast.file_id, SyntaxNodePtr::from(AstPtr::new(&node))) + } + MacroCallKind::Attr(ast, _) => { + let node = ast.to_node(db.upcast()); + (ast.file_id, SyntaxNodePtr::from(AstPtr::new(&node))) + } + }; + sink.push(MacroError { file, node: ast, message: message.clone() }); + } } } } -- cgit v1.2.3