Fixes adjustment of max_width within macros#6651
Conversation
|
@ytmimi, could you take a look? Happy to make changes if necessary. |
|
@Manishearth I see you merging some recent PRs. Do you know who can take a look at this? |
|
@csmulhern So we're trying to help spread out the maintainer burden, but there are a lot of open PRs right now and we're still triaging them. Open PRs which change formatted code are tricky because it needs to be determined if it's a rbeaking change or not. We'll get to them eventually, but no promises. It will help if you show a before and after of the formatted code. |
|
It seems like from the PR description that this doesn't actually impact formatting, this makes certain previously-unintentionally-invalid values of a config start working again. |
|
Yes, this (mostly*) shouldn't impact formatting. Today, we have a bunch of properties that control breakpoints for how long certain lines can be. This is primarily In order to support formatting in macros, it looks like rustfmt just trims down This change makes the reduction based on the indent symmetric across all of the max_width style properties, and adds a new method to ensure this is done universally. * The edge case where this would affect formatting would be if e.g. |
|
@Manishearth / @ytmimi, any feedback on this? |
|
@jieyouxu do you know what the status is re: if these sort of PRs are ready for review? Is there anything I can do to help make this more reviewable? |
It's largely (rustfmt) review bandwidth that's fairly scarce. I can do a re-review this weekend. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Hi, thanks for the PR and sorry for the slow response. I have a few questions/suggestions.
@rustbot author
There was a problem hiding this comment.
Question:
- The edge case where this would affect formatting would be if e.g.
max_widthwas set to 80, andfn_call_widthwas set to 76, and the macro body indent was 8. In today's world,fn_call_widthin the macro body would be larger than themax_widthwithin the macro body (72), and would get clamped down to 72. With this fix, both would be reduced by 8 (max_width80 -> 72,fn_call_width76 -> 68).
Is there a (albeit synthetic) test that can demonstrate the after effect (of this edge case)?
There was a problem hiding this comment.
I've added a prior commit in the chain that adds test fixtures for these macro use cases, with both default heuristics, and manually crafted widths that will trigger this edge case when the macro body indent exceeds the difference between the custom width and max_width.
Note that the error log currently happens whenever the "clipping" behavior occurred, which is for any file with a macro body if you have custom width values close to / equal to max_width.
This change fixes the error logging, but it only effects formatting when the length of a line is specifically within the gap between max_width - <custom width> and max_width - <macro body indent>.
The long variant of these newly added tests is the macro arm specifically crafted to trigger these conditions. You'll notice that the long variant is the same in source and target before the commit.
When it comes to this being formatting affecting:
- I believe these edge cases should be formatted the way they are post-commit. The user expressed for e.g.
fn_call_widthto be less thanmax_width, and so clipping tomax_widthfeels inappropriate. format_macro_bodiesis an unstable option, so I believe formatting changes should be tolerated here regardless of compatibility.
There was a problem hiding this comment.
Thanks for the detailed explanation, this is what I was having trouble figuring out 👍
Note
I consider this review chain resolved, but intentionally leaving open for other reviewers for context.
|
(Also sorry about the force-push, initially I thought that I needed to run DiffCheck, but then it occurred to me the effect of the changes here is basically not going to show up in the corpus AIUI.) |
This comment has been minimized.
This comment has been minimized.
|
@rustbot ready |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
| /// arms within a declarative macro. | ||
| #[allow(unreachable_pub)] | ||
| pub fn reduce_max_width(&mut self, delta: usize) { | ||
| self.adjust_max_width(-1 * delta as isize); |
There was a problem hiding this comment.
Remark: in theory, if we want to be very careful around handling of large widths, this has a potential problem where delta's usize -> isize cast can wraparound for sufficiently large usize, namely [isize::MAX + 1, usize::MAX].
However, I don't think this is a blocking concern for this PR and should not be something that's run into super frequently in practice (esp. if host pointer width is 64+). AFAICT in other parts of the code base we are also not super careful about large widths, so even if this wraps around there are other parts of the codebase that will have similar problems, AFAIK.
Fixes #5404.
Minimal example:
Without this change:
With this change: