mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
Simplify eager macro error handling
This commit is contained in:
parent
a5558cdfe5
commit
6ae8d49e15
6 changed files with 118 additions and 209 deletions
|
@ -65,11 +65,11 @@ use hir_expand::{
|
|||
builtin_attr_macro::BuiltinAttrExpander,
|
||||
builtin_derive_macro::BuiltinDeriveExpander,
|
||||
builtin_fn_macro::{BuiltinFnLikeExpander, EagerExpander},
|
||||
eager::{expand_eager_macro, ErrorEmitted, ErrorSink},
|
||||
eager::expand_eager_macro,
|
||||
hygiene::Hygiene,
|
||||
proc_macro::ProcMacroExpander,
|
||||
AstId, ExpandError, ExpandTo, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId,
|
||||
MacroDefKind, UnresolvedMacro,
|
||||
AstId, ExpandError, ExpandResult, ExpandTo, HirFileId, InFile, MacroCallId, MacroCallKind,
|
||||
MacroDefId, MacroDefKind, UnresolvedMacro,
|
||||
};
|
||||
use item_tree::ExternBlock;
|
||||
use la_arena::Idx;
|
||||
|
@ -795,7 +795,7 @@ pub trait AsMacroCall {
|
|||
krate: CrateId,
|
||||
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
|
||||
) -> Option<MacroCallId> {
|
||||
self.as_call_id_with_errors(db, krate, resolver, &mut |_| ()).ok()?.ok()
|
||||
self.as_call_id_with_errors(db, krate, resolver).ok()?.value
|
||||
}
|
||||
|
||||
fn as_call_id_with_errors(
|
||||
|
@ -803,8 +803,7 @@ pub trait AsMacroCall {
|
|||
db: &dyn db::DefDatabase,
|
||||
krate: CrateId,
|
||||
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
|
||||
error_sink: &mut dyn FnMut(ExpandError),
|
||||
) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro>;
|
||||
) -> Result<ExpandResult<Option<MacroCallId>>, UnresolvedMacro>;
|
||||
}
|
||||
|
||||
impl AsMacroCall for InFile<&ast::MacroCall> {
|
||||
|
@ -813,21 +812,15 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
|
|||
db: &dyn db::DefDatabase,
|
||||
krate: CrateId,
|
||||
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
|
||||
mut error_sink: &mut dyn FnMut(ExpandError),
|
||||
) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
|
||||
) -> Result<ExpandResult<Option<MacroCallId>>, UnresolvedMacro> {
|
||||
let expands_to = hir_expand::ExpandTo::from_call_site(self.value);
|
||||
let ast_id = AstId::new(self.file_id, db.ast_id_map(self.file_id).ast_id(self.value));
|
||||
let h = Hygiene::new(db.upcast(), self.file_id);
|
||||
let path =
|
||||
self.value.path().and_then(|path| path::ModPath::from_src(db.upcast(), path, &h));
|
||||
|
||||
let path = match error_sink
|
||||
.option(path, || ExpandError::Other("malformed macro invocation".into()))
|
||||
{
|
||||
Ok(path) => path,
|
||||
Err(error) => {
|
||||
return Ok(Err(error));
|
||||
}
|
||||
let Some(path) = path else {
|
||||
return Ok(ExpandResult::only_err(ExpandError::Other("malformed macro invocation".into())));
|
||||
};
|
||||
|
||||
macro_call_as_call_id(
|
||||
|
@ -836,7 +829,6 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
|
|||
expands_to,
|
||||
krate,
|
||||
resolver,
|
||||
error_sink,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -860,21 +852,23 @@ fn macro_call_as_call_id(
|
|||
expand_to: ExpandTo,
|
||||
krate: CrateId,
|
||||
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
|
||||
error_sink: &mut dyn FnMut(ExpandError),
|
||||
) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
|
||||
) -> Result<ExpandResult<Option<MacroCallId>>, UnresolvedMacro> {
|
||||
let def =
|
||||
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()));
|
||||
|
||||
expand_eager_macro(db.upcast(), krate, macro_call, def, &resolver, error_sink)?
|
||||
expand_eager_macro(db.upcast(), krate, macro_call, def, &resolver)?
|
||||
} else {
|
||||
Ok(def.as_lazy_macro(
|
||||
db.upcast(),
|
||||
krate,
|
||||
MacroCallKind::FnLike { ast_id: call.ast_id, expand_to },
|
||||
))
|
||||
ExpandResult {
|
||||
value: Some(def.as_lazy_macro(
|
||||
db.upcast(),
|
||||
krate,
|
||||
MacroCallKind::FnLike { ast_id: call.ast_id, expand_to },
|
||||
)),
|
||||
err: None,
|
||||
}
|
||||
};
|
||||
Ok(res)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue