rust/compiler/rustc_parse/src/parser
Jacob Pratt ea8bc3b4be
Rollup merge of #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:

- 13170cd787/compiler/rustc_ast_pretty/src/pprust/state/expr.rs (L283-L287)

- 13170cd787/compiler/rustc_hir_pretty/src/lib.rs (L1295-L1299)

- 13170cd787/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.
2024-12-21 01:18:43 -05:00
..
mut_visit Re-export more rustc_span::symbol things from rustc_span. 2024-12-18 13:38:53 +11:00
tokenstream Rename RefTokenTreeCursor. 2024-12-18 10:39:07 +11:00
attr.rs Speed up Parser::expected_token_types. 2024-12-19 16:05:41 +11:00
attr_wrapper.rs Introduce InvisibleOrigin on invisible delimiters. 2024-11-21 08:16:54 +11:00
diagnostics.rs Reduce the amount of explicit FatalError.raise() 2024-12-20 14:09:25 +00:00
expr.rs Change comparison operators to have Fixity::None 2024-12-20 20:12:22 -08:00
generics.rs Speed up Parser::expected_token_types. 2024-12-19 16:05:41 +11:00
item.rs Speed up Parser::expected_token_types. 2024-12-19 16:05:41 +11:00
mod.rs Fix Parser size assertion on s390x. 2024-12-19 20:06:44 +11:00
nonterminal.rs Re-export more rustc_span::symbol things from rustc_span. 2024-12-18 13:38:53 +11:00
pat.rs Speed up Parser::expected_token_types. 2024-12-19 16:05:41 +11:00
path.rs Reduce the amount of explicit FatalError.raise() 2024-12-20 14:09:25 +00:00
stmt.rs Do not suggest foo.Bar 2024-12-21 03:02:07 +00:00
tests.rs Rollup merge of #134161 - nnethercote:overhaul-token-cursors, r=spastorino 2024-12-18 22:56:53 +08:00
token_type.rs Make TokenType::from_u32 foolproof. 2024-12-19 16:05:41 +11:00
ty.rs Speed up Parser::expected_token_types. 2024-12-19 16:05:41 +11:00