Skip to content

Commit 2009556

Browse files
Merge #8542
8542: Include path in `unresolved-macro-call` diagnostic r=matklad a=jonas-schievink Co-authored-by: Jonas Schievink <[email protected]>
2 parents 5274eb1 + ff85837 commit 2009556

File tree

8 files changed

+40
-20
lines changed

8 files changed

+40
-20
lines changed

crates/hir_def/src/body/lower.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,9 +568,13 @@ impl ExprCollector<'_> {
568568

569569
let res = match res {
570570
Ok(res) => res,
571-
Err(UnresolvedMacro) => {
571+
Err(UnresolvedMacro { path }) => {
572572
self.source_map.diagnostics.push(BodyDiagnostic::UnresolvedMacroCall(
573-
UnresolvedMacroCall { file: outer_file, node: syntax_ptr.cast().unwrap() },
573+
UnresolvedMacroCall {
574+
file: outer_file,
575+
node: syntax_ptr.cast().unwrap(),
576+
path,
577+
},
574578
));
575579
collector(self, None);
576580
return;

crates/hir_def/src/body/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ fn unresolved_macro_diag() {
180180
r#"
181181
fn f() {
182182
m!();
183-
//^^^^ unresolved macro call
183+
//^^^^ unresolved macro `m!`
184184
}
185185
"#,
186186
);

crates/hir_def/src/diagnostics.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use hir_expand::diagnostics::{Diagnostic, DiagnosticCode, DiagnosticSink};
88
use hir_expand::{HirFileId, InFile};
99
use syntax::{ast, AstPtr, SyntaxNodePtr, TextRange};
1010

11-
use crate::{db::DefDatabase, DefWithBodyId};
11+
use crate::{db::DefDatabase, path::ModPath, DefWithBodyId};
1212

1313
pub fn validate_body(db: &dyn DefDatabase, owner: DefWithBodyId, sink: &mut DiagnosticSink<'_>) {
1414
let source_map = db.body_with_source_map(owner).1;
@@ -103,14 +103,15 @@ impl Diagnostic for UnresolvedImport {
103103
pub struct UnresolvedMacroCall {
104104
pub file: HirFileId,
105105
pub node: AstPtr<ast::MacroCall>,
106+
pub path: ModPath,
106107
}
107108

108109
impl Diagnostic for UnresolvedMacroCall {
109110
fn code(&self) -> DiagnosticCode {
110111
DiagnosticCode("unresolved-macro-call")
111112
}
112113
fn message(&self) -> String {
113-
"unresolved macro call".to_string()
114+
format!("unresolved macro `{}!`", self.path)
114115
}
115116
fn display_source(&self) -> InFile<SyntaxNodePtr> {
116117
InFile::new(self.file, self.node.clone().into())

crates/hir_def/src/lib.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ use hir_expand::{
6666
};
6767
use la_arena::Idx;
6868
use nameres::DefMap;
69+
use path::ModPath;
6970
use syntax::ast;
7071

7172
use crate::builtin_type::BuiltinType;
@@ -675,7 +676,9 @@ impl<T: ast::AstNode> AstIdWithPath<T> {
675676
}
676677
}
677678

678-
pub struct UnresolvedMacro;
679+
pub struct UnresolvedMacro {
680+
pub path: ModPath,
681+
}
679682

680683
fn macro_call_as_call_id(
681684
call: &AstIdWithPath<ast::MacroCall>,
@@ -684,7 +687,8 @@ fn macro_call_as_call_id(
684687
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
685688
error_sink: &mut dyn FnMut(mbe::ExpandError),
686689
) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
687-
let def: MacroDefId = resolver(call.path.clone()).ok_or(UnresolvedMacro)?;
690+
let def: MacroDefId =
691+
resolver(call.path.clone()).ok_or_else(|| UnresolvedMacro { path: call.path.clone() })?;
688692

689693
let res = if let MacroDefKind::BuiltInEager(..) = def.kind {
690694
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(
714718
krate: CrateId,
715719
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
716720
) -> Result<MacroCallId, UnresolvedMacro> {
717-
let def: MacroDefId = resolver(item_attr.path.clone()).ok_or(UnresolvedMacro)?;
718-
let last_segment = item_attr.path.segments().last().ok_or(UnresolvedMacro)?;
721+
let def: MacroDefId = resolver(item_attr.path.clone())
722+
.ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
723+
let last_segment = item_attr
724+
.path
725+
.segments()
726+
.last()
727+
.ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
719728
let res = def
720729
.as_lazy_macro(
721730
db.upcast(),

crates/hir_def/src/nameres.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ mod diagnostics {
481481

482482
UnresolvedProcMacro { ast: MacroCallKind },
483483

484-
UnresolvedMacroCall { ast: AstId<ast::MacroCall> },
484+
UnresolvedMacroCall { ast: AstId<ast::MacroCall>, path: ModPath },
485485

486486
MacroError { ast: MacroCallKind, message: String },
487487
}
@@ -546,8 +546,9 @@ mod diagnostics {
546546
pub(super) fn unresolved_macro_call(
547547
container: LocalModuleId,
548548
ast: AstId<ast::MacroCall>,
549+
path: ModPath,
549550
) -> Self {
550-
Self { in_module: container, kind: DiagnosticKind::UnresolvedMacroCall { ast } }
551+
Self { in_module: container, kind: DiagnosticKind::UnresolvedMacroCall { ast, path } }
551552
}
552553

553554
pub(super) fn add_to(
@@ -662,9 +663,13 @@ mod diagnostics {
662663
});
663664
}
664665

665-
DiagnosticKind::UnresolvedMacroCall { ast } => {
666+
DiagnosticKind::UnresolvedMacroCall { ast, path } => {
666667
let node = ast.to_node(db.upcast());
667-
sink.push(UnresolvedMacroCall { file: ast.file_id, node: AstPtr::new(&node) });
668+
sink.push(UnresolvedMacroCall {
669+
file: ast.file_id,
670+
node: AstPtr::new(&node),
671+
path: path.clone(),
672+
});
668673
}
669674

670675
DiagnosticKind::MacroError { ast, message } => {

crates/hir_def/src/nameres/collector.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ impl DefCollector<'_> {
829829
res = ReachedFixedPoint::No;
830830
return false;
831831
}
832-
Err(UnresolvedMacro) | Ok(Err(_)) => {}
832+
Err(UnresolvedMacro { .. }) | Ok(Err(_)) => {}
833833
}
834834
}
835835
MacroDirectiveKind::Derive { ast_id, derive_attr } => {
@@ -845,7 +845,7 @@ impl DefCollector<'_> {
845845
res = ReachedFixedPoint::No;
846846
return false;
847847
}
848-
Err(UnresolvedMacro) => (),
848+
Err(UnresolvedMacro { .. }) => (),
849849
}
850850
}
851851
}
@@ -943,10 +943,11 @@ impl DefCollector<'_> {
943943
&mut |_| (),
944944
) {
945945
Ok(_) => (),
946-
Err(UnresolvedMacro) => {
946+
Err(UnresolvedMacro { path }) => {
947947
self.def_map.diagnostics.push(DefDiagnostic::unresolved_macro_call(
948948
directive.module_id,
949949
ast_id.ast_id,
950+
path,
950951
));
951952
}
952953
},
@@ -1530,7 +1531,7 @@ impl ModCollector<'_, '_> {
15301531
));
15311532
return;
15321533
}
1533-
Err(UnresolvedMacro) => (),
1534+
Err(UnresolvedMacro { .. }) => (),
15341535
}
15351536

15361537
// Case 2: resolve in module scope, expand during name resolution.

crates/hir_def/src/nameres/tests/diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ fn unresolved_legacy_scope_macro() {
170170
171171
m!();
172172
m2!();
173-
//^^^^^^ unresolved macro call
173+
//^^^^^^ unresolved macro `self::m2!`
174174
"#,
175175
);
176176
}
@@ -187,7 +187,7 @@ fn unresolved_module_scope_macro() {
187187
188188
self::m!();
189189
self::m2!();
190-
//^^^^^^^^^^^^ unresolved macro call
190+
//^^^^^^^^^^^^ unresolved macro `self::m2!`
191191
"#,
192192
);
193193
}

crates/ide/src/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ fn test_fn() {
725725
expect![[r#"
726726
[
727727
Diagnostic {
728-
message: "unresolved macro call",
728+
message: "unresolved macro `foo::bar!`",
729729
range: 5..8,
730730
severity: Error,
731731
fix: None,

0 commit comments

Comments
 (0)