2020-03-02 06:05:15 +00:00
|
|
|
//! Eager expansion related utils
|
2020-03-02 16:10:20 +00:00
|
|
|
//!
|
|
|
|
//! Here is a dump of a discussion from Vadim Petrochenkov about Eager Expansion and
|
|
|
|
//! Its name resolution :
|
|
|
|
//!
|
|
|
|
//! > Eagerly expanded macros (and also macros eagerly expanded by eagerly expanded macros,
|
|
|
|
//! > which actually happens in practice too!) are resolved at the location of the "root" macro
|
|
|
|
//! > that performs the eager expansion on its arguments.
|
|
|
|
//! > If some name cannot be resolved at the eager expansion time it's considered unresolved,
|
|
|
|
//! > even if becomes available later (e.g. from a glob import or other macro).
|
|
|
|
//!
|
|
|
|
//! > Eagerly expanded macros don't add anything to the module structure of the crate and
|
|
|
|
//! > don't build any speculative module structures, i.e. they are expanded in a "flat"
|
|
|
|
//! > way even if tokens in them look like modules.
|
|
|
|
//!
|
|
|
|
//! > In other words, it kinda works for simple cases for which it was originally intended,
|
|
|
|
//! > and we need to live with it because it's available on stable and widely relied upon.
|
|
|
|
//!
|
|
|
|
//!
|
2021-06-14 04:57:10 +00:00
|
|
|
//! See the full discussion : <https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Eager.20expansion.20of.20built-in.20macros>
|
2021-09-05 19:30:06 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use base_db::CrateId;
|
|
|
|
use syntax::{ted, SyntaxNode};
|
2020-03-02 06:05:15 +00:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
ast::{self, AstNode},
|
|
|
|
db::AstDatabase,
|
2022-01-26 17:31:07 +00:00
|
|
|
hygiene::Hygiene,
|
|
|
|
mod_path::ModPath,
|
2022-02-21 18:14:06 +00:00
|
|
|
EagerCallInfo, ExpandError, ExpandResult, ExpandTo, InFile, MacroCallId, MacroCallKind,
|
|
|
|
MacroCallLoc, MacroDefId, MacroDefKind, UnresolvedMacro,
|
2020-03-02 06:05:15 +00:00
|
|
|
};
|
|
|
|
|
2021-04-10 15:49:12 +00:00
|
|
|
#[derive(Debug)]
|
2020-12-02 15:52:14 +00:00
|
|
|
pub struct ErrorEmitted {
|
|
|
|
_private: (),
|
|
|
|
}
|
|
|
|
|
2021-03-17 06:31:14 +00:00
|
|
|
pub trait ErrorSink {
|
2022-02-21 18:14:06 +00:00
|
|
|
fn emit(&mut self, err: ExpandError);
|
2020-12-02 15:52:14 +00:00
|
|
|
|
|
|
|
fn option<T>(
|
|
|
|
&mut self,
|
|
|
|
opt: Option<T>,
|
2022-02-21 18:14:06 +00:00
|
|
|
error: impl FnOnce() -> ExpandError,
|
2020-12-02 15:52:14 +00:00
|
|
|
) -> 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>,
|
2022-02-21 18:14:06 +00:00
|
|
|
error: impl FnOnce() -> ExpandError,
|
2020-12-02 15:52:14 +00:00
|
|
|
) -> Result<T, ErrorEmitted> {
|
|
|
|
self.option(opt(), error)
|
|
|
|
}
|
|
|
|
|
2022-02-21 18:14:06 +00:00
|
|
|
fn result<T>(&mut self, res: Result<T, ExpandError>) -> Result<T, ErrorEmitted> {
|
2020-12-02 15:52:14 +00:00
|
|
|
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"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 18:14:06 +00:00
|
|
|
impl ErrorSink for &'_ mut dyn FnMut(ExpandError) {
|
|
|
|
fn emit(&mut self, err: ExpandError) {
|
2020-12-02 15:52:14 +00:00
|
|
|
self(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-03 17:13:20 +00:00
|
|
|
pub fn expand_eager_macro(
|
2020-03-13 15:05:46 +00:00
|
|
|
db: &dyn AstDatabase,
|
2020-06-11 10:08:24 +00:00
|
|
|
krate: CrateId,
|
2020-03-03 17:13:20 +00:00
|
|
|
macro_call: InFile<ast::MacroCall>,
|
|
|
|
def: MacroDefId,
|
2022-01-26 17:31:07 +00:00
|
|
|
resolver: &dyn Fn(ModPath) -> Option<MacroDefId>,
|
2022-02-21 18:14:06 +00:00
|
|
|
diagnostic_sink: &mut dyn FnMut(ExpandError),
|
2022-01-26 17:31:07 +00:00
|
|
|
) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
|
2022-01-27 15:57:53 +00:00
|
|
|
let hygiene = Hygiene::new(db, macro_call.file_id);
|
2022-01-24 16:27:39 +00:00
|
|
|
let parsed_args = macro_call
|
|
|
|
.value
|
|
|
|
.token_tree()
|
|
|
|
.map(|tt| mbe::syntax_node_to_token_tree(tt.syntax()).0)
|
|
|
|
.unwrap_or_default();
|
2020-03-03 17:13:20 +00:00
|
|
|
|
2020-12-22 13:42:28 +00:00
|
|
|
let ast_map = db.ast_id_map(macro_call.file_id);
|
|
|
|
let call_id = InFile::new(macro_call.file_id, ast_map.ast_id(¯o_call.value));
|
2021-09-05 19:30:06 +00:00
|
|
|
let expand_to = ExpandTo::from_call_site(¯o_call.value);
|
2020-12-22 13:42:28 +00:00
|
|
|
|
2020-03-03 18:41:33 +00:00
|
|
|
// Note:
|
|
|
|
// When `lazy_expand` is called, its *parent* file must be already exists.
|
2020-03-03 18:57:54 +00:00
|
|
|
// Here we store an eager macro id for the argument expanded subtree here
|
2020-03-03 18:41:33 +00:00
|
|
|
// for that purpose.
|
2021-11-14 15:25:40 +00:00
|
|
|
let arg_id = db.intern_macro_call(MacroCallLoc {
|
2021-05-19 18:19:08 +00:00
|
|
|
def,
|
|
|
|
krate,
|
|
|
|
eager: Some(EagerCallInfo {
|
2021-05-19 18:23:26 +00:00
|
|
|
arg_or_expansion: Arc::new(parsed_args.clone()),
|
2021-03-21 15:02:01 +00:00
|
|
|
included_file: None,
|
2021-05-19 18:19:08 +00:00
|
|
|
}),
|
2021-09-05 19:30:06 +00:00
|
|
|
kind: MacroCallKind::FnLike { ast_id: call_id, expand_to: ExpandTo::Expr },
|
2020-03-06 14:58:45 +00:00
|
|
|
});
|
2020-03-03 18:41:33 +00:00
|
|
|
|
2022-01-24 16:27:39 +00:00
|
|
|
let parsed_args = mbe::token_tree_to_syntax_node(&parsed_args, mbe::TopEntryPoint::Expr).0;
|
2022-01-26 17:31:07 +00:00
|
|
|
let result = match eager_macro_recur(
|
2020-03-06 14:58:45 +00:00
|
|
|
db,
|
2022-01-27 15:57:53 +00:00
|
|
|
&hygiene,
|
2022-01-26 17:31:07 +00:00
|
|
|
InFile::new(arg_id.as_file(), parsed_args.syntax_node()),
|
2020-06-11 10:08:24 +00:00
|
|
|
krate,
|
2020-03-06 14:58:45 +00:00
|
|
|
resolver,
|
2020-12-03 16:54:43 +00:00
|
|
|
diagnostic_sink,
|
2022-01-26 17:31:07 +00:00
|
|
|
) {
|
|
|
|
Ok(Ok(it)) => it,
|
|
|
|
Ok(Err(err)) => return Ok(Err(err)),
|
|
|
|
Err(err) => return Err(err),
|
|
|
|
};
|
2022-01-24 16:27:39 +00:00
|
|
|
let subtree = to_subtree(&result);
|
2020-03-03 17:13:20 +00:00
|
|
|
|
2021-03-18 14:37:14 +00:00
|
|
|
if let MacroDefKind::BuiltInEager(eager, _) = def.kind {
|
2020-12-02 15:52:14 +00:00
|
|
|
let res = eager.expand(db, arg_id, &subtree);
|
2022-01-21 11:58:06 +00:00
|
|
|
if let Some(err) = res.err {
|
2022-03-12 12:43:53 +00:00
|
|
|
diagnostic_sink(err);
|
2022-01-21 11:58:06 +00:00
|
|
|
}
|
2020-12-02 15:52:14 +00:00
|
|
|
|
2021-05-19 18:19:08 +00:00
|
|
|
let loc = MacroCallLoc {
|
2021-03-21 15:02:01 +00:00
|
|
|
def,
|
|
|
|
krate,
|
2021-05-19 18:19:08 +00:00
|
|
|
eager: Some(EagerCallInfo {
|
2022-01-21 11:58:06 +00:00
|
|
|
arg_or_expansion: Arc::new(res.value.subtree),
|
|
|
|
included_file: res.value.included_file,
|
2021-05-19 18:19:08 +00:00
|
|
|
}),
|
2021-09-05 19:30:06 +00:00
|
|
|
kind: MacroCallKind::FnLike { ast_id: call_id, expand_to },
|
2021-03-21 15:02:01 +00:00
|
|
|
};
|
2020-03-03 17:13:20 +00:00
|
|
|
|
2022-01-26 17:31:07 +00:00
|
|
|
Ok(Ok(db.intern_macro_call(loc)))
|
2020-03-03 17:13:20 +00:00
|
|
|
} else {
|
2023-01-09 18:36:22 +00:00
|
|
|
panic!("called `expand_eager_macro` on non-eager macro def {def:?}");
|
2020-03-03 17:13:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 16:27:39 +00:00
|
|
|
fn to_subtree(node: &SyntaxNode) -> tt::Subtree {
|
2021-04-03 23:46:45 +00:00
|
|
|
let mut subtree = mbe::syntax_node_to_token_tree(node).0;
|
2020-03-02 06:05:15 +00:00
|
|
|
subtree.delimiter = None;
|
2022-01-24 16:27:39 +00:00
|
|
|
subtree
|
2020-03-02 06:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn lazy_expand(
|
2020-03-13 15:05:46 +00:00
|
|
|
db: &dyn AstDatabase,
|
2020-03-02 06:05:15 +00:00
|
|
|
def: &MacroDefId,
|
|
|
|
macro_call: InFile<ast::MacroCall>,
|
2020-06-11 10:08:24 +00:00
|
|
|
krate: CrateId,
|
2020-12-02 15:52:14 +00:00
|
|
|
) -> ExpandResult<Option<InFile<SyntaxNode>>> {
|
2020-03-02 06:05:15 +00:00
|
|
|
let ast_id = db.ast_id_map(macro_call.file_id).ast_id(¯o_call.value);
|
|
|
|
|
2021-09-05 19:30:06 +00:00
|
|
|
let expand_to = ExpandTo::from_call_site(¯o_call.value);
|
2021-06-07 11:59:01 +00:00
|
|
|
let id = def.as_lazy_macro(
|
2021-06-13 04:18:15 +00:00
|
|
|
db,
|
|
|
|
krate,
|
2021-09-05 19:30:06 +00:00
|
|
|
MacroCallKind::FnLike { ast_id: macro_call.with_value(ast_id), expand_to },
|
2021-06-13 04:18:15 +00:00
|
|
|
);
|
2020-03-02 06:05:15 +00:00
|
|
|
|
2020-12-02 15:52:14 +00:00
|
|
|
let err = db.macro_expand_error(id);
|
|
|
|
let value = db.parse_or_expand(id.as_file()).map(|node| InFile::new(id.as_file(), node));
|
|
|
|
|
|
|
|
ExpandResult { value, err }
|
2020-03-02 06:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn eager_macro_recur(
|
2020-03-13 15:05:46 +00:00
|
|
|
db: &dyn AstDatabase,
|
2022-01-27 15:57:53 +00:00
|
|
|
hygiene: &Hygiene,
|
2020-03-02 06:05:15 +00:00
|
|
|
curr: InFile<SyntaxNode>,
|
2020-06-11 10:08:24 +00:00
|
|
|
krate: CrateId,
|
2022-01-26 17:31:07 +00:00
|
|
|
macro_resolver: &dyn Fn(ModPath) -> Option<MacroDefId>,
|
2022-02-21 18:14:06 +00:00
|
|
|
mut diagnostic_sink: &mut dyn FnMut(ExpandError),
|
2022-01-26 17:31:07 +00:00
|
|
|
) -> Result<Result<SyntaxNode, ErrorEmitted>, UnresolvedMacro> {
|
2021-05-26 15:34:50 +00:00
|
|
|
let original = curr.value.clone_for_update();
|
2020-03-02 06:05:15 +00:00
|
|
|
|
2021-04-19 17:28:41 +00:00
|
|
|
let children = original.descendants().filter_map(ast::MacroCall::cast);
|
|
|
|
let mut replacements = Vec::new();
|
2020-03-02 06:05:15 +00:00
|
|
|
|
|
|
|
// Collect replacement
|
|
|
|
for child in children {
|
2022-03-12 12:04:13 +00:00
|
|
|
let def = match child.path().and_then(|path| ModPath::from_src(db, path, hygiene)) {
|
2023-01-09 18:36:22 +00:00
|
|
|
Some(path) => macro_resolver(path.clone()).ok_or(UnresolvedMacro { path })?,
|
2022-01-26 17:31:07 +00:00
|
|
|
None => {
|
2022-02-21 18:14:06 +00:00
|
|
|
diagnostic_sink(ExpandError::Other("malformed macro invocation".into()));
|
2022-01-26 17:31:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
2020-03-02 06:05:15 +00:00
|
|
|
let insert = match def.kind {
|
2021-03-18 14:37:14 +00:00
|
|
|
MacroDefKind::BuiltInEager(..) => {
|
2022-01-26 17:31:07 +00:00
|
|
|
let id = match expand_eager_macro(
|
2020-06-11 10:08:24 +00:00
|
|
|
db,
|
|
|
|
krate,
|
|
|
|
curr.with_value(child.clone()),
|
|
|
|
def,
|
|
|
|
macro_resolver,
|
2020-12-03 16:54:43 +00:00
|
|
|
diagnostic_sink,
|
2022-01-26 17:31:07 +00:00
|
|
|
) {
|
|
|
|
Ok(Ok(it)) => it,
|
|
|
|
Ok(Err(err)) => return Ok(Err(err)),
|
|
|
|
Err(err) => return Err(err),
|
|
|
|
};
|
2020-12-02 15:52:14 +00:00
|
|
|
db.parse_or_expand(id.as_file())
|
|
|
|
.expect("successful macro expansion should be parseable")
|
2021-04-19 17:28:41 +00:00
|
|
|
.clone_for_update()
|
2020-03-02 06:05:15 +00:00
|
|
|
}
|
2021-03-18 14:37:14 +00:00
|
|
|
MacroDefKind::Declarative(_)
|
|
|
|
| MacroDefKind::BuiltIn(..)
|
2021-06-09 16:02:31 +00:00
|
|
|
| MacroDefKind::BuiltInAttr(..)
|
2021-03-18 14:37:14 +00:00
|
|
|
| MacroDefKind::BuiltInDerive(..)
|
2021-03-18 15:11:18 +00:00
|
|
|
| MacroDefKind::ProcMacro(..) => {
|
2020-12-02 15:52:14 +00:00
|
|
|
let res = lazy_expand(db, &def, curr.with_value(child.clone()), krate);
|
2022-01-26 17:31:07 +00:00
|
|
|
let val = match diagnostic_sink.expand_result_option(res) {
|
|
|
|
Ok(it) => it,
|
|
|
|
Err(err) => return Ok(Err(err)),
|
|
|
|
};
|
2020-12-02 15:52:14 +00:00
|
|
|
|
2020-03-02 06:05:15 +00:00
|
|
|
// replace macro inside
|
2022-01-27 15:57:53 +00:00
|
|
|
let hygiene = Hygiene::new(db, val.file_id);
|
|
|
|
match eager_macro_recur(db, &hygiene, val, krate, macro_resolver, diagnostic_sink) {
|
2022-01-26 17:31:07 +00:00
|
|
|
Ok(Ok(it)) => it,
|
|
|
|
Ok(Err(err)) => return Ok(Err(err)),
|
|
|
|
Err(err) => return Err(err),
|
|
|
|
}
|
2020-03-02 06:05:15 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-04-19 17:28:41 +00:00
|
|
|
// check if the whole original syntax is replaced
|
2021-01-03 09:56:59 +00:00
|
|
|
if child.syntax() == &original {
|
2022-01-26 17:31:07 +00:00
|
|
|
return Ok(Ok(insert));
|
2021-01-03 09:56:59 +00:00
|
|
|
}
|
|
|
|
|
2021-04-19 17:28:41 +00:00
|
|
|
replacements.push((child, insert));
|
2020-03-02 06:05:15 +00:00
|
|
|
}
|
|
|
|
|
2021-04-19 17:28:41 +00:00
|
|
|
replacements.into_iter().rev().for_each(|(old, new)| ted::replace(old.syntax(), new));
|
2022-01-26 17:31:07 +00:00
|
|
|
Ok(Ok(original))
|
2020-03-02 06:05:15 +00:00
|
|
|
}
|