-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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 parenthesization of chained comparisons by pretty-printer #134600
Merged
Conversation
This file contains 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
oli-obk
approved these changes
Dec 21, 2024
@bors r+ rollup |
bors
added a commit
to rust-lang-ci/rust
that referenced
this pull request
Dec 21, 2024
Rollup of 7 pull requests Successful merges: - rust-lang#133087 (Detect missing `.` in method chain in `let` bindings and statements) - rust-lang#134575 (Handle `DropKind::ForLint` in coroutines correctly) - rust-lang#134576 (Improve prose around basic examples of Iter and IterMut) - rust-lang#134577 (Improve prose around `as_slice` example of Iter) - rust-lang#134579 (Improve prose around into_slice example of IterMut) - rust-lang#134593 (Less unwrap() in documentation) - rust-lang#134600 (Fix parenthesization of chained comparisons by pretty-printer) r? `@ghost` `@rustbot` modify labels: rollup
rust-timer
added a commit
to rust-lang-ci/rust
that referenced
this pull request
Dec 21, 2024
Rollup merge of rust-lang#134600 - dtolnay:chainedcomparison, r=oli-obk Fix parenthesization of chained comparisons by pretty-printer Example: ```rust macro_rules! repro { () => { 1 < 2 }; } fn main() { let _ = repro!() == false; } ``` Previously `-Zunpretty=expanded` would pretty-print this syntactically invalid output: `fn main() { let _ = 1 < 2 == false; }` ```console error: comparison operators cannot be chained --> <anon>:8:23 | 8 | fn main() { let _ = 1 < 2 == false; } | ^ ^^ | help: parenthesize the comparison | 8 | fn main() { let _ = (1 < 2) == false; } | + + ``` With the fix, it will print `fn main() { let _ = (1 < 2) == false; }`. Making `-Zunpretty=expanded` consistently produce syntactically valid Rust output is important because that is what makes it possible for `cargo expand` to format and perform filtering on the expanded code. ## Review notes According to `rg '\.fixity\(\)' compiler/` the `fixity` function is called only 3 places: - https://github.com/rust-lang/rust/blob/13170cd787cb733ed24842ee825bcbd98dc01476/compiler/rustc_ast_pretty/src/pprust/state/expr.rs#L283-L287 - https://github.com/rust-lang/rust/blob/13170cd787cb733ed24842ee825bcbd98dc01476/compiler/rustc_hir_pretty/src/lib.rs#L1295-L1299 - https://github.com/rust-lang/rust/blob/13170cd787cb733ed24842ee825bcbd98dc01476/compiler/rustc_parse/src/parser/expr.rs#L282-L289 The 2 pretty printers definitely want to treat comparisons using `Fixity::None`. That's the whole bug being fixed. Meanwhile, the parser's `Fixity::None` codepath is previously unreachable as indicated by the comment, so as long as `Fixity::None` here behaves exactly the way that `Fixity::Left` used to behave, you can tell that this PR definitely does not constitute any behavior change for the parser. My guess for why comparison operators were set to `Fixity::Left` instead of `Fixity::None` is that it's a very old workaround for giving a good chained comparisons diagnostic (like what I pasted above). Nowadays that is handled by a different dedicated codepath.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
A-pretty
Area: Pretty printing (including `-Z unpretty`)
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
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.
Example:
Previously
-Zunpretty=expanded
would pretty-print this syntactically invalid output:fn main() { let _ = 1 < 2 == false; }
With the fix, it will print
fn main() { let _ = (1 < 2) == false; }
.Making
-Zunpretty=expanded
consistently produce syntactically valid Rust output is important because that is what makes it possible forcargo expand
to format and perform filtering on the expanded code.Review notes
According to
rg '\.fixity\(\)' compiler/
thefixity
function is called only 3 places:rust/compiler/rustc_ast_pretty/src/pprust/state/expr.rs
Lines 283 to 287 in 13170cd
rust/compiler/rustc_hir_pretty/src/lib.rs
Lines 1295 to 1299 in 13170cd
rust/compiler/rustc_parse/src/parser/expr.rs
Lines 282 to 289 in 13170cd
The 2 pretty printers definitely want to treat comparisons using
Fixity::None
. That's the whole bug being fixed. Meanwhile, the parser'sFixity::None
codepath is previously unreachable as indicated by the comment, so as long asFixity::None
here behaves exactly the way thatFixity::Left
used to behave, you can tell that this PR definitely does not constitute any behavior change for the parser.My guess for why comparison operators were set to
Fixity::Left
instead ofFixity::None
is that it's a very old workaround for giving a good chained comparisons diagnostic (like what I pasted above). Nowadays that is handled by a different dedicated codepath.