Skip to content

Format tests and benches with rustfmt #2097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

dtolnay
Copy link
Member

@dtolnay dtolnay commented Apr 30, 2022

4 commits:

  • Add rustfmt::skip to some files — handwritten; some files that rustfmt crashes on, or that I could tell have meaningful bespoke formatting that shouldn't be autoformatted.
  • Rustfmt tests/**/*.rs — generated by find . name '*.rs' | xargs rustfmt +nightly --edition=2021
  • Bless stderr files to update line numbers — generated by compiletest
  • Manual fixes to compiletest annotations — handwritten, changing //~ ERROR to //~^ ERROR and similar.

dtolnay added 4 commits April 30, 2022 11:42
Four of the files being skipped here are because rustfmt is buggy (see
the error messages below). The others have clearly preferable manual
formatting.

    error[internal]: left behind trailing whitespace
      --> /git/miri/tests/compile-fail/validity/transmute_through_ptr.rs:18:18:1
       |
    18 |
       | ^^^^
       |

    warning: rustfmt has failed to format. See previous 1 errors.

    error[internal]: left behind trailing whitespace
      --> /git/miri/tests/compile-fail/stacked_borrows/illegal_read2.rs:10:10:1
       |
    10 |
       | ^^^^
       |

    warning: rustfmt has failed to format. See previous 1 errors.

    error[internal]: left behind trailing whitespace
      --> /git/miri/tests/compile-fail/stacked_borrows/illegal_read5.rs:15:15:1
       |
    15 |
       | ^^^^
       |

    warning: rustfmt has failed to format. See previous 1 errors.

    error[internal]: left behind trailing whitespace
      --> /git/miri/tests/compile-fail/stacked_borrows/illegal_read1.rs:10:10:1
       |
    10 |
       | ^^^^
       |

    warning: rustfmt has failed to format. See previous 1 errors.
Generated by:

    find . name '*.rs' | xargs rustfmt +nightly --edition=2021
@bors
Copy link
Contributor

bors commented May 1, 2022

☔ The latest upstream changes (presumably #2095) made this pull request unmergeable. Please resolve the merge conflicts.

@RalfJung
Copy link
Member

RalfJung commented May 1, 2022

I think formatting those files only really makes sense if we then also check their formatting in the corresponding CI job. How hard would that be?

Comment on lines +53 to +54
fn do_panic() {
// In large, friendly letters :)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn do_panic() {
// In large, friendly letters :)
fn do_panic() {

What rustfmt does here makes no sense (the comment comments on the fn signature, not the body). It's also not a critical comment...

unsafe { copy_nonoverlapping(std::ptr::null(), ptr, 0); } //~ ERROR: memory access failed: null pointer is not a valid pointer
unsafe {
copy_nonoverlapping(std::ptr::null(), ptr, 0);
} //~^ ERROR: memory access failed: null pointer is not a valid pointer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO for cases like this (of which we have plenty) it'd be better to keep the comment in the same line as the function call, rather than have it after the unsafe block.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I did this based on line length. Notice for example tests/compile-fail/box-cell-alias.rs which has a shorter function call and shorter comment, the change is like this:

-    unsafe { (*ptr).set(20); } //~ ERROR does not exist in the borrow stack
+    unsafe {
+        (*ptr).set(20); //~ ERROR does not exist in the borrow stack
+    }

Basically if the comment fit without exceeding ~80 columns, I put it on the same line, otherwise the next line. It's clear to compiletest from the //~ vs //~^ whether the expected error is talking about the current line vs previous line.

The alternatives I see are the following, but I find both less good than what the PR is currently doing:

    // very long line
    unsafe {
        copy_nonoverlapping(std::ptr::null(), ptr, 0); //~ ERROR: memory access failed: null pointer is not a valid pointer
    }
    // more vertical space for no real benefit, I think
    unsafe {
        copy_nonoverlapping(std::ptr::null(), ptr, 0);
        //~^ ERROR: memory access failed: null pointer is not a valid pointer
    }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find both better than what the PR is doing. 🤷
Having the comment after the } is rather odd IMO, it goes against the structure of the code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay! Will update.

Copy link
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am in general on board with formatting our tests, bud sadly rustfmt likes to move comments around in a way that changes their meaning by attaching them to different parts of the code. So this needs to be checked by hand.

GH doesn't even render the diffs of the last tests any more, so it'd probably be better to do this over multiple PRs.

@@ -1,6 +1,7 @@
// A callee may not read the destination of our `&mut` without
// us noticing.

#[rustfmt::skip]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this one need skipping?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is mentioned in the commit message — it's because rustfmt is buggy and produces a noisy error message whenever this file is formatted.

error[internal]: left behind trailing whitespace
  --> tests/compile-fail/stacked_borrows/illegal_read2.rs:10:10:1
   |
10 |     
   | ^^^^
   |

warning: rustfmt has failed to format. See previous 1 errors.

I could alternatively try to tweak the contents of this file to work around the rustfmt bug.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, strange.
Ideally the attribute would come with a comment referencing the rustfmt bugreport. But just a quick // work around rustfmt bug would also do it.

let _val = *raw; //~ ERROR borrow stack
} }
fn main() {
unsafe {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of the double-indentation rustfmt introduces here... but I guess it doesn't really matter since these are just tests.

let x: *mut u32 = xref as *const _ as *mut _;
unsafe {
*x = 42;
} // invalidates shared ref, activates raw
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment should be with the assignment, not the end of the block

@@ -2,7 +2,8 @@
// compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows

fn main() {
for _ in 0..10 { // Try many times as this might work by chance.
for _ in 0..10 {
// Try many times as this might work by chance.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another case of rusfmt moving a comment to the wrong spot (the next few tests in the diff have the same issue)

unsafe { *x = 44; } // out-of-bounds enum tag
unsafe {
*x = 44;
} // out-of-bounds enum tag
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment should be with assignment

10, // Wake up at most 10 threads.
),
1
); // Woken up one thread.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment moved to wrong line

0b1001, // bitset
),
0
); // Didn't match any thread.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another comment on the wrong line

0b0110, // bitset
),
1
); // Woken up one thread.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And another one

for &size in &[2, 8, 64] {
// size less than and bigger than alignment
for &align in &[4, 8, 16, 32] {
// Be sure to cover less than and bigger than `MIN_ALIGN` for all architectures
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These comments also moved to the wrong spot

@dtolnay dtolnay marked this pull request as draft May 17, 2022 00:26
@RalfJung RalfJung added the S-waiting-on-author Status: Waiting for the PR author to address review comments label May 23, 2022
Copy link
Member Author

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am coming back to this after getting derailed by an apartment move.

Let me close this PR and I'll follow up in multiple more easily reviewable PRs, to incorporate all of the feedback above, and to avoid GitHub concealing the diffs beyond a certain number of files.

@dtolnay dtolnay closed this Jun 20, 2022
@dtolnay dtolnay deleted the rustfmt branch June 20, 2022 00:21
bors added a commit that referenced this pull request Jun 20, 2022
Add rustfmt::skip to some files

Extracted from #2097.

Five of the files being skipped here are because rustfmt is buggy (rust-lang/rustfmt#5391; see the error messages below). The other two have clearly preferable manual formatting.

```console
error[internal]: left behind trailing whitespace
  --> tests/fail/validity/transmute_through_ptr.rs:18:18:1
   |
18 |
   | ^^^^
   |

warning: rustfmt has failed to format. See previous 1 errors.

error[internal]: left behind trailing whitespace
  --> tests/fail/stacked_borrows/illegal_read2.rs:10:10:1
   |
10 |
   | ^^^^
   |

warning: rustfmt has failed to format. See previous 1 errors.

error[internal]: left behind trailing whitespace
  --> tests/fail/stacked_borrows/illegal_read5.rs:15:15:1
   |
15 |
   | ^^^^
   |

warning: rustfmt has failed to format. See previous 1 errors.

error[internal]: left behind trailing whitespace
  --> tests/fail/stacked_borrows/illegal_read1.rs:10:10:1
   |
10 |
   | ^^^^
   |

warning: rustfmt has failed to format. See previous 1 errors.

error[internal]: left behind trailing whitespace
 --> /git/miri/tests/fail/erroneous_const2.rs:9:9:1
  |
9 |
  | ^^^^
  |

warning: rustfmt has failed to format. See previous 1 errors.
```
@RalfJung
Copy link
Member

Thanks!
So, do you have a plan for having CI check this formatting? How tricky are the commands you have to run locally to test if all formatting was done successfully?

bors added a commit that referenced this pull request Jun 21, 2022
Format tests and benches with rustfmt (1-50 of 300)

Extracted from #2097.

I filtered this PR to contain exclusively "easy" cases to start off with, i.e. where there is no compiletest_rs (or other) comment in the vicinity that might need to get manually repositioned.
bors added a commit that referenced this pull request Jun 21, 2022
Format tests with rustfmt (101-150 of 300)

Extracted from #2097.

Like #2244, these are "easy" cases that do not involve moving around comments.
bors added a commit that referenced this pull request Jun 21, 2022
Format tests with rustfmt (51-100 of 300)

Extracted from #2097.

Like #2244, this is intended to be "easy" cases which don't involve comments in the vicinity.
bors added a commit that referenced this pull request Jun 21, 2022
Format tests with rustfmt (151-200 of 300)

Extracted from #2097.

This PR is still only doing the easy cases with no comments involved.

In the next PRs after this, I'll start grouping by common comment patterns, e.g. all the cases resembling #2097 (comment) together in one PR.
bors added a commit that referenced this pull request Jun 22, 2022
Format tests with rustfmt (201-224 of 300)

Extracted from #2097. Last of the easy cases which do not involve moving around a comment.
bors added a commit that referenced this pull request Jun 22, 2022
Format tests with rustfmt (276-287 of 299)

Extracted from #2097.

This is one half of the last 24 files (left for last because they require more unique attention than the first 275 "easy" files).

I'll comment below to call attention to cases where I exercised my own judgement in how to format the test.

rust-lang/rustfmt#3255 is especially annoying: rustfmt does not like `…( //` and `…{ //`.
bors added a commit that referenced this pull request Jun 22, 2022
Format tests with rustfmt (225-275 of 300)

Extracted from #2097.

These cases all involve a line comment at the end of a block that rustfmt has chosen to wrap.

```diff
- unsafe { (*ptr).set(20); } //~ ERROR does not exist in the borrow stack
+ unsafe {
+     (*ptr).set(20);
+ } //~ ERROR does not exist in the borrow stack
```

I have moved all of those comments back onto the same line as the content of the block instead, as was indicated being `@RalfJung's` preference in #2097 (comment).

```diff
+ unsafe {
+     (*ptr).set(20); //~ ERROR does not exist in the borrow stack
+ }
```
bors added a commit that referenced this pull request Jun 22, 2022
Format tests with rustfmt (288-299 of 299)

Extracted from #2097.

I'll make a separate PR to enable checking the `tests` directory's formatting in CI. I'll need to rebase that after both this and #2254 have landed, and if any new non-rustfmt-formatted files appear in the meantime, we can include formatting those in the same PR that enables the CI.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: Waiting for the PR author to address review comments
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants