Skip to content

Commit bf4a37d

Browse files
authored
Rollup merge of #74980 - davidtwco:issue-74745-pprust-regression-test, r=petrochenkov
pprust: adjust mixed comment printing and add regression test for #74745 Fixes #74745. This PR adds a regression test for #74745. While a `ignore-tidy-trailing-lines` header is required, this doesn't stop the test from reproducing, so long as there is no newline at the end of the file. However, adding the header comments made the test fail due to a bug in pprust - so this PR also adjusts the pretty printing of mixed comments so that the initial zero-break isn't emitted at the beginning of the line. Through this, the `block-comment-wchar` test can have the `pp-exact` file removed, as it no longer converges from pretty printing of the source.
2 parents 19cefa6 + 1530563 commit bf4a37d

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

src/librustc_ast_pretty/pprust.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,9 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
450450
fn print_comment(&mut self, cmnt: &comments::Comment) {
451451
match cmnt.style {
452452
comments::Mixed => {
453-
self.zerobreak();
453+
if !self.is_beginning_of_line() {
454+
self.zerobreak();
455+
}
454456
if let Some((last, lines)) = cmnt.lines.split_last() {
455457
self.ibox(0);
456458

src/test/pretty/block-comment-wchar.pp

-2
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,13 @@
7373
*/
7474

7575

76-
7776
/* */
7877

7978
/*
8079
Hello from offset 6
8180
Space 6+2: compare A
8281
Ogham Space Mark 6+2: compare B
8382
*/
84-
8583
/* */
8684

8785
/*

src/test/pretty/issue-74745.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// ignore-tidy-trailing-newlines
2+
// pretty-compare-only
3+
4+
/*
5+
*/

src/tools/compiletest/src/runtest.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -178,27 +178,30 @@ pub fn make_diff(expected: &str, actual: &str, context_size: usize) -> Vec<Misma
178178
results
179179
}
180180

181-
fn print_diff(expected: &str, actual: &str, context_size: usize) {
181+
fn write_diff(expected: &str, actual: &str, context_size: usize) -> String {
182+
use std::fmt::Write;
183+
let mut output = String::new();
182184
let diff_results = make_diff(expected, actual, context_size);
183185
for result in diff_results {
184186
let mut line_number = result.line_number;
185187
for line in result.lines {
186188
match line {
187189
DiffLine::Expected(e) => {
188-
println!("-\t{}", e);
190+
writeln!(output, "-\t{}", e).unwrap();
189191
line_number += 1;
190192
}
191193
DiffLine::Context(c) => {
192-
println!("{}\t{}", line_number, c);
194+
writeln!(output, "{}\t{}", line_number, c).unwrap();
193195
line_number += 1;
194196
}
195197
DiffLine::Resulting(r) => {
196-
println!("+\t{}", r);
198+
writeln!(output, "+\t{}", r).unwrap();
197199
}
198200
}
199201
}
200-
println!();
202+
writeln!(output, "").unwrap();
201203
}
204+
output
202205
}
203206

204207
pub fn run(config: Config, testpaths: &TestPaths, revision: Option<&str>) {
@@ -655,8 +658,12 @@ impl<'test> TestCx<'test> {
655658
------------------------------------------\n\
656659
{}\n\
657660
------------------------------------------\n\
658-
\n",
659-
expected, actual
661+
diff:\n\
662+
------------------------------------------\n\
663+
{}\n",
664+
expected,
665+
actual,
666+
write_diff(expected, actual, 3),
660667
));
661668
}
662669
}
@@ -3227,7 +3234,7 @@ impl<'test> TestCx<'test> {
32273234
}
32283235
let expected_string = fs::read_to_string(&expected_file).unwrap();
32293236
if dumped_string != expected_string {
3230-
print_diff(&expected_string, &dumped_string, 3);
3237+
print!("{}", write_diff(&expected_string, &dumped_string, 3));
32313238
panic!(
32323239
"Actual MIR output differs from expected MIR output {}",
32333240
expected_file.display()
@@ -3452,7 +3459,7 @@ impl<'test> TestCx<'test> {
34523459
println!("normalized {}:\n{}\n", kind, actual);
34533460
} else {
34543461
println!("diff of {}:\n", kind);
3455-
print_diff(expected, actual, 3);
3462+
print!("{}", write_diff(expected, actual, 3));
34563463
}
34573464
}
34583465

0 commit comments

Comments
 (0)