]> git.lizzy.rs Git - rust.git/blob - crates/hir_def/src/macro_expansion_tests.rs
internal: add integrated test for token censoring
[rust.git] / crates / hir_def / src / macro_expansion_tests.rs
1 //! This module contains tests for macro expansion. Effectively, it covers `tt`,
2 //! `mbe`, `proc_macro_api` and `hir_expand` crates. This might seem like a
3 //! wrong architecture at the first glance, but is intentional.
4 //!
5 //! Physically, macro expansion process is intertwined with name resolution. You
6 //! can not expand *just* the syntax. So, to be able to write integration tests
7 //! of the "expand this code please" form, we have to do it after name
8 //! resolution. That is, in this crate. We *could* fake some dependencies and
9 //! write unit-tests (in fact, we used to do that), but that makes tests brittle
10 //! and harder to understand.
11
12 mod mbe;
13 mod builtin_fn_macro;
14 mod builtin_derive_macro;
15 mod proc_macros;
16
17 use std::{iter, ops::Range};
18
19 use base_db::{fixture::WithFixture, SourceDatabase};
20 use expect_test::Expect;
21 use hir_expand::{db::AstDatabase, InFile, MacroFile};
22 use stdx::format_to;
23 use syntax::{
24     ast::{self, edit::IndentLevel},
25     AstNode,
26     SyntaxKind::{COMMENT, EOF, IDENT, LIFETIME_IDENT},
27     SyntaxNode, T,
28 };
29
30 use crate::{
31     db::DefDatabase, nameres::ModuleSource, resolver::HasResolver, src::HasSource, test_db::TestDB,
32     AdtId, AsMacroCall, Lookup, ModuleDefId,
33 };
34
35 #[track_caller]
36 fn check(ra_fixture: &str, mut expect: Expect) {
37     let db = TestDB::with_files(ra_fixture);
38     let krate = db.crate_graph().iter().next().unwrap();
39     let def_map = db.crate_def_map(krate);
40     let local_id = def_map.root();
41     let module = def_map.module_id(local_id);
42     let resolver = module.resolver(&db);
43     let source = def_map[local_id].definition_source(&db);
44     let source_file = match source.value {
45         ModuleSource::SourceFile(it) => it,
46         ModuleSource::Module(_) | ModuleSource::BlockExpr(_) => panic!(),
47     };
48
49     // What we want to do is to replace all macros (fn-like, derive, attr) with
50     // their expansions. Turns out, we don't actually store enough information
51     // to do this precisely though! Specifically, if a macro expands to nothing,
52     // it leaves zero traces in def-map, so we can't get its expansion after the
53     // fact.
54     //
55     // This is the usual
56     // <https://github.com/rust-analyzer/rust-analyzer/issues/3407>
57     // resolve/record tension!
58     //
59     // So here we try to do a resolve, which is necessary a heuristic. For macro
60     // calls, we use `as_call_id_with_errors`. For derives, we look at the impls
61     // in the module and assume that, if impls's source is a different
62     // `HirFileId`, than it came from macro expansion.
63
64     let mut expansions = Vec::new();
65     for macro_call in source_file.syntax().descendants().filter_map(ast::MacroCall::cast) {
66         let macro_call = InFile::new(source.file_id, &macro_call);
67         let mut error = None;
68         let macro_call_id = macro_call
69             .as_call_id_with_errors(
70                 &db,
71                 krate,
72                 |path| resolver.resolve_path_as_macro(&db, &path),
73                 &mut |err| error = Some(err),
74             )
75             .unwrap()
76             .unwrap();
77         let macro_file = MacroFile { macro_call_id };
78         let mut expansion_result = db.parse_macro_expansion(macro_file);
79         expansion_result.err = expansion_result.err.or(error);
80         expansions.push((macro_call.value.clone(), expansion_result));
81     }
82
83     let mut expanded_text = source_file.to_string();
84
85     for (call, exp) in expansions.into_iter().rev() {
86         let mut tree = false;
87         let mut expect_errors = false;
88         for comment in call.syntax().children_with_tokens().filter(|it| it.kind() == COMMENT) {
89             tree |= comment.to_string().contains("+tree");
90             expect_errors |= comment.to_string().contains("+errors");
91         }
92
93         let mut expn_text = String::new();
94         if let Some(err) = exp.err {
95             format_to!(expn_text, "/* error: {} */", err);
96         }
97         if let Some((parse, _token_map)) = exp.value {
98             if expect_errors {
99                 assert!(!parse.errors().is_empty(), "no parse errors in expansion");
100                 for e in parse.errors() {
101                     format_to!(expn_text, "/* parse error: {} */\n", e);
102                 }
103             } else {
104                 assert!(
105                     parse.errors().is_empty(),
106                     "parse errors in expansion: \n{:#?}",
107                     parse.errors()
108                 );
109             }
110             let pp = pretty_print_macro_expansion(parse.syntax_node());
111             let indent = IndentLevel::from_node(call.syntax());
112             let pp = reindent(indent, pp);
113             format_to!(expn_text, "{}", pp);
114
115             if tree {
116                 let tree = format!("{:#?}", parse.syntax_node())
117                     .split_inclusive("\n")
118                     .map(|line| format!("// {}", line))
119                     .collect::<String>();
120                 format_to!(expn_text, "\n{}", tree)
121             }
122         }
123         let range = call.syntax().text_range();
124         let range: Range<usize> = range.into();
125         expanded_text.replace_range(range, &expn_text)
126     }
127
128     for decl_id in def_map[local_id].scope.declarations() {
129         if let ModuleDefId::AdtId(AdtId::StructId(struct_id)) = decl_id {
130             let src = struct_id.lookup(&db).source(&db);
131             if src.file_id.is_attr_macro(&db) || src.file_id.is_custom_derive(&db) {
132                 let pp = pretty_print_macro_expansion(src.value.syntax().clone());
133                 format_to!(expanded_text, "\n{}", pp)
134             }
135         }
136     }
137
138     for impl_id in def_map[local_id].scope.impls() {
139         let src = impl_id.lookup(&db).source(&db);
140         if src.file_id.is_builtin_derive(&db).is_some() {
141             let pp = pretty_print_macro_expansion(src.value.syntax().clone());
142             format_to!(expanded_text, "\n{}", pp)
143         }
144     }
145
146     expect.indent(false);
147     expect.assert_eq(&expanded_text);
148 }
149
150 fn reindent(indent: IndentLevel, pp: String) -> String {
151     if !pp.contains('\n') {
152         return pp;
153     }
154     let mut lines = pp.split_inclusive('\n');
155     let mut res = lines.next().unwrap().to_string();
156     for line in lines {
157         if line.trim().is_empty() {
158             res.push_str(&line)
159         } else {
160             format_to!(res, "{}{}", indent, line)
161         }
162     }
163     res
164 }
165
166 fn pretty_print_macro_expansion(expn: SyntaxNode) -> String {
167     let mut res = String::new();
168     let mut prev_kind = EOF;
169     let mut indent_level = 0;
170     for token in iter::successors(expn.first_token(), |t| t.next_token()) {
171         let curr_kind = token.kind();
172         let space = match (prev_kind, curr_kind) {
173             _ if prev_kind.is_trivia() || curr_kind.is_trivia() => "",
174             (T!['{'], T!['}']) => "",
175             (T![=], _) | (_, T![=]) => " ",
176             (_, T!['{']) => " ",
177             (T![;] | T!['{'] | T!['}'], _) => "\n",
178             (_, T!['}']) => "\n",
179             (IDENT | LIFETIME_IDENT, IDENT | LIFETIME_IDENT) => " ",
180             _ if prev_kind.is_keyword() && curr_kind.is_keyword() => " ",
181             (IDENT, _) if curr_kind.is_keyword() => " ",
182             (_, IDENT) if prev_kind.is_keyword() => " ",
183             (T![>], IDENT) => " ",
184             (T![>], _) if curr_kind.is_keyword() => " ",
185             (T![->], _) | (_, T![->]) => " ",
186             (T![&&], _) | (_, T![&&]) => " ",
187             (T![,], _) => " ",
188             (T![:], IDENT | T!['(']) => " ",
189             (T![:], _) if curr_kind.is_keyword() => " ",
190             (T![fn], T!['(']) => "",
191             (T![']'], _) if curr_kind.is_keyword() => " ",
192             (T![']'], T![#]) => "\n",
193             _ if prev_kind.is_keyword() => " ",
194             _ => "",
195         };
196
197         match prev_kind {
198             T!['{'] => indent_level += 1,
199             T!['}'] => indent_level -= 1,
200             _ => (),
201         }
202
203         res.push_str(space);
204         if space == "\n" {
205             let level = if curr_kind == T!['}'] { indent_level - 1 } else { indent_level };
206             res.push_str(&"    ".repeat(level));
207         }
208         prev_kind = curr_kind;
209         format_to!(res, "{}", token)
210     }
211     res
212 }