Make basic use of spans for macro expansion errors

This commit is contained in:
Lukas Wirth 2024-07-29 14:52:40 +02:00
parent 7beac14cba
commit ae9c553902
24 changed files with 392 additions and 333 deletions

View file

@ -661,6 +661,7 @@ impl<'a> AssocItemCollector<'a> {
self.diagnostics.push(DefDiagnostic::macro_error(
self.module_id.local_id,
ast_id,
(*attr.path).clone(),
err,
));
continue 'attrs;

View file

@ -6,8 +6,8 @@ use base_db::CrateId;
use cfg::CfgOptions;
use drop_bomb::DropBomb;
use hir_expand::{
attrs::RawAttrs, mod_path::ModPath, span_map::SpanMap, ExpandError, ExpandResult, HirFileId,
InFile, MacroCallId,
attrs::RawAttrs, mod_path::ModPath, span_map::SpanMap, ExpandError, ExpandErrorKind,
ExpandResult, HirFileId, InFile, Lookup, MacroCallId,
};
use limit::Limit;
use span::SyntaxContextId;
@ -160,26 +160,30 @@ impl Expander {
// so don't return overflow error here to avoid diagnostics duplication.
cov_mark::hit!(overflow_but_not_me);
return ExpandResult::ok(None);
} else if self.recursion_limit.check(self.recursion_depth as usize + 1).is_err() {
self.recursion_depth = u32::MAX;
cov_mark::hit!(your_stack_belongs_to_me);
return ExpandResult::only_err(ExpandError::RecursionOverflow);
}
let ExpandResult { value, err } = op(self);
let Some(call_id) = value else {
return ExpandResult { value: None, err };
};
if self.recursion_limit.check(self.recursion_depth as usize + 1).is_err() {
self.recursion_depth = u32::MAX;
cov_mark::hit!(your_stack_belongs_to_me);
return ExpandResult::only_err(ExpandError::new(
db.macro_arg_considering_derives(call_id, &call_id.lookup(db.upcast()).kind).2,
ExpandErrorKind::RecursionOverflow,
));
}
let macro_file = call_id.as_macro_file();
let res = db.parse_macro_expansion(macro_file);
let err = err.or(res.err);
ExpandResult {
value: match err {
value: match &err {
// If proc-macro is disabled or unresolved, we want to expand to a missing expression
// instead of an empty tree which might end up in an empty block.
Some(ExpandError::MissingProcMacroExpander(_)) => None,
Some(e) if matches!(e.kind(), ExpandErrorKind::MissingProcMacroExpander(_)) => None,
_ => (|| {
let parse = res.value.0.cast::<T>()?;

View file

@ -1434,7 +1434,10 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
});
let Some((call_site, path)) = path else {
return Ok(ExpandResult::only_err(ExpandError::other("malformed macro invocation")));
return Ok(ExpandResult::only_err(ExpandError::other(
span_map.span_for_range(self.value.syntax().text_range()),
"malformed macro invocation",
)));
};
macro_call_as_call_id_with_eager(

View file

@ -1084,7 +1084,7 @@ fn main() {
macro_rules! concat_bytes {}
fn main() {
let x = /* error: unexpected token in input */b"";
let x = /* error: unexpected token */b"";
}
"#]],

View file

@ -1324,6 +1324,7 @@ impl DefCollector<'_> {
self.def_map.diagnostics.push(DefDiagnostic::macro_error(
directive.module_id,
ast_id,
(**path).clone(),
err,
));
return recollect_without(self);

View file

@ -3,7 +3,7 @@
use std::ops::Not;
use cfg::{CfgExpr, CfgOptions};
use hir_expand::{attrs::AttrId, ExpandError, MacroCallKind};
use hir_expand::{attrs::AttrId, ExpandErrorKind, MacroCallKind};
use la_arena::Idx;
use syntax::ast;
@ -25,7 +25,7 @@ pub enum DefDiagnosticKind {
InvalidDeriveTarget { ast: AstId<ast::Item>, id: usize },
MalformedDerive { ast: AstId<ast::Adt>, id: usize },
MacroDefError { ast: AstId<ast::Macro>, message: String },
MacroError { ast: AstId<ast::Item>, err: ExpandError },
MacroError { ast: AstId<ast::Item>, path: ModPath, err: ExpandErrorKind },
}
#[derive(Clone, Debug, PartialEq, Eq)]
@ -82,8 +82,13 @@ impl DefDiagnostic {
Self { in_module: container, kind: DefDiagnosticKind::UnresolvedImport { id, index } }
}
pub fn macro_error(container: LocalModuleId, ast: AstId<ast::Item>, err: ExpandError) -> Self {
Self { in_module: container, kind: DefDiagnosticKind::MacroError { ast, err } }
pub fn macro_error(
container: LocalModuleId,
ast: AstId<ast::Item>,
path: ModPath,
err: ExpandErrorKind,
) -> Self {
Self { in_module: container, kind: DefDiagnosticKind::MacroError { ast, path, err } }
}
pub fn unconfigured_code(