Fix closures.rs indent double-counting for wrapped params with return… - #7015
Open
saberoueslati wants to merge 1 commit into
Open
Fix closures.rs indent double-counting for wrapped params with return…#7015saberoueslati wants to merge 1 commit into
saberoueslati wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #7012: rustfmt gives up formatting a statement when a method-chain argument is a closure whose parameter list wraps onto multiple lines and has an explicit return type, at moderate nesting depth. The reported symptom is that nothing changes and a misindented
.try_collect()?is left as-is.Root cause
In
rewrite_closure_fn_decl(src/closures.rs), when the closure's parameters wrap, the return type is placed on its own line prefixed withparam_offsetindentation.last_line_width(&prefix)then returns an absolute column rather than a width. The caller applies this value viashape.offset_left(extra_offset, span), which adds it on top ofshape.indent, double counting the indent. For the reported code this makesextra_offsetexceedshape.width, the rewrite returnsExceedsMaxWidth, and the error propagates up through the closure, the.map()call, the chain, and theletstatement, so rustfmt falls back to emitting the original source untouched.Fix
When the prefix wraps, subtract
shape.used_width()from the computed offset so it is relative to the shape instead of absolute. This is gated behindstyle_edition >= Edition2027because it changes the formatting of closures that already succeed today (a probe closure with a multiline param list and-> Treturn type joins its body onto one line up to 86 chars instead of 78). This follows the same gating precedent as #6835 for issue #6831.Testing
tests/source/issue_7012_style_edition_2024.rsand matching target, showing the current stable behavior is unchanged (rustfmt still leaves the statement unformatted).tests/source/issue_7012_style_edition_2027.rsand matching target, showing.try_collect()?correctly aligned understyle_edition=2027.--check.cargo testsuite locally, all system, idempotence, and integration tests pass with no changes to any pre-existing target file.Changelog
Added an entry under
Unreleased > Fixednoting the style edition 2027 gated fix.