Skip to content

Commit

Permalink
Rollup merge of #74980 - davidtwco:issue-74745-pprust-regression-test…
Browse files Browse the repository at this point in the history
…, 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.
  • Loading branch information
Manishearth authored Aug 2, 2020
2 parents 19cefa6 + 1530563 commit bf4a37d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/librustc_ast_pretty/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,9 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
fn print_comment(&mut self, cmnt: &comments::Comment) {
match cmnt.style {
comments::Mixed => {
self.zerobreak();
if !self.is_beginning_of_line() {
self.zerobreak();
}
if let Some((last, lines)) = cmnt.lines.split_last() {
self.ibox(0);

Expand Down
2 changes: 0 additions & 2 deletions src/test/pretty/block-comment-wchar.pp
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,13 @@
*/



/* */

/*
Hello from offset 6
Space 6+2: compare A
Ogham Space Mark 6+2: compare B
*/

/* */

/*
Expand Down
5 changes: 5 additions & 0 deletions src/test/pretty/issue-74745.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// ignore-tidy-trailing-newlines
// pretty-compare-only

/*
*/
25 changes: 16 additions & 9 deletions src/tools/compiletest/src/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,27 +178,30 @@ pub fn make_diff(expected: &str, actual: &str, context_size: usize) -> Vec<Misma
results
}

fn print_diff(expected: &str, actual: &str, context_size: usize) {
fn write_diff(expected: &str, actual: &str, context_size: usize) -> String {
use std::fmt::Write;
let mut output = String::new();
let diff_results = make_diff(expected, actual, context_size);
for result in diff_results {
let mut line_number = result.line_number;
for line in result.lines {
match line {
DiffLine::Expected(e) => {
println!("-\t{}", e);
writeln!(output, "-\t{}", e).unwrap();
line_number += 1;
}
DiffLine::Context(c) => {
println!("{}\t{}", line_number, c);
writeln!(output, "{}\t{}", line_number, c).unwrap();
line_number += 1;
}
DiffLine::Resulting(r) => {
println!("+\t{}", r);
writeln!(output, "+\t{}", r).unwrap();
}
}
}
println!();
writeln!(output, "").unwrap();
}
output
}

pub fn run(config: Config, testpaths: &TestPaths, revision: Option<&str>) {
Expand Down Expand Up @@ -655,8 +658,12 @@ impl<'test> TestCx<'test> {
------------------------------------------\n\
{}\n\
------------------------------------------\n\
\n",
expected, actual
diff:\n\
------------------------------------------\n\
{}\n",
expected,
actual,
write_diff(expected, actual, 3),
));
}
}
Expand Down Expand Up @@ -3227,7 +3234,7 @@ impl<'test> TestCx<'test> {
}
let expected_string = fs::read_to_string(&expected_file).unwrap();
if dumped_string != expected_string {
print_diff(&expected_string, &dumped_string, 3);
print!("{}", write_diff(&expected_string, &dumped_string, 3));
panic!(
"Actual MIR output differs from expected MIR output {}",
expected_file.display()
Expand Down Expand Up @@ -3452,7 +3459,7 @@ impl<'test> TestCx<'test> {
println!("normalized {}:\n{}\n", kind, actual);
} else {
println!("diff of {}:\n", kind);
print_diff(expected, actual, 3);
print!("{}", write_diff(expected, actual, 3));
}
}

Expand Down

0 comments on commit bf4a37d

Please sign in to comment.