Skip to content

Commit b2b703b

Browse files
bors[bot]matklad
andauthored
Merge #10496
10496: internal: move some macro tests r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 9f7bf0a + 3e8ef94 commit b2b703b

File tree

22 files changed

+309
-285
lines changed

22 files changed

+309
-285
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cfg/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ tt = { path = "../tt", version = "0.0.0" }
1616
[dev-dependencies]
1717
mbe = { path = "../mbe" }
1818
syntax = { path = "../syntax" }
19-
expect-test = "1.1"
19+
expect-test = "1.2.0-pre.1"
2020
oorandom = "11"
2121
# We depend on both individually instead of using `features = ["derive"]` to microoptimize the
2222
# build graph: if the feature was enabled, syn would be built early on in the graph if `smolstr`

crates/hir_def/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ limit = { path = "../limit", version = "0.0.0" }
3535

3636
[dev-dependencies]
3737
test_utils = { path = "../test_utils" }
38-
expect-test = "1.1"
38+
expect-test = "1.2.0-pre.1"

crates/hir_def/src/macro_expansion_tests.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::{
2828
db::DefDatabase, nameres::ModuleSource, resolver::HasResolver, test_db::TestDB, AsMacroCall,
2929
};
3030

31-
fn check(ra_fixture: &str, expect: Expect) {
31+
fn check(ra_fixture: &str, mut expect: Expect) {
3232
let db = TestDB::with_files(ra_fixture);
3333
let krate = db.crate_graph().iter().next().unwrap();
3434
let def_map = db.crate_def_map(krate);
@@ -65,16 +65,29 @@ fn check(ra_fixture: &str, expect: Expect) {
6565
format_to!(expn_text, "/* error: {} */", err);
6666
}
6767
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+
);
6873
let pp = pretty_print_macro_expansion(parse.syntax_node());
6974
let indent = IndentLevel::from_node(call.syntax());
7075
let pp = reindent(indent, pp);
7176
format_to!(expn_text, "{}", pp);
77+
if call.to_string().contains("// +tree") {
78+
let tree = format!("{:#?}", parse.syntax_node())
79+
.split_inclusive("\n")
80+
.map(|line| format!("// {}", line))
81+
.collect::<String>();
82+
format_to!(expn_text, "\n{}", tree)
83+
}
7284
}
7385
let range = call.syntax().text_range();
7486
let range: Range<usize> = range.into();
7587
expanded_text.replace_range(range, &expn_text)
7688
}
7789

90+
expect.indent(false);
7891
expect.assert_eq(&expanded_text);
7992
}
8093

@@ -97,20 +110,37 @@ fn reindent(indent: IndentLevel, pp: String) -> String {
97110
fn pretty_print_macro_expansion(expn: SyntaxNode) -> String {
98111
let mut res = String::new();
99112
let mut prev_kind = SyntaxKind::EOF;
113+
let mut indent_level = 0;
100114
for token in iter::successors(expn.first_token(), |t| t.next_token()) {
101115
let curr_kind = token.kind();
102116
let space = match (prev_kind, curr_kind) {
103117
_ if prev_kind.is_trivia() || curr_kind.is_trivia() => "",
118+
(T!['{'], T!['}']) => "",
104119
(T![=], _) | (_, T![=]) => " ",
105120
(_, T!['{']) => " ",
106-
(T![;] | T!['}'], _) => "\n",
121+
(T![;] | T!['{'] | T!['}'], _) => "\n",
122+
(_, T!['}']) => "\n",
107123
(IDENT | LIFETIME_IDENT, IDENT | LIFETIME_IDENT) => " ",
108124
(IDENT, _) if curr_kind.is_keyword() => " ",
109125
(_, IDENT) if prev_kind.is_keyword() => " ",
126+
(T![>], IDENT) => " ",
127+
(T![>], _) if curr_kind.is_keyword() => " ",
128+
(T![->], _) | (_, T![->]) => " ",
129+
(T![&&], _) | (_, T![&&]) => " ",
110130
_ => "",
111131
};
112132

133+
match prev_kind {
134+
T!['{'] => indent_level += 1,
135+
T!['}'] => indent_level -= 1,
136+
_ => (),
137+
}
138+
113139
res.push_str(space);
140+
if space == "\n" {
141+
let level = if curr_kind == T!['}'] { indent_level - 1 } else { indent_level };
142+
res.push_str(&" ".repeat(level));
143+
}
114144
prev_kind = curr_kind;
115145
format_to!(res, "{}", token)
116146
}

0 commit comments

Comments
 (0)