1 //! Eager expansion related utils
3 //! Here is a dump of a discussion from Vadim Petrochenkov about Eager Expansion and
4 //! Its name resolution :
6 //! > Eagerly expanded macros (and also macros eagerly expanded by eagerly expanded macros,
7 //! > which actually happens in practice too!) are resolved at the location of the "root" macro
8 //! > that performs the eager expansion on its arguments.
9 //! > If some name cannot be resolved at the eager expansion time it's considered unresolved,
10 //! > even if becomes available later (e.g. from a glob import or other macro).
12 //! > Eagerly expanded macros don't add anything to the module structure of the crate and
13 //! > don't build any speculative module structures, i.e. they are expanded in a "flat"
14 //! > way even if tokens in them look like modules.
16 //! > In other words, it kinda works for simple cases for which it was originally intended,
17 //! > and we need to live with it because it's available on stable and widely relied upon.
20 //! See the full discussion : <https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Eager.20expansion.20of.20built-in.20macros>
24 use syntax::{ted, SyntaxNode};
31 EagerCallInfo, ExpandError, ExpandResult, ExpandTo, InFile, MacroCallId, MacroCallKind,
32 MacroCallLoc, MacroDefId, MacroDefKind, UnresolvedMacro,
36 pub struct ErrorEmitted {
41 fn emit(&mut self, err: ExpandError);
46 error: impl FnOnce() -> ExpandError,
47 ) -> Result<T, ErrorEmitted> {
52 Err(ErrorEmitted { _private: () })
59 opt: impl FnOnce() -> Option<T>,
60 error: impl FnOnce() -> ExpandError,
61 ) -> Result<T, ErrorEmitted> {
62 self.option(opt(), error)
65 fn result<T>(&mut self, res: Result<T, ExpandError>) -> Result<T, ErrorEmitted> {
70 Err(ErrorEmitted { _private: () })
75 fn expand_result_option<T>(&mut self, res: ExpandResult<Option<T>>) -> Result<T, ErrorEmitted> {
76 match (res.value, res.err) {
77 (None, Some(err)) => {
79 Err(ErrorEmitted { _private: () })
81 (Some(value), opt_err) => {
82 if let Some(err) = opt_err {
87 (None, None) => unreachable!("`ExpandResult` without value or error"),
92 impl ErrorSink for &'_ mut dyn FnMut(ExpandError) {
93 fn emit(&mut self, err: ExpandError) {
98 pub fn expand_eager_macro(
101 macro_call: InFile<ast::MacroCall>,
103 resolver: &dyn Fn(ModPath) -> Option<MacroDefId>,
104 diagnostic_sink: &mut dyn FnMut(ExpandError),
105 ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
106 let hygiene = Hygiene::new(db, macro_call.file_id);
107 let parsed_args = macro_call
110 .map(|tt| mbe::syntax_node_to_token_tree(tt.syntax()).0)
111 .unwrap_or_default();
113 let ast_map = db.ast_id_map(macro_call.file_id);
114 let call_id = InFile::new(macro_call.file_id, ast_map.ast_id(¯o_call.value));
115 let expand_to = ExpandTo::from_call_site(¯o_call.value);
118 // When `lazy_expand` is called, its *parent* file must be already exists.
119 // Here we store an eager macro id for the argument expanded subtree here
121 let arg_id = db.intern_macro_call(MacroCallLoc {
124 eager: Some(EagerCallInfo {
125 arg_or_expansion: Arc::new(parsed_args.clone()),
128 kind: MacroCallKind::FnLike { ast_id: call_id, expand_to: ExpandTo::Expr },
131 let parsed_args = mbe::token_tree_to_syntax_node(&parsed_args, mbe::TopEntryPoint::Expr).0;
132 let result = match eager_macro_recur(
135 InFile::new(arg_id.as_file(), parsed_args.syntax_node()),
141 Ok(Err(err)) => return Ok(Err(err)),
142 Err(err) => return Err(err),
144 let subtree = to_subtree(&result);
146 if let MacroDefKind::BuiltInEager(eager, _) = def.kind {
147 let res = eager.expand(db, arg_id, &subtree);
148 if let Some(err) = res.err {
149 diagnostic_sink(err.into());
152 let loc = MacroCallLoc {
155 eager: Some(EagerCallInfo {
156 arg_or_expansion: Arc::new(res.value.subtree),
157 included_file: res.value.included_file,
159 kind: MacroCallKind::FnLike { ast_id: call_id, expand_to },
162 Ok(Ok(db.intern_macro_call(loc)))
164 panic!("called `expand_eager_macro` on non-eager macro def {:?}", def);
168 fn to_subtree(node: &SyntaxNode) -> tt::Subtree {
169 let mut subtree = mbe::syntax_node_to_token_tree(node).0;
170 subtree.delimiter = None;
175 db: &dyn AstDatabase,
177 macro_call: InFile<ast::MacroCall>,
179 ) -> ExpandResult<Option<InFile<SyntaxNode>>> {
180 let ast_id = db.ast_id_map(macro_call.file_id).ast_id(¯o_call.value);
182 let expand_to = ExpandTo::from_call_site(¯o_call.value);
183 let id = def.as_lazy_macro(
186 MacroCallKind::FnLike { ast_id: macro_call.with_value(ast_id), expand_to },
189 let err = db.macro_expand_error(id);
190 let value = db.parse_or_expand(id.as_file()).map(|node| InFile::new(id.as_file(), node));
192 ExpandResult { value, err }
195 fn eager_macro_recur(
196 db: &dyn AstDatabase,
198 curr: InFile<SyntaxNode>,
200 macro_resolver: &dyn Fn(ModPath) -> Option<MacroDefId>,
201 mut diagnostic_sink: &mut dyn FnMut(ExpandError),
202 ) -> Result<Result<SyntaxNode, ErrorEmitted>, UnresolvedMacro> {
203 let original = curr.value.clone_for_update();
205 let children = original.descendants().filter_map(ast::MacroCall::cast);
206 let mut replacements = Vec::new();
208 // Collect replacement
209 for child in children {
210 let def = match child.path().and_then(|path| ModPath::from_src(db, path, &hygiene)) {
211 Some(path) => macro_resolver(path.clone()).ok_or_else(|| UnresolvedMacro { path })?,
213 diagnostic_sink(ExpandError::Other("malformed macro invocation".into()));
217 let insert = match def.kind {
218 MacroDefKind::BuiltInEager(..) => {
219 let id = match expand_eager_macro(
222 curr.with_value(child.clone()),
228 Ok(Err(err)) => return Ok(Err(err)),
229 Err(err) => return Err(err),
231 db.parse_or_expand(id.as_file())
232 .expect("successful macro expansion should be parseable")
235 MacroDefKind::Declarative(_)
236 | MacroDefKind::BuiltIn(..)
237 | MacroDefKind::BuiltInAttr(..)
238 | MacroDefKind::BuiltInDerive(..)
239 | MacroDefKind::ProcMacro(..) => {
240 let res = lazy_expand(db, &def, curr.with_value(child.clone()), krate);
241 let val = match diagnostic_sink.expand_result_option(res) {
243 Err(err) => return Ok(Err(err)),
246 // replace macro inside
247 let hygiene = Hygiene::new(db, val.file_id);
248 match eager_macro_recur(db, &hygiene, val, krate, macro_resolver, diagnostic_sink) {
250 Ok(Err(err)) => return Ok(Err(err)),
251 Err(err) => return Err(err),
256 // check if the whole original syntax is replaced
257 if child.syntax() == &original {
258 return Ok(Ok(insert));
261 replacements.push((child, insert));
264 replacements.into_iter().rev().for_each(|(old, new)| ted::replace(old.syntax(), new));