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.
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.
14 mod builtin_derive_macro;
17 use std::{iter, ops::Range};
19 use base_db::{fixture::WithFixture, SourceDatabase};
20 use expect_test::Expect;
21 use hir_expand::{db::AstDatabase, InFile, MacroFile};
24 ast::{self, edit::IndentLevel},
26 SyntaxKind::{COMMENT, EOF, IDENT, LIFETIME_IDENT},
31 db::DefDatabase, nameres::ModuleSource, resolver::HasResolver, src::HasSource, test_db::TestDB,
32 AdtId, AsMacroCall, Lookup, ModuleDefId,
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!(),
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
56 // <https://github.com/rust-analyzer/rust-analyzer/issues/3407>
57 // resolve/record tension!
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.
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, ¯o_call);
68 let macro_call_id = macro_call
69 .as_call_id_with_errors(
72 |path| resolver.resolve_path_as_macro(&db, &path),
73 &mut |err| error = Some(err),
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));
83 let mut expanded_text = source_file.to_string();
85 for (call, exp) in expansions.into_iter().rev() {
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");
93 let mut expn_text = String::new();
94 if let Some(err) = exp.err {
95 format_to!(expn_text, "/* error: {} */", err);
97 if let Some((parse, _token_map)) = exp.value {
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);
105 parse.errors().is_empty(),
106 "parse errors in expansion: \n{:#?}",
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);
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)
123 let range = call.syntax().text_range();
124 let range: Range<usize> = range.into();
125 expanded_text.replace_range(range, &expn_text)
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)
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)
146 expect.indent(false);
147 expect.assert_eq(&expanded_text);
150 fn reindent(indent: IndentLevel, pp: String) -> String {
151 if !pp.contains('\n') {
154 let mut lines = pp.split_inclusive('\n');
155 let mut res = lines.next().unwrap().to_string();
157 if line.trim().is_empty() {
160 format_to!(res, "{}{}", indent, line)
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![=]) => " ",
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![&&]) => " ",
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() => " ",
198 T!['{'] => indent_level += 1,
199 T!['}'] => indent_level -= 1,
205 let level = if curr_kind == T!['}'] { indent_level - 1 } else { indent_level };
206 res.push_str(&" ".repeat(level));
208 prev_kind = curr_kind;
209 format_to!(res, "{}", token)