Include path in unresolved-macro-call diagnostic

This commit is contained in:
Jonas Schievink 2021-04-16 15:48:03 +02:00
parent 75371eb0fa
commit ff858376aa
8 changed files with 40 additions and 20 deletions

View file

@ -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;

View file

@ -180,7 +180,7 @@ fn unresolved_macro_diag() {
r#"
fn f() {
m!();
//^^^^ unresolved macro call
//^^^^ unresolved macro `m!`
}
"#,
);

View file

@ -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<ast::MacroCall>,
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<SyntaxNodePtr> {
InFile::new(self.file, self.node.clone().into())

View file

@ -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<T: ast::AstNode> AstIdWithPath<T> {
}
}
pub struct UnresolvedMacro;
pub struct UnresolvedMacro {
pub path: ModPath,
}
fn macro_call_as_call_id(
call: &AstIdWithPath<ast::MacroCall>,
@ -684,7 +687,8 @@ fn macro_call_as_call_id(
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
error_sink: &mut dyn FnMut(mbe::ExpandError),
) -> Result<Result<MacroCallId, ErrorEmitted>, 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<MacroDefId>,
) -> Result<MacroCallId, UnresolvedMacro> {
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(),

View file

@ -481,7 +481,7 @@ mod diagnostics {
UnresolvedProcMacro { ast: MacroCallKind },
UnresolvedMacroCall { ast: AstId<ast::MacroCall> },
UnresolvedMacroCall { ast: AstId<ast::MacroCall>, path: ModPath },
MacroError { ast: MacroCallKind, message: String },
}
@ -546,8 +546,9 @@ mod diagnostics {
pub(super) fn unresolved_macro_call(
container: LocalModuleId,
ast: AstId<ast::MacroCall>,
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 } => {

View file

@ -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.

View file

@ -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!`
"#,
);
}

View file

@ -725,7 +725,7 @@ fn test_fn() {
expect![[r#"
[
Diagnostic {
message: "unresolved macro call",
message: "unresolved macro `foo::bar!`",
range: 5..8,
severity: Error,
fix: None,