From 2df637f41900e50bbe47d2628554f7ffd167749a Mon Sep 17 00:00:00 2001 From: Brandon Date: Sat, 13 Mar 2021 21:28:10 -0800 Subject: Fix incorrect diagnositics for failing built in eager macros --- crates/hir_def/src/nameres/collector.rs | 33 ++++++++++++------ crates/hir_def/src/nameres/tests/diagnostics.rs | 45 +++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 10 deletions(-) (limited to 'crates') diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index 9ed48c506..9590b8ce3 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs @@ -13,7 +13,7 @@ use hir_expand::{ builtin_macro::find_builtin_macro, name::{AsName, Name}, proc_macro::ProcMacroExpander, - HirFileId, MacroCallId, MacroDefId, MacroDefKind, + HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, }; use hir_expand::{InFile, MacroCallLoc}; use rustc_hash::{FxHashMap, FxHashSet}; @@ -1455,7 +1455,8 @@ impl ModCollector<'_, '_> { let mut ast_id = AstIdWithPath::new(self.file_id, mac.ast_id, mac.path.clone()); // Case 1: try to resolve in legacy scope and expand macro_rules - if let Ok(Ok(macro_call_id)) = macro_call_as_call_id( + let mut error = None; + match macro_call_as_call_id( &ast_id, self.def_collector.db, self.def_collector.def_map.krate, @@ -1468,16 +1469,28 @@ impl ModCollector<'_, '_> { ) }) }, - &mut |_err| (), + &mut |err| error = Some(err), ) { - self.def_collector.unexpanded_macros.push(MacroDirective { - module_id: self.module_id, - ast_id, - legacy: Some(macro_call_id), - depth: self.macro_depth + 1, - }); + Ok(Ok(macro_call_id)) => { + self.def_collector.unexpanded_macros.push(MacroDirective { + module_id: self.module_id, + ast_id, + legacy: Some(macro_call_id), + depth: self.macro_depth + 1, + }); - return; + return; + } + Ok(Err(_)) => { + // Built-in macro failed eager expansion. + self.def_collector.def_map.diagnostics.push(DefDiagnostic::macro_error( + self.module_id, + MacroCallKind::FnLike(ast_id.ast_id), + error.map(|e| e.to_string()).unwrap_or_else(|| String::from("macro error")), + )); + return; + } + 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 d5ef8ceb5..bfc1ab13f 100644 --- a/crates/hir_def/src/nameres/tests/diagnostics.rs +++ b/crates/hir_def/src/nameres/tests/diagnostics.rs @@ -152,3 +152,48 @@ fn inactive_via_cfg_attr() { "#, ); } + +#[test] +fn unresolved_legacy_scope_macro() { + check_diagnostics( + r#" + //- /lib.rs + macro_rules! m { () => {} } + + m!(); + m2!(); + //^^^^^^ unresolved macro call + "#, + ); +} + +#[test] +fn unresolved_module_scope_macro() { + check_diagnostics( + r#" + //- /lib.rs + mod mac { + #[macro_export] + macro_rules! m { () => {} } + } + + self::m!(); + self::m2!(); + //^^^^^^^^^^^^ unresolved macro call + "#, + ); +} + +#[test] +fn builtin_macro_fails_expansion() { + check_diagnostics( + r#" + //- /lib.rs + #[rustc_builtin_macro] + macro_rules! include { () => {} } + + include!("doesntexist"); + //^^^^^^^^^^^^^^^^^^^^^^^^ could not convert tokens + "#, + ); +} -- cgit v1.2.3