aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_def/src/nameres
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_def/src/nameres')
-rw-r--r--crates/hir_def/src/nameres/collector.rs33
-rw-r--r--crates/hir_def/src/nameres/tests/diagnostics.rs45
2 files changed, 68 insertions, 10 deletions
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::{
13 builtin_macro::find_builtin_macro, 13 builtin_macro::find_builtin_macro,
14 name::{AsName, Name}, 14 name::{AsName, Name},
15 proc_macro::ProcMacroExpander, 15 proc_macro::ProcMacroExpander,
16 HirFileId, MacroCallId, MacroDefId, MacroDefKind, 16 HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
17}; 17};
18use hir_expand::{InFile, MacroCallLoc}; 18use hir_expand::{InFile, MacroCallLoc};
19use rustc_hash::{FxHashMap, FxHashSet}; 19use rustc_hash::{FxHashMap, FxHashSet};
@@ -1455,7 +1455,8 @@ impl ModCollector<'_, '_> {
1455 let mut ast_id = AstIdWithPath::new(self.file_id, mac.ast_id, mac.path.clone()); 1455 let mut ast_id = AstIdWithPath::new(self.file_id, mac.ast_id, mac.path.clone());
1456 1456
1457 // Case 1: try to resolve in legacy scope and expand macro_rules 1457 // Case 1: try to resolve in legacy scope and expand macro_rules
1458 if let Ok(Ok(macro_call_id)) = macro_call_as_call_id( 1458 let mut error = None;
1459 match macro_call_as_call_id(
1459 &ast_id, 1460 &ast_id,
1460 self.def_collector.db, 1461 self.def_collector.db,
1461 self.def_collector.def_map.krate, 1462 self.def_collector.def_map.krate,
@@ -1468,16 +1469,28 @@ impl ModCollector<'_, '_> {
1468 ) 1469 )
1469 }) 1470 })
1470 }, 1471 },
1471 &mut |_err| (), 1472 &mut |err| error = Some(err),
1472 ) { 1473 ) {
1473 self.def_collector.unexpanded_macros.push(MacroDirective { 1474 Ok(Ok(macro_call_id)) => {
1474 module_id: self.module_id, 1475 self.def_collector.unexpanded_macros.push(MacroDirective {
1475 ast_id, 1476 module_id: self.module_id,
1476 legacy: Some(macro_call_id), 1477 ast_id,
1477 depth: self.macro_depth + 1, 1478 legacy: Some(macro_call_id),
1478 }); 1479 depth: self.macro_depth + 1,
1480 });
1479 1481
1480 return; 1482 return;
1483 }
1484 Ok(Err(_)) => {
1485 // Built-in macro failed eager expansion.
1486 self.def_collector.def_map.diagnostics.push(DefDiagnostic::macro_error(
1487 self.module_id,
1488 MacroCallKind::FnLike(ast_id.ast_id),
1489 error.map(|e| e.to_string()).unwrap_or_else(|| String::from("macro error")),
1490 ));
1491 return;
1492 }
1493 Err(UnresolvedMacro) => (),
1481 } 1494 }
1482 1495
1483 // Case 2: resolve in module scope, expand during name resolution. 1496 // 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() {
152 "#, 152 "#,
153 ); 153 );
154} 154}
155
156#[test]
157fn unresolved_legacy_scope_macro() {
158 check_diagnostics(
159 r#"
160 //- /lib.rs
161 macro_rules! m { () => {} }
162
163 m!();
164 m2!();
165 //^^^^^^ unresolved macro call
166 "#,
167 );
168}
169
170#[test]
171fn unresolved_module_scope_macro() {
172 check_diagnostics(
173 r#"
174 //- /lib.rs
175 mod mac {
176 #[macro_export]
177 macro_rules! m { () => {} }
178 }
179
180 self::m!();
181 self::m2!();
182 //^^^^^^^^^^^^ unresolved macro call
183 "#,
184 );
185}
186
187#[test]
188fn builtin_macro_fails_expansion() {
189 check_diagnostics(
190 r#"
191 //- /lib.rs
192 #[rustc_builtin_macro]
193 macro_rules! include { () => {} }
194
195 include!("doesntexist");
196 //^^^^^^^^^^^^^^^^^^^^^^^^ could not convert tokens
197 "#,
198 );
199}