]> git.lizzy.rs Git - rust.git/blob - crates/hir_expand/src/db.rs
Merge #10076
[rust.git] / crates / hir_expand / src / db.rs
1 //! Defines database & queries for macro expansion.
2
3 use std::sync::Arc;
4
5 use base_db::{salsa, SourceDatabase};
6 use itertools::Itertools;
7 use limit::Limit;
8 use mbe::{ExpandError, ExpandResult};
9 use parser::{FragmentKind, T};
10 use syntax::{
11     algo::diff,
12     ast::{self, AttrsOwner, NameOwner},
13     AstNode, GreenNode, Parse, SyntaxNode, SyntaxToken, TextRange,
14 };
15
16 use crate::{
17     ast_id_map::AstIdMap, hygiene::HygieneFrame, BuiltinAttrExpander, BuiltinDeriveExpander,
18     BuiltinFnLikeExpander, HirFileId, HirFileIdRepr, MacroCallId, MacroCallKind, MacroCallLoc,
19     MacroDefId, MacroDefKind, MacroFile, ProcMacroExpander,
20 };
21
22 /// Total limit on the number of tokens produced by any macro invocation.
23 ///
24 /// If an invocation produces more tokens than this limit, it will not be stored in the database and
25 /// an error will be emitted.
26 ///
27 /// Actual max for `analysis-stats .` at some point: 30672.
28 static TOKEN_LIMIT: Limit = Limit::new(524_288);
29
30 #[derive(Debug, Clone, Eq, PartialEq)]
31 pub enum TokenExpander {
32     /// Old-style `macro_rules`.
33     MacroRules { mac: mbe::MacroRules, def_site_token_map: mbe::TokenMap },
34     /// AKA macros 2.0.
35     MacroDef { mac: mbe::MacroDef, def_site_token_map: mbe::TokenMap },
36     /// Stuff like `line!` and `file!`.
37     Builtin(BuiltinFnLikeExpander),
38     /// `global_allocator` and such.
39     BuiltinAttr(BuiltinAttrExpander),
40     /// `derive(Copy)` and such.
41     BuiltinDerive(BuiltinDeriveExpander),
42     /// The thing we love the most here in rust-analyzer -- procedural macros.
43     ProcMacro(ProcMacroExpander),
44 }
45
46 impl TokenExpander {
47     fn expand(
48         &self,
49         db: &dyn AstDatabase,
50         id: MacroCallId,
51         tt: &tt::Subtree,
52     ) -> mbe::ExpandResult<tt::Subtree> {
53         match self {
54             TokenExpander::MacroRules { mac, .. } => mac.expand(tt),
55             TokenExpander::MacroDef { mac, .. } => mac.expand(tt),
56             TokenExpander::Builtin(it) => it.expand(db, id, tt),
57             TokenExpander::BuiltinAttr(it) => it.expand(db, id, tt),
58             TokenExpander::BuiltinDerive(it) => it.expand(db, id, tt),
59             TokenExpander::ProcMacro(_) => {
60                 // We store the result in salsa db to prevent non-deterministic behavior in
61                 // some proc-macro implementation
62                 // See #4315 for details
63                 db.expand_proc_macro(id)
64             }
65         }
66     }
67
68     pub(crate) fn map_id_down(&self, id: tt::TokenId) -> tt::TokenId {
69         match self {
70             TokenExpander::MacroRules { mac, .. } => mac.map_id_down(id),
71             TokenExpander::MacroDef { mac, .. } => mac.map_id_down(id),
72             TokenExpander::Builtin(..)
73             | TokenExpander::BuiltinAttr(..)
74             | TokenExpander::BuiltinDerive(..)
75             | TokenExpander::ProcMacro(..) => id,
76         }
77     }
78
79     pub(crate) fn map_id_up(&self, id: tt::TokenId) -> (tt::TokenId, mbe::Origin) {
80         match self {
81             TokenExpander::MacroRules { mac, .. } => mac.map_id_up(id),
82             TokenExpander::MacroDef { mac, .. } => mac.map_id_up(id),
83             TokenExpander::Builtin(..)
84             | TokenExpander::BuiltinAttr(..)
85             | TokenExpander::BuiltinDerive(..)
86             | TokenExpander::ProcMacro(..) => (id, mbe::Origin::Call),
87         }
88     }
89 }
90
91 // FIXME: rename to ExpandDatabase
92 #[salsa::query_group(AstDatabaseStorage)]
93 pub trait AstDatabase: SourceDatabase {
94     fn ast_id_map(&self, file_id: HirFileId) -> Arc<AstIdMap>;
95
96     /// Main public API -- parses a hir file, not caring whether it's a real
97     /// file or a macro expansion.
98     #[salsa::transparent]
99     fn parse_or_expand(&self, file_id: HirFileId) -> Option<SyntaxNode>;
100     /// Implementation for the macro case.
101     fn parse_macro_expansion(
102         &self,
103         macro_file: MacroFile,
104     ) -> ExpandResult<Option<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)>>;
105
106     /// Macro ids. That's probably the tricksiest bit in rust-analyzer, and the
107     /// reason why we use salsa at all.
108     ///
109     /// We encode macro definitions into ids of macro calls, this what allows us
110     /// to be incremental.
111     #[salsa::interned]
112     fn intern_macro(&self, macro_call: MacroCallLoc) -> MacroCallId;
113
114     /// Lowers syntactic macro call to a token tree representation.
115     #[salsa::transparent]
116     fn macro_arg(&self, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>>;
117     /// Extracts syntax node, corresponding to a macro call. That's a firewall
118     /// query, only typing in the macro call itself changes the returned
119     /// subtree.
120     fn macro_arg_text(&self, id: MacroCallId) -> Option<GreenNode>;
121     /// Gets the expander for this macro. This compiles declarative macros, and
122     /// just fetches procedural ones.
123     fn macro_def(&self, id: MacroDefId) -> Option<Arc<TokenExpander>>;
124
125     /// Expand macro call to a token tree. This query is LRUed (we keep 128 or so results in memory)
126     fn macro_expand(&self, macro_call: MacroCallId) -> ExpandResult<Option<Arc<tt::Subtree>>>;
127     /// Special case of the previous query for procedural macros. We can't LRU
128     /// proc macros, since they are not deterministic in general, and
129     /// non-determinism breaks salsa in a very, very, very bad way. @edwin0cheng
130     /// heroically debugged this once!
131     fn expand_proc_macro(&self, call: MacroCallId) -> ExpandResult<tt::Subtree>;
132     /// Firewall query that returns the error from the `macro_expand` query.
133     fn macro_expand_error(&self, macro_call: MacroCallId) -> Option<ExpandError>;
134
135     fn hygiene_frame(&self, file_id: HirFileId) -> Arc<HygieneFrame>;
136 }
137
138 /// This expands the given macro call, but with different arguments. This is
139 /// used for completion, where we want to see what 'would happen' if we insert a
140 /// token. The `token_to_map` mapped down into the expansion, with the mapped
141 /// token returned.
142 pub fn expand_speculative(
143     db: &dyn AstDatabase,
144     actual_macro_call: MacroCallId,
145     speculative_args: &ast::TokenTree,
146     token_to_map: SyntaxToken,
147 ) -> Option<(SyntaxNode, SyntaxToken)> {
148     let (tt, tmap_1) = mbe::syntax_node_to_token_tree(speculative_args.syntax());
149     let range =
150         token_to_map.text_range().checked_sub(speculative_args.syntax().text_range().start())?;
151     let token_id = tmap_1.token_by_range(range)?;
152
153     let macro_def = {
154         let loc: MacroCallLoc = db.lookup_intern_macro(actual_macro_call);
155         db.macro_def(loc.def)?
156     };
157
158     let speculative_expansion = macro_def.expand(db, actual_macro_call, &tt);
159
160     let fragment_kind = macro_fragment_kind(db, actual_macro_call);
161
162     let (node, tmap_2) =
163         mbe::token_tree_to_syntax_node(&speculative_expansion.value, fragment_kind).ok()?;
164
165     let token_id = macro_def.map_id_down(token_id);
166     let range = tmap_2.first_range_by_token(token_id, token_to_map.kind())?;
167     let token = node.syntax_node().covering_element(range).into_token()?;
168     Some((node.syntax_node(), token))
169 }
170
171 fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc<AstIdMap> {
172     let map = db.parse_or_expand(file_id).map(|it| AstIdMap::from_source(&it)).unwrap_or_default();
173     Arc::new(map)
174 }
175
176 fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Option<SyntaxNode> {
177     match file_id.0 {
178         HirFileIdRepr::FileId(file_id) => Some(db.parse(file_id).tree().syntax().clone()),
179         HirFileIdRepr::MacroFile(macro_file) => {
180             db.parse_macro_expansion(macro_file).value.map(|(it, _)| it.syntax_node())
181         }
182     }
183 }
184
185 fn parse_macro_expansion(
186     db: &dyn AstDatabase,
187     macro_file: MacroFile,
188 ) -> ExpandResult<Option<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)>> {
189     let _p = profile::span("parse_macro_expansion");
190     let result = db.macro_expand(macro_file.macro_call_id);
191
192     if let Some(err) = &result.err {
193         // Note:
194         // The final goal we would like to make all parse_macro success,
195         // such that the following log will not call anyway.
196         let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id);
197         let node = loc.kind.to_node(db);
198
199         // collect parent information for warning log
200         let parents =
201             std::iter::successors(loc.kind.file_id().call_node(db), |it| it.file_id.call_node(db))
202                 .map(|n| format!("{:#}", n.value))
203                 .collect::<Vec<_>>()
204                 .join("\n");
205
206         tracing::warn!(
207             "fail on macro_parse: (reason: {:?} macro_call: {:#}) parents: {}",
208             err,
209             node.value,
210             parents
211         );
212     }
213     let tt = match result.value {
214         Some(tt) => tt,
215         None => return ExpandResult { value: None, err: result.err },
216     };
217
218     let fragment_kind = macro_fragment_kind(db, macro_file.macro_call_id);
219
220     tracing::debug!("expanded = {}", tt.as_debug_string());
221     tracing::debug!("kind = {:?}", fragment_kind);
222
223     let (parse, rev_token_map) = match mbe::token_tree_to_syntax_node(&tt, fragment_kind) {
224         Ok(it) => it,
225         Err(err) => {
226             tracing::debug!(
227                 "failed to parse expansion to {:?} = {}",
228                 fragment_kind,
229                 tt.as_debug_string()
230             );
231             return ExpandResult::only_err(err);
232         }
233     };
234
235     match result.err {
236         Some(err) => {
237             // Safety check for recursive identity macro.
238             let node = parse.syntax_node();
239             let file: HirFileId = macro_file.into();
240             let call_node = match file.call_node(db) {
241                 Some(it) => it,
242                 None => {
243                     return ExpandResult::only_err(err);
244                 }
245             };
246             if is_self_replicating(&node, &call_node.value) {
247                 ExpandResult::only_err(err)
248             } else {
249                 ExpandResult { value: Some((parse, Arc::new(rev_token_map))), err: Some(err) }
250             }
251         }
252         None => {
253             tracing::debug!("parse = {:?}", parse.syntax_node().kind());
254             ExpandResult { value: Some((parse, Arc::new(rev_token_map))), err: None }
255         }
256     }
257 }
258
259 fn macro_arg(db: &dyn AstDatabase, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>> {
260     let arg = db.macro_arg_text(id)?;
261     let loc = db.lookup_intern_macro(id);
262
263     let node = SyntaxNode::new_root(arg);
264     let censor = match loc.kind {
265         MacroCallKind::FnLike { .. } => None,
266         MacroCallKind::Derive { derive_attr_index, .. } => match ast::Item::cast(node.clone()) {
267             Some(item) => item
268                 .attrs()
269                 .map(|attr| attr.syntax().text_range())
270                 .take(derive_attr_index as usize + 1)
271                 .fold1(TextRange::cover),
272             None => None,
273         },
274         MacroCallKind::Attr { invoc_attr_index, .. } => match ast::Item::cast(node.clone()) {
275             Some(item) => {
276                 item.attrs().nth(invoc_attr_index as usize).map(|attr| attr.syntax().text_range())
277             }
278             None => None,
279         },
280     };
281     let (mut tt, tmap) = mbe::syntax_node_to_token_tree_censored(&node, censor);
282
283     if loc.def.is_proc_macro() {
284         // proc macros expect their inputs without parentheses, MBEs expect it with them included
285         tt.delimiter = None;
286     }
287
288     Some(Arc::new((tt, tmap)))
289 }
290
291 fn macro_arg_text(db: &dyn AstDatabase, id: MacroCallId) -> Option<GreenNode> {
292     let loc = db.lookup_intern_macro(id);
293     let arg = loc.kind.arg(db)?;
294     if matches!(loc.kind, MacroCallKind::FnLike { .. }) {
295         let first = arg.first_child_or_token().map_or(T![.], |it| it.kind());
296         let last = arg.last_child_or_token().map_or(T![.], |it| it.kind());
297         let well_formed_tt =
298             matches!((first, last), (T!['('], T![')']) | (T!['['], T![']']) | (T!['{'], T!['}']));
299         if !well_formed_tt {
300             // Don't expand malformed (unbalanced) macro invocations. This is
301             // less than ideal, but trying to expand unbalanced  macro calls
302             // sometimes produces pathological, deeply nested code which breaks
303             // all kinds of things.
304             //
305             // Some day, we'll have explicit recursion counters for all
306             // recursive things, at which point this code might be removed.
307             cov_mark::hit!(issue9358_bad_macro_stack_overflow);
308             return None;
309         }
310     }
311     Some(arg.green().into())
312 }
313
314 fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<TokenExpander>> {
315     match id.kind {
316         MacroDefKind::Declarative(ast_id) => match ast_id.to_node(db) {
317             ast::Macro::MacroRules(macro_rules) => {
318                 let arg = macro_rules.token_tree()?;
319                 let (tt, def_site_token_map) = mbe::syntax_node_to_token_tree(arg.syntax());
320                 let mac = match mbe::MacroRules::parse(&tt) {
321                     Ok(it) => it,
322                     Err(err) => {
323                         let name = macro_rules.name().map(|n| n.to_string()).unwrap_or_default();
324                         tracing::warn!("fail on macro_def parse ({}): {:?} {:#?}", name, err, tt);
325                         return None;
326                     }
327                 };
328                 Some(Arc::new(TokenExpander::MacroRules { mac, def_site_token_map }))
329             }
330             ast::Macro::MacroDef(macro_def) => {
331                 let arg = macro_def.body()?;
332                 let (tt, def_site_token_map) = mbe::syntax_node_to_token_tree(arg.syntax());
333                 let mac = match mbe::MacroDef::parse(&tt) {
334                     Ok(it) => it,
335                     Err(err) => {
336                         let name = macro_def.name().map(|n| n.to_string()).unwrap_or_default();
337                         tracing::warn!("fail on macro_def parse ({}): {:?} {:#?}", name, err, tt);
338                         return None;
339                     }
340                 };
341                 Some(Arc::new(TokenExpander::MacroDef { mac, def_site_token_map }))
342             }
343         },
344         MacroDefKind::BuiltIn(expander, _) => Some(Arc::new(TokenExpander::Builtin(expander))),
345         MacroDefKind::BuiltInAttr(expander, _) => {
346             Some(Arc::new(TokenExpander::BuiltinAttr(expander)))
347         }
348         MacroDefKind::BuiltInDerive(expander, _) => {
349             Some(Arc::new(TokenExpander::BuiltinDerive(expander)))
350         }
351         MacroDefKind::BuiltInEager(..) => None,
352         MacroDefKind::ProcMacro(expander, ..) => Some(Arc::new(TokenExpander::ProcMacro(expander))),
353     }
354 }
355
356 fn macro_expand(db: &dyn AstDatabase, id: MacroCallId) -> ExpandResult<Option<Arc<tt::Subtree>>> {
357     let _p = profile::span("macro_expand");
358     let loc: MacroCallLoc = db.lookup_intern_macro(id);
359     if let Some(eager) = &loc.eager {
360         return ExpandResult {
361             value: Some(eager.arg_or_expansion.clone()),
362             // FIXME: There could be errors here!
363             err: None,
364         };
365     }
366
367     let macro_arg = match db.macro_arg(id) {
368         Some(it) => it,
369         None => return ExpandResult::str_err("Failed to lower macro args to token tree".into()),
370     };
371
372     let macro_rules = match db.macro_def(loc.def) {
373         Some(it) => it,
374         None => return ExpandResult::str_err("Failed to find macro definition".into()),
375     };
376     let ExpandResult { value: tt, err } = macro_rules.expand(db, id, &macro_arg.0);
377     // Set a hard limit for the expanded tt
378     let count = tt.count();
379     // XXX: Make ExpandResult a real error and use .map_err instead?
380     if TOKEN_LIMIT.check(count).is_err() {
381         return ExpandResult::str_err(format!(
382             "macro invocation exceeds token limit: produced {} tokens, limit is {}",
383             count,
384             TOKEN_LIMIT.inner(),
385         ));
386     }
387
388     ExpandResult { value: Some(Arc::new(tt)), err }
389 }
390
391 fn macro_expand_error(db: &dyn AstDatabase, macro_call: MacroCallId) -> Option<ExpandError> {
392     db.macro_expand(macro_call).err
393 }
394
395 fn expand_proc_macro(db: &dyn AstDatabase, id: MacroCallId) -> ExpandResult<tt::Subtree> {
396     let loc: MacroCallLoc = db.lookup_intern_macro(id);
397     let macro_arg = match db.macro_arg(id) {
398         Some(it) => it,
399         None => return ExpandResult::str_err("No arguments for proc-macro".to_string()),
400     };
401
402     let expander = match loc.def.kind {
403         MacroDefKind::ProcMacro(expander, ..) => expander,
404         _ => unreachable!(),
405     };
406
407     let attr_arg = match &loc.kind {
408         MacroCallKind::Attr { attr_args, .. } => {
409             let mut attr_args = attr_args.0.clone();
410             mbe::Shift::new(&macro_arg.0).shift_all(&mut attr_args);
411             Some(attr_args)
412         }
413         _ => None,
414     };
415
416     expander.expand(db, loc.krate, &macro_arg.0, attr_arg.as_ref())
417 }
418
419 fn is_self_replicating(from: &SyntaxNode, to: &SyntaxNode) -> bool {
420     if diff(from, to).is_empty() {
421         return true;
422     }
423     if let Some(stmts) = ast::MacroStmts::cast(from.clone()) {
424         if stmts.statements().any(|stmt| diff(stmt.syntax(), to).is_empty()) {
425             return true;
426         }
427         if let Some(expr) = stmts.expr() {
428             if diff(expr.syntax(), to).is_empty() {
429                 return true;
430             }
431         }
432     }
433     false
434 }
435
436 fn hygiene_frame(db: &dyn AstDatabase, file_id: HirFileId) -> Arc<HygieneFrame> {
437     Arc::new(HygieneFrame::new(db, file_id))
438 }
439
440 fn macro_fragment_kind(db: &dyn AstDatabase, id: MacroCallId) -> FragmentKind {
441     let loc: MacroCallLoc = db.lookup_intern_macro(id);
442     loc.kind.fragment_kind()
443 }