-
Notifications
You must be signed in to change notification settings - Fork 384
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
Conversation
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
☔ The latest upstream changes (presumably #2095) made this pull request unmergeable. Please resolve the merge conflicts. |
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? |
fn do_panic() { | ||
// In large, friendly letters :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
}
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay! Will update.
There was a problem hiding this 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] |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this 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.
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. ```
Thanks! |
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.
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.
Format tests with rustfmt (201-224 of 300) Extracted from #2097. Last of the easy cases which do not involve moving around a comment.
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 `…{ //`.
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 + } ```
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.
4 commits:
find . name '*.rs' | xargs rustfmt +nightly --edition=2021
compiletest
//~ ERROR
to//~^ ERROR
and similar.