2021-10-09 05:42:32 -05:00
|
|
|
//! This module contains tests for macro expansion. Effectively, it covers `tt`,
|
|
|
|
//! `mbe`, `proc_macro_api` and `hir_expand` crates. This might seem like a
|
|
|
|
//! wrong architecture at the first glance, but is intentional.
|
|
|
|
//!
|
|
|
|
//! Physically, macro expansion process is intertwined with name resolution. You
|
|
|
|
//! can not expand *just* the syntax. So, to be able to write integration tests
|
|
|
|
//! of the "expand this code please" form, we have to do it after name
|
|
|
|
//! resolution. That is, in this crate. We *could* fake some dependencies and
|
|
|
|
//! write unit-tests (in fact, we used to do that), but that makes tests brittle
|
|
|
|
//! and harder to understand.
|
|
|
|
|
2021-10-09 06:48:38 -05:00
|
|
|
mod mbe;
|
2021-10-10 07:44:03 -05:00
|
|
|
mod builtin_fn_macro;
|
2021-10-09 06:48:38 -05:00
|
|
|
|
2021-10-09 06:18:53 -05:00
|
|
|
use std::{iter, ops::Range};
|
2021-10-09 05:42:32 -05:00
|
|
|
|
|
|
|
use base_db::{fixture::WithFixture, SourceDatabase};
|
2021-10-09 06:48:38 -05:00
|
|
|
use expect_test::Expect;
|
2021-10-09 05:42:32 -05:00
|
|
|
use hir_expand::{db::AstDatabase, InFile, MacroFile};
|
|
|
|
use stdx::format_to;
|
2021-10-09 06:18:53 -05:00
|
|
|
use syntax::{
|
2021-10-09 06:38:57 -05:00
|
|
|
ast::{self, edit::IndentLevel},
|
|
|
|
AstNode,
|
2021-10-10 05:21:42 -05:00
|
|
|
SyntaxKind::{COMMENT, EOF, IDENT, LIFETIME_IDENT},
|
2021-10-09 06:18:53 -05:00
|
|
|
SyntaxNode, T,
|
|
|
|
};
|
2021-10-09 05:42:32 -05:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
db::DefDatabase, nameres::ModuleSource, resolver::HasResolver, test_db::TestDB, AsMacroCall,
|
|
|
|
};
|
|
|
|
|
2021-10-10 06:08:49 -05:00
|
|
|
#[track_caller]
|
2021-10-09 09:17:37 -05:00
|
|
|
fn check(ra_fixture: &str, mut expect: Expect) {
|
2021-10-09 05:42:32 -05:00
|
|
|
let db = TestDB::with_files(ra_fixture);
|
|
|
|
let krate = db.crate_graph().iter().next().unwrap();
|
|
|
|
let def_map = db.crate_def_map(krate);
|
|
|
|
let local_id = def_map.root();
|
|
|
|
let module = def_map.module_id(local_id);
|
|
|
|
let resolver = module.resolver(&db);
|
|
|
|
let source = def_map[local_id].definition_source(&db);
|
|
|
|
let source_file = match source.value {
|
|
|
|
ModuleSource::SourceFile(it) => it,
|
|
|
|
ModuleSource::Module(_) | ModuleSource::BlockExpr(_) => panic!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut expansions = Vec::new();
|
|
|
|
for macro_call in source_file.syntax().descendants().filter_map(ast::MacroCall::cast) {
|
|
|
|
let macro_call = InFile::new(source.file_id, ¯o_call);
|
2021-10-10 07:28:24 -05:00
|
|
|
let mut error = None;
|
2021-10-09 05:42:32 -05:00
|
|
|
let macro_call_id = macro_call
|
|
|
|
.as_call_id_with_errors(
|
|
|
|
&db,
|
|
|
|
krate,
|
|
|
|
|path| resolver.resolve_path_as_macro(&db, &path),
|
2021-10-10 07:28:24 -05:00
|
|
|
&mut |err| error = Some(err),
|
2021-10-09 05:42:32 -05:00
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
let macro_file = MacroFile { macro_call_id };
|
2021-10-10 07:28:24 -05:00
|
|
|
let mut expansion_result = db.parse_macro_expansion(macro_file);
|
|
|
|
expansion_result.err = expansion_result.err.or(error);
|
2021-10-09 05:42:32 -05:00
|
|
|
expansions.push((macro_call.value.clone(), expansion_result));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut expanded_text = source_file.to_string();
|
|
|
|
for (call, exp) in expansions.into_iter().rev() {
|
2021-10-10 05:21:42 -05:00
|
|
|
let mut tree = false;
|
|
|
|
let mut expect_errors = false;
|
|
|
|
for comment in call.syntax().children_with_tokens().filter(|it| it.kind() == COMMENT) {
|
|
|
|
tree |= comment.to_string().contains("+tree");
|
|
|
|
expect_errors |= comment.to_string().contains("+errors");
|
|
|
|
}
|
|
|
|
|
2021-10-09 05:42:32 -05:00
|
|
|
let mut expn_text = String::new();
|
|
|
|
if let Some(err) = exp.err {
|
|
|
|
format_to!(expn_text, "/* error: {} */", err);
|
|
|
|
}
|
|
|
|
if let Some((parse, _token_map)) = exp.value {
|
2021-10-10 05:21:42 -05:00
|
|
|
if expect_errors {
|
|
|
|
assert!(!parse.errors().is_empty(), "no parse errors in expansion");
|
|
|
|
for e in parse.errors() {
|
|
|
|
format_to!(expn_text, "/* parse error: {} */\n", e);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert!(
|
|
|
|
parse.errors().is_empty(),
|
|
|
|
"parse errors in expansion: \n{:#?}",
|
|
|
|
parse.errors()
|
|
|
|
);
|
|
|
|
}
|
2021-10-09 06:18:53 -05:00
|
|
|
let pp = pretty_print_macro_expansion(parse.syntax_node());
|
2021-10-09 06:38:57 -05:00
|
|
|
let indent = IndentLevel::from_node(call.syntax());
|
|
|
|
let pp = reindent(indent, pp);
|
2021-10-09 06:18:53 -05:00
|
|
|
format_to!(expn_text, "{}", pp);
|
2021-10-10 05:21:42 -05:00
|
|
|
|
|
|
|
if tree {
|
2021-10-09 09:58:17 -05:00
|
|
|
let tree = format!("{:#?}", parse.syntax_node())
|
|
|
|
.split_inclusive("\n")
|
|
|
|
.map(|line| format!("// {}", line))
|
|
|
|
.collect::<String>();
|
|
|
|
format_to!(expn_text, "\n{}", tree)
|
|
|
|
}
|
2021-10-09 05:42:32 -05:00
|
|
|
}
|
|
|
|
let range = call.syntax().text_range();
|
|
|
|
let range: Range<usize> = range.into();
|
|
|
|
expanded_text.replace_range(range, &expn_text)
|
|
|
|
}
|
|
|
|
|
2021-10-09 09:17:37 -05:00
|
|
|
expect.indent(false);
|
2021-10-09 05:42:32 -05:00
|
|
|
expect.assert_eq(&expanded_text);
|
|
|
|
}
|
|
|
|
|
2021-10-09 06:38:57 -05:00
|
|
|
fn reindent(indent: IndentLevel, pp: String) -> String {
|
|
|
|
if !pp.contains('\n') {
|
|
|
|
return pp;
|
|
|
|
}
|
|
|
|
let mut lines = pp.split_inclusive('\n');
|
|
|
|
let mut res = lines.next().unwrap().to_string();
|
|
|
|
for line in lines {
|
|
|
|
if line.trim().is_empty() {
|
|
|
|
res.push_str(&line)
|
|
|
|
} else {
|
|
|
|
format_to!(res, "{}{}", indent, line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2021-10-09 06:18:53 -05:00
|
|
|
fn pretty_print_macro_expansion(expn: SyntaxNode) -> String {
|
|
|
|
let mut res = String::new();
|
2021-10-10 03:22:12 -05:00
|
|
|
let mut prev_kind = EOF;
|
2021-10-09 09:36:41 -05:00
|
|
|
let mut indent_level = 0;
|
2021-10-09 06:18:53 -05:00
|
|
|
for token in iter::successors(expn.first_token(), |t| t.next_token()) {
|
|
|
|
let curr_kind = token.kind();
|
2021-10-09 06:38:57 -05:00
|
|
|
let space = match (prev_kind, curr_kind) {
|
|
|
|
_ if prev_kind.is_trivia() || curr_kind.is_trivia() => "",
|
2021-10-09 09:27:38 -05:00
|
|
|
(T!['{'], T!['}']) => "",
|
2021-10-09 06:38:57 -05:00
|
|
|
(T![=], _) | (_, T![=]) => " ",
|
2021-10-09 08:22:42 -05:00
|
|
|
(_, T!['{']) => " ",
|
2021-10-09 09:27:38 -05:00
|
|
|
(T![;] | T!['{'] | T!['}'], _) => "\n",
|
|
|
|
(_, T!['}']) => "\n",
|
2021-10-09 08:19:19 -05:00
|
|
|
(IDENT | LIFETIME_IDENT, IDENT | LIFETIME_IDENT) => " ",
|
2021-10-09 15:33:43 -05:00
|
|
|
_ if prev_kind.is_keyword() && curr_kind.is_keyword() => " ",
|
2021-10-09 06:38:57 -05:00
|
|
|
(IDENT, _) if curr_kind.is_keyword() => " ",
|
|
|
|
(_, IDENT) if prev_kind.is_keyword() => " ",
|
2021-10-09 10:15:05 -05:00
|
|
|
(T![>], IDENT) => " ",
|
|
|
|
(T![>], _) if curr_kind.is_keyword() => " ",
|
|
|
|
(T![->], _) | (_, T![->]) => " ",
|
2021-10-09 10:18:56 -05:00
|
|
|
(T![&&], _) | (_, T![&&]) => " ",
|
2021-10-10 06:21:47 -05:00
|
|
|
(T![,], _) => " ",
|
|
|
|
(T![:], IDENT | T!['(']) => " ",
|
|
|
|
(T![:], _) if curr_kind.is_keyword() => " ",
|
2021-10-10 03:22:12 -05:00
|
|
|
(T![fn], T!['(']) => "",
|
2021-10-10 03:29:26 -05:00
|
|
|
(T![']'], _) if curr_kind.is_keyword() => " ",
|
2021-10-10 03:39:08 -05:00
|
|
|
(T![']'], T![#]) => "\n",
|
2021-10-10 03:22:12 -05:00
|
|
|
_ if prev_kind.is_keyword() => " ",
|
2021-10-09 06:38:57 -05:00
|
|
|
_ => "",
|
2021-10-09 06:18:53 -05:00
|
|
|
};
|
|
|
|
|
2021-10-09 09:36:41 -05:00
|
|
|
match prev_kind {
|
|
|
|
T!['{'] => indent_level += 1,
|
|
|
|
T!['}'] => indent_level -= 1,
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
2021-10-09 06:38:57 -05:00
|
|
|
res.push_str(space);
|
2021-10-09 10:15:05 -05:00
|
|
|
if space == "\n" {
|
|
|
|
let level = if curr_kind == T!['}'] { indent_level - 1 } else { indent_level };
|
|
|
|
res.push_str(&" ".repeat(level));
|
2021-10-09 09:36:41 -05:00
|
|
|
}
|
2021-10-09 06:18:53 -05:00
|
|
|
prev_kind = curr_kind;
|
|
|
|
format_to!(res, "{}", token)
|
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|