From ff858376aa4a974cda33a269b4c2d34cbda21bed Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Fri, 16 Apr 2021 15:48:03 +0200 Subject: Include path in `unresolved-macro-call` diagnostic --- crates/hir_def/src/body/lower.rs | 8 ++++++-- crates/hir_def/src/body/tests.rs | 2 +- crates/hir_def/src/diagnostics.rs | 5 +++-- crates/hir_def/src/lib.rs | 17 +++++++++++++---- crates/hir_def/src/nameres.rs | 13 +++++++++---- crates/hir_def/src/nameres/collector.rs | 9 +++++---- crates/hir_def/src/nameres/tests/diagnostics.rs | 4 ++-- 7 files changed, 39 insertions(+), 19 deletions(-) (limited to 'crates/hir_def') diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs index ed07d6928..c0b0b7841 100644 --- a/crates/hir_def/src/body/lower.rs +++ b/crates/hir_def/src/body/lower.rs @@ -568,9 +568,13 @@ impl ExprCollector<'_> { let res = match res { Ok(res) => res, - Err(UnresolvedMacro) => { + Err(UnresolvedMacro { path }) => { self.source_map.diagnostics.push(BodyDiagnostic::UnresolvedMacroCall( - UnresolvedMacroCall { file: outer_file, node: syntax_ptr.cast().unwrap() }, + UnresolvedMacroCall { + file: outer_file, + node: syntax_ptr.cast().unwrap(), + path, + }, )); collector(self, None); return; diff --git a/crates/hir_def/src/body/tests.rs b/crates/hir_def/src/body/tests.rs index c1d3e998f..63f5fe88d 100644 --- a/crates/hir_def/src/body/tests.rs +++ b/crates/hir_def/src/body/tests.rs @@ -180,7 +180,7 @@ fn unresolved_macro_diag() { r#" fn f() { m!(); - //^^^^ unresolved macro call + //^^^^ unresolved macro `m!` } "#, ); diff --git a/crates/hir_def/src/diagnostics.rs b/crates/hir_def/src/diagnostics.rs index 97abf8653..a71ae2668 100644 --- a/crates/hir_def/src/diagnostics.rs +++ b/crates/hir_def/src/diagnostics.rs @@ -8,7 +8,7 @@ use hir_expand::diagnostics::{Diagnostic, DiagnosticCode, DiagnosticSink}; use hir_expand::{HirFileId, InFile}; use syntax::{ast, AstPtr, SyntaxNodePtr, TextRange}; -use crate::{db::DefDatabase, DefWithBodyId}; +use crate::{db::DefDatabase, path::ModPath, DefWithBodyId}; pub fn validate_body(db: &dyn DefDatabase, owner: DefWithBodyId, sink: &mut DiagnosticSink<'_>) { let source_map = db.body_with_source_map(owner).1; @@ -103,6 +103,7 @@ impl Diagnostic for UnresolvedImport { pub struct UnresolvedMacroCall { pub file: HirFileId, pub node: AstPtr, + pub path: ModPath, } impl Diagnostic for UnresolvedMacroCall { @@ -110,7 +111,7 @@ impl Diagnostic for UnresolvedMacroCall { DiagnosticCode("unresolved-macro-call") } fn message(&self) -> String { - "unresolved macro call".to_string() + format!("unresolved macro `{}!`", self.path) } fn display_source(&self) -> InFile { InFile::new(self.file, self.node.clone().into()) diff --git a/crates/hir_def/src/lib.rs b/crates/hir_def/src/lib.rs index ffee05500..000567d99 100644 --- a/crates/hir_def/src/lib.rs +++ b/crates/hir_def/src/lib.rs @@ -66,6 +66,7 @@ use hir_expand::{ }; use la_arena::Idx; use nameres::DefMap; +use path::ModPath; use syntax::ast; use crate::builtin_type::BuiltinType; @@ -675,7 +676,9 @@ impl AstIdWithPath { } } -pub struct UnresolvedMacro; +pub struct UnresolvedMacro { + pub path: ModPath, +} fn macro_call_as_call_id( call: &AstIdWithPath, @@ -684,7 +687,8 @@ fn macro_call_as_call_id( resolver: impl Fn(path::ModPath) -> Option, error_sink: &mut dyn FnMut(mbe::ExpandError), ) -> Result, UnresolvedMacro> { - let def: MacroDefId = resolver(call.path.clone()).ok_or(UnresolvedMacro)?; + let def: MacroDefId = + resolver(call.path.clone()).ok_or_else(|| UnresolvedMacro { path: call.path.clone() })?; let res = if let MacroDefKind::BuiltInEager(..) = def.kind { let macro_call = InFile::new(call.ast_id.file_id, call.ast_id.to_node(db.upcast())); @@ -714,8 +718,13 @@ fn derive_macro_as_call_id( krate: CrateId, resolver: impl Fn(path::ModPath) -> Option, ) -> Result { - let def: MacroDefId = resolver(item_attr.path.clone()).ok_or(UnresolvedMacro)?; - let last_segment = item_attr.path.segments().last().ok_or(UnresolvedMacro)?; + let def: MacroDefId = resolver(item_attr.path.clone()) + .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?; + let last_segment = item_attr + .path + .segments() + .last() + .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?; let res = def .as_lazy_macro( db.upcast(), diff --git a/crates/hir_def/src/nameres.rs b/crates/hir_def/src/nameres.rs index 9e181751c..542f190a1 100644 --- a/crates/hir_def/src/nameres.rs +++ b/crates/hir_def/src/nameres.rs @@ -481,7 +481,7 @@ mod diagnostics { UnresolvedProcMacro { ast: MacroCallKind }, - UnresolvedMacroCall { ast: AstId }, + UnresolvedMacroCall { ast: AstId, path: ModPath }, MacroError { ast: MacroCallKind, message: String }, } @@ -546,8 +546,9 @@ mod diagnostics { pub(super) fn unresolved_macro_call( container: LocalModuleId, ast: AstId, + path: ModPath, ) -> Self { - Self { in_module: container, kind: DiagnosticKind::UnresolvedMacroCall { ast } } + Self { in_module: container, kind: DiagnosticKind::UnresolvedMacroCall { ast, path } } } pub(super) fn add_to( @@ -662,9 +663,13 @@ mod diagnostics { }); } - DiagnosticKind::UnresolvedMacroCall { ast } => { + DiagnosticKind::UnresolvedMacroCall { ast, path } => { let node = ast.to_node(db.upcast()); - sink.push(UnresolvedMacroCall { file: ast.file_id, node: AstPtr::new(&node) }); + sink.push(UnresolvedMacroCall { + file: ast.file_id, + node: AstPtr::new(&node), + path: path.clone(), + }); } DiagnosticKind::MacroError { ast, message } => { diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index fb4ddff5e..05ceb1efb 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs @@ -829,7 +829,7 @@ impl DefCollector<'_> { res = ReachedFixedPoint::No; return false; } - Err(UnresolvedMacro) | Ok(Err(_)) => {} + Err(UnresolvedMacro { .. }) | Ok(Err(_)) => {} } } MacroDirectiveKind::Derive { ast_id, derive_attr } => { @@ -845,7 +845,7 @@ impl DefCollector<'_> { res = ReachedFixedPoint::No; return false; } - Err(UnresolvedMacro) => (), + Err(UnresolvedMacro { .. }) => (), } } } @@ -943,10 +943,11 @@ impl DefCollector<'_> { &mut |_| (), ) { Ok(_) => (), - Err(UnresolvedMacro) => { + Err(UnresolvedMacro { path }) => { self.def_map.diagnostics.push(DefDiagnostic::unresolved_macro_call( directive.module_id, ast_id.ast_id, + path, )); } }, @@ -1530,7 +1531,7 @@ impl ModCollector<'_, '_> { )); return; } - Err(UnresolvedMacro) => (), + Err(UnresolvedMacro { .. }) => (), } // Case 2: resolve in module scope, expand during name resolution. diff --git a/crates/hir_def/src/nameres/tests/diagnostics.rs b/crates/hir_def/src/nameres/tests/diagnostics.rs index 1ac88fc89..543975e07 100644 --- a/crates/hir_def/src/nameres/tests/diagnostics.rs +++ b/crates/hir_def/src/nameres/tests/diagnostics.rs @@ -170,7 +170,7 @@ fn unresolved_legacy_scope_macro() { m!(); m2!(); - //^^^^^^ unresolved macro call + //^^^^^^ unresolved macro `self::m2!` "#, ); } @@ -187,7 +187,7 @@ fn unresolved_module_scope_macro() { self::m!(); self::m2!(); - //^^^^^^^^^^^^ unresolved macro call + //^^^^^^^^^^^^ unresolved macro `self::m2!` "#, ); } -- cgit v1.2.3