8542: Include path in `unresolved-macro-call` diagnostic r=matklad a=jonas-schievink



Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
This commit is contained in:
bors[bot] 2021-04-16 20:52:56 +00:00 committed by GitHub
commit 2009556472
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 40 additions and 20 deletions

View File

@ -568,9 +568,13 @@ fn collect_macro_call<F: FnMut(&mut Self, Option<T>), T: ast::AstNode>(
let res = match res { let res = match res {
Ok(res) => res, Ok(res) => res,
Err(UnresolvedMacro) => { Err(UnresolvedMacro { path }) => {
self.source_map.diagnostics.push(BodyDiagnostic::UnresolvedMacroCall( 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); collector(self, None);
return; return;

View File

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

View File

@ -8,7 +8,7 @@
use hir_expand::{HirFileId, InFile}; use hir_expand::{HirFileId, InFile};
use syntax::{ast, AstPtr, SyntaxNodePtr, TextRange}; 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<'_>) { pub fn validate_body(db: &dyn DefDatabase, owner: DefWithBodyId, sink: &mut DiagnosticSink<'_>) {
let source_map = db.body_with_source_map(owner).1; let source_map = db.body_with_source_map(owner).1;
@ -103,6 +103,7 @@ fn is_experimental(&self) -> bool {
pub struct UnresolvedMacroCall { pub struct UnresolvedMacroCall {
pub file: HirFileId, pub file: HirFileId,
pub node: AstPtr<ast::MacroCall>, pub node: AstPtr<ast::MacroCall>,
pub path: ModPath,
} }
impl Diagnostic for UnresolvedMacroCall { impl Diagnostic for UnresolvedMacroCall {
@ -110,7 +111,7 @@ fn code(&self) -> DiagnosticCode {
DiagnosticCode("unresolved-macro-call") DiagnosticCode("unresolved-macro-call")
} }
fn message(&self) -> String { fn message(&self) -> String {
"unresolved macro call".to_string() format!("unresolved macro `{}!`", self.path)
} }
fn display_source(&self) -> InFile<SyntaxNodePtr> { fn display_source(&self) -> InFile<SyntaxNodePtr> {
InFile::new(self.file, self.node.clone().into()) InFile::new(self.file, self.node.clone().into())

View File

@ -66,6 +66,7 @@ macro_rules! eprintln {
}; };
use la_arena::Idx; use la_arena::Idx;
use nameres::DefMap; use nameres::DefMap;
use path::ModPath;
use syntax::ast; use syntax::ast;
use crate::builtin_type::BuiltinType; use crate::builtin_type::BuiltinType;
@ -675,7 +676,9 @@ fn new(file_id: HirFileId, ast_id: FileAstId<T>, path: path::ModPath) -> AstIdWi
} }
} }
pub struct UnresolvedMacro; pub struct UnresolvedMacro {
pub path: ModPath,
}
fn macro_call_as_call_id( fn macro_call_as_call_id(
call: &AstIdWithPath<ast::MacroCall>, call: &AstIdWithPath<ast::MacroCall>,
@ -684,7 +687,8 @@ fn macro_call_as_call_id(
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>, resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
error_sink: &mut dyn FnMut(mbe::ExpandError), error_sink: &mut dyn FnMut(mbe::ExpandError),
) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> { ) -> 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 res = if let MacroDefKind::BuiltInEager(..) = def.kind {
let macro_call = InFile::new(call.ast_id.file_id, call.ast_id.to_node(db.upcast())); 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, krate: CrateId,
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>, resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
) -> Result<MacroCallId, UnresolvedMacro> { ) -> Result<MacroCallId, UnresolvedMacro> {
let def: MacroDefId = resolver(item_attr.path.clone()).ok_or(UnresolvedMacro)?; let def: MacroDefId = resolver(item_attr.path.clone())
let last_segment = item_attr.path.segments().last().ok_or(UnresolvedMacro)?; .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 let res = def
.as_lazy_macro( .as_lazy_macro(
db.upcast(), db.upcast(),

View File

@ -481,7 +481,7 @@ enum DiagnosticKind {
UnresolvedProcMacro { ast: MacroCallKind }, UnresolvedProcMacro { ast: MacroCallKind },
UnresolvedMacroCall { ast: AstId<ast::MacroCall> }, UnresolvedMacroCall { ast: AstId<ast::MacroCall>, path: ModPath },
MacroError { ast: MacroCallKind, message: String }, MacroError { ast: MacroCallKind, message: String },
} }
@ -546,8 +546,9 @@ pub(super) fn macro_error(
pub(super) fn unresolved_macro_call( pub(super) fn unresolved_macro_call(
container: LocalModuleId, container: LocalModuleId,
ast: AstId<ast::MacroCall>, ast: AstId<ast::MacroCall>,
path: ModPath,
) -> Self { ) -> Self {
Self { in_module: container, kind: DiagnosticKind::UnresolvedMacroCall { ast } } Self { in_module: container, kind: DiagnosticKind::UnresolvedMacroCall { ast, path } }
} }
pub(super) fn add_to( pub(super) fn add_to(
@ -662,9 +663,13 @@ pub(super) fn add_to(
}); });
} }
DiagnosticKind::UnresolvedMacroCall { ast } => { DiagnosticKind::UnresolvedMacroCall { ast, path } => {
let node = ast.to_node(db.upcast()); 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 } => { DiagnosticKind::MacroError { ast, message } => {

View File

@ -829,7 +829,7 @@ fn resolve_macros(&mut self) -> ReachedFixedPoint {
res = ReachedFixedPoint::No; res = ReachedFixedPoint::No;
return false; return false;
} }
Err(UnresolvedMacro) | Ok(Err(_)) => {} Err(UnresolvedMacro { .. }) | Ok(Err(_)) => {}
} }
} }
MacroDirectiveKind::Derive { ast_id, derive_attr } => { MacroDirectiveKind::Derive { ast_id, derive_attr } => {
@ -845,7 +845,7 @@ fn resolve_macros(&mut self) -> ReachedFixedPoint {
res = ReachedFixedPoint::No; res = ReachedFixedPoint::No;
return false; return false;
} }
Err(UnresolvedMacro) => (), Err(UnresolvedMacro { .. }) => (),
} }
} }
} }
@ -943,10 +943,11 @@ fn finish(mut self) -> DefMap {
&mut |_| (), &mut |_| (),
) { ) {
Ok(_) => (), Ok(_) => (),
Err(UnresolvedMacro) => { Err(UnresolvedMacro { path }) => {
self.def_map.diagnostics.push(DefDiagnostic::unresolved_macro_call( self.def_map.diagnostics.push(DefDiagnostic::unresolved_macro_call(
directive.module_id, directive.module_id,
ast_id.ast_id, ast_id.ast_id,
path,
)); ));
} }
}, },
@ -1530,7 +1531,7 @@ fn collect_macro_call(&mut self, mac: &MacroCall) {
)); ));
return; return;
} }
Err(UnresolvedMacro) => (), Err(UnresolvedMacro { .. }) => (),
} }
// Case 2: resolve in module scope, expand during name resolution. // Case 2: resolve in module scope, expand during name resolution.

View File

@ -170,7 +170,7 @@ macro_rules! m { () => {} }
m!(); m!();
m2!(); m2!();
//^^^^^^ unresolved macro call //^^^^^^ unresolved macro `self::m2!`
"#, "#,
); );
} }
@ -187,7 +187,7 @@ macro_rules! m { () => {} }
self::m!(); self::m!();
self::m2!(); self::m2!();
//^^^^^^^^^^^^ unresolved macro call //^^^^^^^^^^^^ unresolved macro `self::m2!`
"#, "#,
); );
} }

View File

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