1 //! Defines database & queries for macro expansion.
5 use base_db::{salsa, SourceDatabase};
7 use mbe::{syntax_node_to_token_tree, ExpandError, ExpandResult};
8 use rustc_hash::FxHashSet;
11 ast::{self, HasAttrs},
12 AstNode, GreenNode, Parse, SyntaxNode, SyntaxToken, T,
16 ast_id_map::AstIdMap, hygiene::HygieneFrame, BuiltinAttrExpander, BuiltinDeriveExpander,
17 BuiltinFnLikeExpander, ExpandTo, HirFileId, HirFileIdRepr, MacroCallId, MacroCallKind,
18 MacroCallLoc, MacroDefId, MacroDefKind, MacroFile, ProcMacroExpander,
21 /// Total limit on the number of tokens produced by any macro invocation.
23 /// If an invocation produces more tokens than this limit, it will not be stored in the database and
24 /// an error will be emitted.
26 /// Actual max for `analysis-stats .` at some point: 30672.
27 static TOKEN_LIMIT: Limit = Limit::new(524_288);
29 #[derive(Debug, Clone, Eq, PartialEq)]
30 pub enum TokenExpander {
31 /// Old-style `macro_rules`.
32 MacroRules { mac: mbe::MacroRules, def_site_token_map: mbe::TokenMap },
34 MacroDef { mac: mbe::MacroDef, def_site_token_map: mbe::TokenMap },
35 /// Stuff like `line!` and `file!`.
36 Builtin(BuiltinFnLikeExpander),
37 /// `global_allocator` and such.
38 BuiltinAttr(BuiltinAttrExpander),
39 /// `derive(Copy)` and such.
40 BuiltinDerive(BuiltinDeriveExpander),
41 /// The thing we love the most here in rust-analyzer -- procedural macros.
42 ProcMacro(ProcMacroExpander),
51 ) -> mbe::ExpandResult<tt::Subtree> {
53 TokenExpander::MacroRules { mac, .. } => mac.expand(tt),
54 TokenExpander::MacroDef { mac, .. } => mac.expand(tt),
55 TokenExpander::Builtin(it) => it.expand(db, id, tt),
56 TokenExpander::BuiltinAttr(it) => it.expand(db, id, tt),
57 TokenExpander::BuiltinDerive(it) => it.expand(db, id, tt),
58 TokenExpander::ProcMacro(_) => {
59 // We store the result in salsa db to prevent non-deterministic behavior in
60 // some proc-macro implementation
61 // See #4315 for details
62 db.expand_proc_macro(id)
67 pub(crate) fn map_id_down(&self, id: tt::TokenId) -> tt::TokenId {
69 TokenExpander::MacroRules { mac, .. } => mac.map_id_down(id),
70 TokenExpander::MacroDef { mac, .. } => mac.map_id_down(id),
71 TokenExpander::Builtin(..)
72 | TokenExpander::BuiltinAttr(..)
73 | TokenExpander::BuiltinDerive(..)
74 | TokenExpander::ProcMacro(..) => id,
78 pub(crate) fn map_id_up(&self, id: tt::TokenId) -> (tt::TokenId, mbe::Origin) {
80 TokenExpander::MacroRules { mac, .. } => mac.map_id_up(id),
81 TokenExpander::MacroDef { mac, .. } => mac.map_id_up(id),
82 TokenExpander::Builtin(..)
83 | TokenExpander::BuiltinAttr(..)
84 | TokenExpander::BuiltinDerive(..)
85 | TokenExpander::ProcMacro(..) => (id, mbe::Origin::Call),
90 // FIXME: rename to ExpandDatabase
91 #[salsa::query_group(AstDatabaseStorage)]
92 pub trait AstDatabase: SourceDatabase {
93 fn ast_id_map(&self, file_id: HirFileId) -> Arc<AstIdMap>;
95 /// Main public API -- parses a hir file, not caring whether it's a real
96 /// file or a macro expansion.
98 fn parse_or_expand(&self, file_id: HirFileId) -> Option<SyntaxNode>;
99 /// Implementation for the macro case.
100 fn parse_macro_expansion(
102 macro_file: MacroFile,
103 ) -> ExpandResult<Option<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)>>;
105 /// Macro ids. That's probably the tricksiest bit in rust-analyzer, and the
106 /// reason why we use salsa at all.
108 /// We encode macro definitions into ids of macro calls, this what allows us
109 /// to be incremental.
111 fn intern_macro(&self, macro_call: MacroCallLoc) -> MacroCallId;
113 /// Lowers syntactic macro call to a token tree representation.
114 #[salsa::transparent]
115 fn macro_arg(&self, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>>;
116 /// Extracts syntax node, corresponding to a macro call. That's a firewall
117 /// query, only typing in the macro call itself changes the returned
119 fn macro_arg_text(&self, id: MacroCallId) -> Option<GreenNode>;
120 /// Gets the expander for this macro. This compiles declarative macros, and
121 /// just fetches procedural ones.
122 fn macro_def(&self, id: MacroDefId) -> Result<Arc<TokenExpander>, mbe::ParseError>;
124 /// Expand macro call to a token tree. This query is LRUed (we keep 128 or so results in memory)
125 fn macro_expand(&self, macro_call: MacroCallId) -> ExpandResult<Option<Arc<tt::Subtree>>>;
126 /// Special case of the previous query for procedural macros. We can't LRU
127 /// proc macros, since they are not deterministic in general, and
128 /// non-determinism breaks salsa in a very, very, very bad way. @edwin0cheng
129 /// heroically debugged this once!
130 fn expand_proc_macro(&self, call: MacroCallId) -> ExpandResult<tt::Subtree>;
131 /// Firewall query that returns the error from the `macro_expand` query.
132 fn macro_expand_error(&self, macro_call: MacroCallId) -> Option<ExpandError>;
134 fn hygiene_frame(&self, file_id: HirFileId) -> Arc<HygieneFrame>;
137 /// This expands the given macro call, but with different arguments. This is
138 /// used for completion, where we want to see what 'would happen' if we insert a
139 /// token. The `token_to_map` mapped down into the expansion, with the mapped
141 pub fn expand_speculative(
142 db: &dyn AstDatabase,
143 actual_macro_call: MacroCallId,
144 speculative_args: &SyntaxNode,
145 token_to_map: SyntaxToken,
146 ) -> Option<(SyntaxNode, SyntaxToken)> {
147 let loc = db.lookup_intern_macro(actual_macro_call);
148 let macro_def = db.macro_def(loc.def).ok()?;
149 let token_range = token_to_map.text_range();
151 // Build the subtree and token mapping for the speculative args
152 let censor = censor_for_macro_input(&loc, &speculative_args);
153 let (mut tt, spec_args_tmap) =
154 mbe::syntax_node_to_token_tree_censored(&speculative_args, &censor);
156 let (attr_arg, token_id) = match loc.kind {
157 MacroCallKind::Attr { invoc_attr_index, .. } => {
158 // Attributes may have an input token tree, build the subtree and map for this as well
159 // then try finding a token id for our token if it is inside this input subtree.
160 let item = ast::Item::cast(speculative_args.clone())?;
161 let attr = item.attrs().nth(invoc_attr_index as usize)?;
162 match attr.token_tree() {
163 Some(token_tree) => {
164 let (mut tree, map) = syntax_node_to_token_tree(attr.token_tree()?.syntax());
165 tree.delimiter = None;
167 let shift = mbe::Shift::new(&tt);
168 shift.shift_all(&mut tree);
170 let token_id = if token_tree.syntax().text_range().contains_range(token_range) {
171 let attr_input_start =
172 token_tree.left_delimiter_token()?.text_range().start();
173 let range = token_range.checked_sub(attr_input_start)?;
174 let token_id = shift.shift(map.token_by_range(range)?);
179 (Some(tree), token_id)
186 let token_id = match token_id {
187 Some(token_id) => token_id,
188 // token wasn't inside an attribute input so it has to be in the general macro input
190 let range = token_range.checked_sub(speculative_args.text_range().start())?;
191 let token_id = spec_args_tmap.token_by_range(range)?;
192 macro_def.map_id_down(token_id)
196 // Do the actual expansion, we need to directly expand the proc macro due to the attribute args
197 // Otherwise the expand query will fetch the non speculative attribute args and pass those instead.
198 let speculative_expansion = if let MacroDefKind::ProcMacro(expander, ..) = loc.def.kind {
200 expander.expand(db, loc.krate, &tt, attr_arg.as_ref())
202 macro_def.expand(db, actual_macro_call, &tt)
205 let expand_to = macro_expand_to(db, actual_macro_call);
206 let (node, rev_tmap) =
207 token_tree_to_syntax_node(&speculative_expansion.value, expand_to).ok()?;
209 let range = rev_tmap.first_range_by_token(token_id, token_to_map.kind())?;
210 let token = node.syntax_node().covering_element(range).into_token()?;
211 Some((node.syntax_node(), token))
214 fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc<AstIdMap> {
215 let map = db.parse_or_expand(file_id).map(|it| AstIdMap::from_source(&it)).unwrap_or_default();
219 fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Option<SyntaxNode> {
221 HirFileIdRepr::FileId(file_id) => Some(db.parse(file_id).tree().syntax().clone()),
222 HirFileIdRepr::MacroFile(macro_file) => {
223 db.parse_macro_expansion(macro_file).value.map(|(it, _)| it.syntax_node())
228 fn parse_macro_expansion(
229 db: &dyn AstDatabase,
230 macro_file: MacroFile,
231 ) -> ExpandResult<Option<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)>> {
232 let _p = profile::span("parse_macro_expansion");
233 let result = db.macro_expand(macro_file.macro_call_id);
235 if let Some(err) = &result.err {
237 // The final goal we would like to make all parse_macro success,
238 // such that the following log will not call anyway.
239 let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id);
240 let node = loc.kind.to_node(db);
242 // collect parent information for warning log
244 std::iter::successors(loc.kind.file_id().call_node(db), |it| it.file_id.call_node(db))
245 .map(|n| format!("{:#}", n.value))
250 "fail on macro_parse: (reason: {:?} macro_call: {:#}) parents: {}",
256 let tt = match result.value {
258 None => return ExpandResult { value: None, err: result.err },
261 let expand_to = macro_expand_to(db, macro_file.macro_call_id);
263 tracing::debug!("expanded = {}", tt.as_debug_string());
264 tracing::debug!("kind = {:?}", expand_to);
266 let (parse, rev_token_map) = match token_tree_to_syntax_node(&tt, expand_to) {
270 "failed to parse expansion to {:?} = {}",
274 return ExpandResult::only_err(err);
280 // Safety check for recursive identity macro.
281 let node = parse.syntax_node();
282 let file: HirFileId = macro_file.into();
283 let call_node = match file.call_node(db) {
286 return ExpandResult::only_err(err);
289 if is_self_replicating(&node, &call_node.value) {
290 ExpandResult::only_err(err)
292 ExpandResult { value: Some((parse, Arc::new(rev_token_map))), err: Some(err) }
296 tracing::debug!("parse = {:?}", parse.syntax_node().kind());
297 ExpandResult { value: Some((parse, Arc::new(rev_token_map))), err: None }
302 fn macro_arg(db: &dyn AstDatabase, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>> {
303 let arg = db.macro_arg_text(id)?;
304 let loc = db.lookup_intern_macro(id);
306 let node = SyntaxNode::new_root(arg);
307 let censor = censor_for_macro_input(&loc, &node);
308 let (mut tt, tmap) = mbe::syntax_node_to_token_tree_censored(&node, &censor);
310 if loc.def.is_proc_macro() {
311 // proc macros expect their inputs without parentheses, MBEs expect it with them included
315 Some(Arc::new((tt, tmap)))
318 fn censor_for_macro_input(loc: &MacroCallLoc, node: &SyntaxNode) -> FxHashSet<SyntaxNode> {
320 let censor = match loc.kind {
321 MacroCallKind::FnLike { .. } => return None,
322 MacroCallKind::Derive { derive_attr_index, .. } => {
323 cov_mark::hit!(derive_censoring);
324 ast::Item::cast(node.clone())?
326 .take(derive_attr_index as usize + 1)
327 .filter(|attr| attr.simple_name().as_deref() == Some("derive"))
328 .map(|it| it.syntax().clone())
331 MacroCallKind::Attr { invoc_attr_index, .. } => {
332 cov_mark::hit!(attribute_macro_attr_censoring);
333 ast::Item::cast(node.clone())?
335 .nth(invoc_attr_index as usize)
336 .map(|attr| attr.syntax().clone())
346 fn macro_arg_text(db: &dyn AstDatabase, id: MacroCallId) -> Option<GreenNode> {
347 let loc = db.lookup_intern_macro(id);
348 let arg = loc.kind.arg(db)?;
349 if matches!(loc.kind, MacroCallKind::FnLike { .. }) {
350 let first = arg.first_child_or_token().map_or(T![.], |it| it.kind());
351 let last = arg.last_child_or_token().map_or(T![.], |it| it.kind());
353 matches!((first, last), (T!['('], T![')']) | (T!['['], T![']']) | (T!['{'], T!['}']));
355 // Don't expand malformed (unbalanced) macro invocations. This is
356 // less than ideal, but trying to expand unbalanced macro calls
357 // sometimes produces pathological, deeply nested code which breaks
358 // all kinds of things.
360 // Some day, we'll have explicit recursion counters for all
361 // recursive things, at which point this code might be removed.
362 cov_mark::hit!(issue9358_bad_macro_stack_overflow);
366 Some(arg.green().into())
369 fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Result<Arc<TokenExpander>, mbe::ParseError> {
371 MacroDefKind::Declarative(ast_id) => match ast_id.to_node(db) {
372 ast::Macro::MacroRules(macro_rules) => {
373 let arg = macro_rules
375 .ok_or_else(|| mbe::ParseError::Expected("expected a token tree".into()))?;
376 let (tt, def_site_token_map) = mbe::syntax_node_to_token_tree(arg.syntax());
377 let mac = mbe::MacroRules::parse(&tt)?;
378 Ok(Arc::new(TokenExpander::MacroRules { mac, def_site_token_map }))
380 ast::Macro::MacroDef(macro_def) => {
383 .ok_or_else(|| mbe::ParseError::Expected("expected a token tree".into()))?;
384 let (tt, def_site_token_map) = mbe::syntax_node_to_token_tree(arg.syntax());
385 let mac = mbe::MacroDef::parse(&tt)?;
386 Ok(Arc::new(TokenExpander::MacroDef { mac, def_site_token_map }))
389 MacroDefKind::BuiltIn(expander, _) => Ok(Arc::new(TokenExpander::Builtin(expander))),
390 MacroDefKind::BuiltInAttr(expander, _) => {
391 Ok(Arc::new(TokenExpander::BuiltinAttr(expander)))
393 MacroDefKind::BuiltInDerive(expander, _) => {
394 Ok(Arc::new(TokenExpander::BuiltinDerive(expander)))
396 MacroDefKind::BuiltInEager(..) => {
397 // FIXME: Return a random error here just to make the types align.
398 // This obviously should do something real instead.
399 Err(mbe::ParseError::UnexpectedToken("unexpected eager macro".to_string()))
401 MacroDefKind::ProcMacro(expander, ..) => Ok(Arc::new(TokenExpander::ProcMacro(expander))),
405 fn macro_expand(db: &dyn AstDatabase, id: MacroCallId) -> ExpandResult<Option<Arc<tt::Subtree>>> {
406 let _p = profile::span("macro_expand");
407 let loc: MacroCallLoc = db.lookup_intern_macro(id);
408 if let Some(eager) = &loc.eager {
409 return ExpandResult {
410 value: Some(eager.arg_or_expansion.clone()),
411 // FIXME: There could be errors here!
416 let macro_arg = match db.macro_arg(id) {
418 None => return ExpandResult::str_err("Failed to lower macro args to token tree".into()),
421 let expander = match db.macro_def(loc.def) {
423 // FIXME: This is weird -- we effectively report macro *definition*
424 // errors lazily, when we try to expand the macro. Instead, they should
425 // be reported at the definition site (when we construct a def map).
426 Err(err) => return ExpandResult::str_err(format!("invalid macro definition: {}", err)),
428 let ExpandResult { value: tt, err } = expander.expand(db, id, ¯o_arg.0);
429 // Set a hard limit for the expanded tt
430 let count = tt.count();
431 // XXX: Make ExpandResult a real error and use .map_err instead?
432 if TOKEN_LIMIT.check(count).is_err() {
433 return ExpandResult::str_err(format!(
434 "macro invocation exceeds token limit: produced {} tokens, limit is {}",
440 ExpandResult { value: Some(Arc::new(tt)), err }
443 fn macro_expand_error(db: &dyn AstDatabase, macro_call: MacroCallId) -> Option<ExpandError> {
444 db.macro_expand(macro_call).err
447 fn expand_proc_macro(db: &dyn AstDatabase, id: MacroCallId) -> ExpandResult<tt::Subtree> {
448 let loc: MacroCallLoc = db.lookup_intern_macro(id);
449 let macro_arg = match db.macro_arg(id) {
451 None => return ExpandResult::str_err("No arguments for proc-macro".to_string()),
454 let expander = match loc.def.kind {
455 MacroDefKind::ProcMacro(expander, ..) => expander,
459 let attr_arg = match &loc.kind {
460 MacroCallKind::Attr { attr_args, .. } => {
461 let mut attr_args = attr_args.0.clone();
462 mbe::Shift::new(¯o_arg.0).shift_all(&mut attr_args);
468 expander.expand(db, loc.krate, ¯o_arg.0, attr_arg.as_ref())
471 fn is_self_replicating(from: &SyntaxNode, to: &SyntaxNode) -> bool {
472 if diff(from, to).is_empty() {
475 if let Some(stmts) = ast::MacroStmts::cast(from.clone()) {
476 if stmts.statements().any(|stmt| diff(stmt.syntax(), to).is_empty()) {
479 if let Some(expr) = stmts.expr() {
480 if diff(expr.syntax(), to).is_empty() {
488 fn hygiene_frame(db: &dyn AstDatabase, file_id: HirFileId) -> Arc<HygieneFrame> {
489 Arc::new(HygieneFrame::new(db, file_id))
492 fn macro_expand_to(db: &dyn AstDatabase, id: MacroCallId) -> ExpandTo {
493 let loc: MacroCallLoc = db.lookup_intern_macro(id);
497 fn token_tree_to_syntax_node(
500 ) -> Result<(Parse<SyntaxNode>, mbe::TokenMap), ExpandError> {
501 let entry_point = match expand_to {
502 ExpandTo::Statements => mbe::ParserEntryPoint::Statements,
503 ExpandTo::Items => mbe::ParserEntryPoint::Items,
504 ExpandTo::Pattern => mbe::ParserEntryPoint::Pattern,
505 ExpandTo::Type => mbe::ParserEntryPoint::Type,
506 ExpandTo::Expr => mbe::ParserEntryPoint::Expr,
508 mbe::token_tree_to_syntax_node(tt, entry_point)