Skip to content

Commit 7548cb4

Browse files
bors[bot]matklad
andauthored
Merge #10508
10508: internal: move some tests r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 93c52f5 + e5acf65 commit 7548cb4

File tree

10 files changed

+1688
-1479
lines changed

10 files changed

+1688
-1479
lines changed

crates/hir_def/src/macro_expansion_tests.rs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//! and harder to understand.
1111
1212
mod mbe;
13+
mod builtin;
1314

1415
use std::{iter, ops::Range};
1516

@@ -20,14 +21,15 @@ use stdx::format_to;
2021
use syntax::{
2122
ast::{self, edit::IndentLevel},
2223
AstNode,
23-
SyntaxKind::{EOF, IDENT, LIFETIME_IDENT},
24+
SyntaxKind::{COMMENT, EOF, IDENT, LIFETIME_IDENT},
2425
SyntaxNode, T,
2526
};
2627

2728
use crate::{
2829
db::DefDatabase, nameres::ModuleSource, resolver::HasResolver, test_db::TestDB, AsMacroCall,
2930
};
3031

32+
#[track_caller]
3133
fn check(ra_fixture: &str, mut expect: Expect) {
3234
let db = TestDB::with_files(ra_fixture);
3335
let krate = db.crate_graph().iter().next().unwrap();
@@ -44,37 +46,54 @@ fn check(ra_fixture: &str, mut expect: Expect) {
4446
let mut expansions = Vec::new();
4547
for macro_call in source_file.syntax().descendants().filter_map(ast::MacroCall::cast) {
4648
let macro_call = InFile::new(source.file_id, &macro_call);
49+
let mut error = None;
4750
let macro_call_id = macro_call
4851
.as_call_id_with_errors(
4952
&db,
5053
krate,
5154
|path| resolver.resolve_path_as_macro(&db, &path),
52-
&mut |err| panic!("{}", err),
55+
&mut |err| error = Some(err),
5356
)
5457
.unwrap()
5558
.unwrap();
5659
let macro_file = MacroFile { macro_call_id };
57-
let expansion_result = db.parse_macro_expansion(macro_file);
60+
let mut expansion_result = db.parse_macro_expansion(macro_file);
61+
expansion_result.err = expansion_result.err.or(error);
5862
expansions.push((macro_call.value.clone(), expansion_result));
5963
}
6064

6165
let mut expanded_text = source_file.to_string();
6266
for (call, exp) in expansions.into_iter().rev() {
67+
let mut tree = false;
68+
let mut expect_errors = false;
69+
for comment in call.syntax().children_with_tokens().filter(|it| it.kind() == COMMENT) {
70+
tree |= comment.to_string().contains("+tree");
71+
expect_errors |= comment.to_string().contains("+errors");
72+
}
73+
6374
let mut expn_text = String::new();
6475
if let Some(err) = exp.err {
6576
format_to!(expn_text, "/* error: {} */", err);
6677
}
6778
if let Some((parse, _token_map)) = exp.value {
68-
assert!(
69-
parse.errors().is_empty(),
70-
"parse errors in expansion: \n{:#?}",
71-
parse.errors()
72-
);
79+
if expect_errors {
80+
assert!(!parse.errors().is_empty(), "no parse errors in expansion");
81+
for e in parse.errors() {
82+
format_to!(expn_text, "/* parse error: {} */\n", e);
83+
}
84+
} else {
85+
assert!(
86+
parse.errors().is_empty(),
87+
"parse errors in expansion: \n{:#?}",
88+
parse.errors()
89+
);
90+
}
7391
let pp = pretty_print_macro_expansion(parse.syntax_node());
7492
let indent = IndentLevel::from_node(call.syntax());
7593
let pp = reindent(indent, pp);
7694
format_to!(expn_text, "{}", pp);
77-
if call.to_string().contains("// +tree") {
95+
96+
if tree {
7897
let tree = format!("{:#?}", parse.syntax_node())
7998
.split_inclusive("\n")
8099
.map(|line| format!("// {}", line))
@@ -129,6 +148,8 @@ fn pretty_print_macro_expansion(expn: SyntaxNode) -> String {
129148
(T![->], _) | (_, T![->]) => " ",
130149
(T![&&], _) | (_, T![&&]) => " ",
131150
(T![,], _) => " ",
151+
(T![:], IDENT | T!['(']) => " ",
152+
(T![:], _) if curr_kind.is_keyword() => " ",
132153
(T![fn], T!['(']) => "",
133154
(T![']'], _) if curr_kind.is_keyword() => " ",
134155
(T![']'], T![#]) => "\n",

0 commit comments

Comments
 (0)