Internal: Cleanup proc-macro error handling

This commit is contained in:
Lukas Wirth 2024-07-26 14:36:13 +02:00
parent df15b6f668
commit 7beac14cba
39 changed files with 380 additions and 522 deletions

View file

@ -6,7 +6,6 @@
pub use hir_ty::diagnostics::{CaseType, IncorrectCase};
use hir_ty::{db::HirDatabase, diagnostics::BodyValidationDiagnostic, InferenceDiagnostic};
use base_db::CrateId;
use cfg::{CfgExpr, CfgOptions};
use either::Either;
pub use hir_def::VariantId;
@ -15,7 +14,7 @@ use hir_expand::{name::Name, HirFileId, InFile};
use syntax::{ast, AstPtr, SyntaxError, SyntaxNodePtr, TextRange};
use triomphe::Arc;
use crate::{AssocItem, Field, Local, MacroKind, Trait, Type};
use crate::{AssocItem, Field, Local, Trait, Type};
macro_rules! diagnostics {
($($diag:ident,)*) => {
@ -90,7 +89,6 @@ diagnostics![
UnresolvedMethodCall,
UnresolvedModule,
UnresolvedIdent,
UnresolvedProcMacro,
UnusedMut,
UnusedVariable,
];
@ -150,23 +148,12 @@ pub struct InactiveCode {
pub opts: CfgOptions,
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct UnresolvedProcMacro {
pub node: InFile<SyntaxNodePtr>,
/// If the diagnostic can be pinpointed more accurately than via `node`, this is the `TextRange`
/// to use instead.
pub precise_location: Option<TextRange>,
pub macro_name: Option<String>,
pub kind: MacroKind,
/// The crate id of the proc-macro this macro belongs to, or `None` if the proc-macro can't be found.
pub krate: CrateId,
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct MacroError {
pub node: InFile<SyntaxNodePtr>,
pub precise_location: Option<TextRange>,
pub message: String,
pub error: bool,
}
#[derive(Debug, Clone, Eq, PartialEq)]

View file

@ -137,7 +137,7 @@ pub use {
hygiene::{marks_rev, SyntaxContextExt},
inert_attr_macro::AttributeTemplate,
name::Name,
proc_macro::ProcMacros,
proc_macro::{ProcMacros, ProcMacrosBuilder},
tt, ExpandResult, HirFileId, HirFileIdExt, MacroFileId, MacroFileIdExt,
},
hir_ty::{
@ -833,14 +833,10 @@ fn macro_call_diagnostics(
let ValueResult { value: parse_errors, err } = &*e;
if let Some(err) = err {
let loc = db.lookup_intern_macro_call(macro_call_id);
let (node, precise_location, macro_name, kind) = precise_macro_call_location(&loc.kind, db);
let diag = match err {
&hir_expand::ExpandError::UnresolvedProcMacro(krate) => {
UnresolvedProcMacro { node, precise_location, macro_name, kind, krate }.into()
}
err => MacroError { node, precise_location, message: err.to_string() }.into(),
};
acc.push(diag);
let (node, precise_location, _macro_name, _kind) =
precise_macro_call_location(&loc.kind, db);
let (message, error) = err.render_to_string(db.upcast());
acc.push(MacroError { node, precise_location, message, error }.into());
}
if !parse_errors.is_empty() {
@ -895,6 +891,19 @@ fn emit_def_diagnostic_(
acc.push(UnresolvedExternCrate { decl: InFile::new(ast.file_id, item) }.into());
}
DefDiagnosticKind::MacroError { ast, err } => {
let item = ast.to_ptr(db.upcast());
let (message, error) = err.render_to_string(db.upcast());
acc.push(
MacroError {
node: InFile::new(ast.file_id, item.syntax_node_ptr()),
precise_location: None,
message,
error,
}
.into(),
)
}
DefDiagnosticKind::UnresolvedImport { id, index } => {
let file_id = id.file_id();
let item_tree = id.item_tree(db.upcast());
@ -991,13 +1000,6 @@ fn emit_def_diagnostic_(
Some(())
})();
}
DefDiagnosticKind::UnresolvedProcMacro { ast, krate } => {
let (node, precise_location, macro_name, kind) = precise_macro_call_location(ast, db);
acc.push(
UnresolvedProcMacro { node, precise_location, macro_name, kind, krate: *krate }
.into(),
);
}
DefDiagnosticKind::UnresolvedMacroCall { ast, path } => {
let (node, precise_location, _, _) = precise_macro_call_location(ast, db);
acc.push(
@ -1795,20 +1797,17 @@ impl DefWithBody {
BodyDiagnostic::InactiveCode { node, cfg, opts } => {
InactiveCode { node: *node, cfg: cfg.clone(), opts: opts.clone() }.into()
}
BodyDiagnostic::MacroError { node, message } => MacroError {
node: (*node).map(|it| it.into()),
precise_location: None,
message: message.to_string(),
BodyDiagnostic::MacroError { node, err } => {
let (message, error) = err.render_to_string(db.upcast());
MacroError {
node: (*node).map(|it| it.into()),
precise_location: None,
message,
error,
}
.into()
}
.into(),
BodyDiagnostic::UnresolvedProcMacro { node, krate } => UnresolvedProcMacro {
node: (*node).map(|it| it.into()),
precise_location: None,
macro_name: None,
kind: MacroKind::ProcMacro,
krate: *krate,
}
.into(),
BodyDiagnostic::UnresolvedMacroCall { node, path } => UnresolvedMacroCall {
macro_call: (*node).map(|ast_ptr| ast_ptr.into()),
precise_location: None,

View file

@ -19,7 +19,7 @@ use hir_def::{
};
use hir_expand::{
attrs::collect_attrs,
builtin_fn_macro::{BuiltinFnLikeExpander, EagerExpander},
builtin::{BuiltinFnLikeExpander, EagerExpander},
db::ExpandDatabase,
files::InRealFile,
name::AsName,