mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-16 23:24:03 +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
|
@ -138,6 +138,7 @@ impl Expander {
|
||||||
db: &dyn DefDatabase,
|
db: &dyn DefDatabase,
|
||||||
macro_call: ast::MacroCall,
|
macro_call: ast::MacroCall,
|
||||||
) -> Result<ExpandResult<Option<(Mark, T)>>, UnresolvedMacro> {
|
) -> Result<ExpandResult<Option<(Mark, T)>>, UnresolvedMacro> {
|
||||||
|
// FIXME: within_limit should support this, instead of us having to extract the error
|
||||||
let mut unresolved_macro_err = None;
|
let mut unresolved_macro_err = None;
|
||||||
|
|
||||||
let result = self.within_limit(db, |this| {
|
let result = self.within_limit(db, |this| {
|
||||||
|
@ -146,22 +147,13 @@ impl Expander {
|
||||||
let resolver =
|
let resolver =
|
||||||
|path| this.resolve_path_as_macro(db, &path).map(|it| macro_id_to_def_id(db, it));
|
|path| this.resolve_path_as_macro(db, &path).map(|it| macro_id_to_def_id(db, it));
|
||||||
|
|
||||||
let mut err = None;
|
match macro_call.as_call_id_with_errors(db, this.def_map.krate(), resolver) {
|
||||||
let call_id = match macro_call.as_call_id_with_errors(
|
|
||||||
db,
|
|
||||||
this.def_map.krate(),
|
|
||||||
resolver,
|
|
||||||
&mut |e| {
|
|
||||||
err.get_or_insert(e);
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
Ok(call_id) => call_id,
|
Ok(call_id) => call_id,
|
||||||
Err(resolve_err) => {
|
Err(resolve_err) => {
|
||||||
unresolved_macro_err = Some(resolve_err);
|
unresolved_macro_err = Some(resolve_err);
|
||||||
return ExpandResult { value: None, err: None };
|
ExpandResult { value: None, err: None }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
ExpandResult { value: call_id.ok(), err }
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if let Some(err) = unresolved_macro_err {
|
if let Some(err) = unresolved_macro_err {
|
||||||
|
|
|
@ -65,11 +65,11 @@ use hir_expand::{
|
||||||
builtin_attr_macro::BuiltinAttrExpander,
|
builtin_attr_macro::BuiltinAttrExpander,
|
||||||
builtin_derive_macro::BuiltinDeriveExpander,
|
builtin_derive_macro::BuiltinDeriveExpander,
|
||||||
builtin_fn_macro::{BuiltinFnLikeExpander, EagerExpander},
|
builtin_fn_macro::{BuiltinFnLikeExpander, EagerExpander},
|
||||||
eager::{expand_eager_macro, ErrorEmitted, ErrorSink},
|
eager::expand_eager_macro,
|
||||||
hygiene::Hygiene,
|
hygiene::Hygiene,
|
||||||
proc_macro::ProcMacroExpander,
|
proc_macro::ProcMacroExpander,
|
||||||
AstId, ExpandError, ExpandTo, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId,
|
AstId, ExpandError, ExpandResult, ExpandTo, HirFileId, InFile, MacroCallId, MacroCallKind,
|
||||||
MacroDefKind, UnresolvedMacro,
|
MacroDefId, MacroDefKind, UnresolvedMacro,
|
||||||
};
|
};
|
||||||
use item_tree::ExternBlock;
|
use item_tree::ExternBlock;
|
||||||
use la_arena::Idx;
|
use la_arena::Idx;
|
||||||
|
@ -795,7 +795,7 @@ pub trait AsMacroCall {
|
||||||
krate: CrateId,
|
krate: CrateId,
|
||||||
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
|
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
|
||||||
) -> Option<MacroCallId> {
|
) -> 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(
|
fn as_call_id_with_errors(
|
||||||
|
@ -803,8 +803,7 @@ pub trait AsMacroCall {
|
||||||
db: &dyn db::DefDatabase,
|
db: &dyn db::DefDatabase,
|
||||||
krate: CrateId,
|
krate: CrateId,
|
||||||
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
|
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
|
||||||
error_sink: &mut dyn FnMut(ExpandError),
|
) -> Result<ExpandResult<Option<MacroCallId>>, UnresolvedMacro>;
|
||||||
) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AsMacroCall for InFile<&ast::MacroCall> {
|
impl AsMacroCall for InFile<&ast::MacroCall> {
|
||||||
|
@ -813,21 +812,15 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
|
||||||
db: &dyn db::DefDatabase,
|
db: &dyn db::DefDatabase,
|
||||||
krate: CrateId,
|
krate: CrateId,
|
||||||
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
|
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
|
||||||
mut error_sink: &mut dyn FnMut(ExpandError),
|
) -> Result<ExpandResult<Option<MacroCallId>>, UnresolvedMacro> {
|
||||||
) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
|
|
||||||
let expands_to = hir_expand::ExpandTo::from_call_site(self.value);
|
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 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 h = Hygiene::new(db.upcast(), self.file_id);
|
||||||
let path =
|
let path =
|
||||||
self.value.path().and_then(|path| path::ModPath::from_src(db.upcast(), path, &h));
|
self.value.path().and_then(|path| path::ModPath::from_src(db.upcast(), path, &h));
|
||||||
|
|
||||||
let path = match error_sink
|
let Some(path) = path else {
|
||||||
.option(path, || ExpandError::Other("malformed macro invocation".into()))
|
return Ok(ExpandResult::only_err(ExpandError::Other("malformed macro invocation".into())));
|
||||||
{
|
|
||||||
Ok(path) => path,
|
|
||||||
Err(error) => {
|
|
||||||
return Ok(Err(error));
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
macro_call_as_call_id(
|
macro_call_as_call_id(
|
||||||
|
@ -836,7 +829,6 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
|
||||||
expands_to,
|
expands_to,
|
||||||
krate,
|
krate,
|
||||||
resolver,
|
resolver,
|
||||||
error_sink,
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -860,21 +852,23 @@ fn macro_call_as_call_id(
|
||||||
expand_to: ExpandTo,
|
expand_to: ExpandTo,
|
||||||
krate: CrateId,
|
krate: CrateId,
|
||||||
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
|
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
|
||||||
error_sink: &mut dyn FnMut(ExpandError),
|
) -> Result<ExpandResult<Option<MacroCallId>>, UnresolvedMacro> {
|
||||||
) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
|
|
||||||
let def =
|
let def =
|
||||||
resolver(call.path.clone()).ok_or_else(|| UnresolvedMacro { path: call.path.clone() })?;
|
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()));
|
||||||
|
|
||||||
expand_eager_macro(db.upcast(), krate, macro_call, def, &resolver, error_sink)?
|
expand_eager_macro(db.upcast(), krate, macro_call, def, &resolver)?
|
||||||
} else {
|
} else {
|
||||||
Ok(def.as_lazy_macro(
|
ExpandResult {
|
||||||
|
value: Some(def.as_lazy_macro(
|
||||||
db.upcast(),
|
db.upcast(),
|
||||||
krate,
|
krate,
|
||||||
MacroCallKind::FnLike { ast_id: call.ast_id, expand_to },
|
MacroCallKind::FnLike { ast_id: call.ast_id, expand_to },
|
||||||
))
|
)),
|
||||||
|
err: None,
|
||||||
|
}
|
||||||
};
|
};
|
||||||
Ok(res)
|
Ok(res)
|
||||||
}
|
}
|
||||||
|
|
|
@ -125,21 +125,15 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
|
||||||
|
|
||||||
for macro_call in source_file.syntax().descendants().filter_map(ast::MacroCall::cast) {
|
for macro_call in source_file.syntax().descendants().filter_map(ast::MacroCall::cast) {
|
||||||
let macro_call = InFile::new(source.file_id, ¯o_call);
|
let macro_call = InFile::new(source.file_id, ¯o_call);
|
||||||
let mut error = None;
|
let res = macro_call
|
||||||
let macro_call_id = macro_call
|
.as_call_id_with_errors(&db, krate, |path| {
|
||||||
.as_call_id_with_errors(
|
|
||||||
&db,
|
|
||||||
krate,
|
|
||||||
|path| {
|
|
||||||
resolver.resolve_path_as_macro(&db, &path).map(|it| macro_id_to_def_id(&db, it))
|
resolver.resolve_path_as_macro(&db, &path).map(|it| macro_id_to_def_id(&db, it))
|
||||||
},
|
})
|
||||||
&mut |err| error = Some(err),
|
|
||||||
)
|
|
||||||
.unwrap()
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
let macro_call_id = res.value.unwrap();
|
||||||
let macro_file = MacroFile { macro_call_id };
|
let macro_file = MacroFile { macro_call_id };
|
||||||
let mut expansion_result = db.parse_macro_expansion(macro_file);
|
let mut expansion_result = db.parse_macro_expansion(macro_file);
|
||||||
expansion_result.err = expansion_result.err.or(error);
|
expansion_result.err = expansion_result.err.or(res.err);
|
||||||
expansions.push((macro_call.value.clone(), expansion_result, db.macro_arg(macro_call_id)));
|
expansions.push((macro_call.value.clone(), expansion_result, db.macro_arg(macro_call_id)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,8 +16,8 @@ use hir_expand::{
|
||||||
builtin_fn_macro::find_builtin_macro,
|
builtin_fn_macro::find_builtin_macro,
|
||||||
name::{name, AsName, Name},
|
name::{name, AsName, Name},
|
||||||
proc_macro::ProcMacroExpander,
|
proc_macro::ProcMacroExpander,
|
||||||
ExpandTo, HirFileId, InFile, MacroCallId, MacroCallKind, MacroCallLoc, MacroDefId,
|
ExpandResult, ExpandTo, HirFileId, InFile, MacroCallId, MacroCallKind, MacroCallLoc,
|
||||||
MacroDefKind,
|
MacroDefId, MacroDefKind,
|
||||||
};
|
};
|
||||||
use itertools::{izip, Itertools};
|
use itertools::{izip, Itertools};
|
||||||
use la_arena::Idx;
|
use la_arena::Idx;
|
||||||
|
@ -1116,9 +1116,8 @@ impl DefCollector<'_> {
|
||||||
*expand_to,
|
*expand_to,
|
||||||
self.def_map.krate,
|
self.def_map.krate,
|
||||||
resolver_def_id,
|
resolver_def_id,
|
||||||
&mut |_err| (),
|
|
||||||
);
|
);
|
||||||
if let Ok(Ok(call_id)) = call_id {
|
if let Ok(ExpandResult { value: Some(call_id), .. }) = call_id {
|
||||||
push_resolved(directive, call_id);
|
push_resolved(directive, call_id);
|
||||||
res = ReachedFixedPoint::No;
|
res = ReachedFixedPoint::No;
|
||||||
return false;
|
return false;
|
||||||
|
@ -1414,7 +1413,6 @@ impl DefCollector<'_> {
|
||||||
.take_macros()
|
.take_macros()
|
||||||
.map(|it| macro_id_to_def_id(self.db, it))
|
.map(|it| macro_id_to_def_id(self.db, it))
|
||||||
},
|
},
|
||||||
&mut |_| (),
|
|
||||||
);
|
);
|
||||||
if let Err(UnresolvedMacro { path }) = macro_call_as_call_id {
|
if let Err(UnresolvedMacro { path }) = macro_call_as_call_id {
|
||||||
self.def_map.diagnostics.push(DefDiagnostic::unresolved_macro_call(
|
self.def_map.diagnostics.push(DefDiagnostic::unresolved_macro_call(
|
||||||
|
@ -2112,7 +2110,6 @@ impl ModCollector<'_, '_> {
|
||||||
let ast_id = AstIdWithPath::new(self.file_id(), mac.ast_id, ModPath::clone(&mac.path));
|
let ast_id = AstIdWithPath::new(self.file_id(), mac.ast_id, ModPath::clone(&mac.path));
|
||||||
|
|
||||||
// Case 1: try to resolve in legacy scope and expand macro_rules
|
// Case 1: try to resolve in legacy scope and expand macro_rules
|
||||||
let mut error = None;
|
|
||||||
match macro_call_as_call_id(
|
match macro_call_as_call_id(
|
||||||
self.def_collector.db,
|
self.def_collector.db,
|
||||||
&ast_id,
|
&ast_id,
|
||||||
|
@ -2133,21 +2130,20 @@ impl ModCollector<'_, '_> {
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
&mut |err| {
|
|
||||||
error.get_or_insert(err);
|
|
||||||
},
|
|
||||||
) {
|
) {
|
||||||
Ok(Ok(macro_call_id)) => {
|
Ok(res) => {
|
||||||
// Legacy macros need to be expanded immediately, so that any macros they produce
|
// Legacy macros need to be expanded immediately, so that any macros they produce
|
||||||
// are in scope.
|
// are in scope.
|
||||||
|
if let Some(val) = res.value {
|
||||||
self.def_collector.collect_macro_expansion(
|
self.def_collector.collect_macro_expansion(
|
||||||
self.module_id,
|
self.module_id,
|
||||||
macro_call_id,
|
val,
|
||||||
self.macro_depth + 1,
|
self.macro_depth + 1,
|
||||||
container,
|
container,
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(err) = error {
|
if let Some(err) = res.err {
|
||||||
self.def_collector.def_map.diagnostics.push(DefDiagnostic::macro_error(
|
self.def_collector.def_map.diagnostics.push(DefDiagnostic::macro_error(
|
||||||
self.module_id,
|
self.module_id,
|
||||||
MacroCallKind::FnLike { ast_id: ast_id.ast_id, expand_to: mac.expand_to },
|
MacroCallKind::FnLike { ast_id: ast_id.ast_id, expand_to: mac.expand_to },
|
||||||
|
@ -2157,16 +2153,6 @@ impl ModCollector<'_, '_> {
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Ok(Err(_)) => {
|
|
||||||
// Built-in macro failed eager expansion.
|
|
||||||
|
|
||||||
self.def_collector.def_map.diagnostics.push(DefDiagnostic::macro_error(
|
|
||||||
self.module_id,
|
|
||||||
MacroCallKind::FnLike { ast_id: ast_id.ast_id, expand_to: mac.expand_to },
|
|
||||||
error.unwrap().to_string(),
|
|
||||||
));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Err(UnresolvedMacro { .. }) => (),
|
Err(UnresolvedMacro { .. }) => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -279,6 +279,7 @@ fn parse_macro_expansion(
|
||||||
let mbe::ValueResult { value, err } = db.macro_expand(macro_file.macro_call_id);
|
let mbe::ValueResult { value, err } = db.macro_expand(macro_file.macro_call_id);
|
||||||
|
|
||||||
if let Some(err) = &err {
|
if let Some(err) = &err {
|
||||||
|
if tracing::enabled!(tracing::Level::DEBUG) {
|
||||||
// Note:
|
// Note:
|
||||||
// The final goal we would like to make all parse_macro success,
|
// The final goal we would like to make all parse_macro success,
|
||||||
// such that the following log will not call anyway.
|
// such that the following log will not call anyway.
|
||||||
|
@ -286,8 +287,9 @@ fn parse_macro_expansion(
|
||||||
let node = loc.kind.to_node(db);
|
let node = loc.kind.to_node(db);
|
||||||
|
|
||||||
// collect parent information for warning log
|
// collect parent information for warning log
|
||||||
let parents =
|
let parents = std::iter::successors(loc.kind.file_id().call_node(db), |it| {
|
||||||
std::iter::successors(loc.kind.file_id().call_node(db), |it| it.file_id.call_node(db))
|
it.file_id.call_node(db)
|
||||||
|
})
|
||||||
.map(|n| format!("{:#}", n.value))
|
.map(|n| format!("{:#}", n.value))
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join("\n");
|
.join("\n");
|
||||||
|
@ -299,6 +301,7 @@ fn parse_macro_expansion(
|
||||||
parents
|
parents
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
let tt = match value {
|
let tt = match value {
|
||||||
Some(tt) => tt,
|
Some(tt) => tt,
|
||||||
None => return ExpandResult { value: None, err },
|
None => return ExpandResult { value: None, err },
|
||||||
|
@ -466,7 +469,8 @@ fn macro_expand(
|
||||||
Ok(it) => it,
|
Ok(it) => it,
|
||||||
// FIXME: This is weird -- we effectively report macro *definition*
|
// FIXME: This is weird -- we effectively report macro *definition*
|
||||||
// errors lazily, when we try to expand the macro. Instead, they should
|
// errors lazily, when we try to expand the macro. Instead, they should
|
||||||
// be reported at the definition site (when we construct a def map).
|
// be reported at the definition site when we construct a def map.
|
||||||
|
// (Note we do report them also at the definition site in the late diagnostic pass)
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
return ExpandResult::only_err(ExpandError::Other(
|
return ExpandResult::only_err(ExpandError::Other(
|
||||||
format!("invalid macro definition: {err}").into(),
|
format!("invalid macro definition: {err}").into(),
|
||||||
|
|
|
@ -32,77 +32,16 @@ use crate::{
|
||||||
MacroCallLoc, MacroDefId, MacroDefKind, UnresolvedMacro,
|
MacroCallLoc, MacroDefId, MacroDefKind, UnresolvedMacro,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct ErrorEmitted {
|
|
||||||
_private: (),
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait ErrorSink {
|
|
||||||
fn emit(&mut self, err: ExpandError);
|
|
||||||
|
|
||||||
fn option<T>(
|
|
||||||
&mut self,
|
|
||||||
opt: Option<T>,
|
|
||||||
error: impl FnOnce() -> ExpandError,
|
|
||||||
) -> Result<T, ErrorEmitted> {
|
|
||||||
match opt {
|
|
||||||
Some(it) => Ok(it),
|
|
||||||
None => {
|
|
||||||
self.emit(error());
|
|
||||||
Err(ErrorEmitted { _private: () })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn option_with<T>(
|
|
||||||
&mut self,
|
|
||||||
opt: impl FnOnce() -> Option<T>,
|
|
||||||
error: impl FnOnce() -> ExpandError,
|
|
||||||
) -> Result<T, ErrorEmitted> {
|
|
||||||
self.option(opt(), error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn result<T>(&mut self, res: Result<T, ExpandError>) -> Result<T, ErrorEmitted> {
|
|
||||||
match res {
|
|
||||||
Ok(it) => Ok(it),
|
|
||||||
Err(e) => {
|
|
||||||
self.emit(e);
|
|
||||||
Err(ErrorEmitted { _private: () })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn expand_result_option<T>(&mut self, res: ExpandResult<Option<T>>) -> Result<T, ErrorEmitted> {
|
|
||||||
match (res.value, res.err) {
|
|
||||||
(None, Some(err)) => {
|
|
||||||
self.emit(err);
|
|
||||||
Err(ErrorEmitted { _private: () })
|
|
||||||
}
|
|
||||||
(Some(value), opt_err) => {
|
|
||||||
if let Some(err) = opt_err {
|
|
||||||
self.emit(err);
|
|
||||||
}
|
|
||||||
Ok(value)
|
|
||||||
}
|
|
||||||
(None, None) => unreachable!("`ExpandResult` without value or error"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ErrorSink for &'_ mut dyn FnMut(ExpandError) {
|
|
||||||
fn emit(&mut self, err: ExpandError) {
|
|
||||||
self(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn expand_eager_macro(
|
pub fn expand_eager_macro(
|
||||||
db: &dyn ExpandDatabase,
|
db: &dyn ExpandDatabase,
|
||||||
krate: CrateId,
|
krate: CrateId,
|
||||||
macro_call: InFile<ast::MacroCall>,
|
macro_call: InFile<ast::MacroCall>,
|
||||||
def: MacroDefId,
|
def: MacroDefId,
|
||||||
resolver: &dyn Fn(ModPath) -> Option<MacroDefId>,
|
resolver: &dyn Fn(ModPath) -> Option<MacroDefId>,
|
||||||
diagnostic_sink: &mut dyn FnMut(ExpandError),
|
) -> Result<ExpandResult<Option<MacroCallId>>, UnresolvedMacro> {
|
||||||
) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
|
let MacroDefKind::BuiltInEager(eager, _) = def.kind else {
|
||||||
|
panic!("called `expand_eager_macro` on non-eager macro def {def:?}")
|
||||||
|
};
|
||||||
let hygiene = Hygiene::new(db, macro_call.file_id);
|
let hygiene = Hygiene::new(db, macro_call.file_id);
|
||||||
let parsed_args = macro_call
|
let parsed_args = macro_call
|
||||||
.value
|
.value
|
||||||
|
@ -129,24 +68,21 @@ pub fn expand_eager_macro(
|
||||||
});
|
});
|
||||||
|
|
||||||
let parsed_args = mbe::token_tree_to_syntax_node(&parsed_args, mbe::TopEntryPoint::Expr).0;
|
let parsed_args = mbe::token_tree_to_syntax_node(&parsed_args, mbe::TopEntryPoint::Expr).0;
|
||||||
let result = match eager_macro_recur(
|
let ExpandResult { value, mut err } = eager_macro_recur(
|
||||||
db,
|
db,
|
||||||
&hygiene,
|
&hygiene,
|
||||||
InFile::new(arg_id.as_file(), parsed_args.syntax_node()),
|
InFile::new(arg_id.as_file(), parsed_args.syntax_node()),
|
||||||
krate,
|
krate,
|
||||||
resolver,
|
resolver,
|
||||||
diagnostic_sink,
|
)?;
|
||||||
) {
|
let Some(value ) = value else {
|
||||||
Ok(Ok(it)) => it,
|
return Ok(ExpandResult { value: None, err })
|
||||||
Ok(Err(err)) => return Ok(Err(err)),
|
|
||||||
Err(err) => return Err(err),
|
|
||||||
};
|
};
|
||||||
let subtree = to_subtree(&result);
|
let subtree = to_subtree(&value);
|
||||||
|
|
||||||
if let MacroDefKind::BuiltInEager(eager, _) = def.kind {
|
|
||||||
let res = eager.expand(db, arg_id, &subtree);
|
let res = eager.expand(db, arg_id, &subtree);
|
||||||
if let Some(err) = res.err {
|
if err.is_none() {
|
||||||
diagnostic_sink(err);
|
err = res.err;
|
||||||
}
|
}
|
||||||
|
|
||||||
let loc = MacroCallLoc {
|
let loc = MacroCallLoc {
|
||||||
|
@ -159,10 +95,7 @@ pub fn expand_eager_macro(
|
||||||
kind: MacroCallKind::FnLike { ast_id: call_id, expand_to },
|
kind: MacroCallKind::FnLike { ast_id: call_id, expand_to },
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(Ok(db.intern_macro_call(loc)))
|
Ok(ExpandResult { value: Some(db.intern_macro_call(loc)), err })
|
||||||
} else {
|
|
||||||
panic!("called `expand_eager_macro` on non-eager macro def {def:?}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_subtree(node: &SyntaxNode) -> crate::tt::Subtree {
|
fn to_subtree(node: &SyntaxNode) -> crate::tt::Subtree {
|
||||||
|
@ -201,23 +134,25 @@ fn eager_macro_recur(
|
||||||
curr: InFile<SyntaxNode>,
|
curr: InFile<SyntaxNode>,
|
||||||
krate: CrateId,
|
krate: CrateId,
|
||||||
macro_resolver: &dyn Fn(ModPath) -> Option<MacroDefId>,
|
macro_resolver: &dyn Fn(ModPath) -> Option<MacroDefId>,
|
||||||
mut diagnostic_sink: &mut dyn FnMut(ExpandError),
|
) -> Result<ExpandResult<Option<SyntaxNode>>, UnresolvedMacro> {
|
||||||
) -> Result<Result<SyntaxNode, ErrorEmitted>, UnresolvedMacro> {
|
|
||||||
let original = curr.value.clone_for_update();
|
let original = curr.value.clone_for_update();
|
||||||
|
|
||||||
let children = original.descendants().filter_map(ast::MacroCall::cast);
|
let children = original.descendants().filter_map(ast::MacroCall::cast);
|
||||||
let mut replacements = Vec::new();
|
let mut replacements = Vec::new();
|
||||||
|
|
||||||
|
// Note: We only report a single error inside of eager expansions
|
||||||
|
let mut error = None;
|
||||||
|
|
||||||
// Collect replacement
|
// Collect replacement
|
||||||
for child in children {
|
for child in children {
|
||||||
let def = match child.path().and_then(|path| ModPath::from_src(db, path, hygiene)) {
|
let def = match child.path().and_then(|path| ModPath::from_src(db, path, hygiene)) {
|
||||||
Some(path) => macro_resolver(path.clone()).ok_or(UnresolvedMacro { path })?,
|
Some(path) => macro_resolver(path.clone()).ok_or(UnresolvedMacro { path })?,
|
||||||
None => {
|
None => {
|
||||||
diagnostic_sink(ExpandError::Other("malformed macro invocation".into()));
|
error = Some(ExpandError::Other("malformed macro invocation".into()));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let insert = match def.kind {
|
let ExpandResult { value, err } = match def.kind {
|
||||||
MacroDefKind::BuiltInEager(..) => {
|
MacroDefKind::BuiltInEager(..) => {
|
||||||
let id = match expand_eager_macro(
|
let id = match expand_eager_macro(
|
||||||
db,
|
db,
|
||||||
|
@ -225,45 +160,49 @@ fn eager_macro_recur(
|
||||||
curr.with_value(child.clone()),
|
curr.with_value(child.clone()),
|
||||||
def,
|
def,
|
||||||
macro_resolver,
|
macro_resolver,
|
||||||
diagnostic_sink,
|
|
||||||
) {
|
) {
|
||||||
Ok(Ok(it)) => it,
|
Ok(it) => it,
|
||||||
Ok(Err(err)) => return Ok(Err(err)),
|
|
||||||
Err(err) => return Err(err),
|
Err(err) => return Err(err),
|
||||||
};
|
};
|
||||||
db.parse_or_expand(id.as_file())
|
id.map(|call| {
|
||||||
.expect("successful macro expansion should be parseable")
|
call.and_then(|call| db.parse_or_expand(call.as_file()))
|
||||||
.clone_for_update()
|
.map(|it| it.clone_for_update())
|
||||||
|
})
|
||||||
}
|
}
|
||||||
MacroDefKind::Declarative(_)
|
MacroDefKind::Declarative(_)
|
||||||
| MacroDefKind::BuiltIn(..)
|
| MacroDefKind::BuiltIn(..)
|
||||||
| MacroDefKind::BuiltInAttr(..)
|
| MacroDefKind::BuiltInAttr(..)
|
||||||
| MacroDefKind::BuiltInDerive(..)
|
| MacroDefKind::BuiltInDerive(..)
|
||||||
| MacroDefKind::ProcMacro(..) => {
|
| MacroDefKind::ProcMacro(..) => {
|
||||||
let res = lazy_expand(db, &def, curr.with_value(child.clone()), krate);
|
let ExpandResult { value, err } =
|
||||||
let val = match diagnostic_sink.expand_result_option(res) {
|
lazy_expand(db, &def, curr.with_value(child.clone()), krate);
|
||||||
Ok(it) => it,
|
|
||||||
Err(err) => return Ok(Err(err)),
|
|
||||||
};
|
|
||||||
|
|
||||||
|
match value {
|
||||||
|
Some(val) => {
|
||||||
// replace macro inside
|
// replace macro inside
|
||||||
let hygiene = Hygiene::new(db, val.file_id);
|
let hygiene = Hygiene::new(db, val.file_id);
|
||||||
match eager_macro_recur(db, &hygiene, val, krate, macro_resolver, diagnostic_sink) {
|
let ExpandResult { value, err: error } =
|
||||||
Ok(Ok(it)) => it,
|
eager_macro_recur(db, &hygiene, val, krate, macro_resolver)?;
|
||||||
Ok(Err(err)) => return Ok(Err(err)),
|
let err = if err.is_none() { error } else { err };
|
||||||
Err(err) => return Err(err),
|
ExpandResult { value, err }
|
||||||
|
}
|
||||||
|
None => ExpandResult { value: None, err },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
if err.is_some() {
|
||||||
|
error = err;
|
||||||
|
}
|
||||||
// check if the whole original syntax is replaced
|
// check if the whole original syntax is replaced
|
||||||
if child.syntax() == &original {
|
if child.syntax() == &original {
|
||||||
return Ok(Ok(insert));
|
return Ok(ExpandResult { value, err: error });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(insert) = value {
|
||||||
replacements.push((child, insert));
|
replacements.push((child, insert));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
replacements.into_iter().rev().for_each(|(old, new)| ted::replace(old.syntax(), new));
|
replacements.into_iter().rev().for_each(|(old, new)| ted::replace(old.syntax(), new));
|
||||||
Ok(Ok(original))
|
Ok(ExpandResult { value: Some(original), err: error })
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue