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.
12 use std::{iter, ops::Range};
14 use base_db::{fixture::WithFixture, SourceDatabase};
15 use expect_test::{expect, Expect};
16 use hir_expand::{db::AstDatabase, InFile, MacroFile};
20 SyntaxKind::{self, IDENT},
25 db::DefDatabase, nameres::ModuleSource, resolver::HasResolver, test_db::TestDB, AsMacroCall,
28 fn check(ra_fixture: &str, expect: Expect) {
29 let db = TestDB::with_files(ra_fixture);
30 let krate = db.crate_graph().iter().next().unwrap();
31 let def_map = db.crate_def_map(krate);
32 let local_id = def_map.root();
33 let module = def_map.module_id(local_id);
34 let resolver = module.resolver(&db);
35 let source = def_map[local_id].definition_source(&db);
36 let source_file = match source.value {
37 ModuleSource::SourceFile(it) => it,
38 ModuleSource::Module(_) | ModuleSource::BlockExpr(_) => panic!(),
41 let mut expansions = Vec::new();
42 for macro_call in source_file.syntax().descendants().filter_map(ast::MacroCall::cast) {
43 let macro_call = InFile::new(source.file_id, ¯o_call);
44 let macro_call_id = macro_call
45 .as_call_id_with_errors(
48 |path| resolver.resolve_path_as_macro(&db, &path),
49 &mut |err| panic!("{}", err),
53 let macro_file = MacroFile { macro_call_id };
54 let expansion_result = db.parse_macro_expansion(macro_file);
55 expansions.push((macro_call.value.clone(), expansion_result));
58 let mut expanded_text = source_file.to_string();
59 for (call, exp) in expansions.into_iter().rev() {
60 let mut expn_text = String::new();
61 if let Some(err) = exp.err {
62 format_to!(expn_text, "/* error: {} */", err);
64 if let Some((parse, _token_map)) = exp.value {
65 let pp = pretty_print_macro_expansion(parse.syntax_node());
66 format_to!(expn_text, "{}", pp);
68 let range = call.syntax().text_range();
69 let range: Range<usize> = range.into();
70 expanded_text.replace_range(range, &expn_text)
73 expect.assert_eq(&expanded_text);
76 fn pretty_print_macro_expansion(expn: SyntaxNode) -> String {
77 let mut res = String::new();
78 let mut prev_kind = SyntaxKind::EOF;
79 for token in iter::successors(expn.first_token(), |t| t.next_token()) {
80 let curr_kind = token.kind();
81 let needs_space = match (prev_kind, curr_kind) {
82 _ if prev_kind.is_trivia() || curr_kind.is_trivia() => false,
83 (T![=], _) | (_, T![=]) => true,
84 (IDENT, IDENT) => true,
85 (IDENT, _) => curr_kind.is_keyword(),
86 (_, IDENT) => prev_kind.is_keyword(),
93 prev_kind = curr_kind;
94 format_to!(res, "{}", token)
100 fn wrong_nesting_level() {
104 ($($i:ident);*) => ($i)
110 ($($i:ident);*) => ($i)
112 /* error: expected simple binding, found nested binding `i` */
118 fn expansion_does_not_parse_as_expression() {
125 fn f() { let _ = stmts!(); }
132 fn f() { let _ = /* error: could not convert tokens */; }
138 fn round_trips_compound_tokens() {
142 () => { type qual: ::T = qual::T; }
148 () => { type qual: ::T = qual::T; }
150 type qual: ::T = qual::T;