2023-04-17 15:31:39 +00:00
|
|
|
//! Macro expansion utilities.
|
|
|
|
|
2023-11-24 15:38:48 +00:00
|
|
|
use base_db::CrateId;
|
2023-04-17 15:31:39 +00:00
|
|
|
use cfg::CfgOptions;
|
|
|
|
use drop_bomb::DropBomb;
|
|
|
|
use hir_expand::{
|
2023-11-24 15:38:48 +00:00
|
|
|
attrs::RawAttrs, mod_path::ModPath, span::SpanMap, ExpandError, ExpandResult, HirFileId,
|
|
|
|
InFile, MacroCallId,
|
2023-04-17 15:31:39 +00:00
|
|
|
};
|
|
|
|
use limit::Limit;
|
|
|
|
use syntax::{ast, Parse, SyntaxNode};
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
attr::Attrs, db::DefDatabase, lower::LowerCtx, macro_id_to_def_id, path::Path, AsMacroCall,
|
2023-11-24 15:38:48 +00:00
|
|
|
MacroId, ModuleId, UnresolvedMacro,
|
2023-04-17 15:31:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2023-07-04 08:38:23 +00:00
|
|
|
pub struct Expander {
|
2023-04-17 15:31:39 +00:00
|
|
|
cfg_options: CfgOptions,
|
2023-11-25 14:37:40 +00:00
|
|
|
span_map: SpanMap,
|
2023-04-17 15:31:39 +00:00
|
|
|
krate: CrateId,
|
|
|
|
pub(crate) current_file_id: HirFileId,
|
|
|
|
pub(crate) module: ModuleId,
|
|
|
|
/// `recursion_depth == usize::MAX` indicates that the recursion limit has been reached.
|
|
|
|
recursion_depth: u32,
|
|
|
|
recursion_limit: Limit,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Expander {
|
|
|
|
pub fn new(db: &dyn DefDatabase, current_file_id: HirFileId, module: ModuleId) -> Expander {
|
|
|
|
let recursion_limit = db.recursion_limit(module.krate);
|
|
|
|
#[cfg(not(test))]
|
|
|
|
let recursion_limit = Limit::new(recursion_limit as usize);
|
|
|
|
// Without this, `body::tests::your_stack_belongs_to_me` stack-overflows in debug
|
|
|
|
#[cfg(test)]
|
|
|
|
let recursion_limit = Limit::new(std::cmp::min(32, recursion_limit as usize));
|
2023-07-04 08:38:23 +00:00
|
|
|
Expander {
|
|
|
|
current_file_id,
|
|
|
|
module,
|
|
|
|
recursion_depth: 0,
|
|
|
|
recursion_limit,
|
|
|
|
cfg_options: db.crate_graph()[module.krate].cfg_options.clone(),
|
2023-11-25 14:37:40 +00:00
|
|
|
span_map: db.span_map(current_file_id),
|
2023-07-04 08:38:23 +00:00
|
|
|
krate: module.krate,
|
|
|
|
}
|
2023-04-17 15:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn enter_expand<T: ast::AstNode>(
|
|
|
|
&mut self,
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
macro_call: ast::MacroCall,
|
|
|
|
resolver: impl Fn(ModPath) -> Option<MacroId>,
|
|
|
|
) -> Result<ExpandResult<Option<(Mark, Parse<T>)>>, UnresolvedMacro> {
|
|
|
|
// FIXME: within_limit should support this, instead of us having to extract the error
|
|
|
|
let mut unresolved_macro_err = None;
|
|
|
|
|
|
|
|
let result = self.within_limit(db, |this| {
|
|
|
|
let macro_call = InFile::new(this.current_file_id, ¯o_call);
|
|
|
|
match macro_call.as_call_id_with_errors(db.upcast(), this.module.krate(), |path| {
|
|
|
|
resolver(path).map(|it| macro_id_to_def_id(db, it))
|
|
|
|
}) {
|
|
|
|
Ok(call_id) => call_id,
|
|
|
|
Err(resolve_err) => {
|
|
|
|
unresolved_macro_err = Some(resolve_err);
|
|
|
|
ExpandResult { value: None, err: None }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Some(err) = unresolved_macro_err {
|
|
|
|
Err(err)
|
|
|
|
} else {
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn enter_expand_id<T: ast::AstNode>(
|
|
|
|
&mut self,
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
call_id: MacroCallId,
|
|
|
|
) -> ExpandResult<Option<(Mark, Parse<T>)>> {
|
|
|
|
self.within_limit(db, |_this| ExpandResult::ok(Some(call_id)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn enter_expand_inner(
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
call_id: MacroCallId,
|
|
|
|
error: Option<ExpandError>,
|
|
|
|
) -> ExpandResult<Option<InFile<Parse<SyntaxNode>>>> {
|
2023-06-07 09:20:10 +00:00
|
|
|
let macro_file = call_id.as_macro_file();
|
|
|
|
let ExpandResult { value, err } = db.parse_macro_expansion(macro_file);
|
2023-04-17 15:31:39 +00:00
|
|
|
|
2023-06-07 09:20:10 +00:00
|
|
|
ExpandResult { value: Some(InFile::new(macro_file.into(), value.0)), err: error.or(err) }
|
2023-04-17 15:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn exit(&mut self, db: &dyn DefDatabase, mut mark: Mark) {
|
2023-11-25 14:37:40 +00:00
|
|
|
self.span_map = db.span_map(mark.file_id);
|
2023-04-17 15:31:39 +00:00
|
|
|
self.current_file_id = mark.file_id;
|
|
|
|
if self.recursion_depth == u32::MAX {
|
|
|
|
// Recursion limit has been reached somewhere in the macro expansion tree. Reset the
|
|
|
|
// depth only when we get out of the tree.
|
|
|
|
if !self.current_file_id.is_macro() {
|
|
|
|
self.recursion_depth = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.recursion_depth -= 1;
|
|
|
|
}
|
|
|
|
mark.bomb.defuse();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ctx<'a>(&self, db: &'a dyn DefDatabase) -> LowerCtx<'a> {
|
2023-11-25 14:37:40 +00:00
|
|
|
LowerCtx::new(db, self.span_map.clone(), self.current_file_id)
|
2023-04-17 15:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn to_source<T>(&self, value: T) -> InFile<T> {
|
|
|
|
InFile { file_id: self.current_file_id, value }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn parse_attrs(&self, db: &dyn DefDatabase, owner: &dyn ast::HasAttrs) -> Attrs {
|
2023-11-25 14:37:40 +00:00
|
|
|
Attrs::filter(db, self.krate, RawAttrs::new(db.upcast(), owner, self.span_map.as_ref()))
|
2023-04-17 15:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn cfg_options(&self) -> &CfgOptions {
|
2023-07-04 08:38:23 +00:00
|
|
|
&self.cfg_options
|
2023-04-17 15:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn current_file_id(&self) -> HirFileId {
|
|
|
|
self.current_file_id
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn parse_path(&mut self, db: &dyn DefDatabase, path: ast::Path) -> Option<Path> {
|
2023-11-25 14:37:40 +00:00
|
|
|
let ctx = LowerCtx::new(db, self.span_map.clone(), self.current_file_id);
|
2023-04-17 15:31:39 +00:00
|
|
|
Path::from_src(path, &ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn within_limit<F, T: ast::AstNode>(
|
|
|
|
&mut self,
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
op: F,
|
|
|
|
) -> ExpandResult<Option<(Mark, Parse<T>)>>
|
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> ExpandResult<Option<MacroCallId>>,
|
|
|
|
{
|
|
|
|
if self.recursion_depth == u32::MAX {
|
|
|
|
// Recursion limit has been reached somewhere in the macro expansion tree. We should
|
|
|
|
// stop expanding other macro calls in this tree, or else this may result in
|
|
|
|
// exponential number of macro expansions, leading to a hang.
|
|
|
|
//
|
|
|
|
// The overflow error should have been reported when it occurred (see the next branch),
|
|
|
|
// so don't return overflow error here to avoid diagnostics duplication.
|
|
|
|
cov_mark::hit!(overflow_but_not_me);
|
|
|
|
return ExpandResult::only_err(ExpandError::RecursionOverflowPoisoned);
|
|
|
|
} 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);
|
2023-06-07 09:20:10 +00:00
|
|
|
return ExpandResult::only_err(ExpandError::other(
|
|
|
|
"reached recursion limit during macro expansion",
|
2023-04-17 15:31:39 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let ExpandResult { value, err } = op(self);
|
|
|
|
let Some(call_id) = value else {
|
|
|
|
return ExpandResult { value: None, err };
|
|
|
|
};
|
|
|
|
|
2023-08-05 18:00:33 +00:00
|
|
|
let res = Self::enter_expand_inner(db, call_id, err);
|
|
|
|
match res.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::UnresolvedProcMacro(_)) => res.map(|_| None),
|
|
|
|
_ => res.map(|value| {
|
|
|
|
value.and_then(|InFile { file_id, value }| {
|
|
|
|
let parse = value.cast::<T>()?;
|
|
|
|
|
|
|
|
self.recursion_depth += 1;
|
2023-11-25 14:37:40 +00:00
|
|
|
self.span_map = db.span_map(file_id);
|
2023-08-05 18:00:33 +00:00
|
|
|
let old_file_id = std::mem::replace(&mut self.current_file_id, file_id);
|
|
|
|
let mark = Mark {
|
|
|
|
file_id: old_file_id,
|
|
|
|
bomb: DropBomb::new("expansion mark dropped"),
|
|
|
|
};
|
|
|
|
Some((mark, parse))
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
}
|
2023-04-17 15:31:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Mark {
|
|
|
|
file_id: HirFileId,
|
|
|
|
bomb: DropBomb,
|
|
|
|
}
|