-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Fix some of the rustfmt fallout in Miri #67833
Conversation
src/librustc_mir/interpret/cast.rs
Outdated
src.layout.ty | ||
) | ||
} | ||
_ => assert!( |
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.
It is really disappointing that I cannot stop rustfmt from doing silly things like this. :( Cc rust-lang/rustfmt#4004
src/librustc/mir/interpret/error.rs
Outdated
@@ -438,139 +438,165 @@ pub enum UnsupportedOpInfo<'tcx> { | |||
impl fmt::Debug for UnsupportedOpInfo<'tcx> { | |||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |||
use UnsupportedOpInfo::*; | |||
#[rustfmt::skip] // rustfmt and long matches do not go well together |
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 the diff doesn't seem like that much of an improvement to justify this... I'm concerned about people starting to use this in various places throughout the compiler to fit their personal tastes rather than having a uniform style.
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 tend to agree with @Centril here that I would like to avoid #[rustfmt::skip]
. I'd rather find ways to structure the code that make rustfmt happy, or else open issues on rustfmt for extreme cases.
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.
(Tweaking rustfmt.toml seems ok too, though I'd prefer to avoid even that when we can.)
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.
Uh... for me this diff is the difference between "unreadable garbage" and "reasonable code". Like, without this PR, the write!
is formatted in one of three different ways depending on the length of the message and the phase of the moon. Sometimes it's entirely in the same line as the pattern, sometimes it is in a line on its own, and sometimes the write!
is on the line of the pattern but its arguments are in separate lines. On top of that, some arms have curly braces and some don't, even though all just contain a single write!
. How is that not just bad style? The style should be consistent across all arms of this match
. But what rustfmt does is inconsistent, it's hard to parse visually. The entire point of rustfmt, I thought, is to get consistent formatting, but clearly it is doing the opposite here (and for match
es in general). The code is very symmetric, but visually in rustfmt style, there's no symmetry at all.
I opened a rustfmt issue at rust-lang/rustfmt#3995. But until that gets fixed, I don't see any good reason to let a tool's deficiency (rustfmt overall works fairly poorly on match
arms) dictate the format.
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.
Also Cc @Mark-Simulacrum, whose suggestion it was to use rustfmt::skip
on this file.
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.
it's just useful to be able to format-on-save anywhere
I appreciate that's useful for folks who went through the hoops of setting that up. Even as someone who didn't, I appreciate the consistent formatting of function signatures, the sorted imports, stuff like that.
But when considering match
es like this, I honestly don't understand how you can call this a "uniform reading experience" -- there's nothing uniform about how rustfmt formats this match. As far as I am concerned, the formatting of this match after rustfmt is literally worse than anything I ever saw anywhere in the rustc codebase before rustfmt. The way it formats matches will make it much harder for me to work on and review this code-base, due to how it degrades readability. I appreciate that is subjective, but the part where it is very inconsistent and using different styles to do the exact same thing is objectively neither uniform nor consistent.
I think the style in this diff is by far egregious enough to justify rustfmt::skip
. I also don't get the principled opposition to that: we don't usually make ourselves slaves to our own tools, do we? Tidy has had exceptions that were and are applied where reasonable. Not allowing reasonable exceptions would be quite unprecedented. In fact, I count around 30 occurrences of rustfmt::skip
in the rustc repo already (excluding tools and tests). Many of them are for some sort of table or another (but in some sense, this match
is a table), but this one is not. And that makes sense -- rustfmt is a great tool, but like all tools it has its limitations. We should apply the tool where it makes sense, but not mindlessly submit ourselves to whatever the tool happens to do.
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'll just note that sometimes (sometimes) I find let val; ...; val = expr
more readable.
(Not when its mutable state; that's almost never more readable. I'm talking about lazily initialized state here.)
Why do I find it more readable? Well, sometimes the `let val = { ... }; ends up with something where the result expression is so far from the binding that it doesn't fit in my editor's view at once, which means I've effectively lost the relationship (or at least have to keep in in my mental cache).
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 do I find it more readable? Well, sometimes the `let val = { ... }; ends up with something where the result expression is so far from the binding that it doesn't fit in my editor's view at once, which means I've effectively lost the relationship (or at least have to keep in in my mental cache).
Split your functions Felix, so that "doesn't fit" doesn't happen :P
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.
Split your functions Felix, so that "doesn't fit" doesn't happen :P
Sure, when that's an option, it can work well. And I suppose you might also suggest that I ensure that the enums I process never have more than four or five variants.
but sometimes you have to play the hand you are dealt, and I'm glad that I have let x; ... x = ...;
as a tool in my back pocket, even you all aren't willing to use it yourself.
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 I suppose you might also suggest that I ensure that the enums I process never have more than four or five variants.
Not really, but you can split match
arms into separate functions when the match
gets too long, e.g. as in https://doc.rust-lang.org/nightly/nightly-rustc/src/rustc_typeck/check/expr.rs.html#213-289 or https://doc.rust-lang.org/nightly/nightly-rustc/src/rustc_typeck/check/pat.rs.html#119-219 which were unreadable messes before I split those functions apart.
Another |
I guess ultimately this is up to the compiler team, so maybe we should nominate? The higher-level point here is to come up with some kind of a policy for when I just picked that one match as I think it is the most obvious and worst case of match-formatting gone wrong. But also see this commit of rustfmt edits that decrease |
I would personally be in favor of landing the skips as a stopgap and seeing what we can do to upstream fixes into rustfmt, even if those are gated behind some option. In particular, I find that I much prefer to enable people to contribute and we should have a policy that skip can be applied anywhere but should be tagged with a rustfmt issue associated with it. If it becomes more widespread, we can move to trying to limit the cases, but my hope is that it touches 1% of the code and we can mostly not care. |
Hey @RalfJung , @nikomatsakis and I were musing that if Would that be a reasonable way to address this, from your point of view, if it does work? |
It would certainly be a big improvement, yes. :) I was hoping to also lobby for |
@RalfJung oh, right; my intent was that the config flag would apply in both directions. I.e., if the flag is set, then |
Discussed at the compiler team meeting. There isn't much opposition to |
Well, that is disappointing, but I guess there is little I can do here. :( I'll reopen the PR without the |
Fix some of the rustfmt fallout in Miri re-post of rust-lang#67833 without the `rustfmt::skip` r? @oli-obk
I tried to coerce rustfmt into reasonable formatting of matches, but that didn't always work...
r? @oli-obk