31 Commits

Author SHA1 Message Date
David Tolnay
535e11f72e
Add an attribute-related parenthesization edge case 2025-06-20 13:49:12 -07:00
David Tolnay
cbef8f6e07
Add AST pretty-printer tests involving attr on binary operation
This test currently fails (as expected).

    --- stderr -------------------------------
    Pretty-printer lost necessary parentheses
      BEFORE: #[attr] (1 + 1)
       AFTER: #[attr] 1 + 1

    Pretty-printer lost necessary parentheses
      BEFORE: #[attr] (1 as T)
       AFTER: #[attr] 1 as T

    Pretty-printer lost necessary parentheses
      BEFORE: #[attr] (x = 1)
       AFTER: #[attr] x = 1

    Pretty-printer lost necessary parentheses
      BEFORE: #[attr] (x += 1)
       AFTER: #[attr] x += 1
    ------------------------------------------
2025-06-20 13:49:12 -07:00
David Tolnay
1ed0cbf698
Preserve Paren expression's attributes during Unparenthesize 2025-06-19 12:05:46 -07:00
Jacob Pratt
e95fb09dfb
Rollup merge of #142371 - fee1-dead-contrib:push-xqlkumzurkus, r=petrochenkov
avoid `&mut P<T>` in `visit_expr` etc methods

trying a different way than rust-lang/rust#141636
r? ghost
2025-06-17 23:19:34 +02:00
David Tolnay
d75b0ae7ce
Add AST pretty-printer tests involving attr precedence
This test currently fails (as expected).

    --- stderr -------------------------------
    Pretty-printer lost necessary parentheses
      BEFORE: (#[attr] loop {}).field
       AFTER: #[attr] loop {}.field
    ------------------------------------------
2025-06-13 13:59:21 -07:00
Matthias Krüger
b12bb2530b
Rollup merge of #134847 - dtolnay:asymmetrical, r=fmease
Implement asymmetrical precedence for closures and jumps

I have been through a series of asymmetrical precedence designs in Syn, and finally have one that I like and is worth backporting into rustc. It is based on just 2 bits of state: `next_operator_can_begin_expr` and `next_operator_can_continue_expr`.

Asymmetrical precedence is the thing that enables `(return 1) + 1` to require parentheses while `1 + return 1` does not, despite `+` always having stronger precedence than `return` [according to the Rust Reference](https://doc.rust-lang.org/1.83.0/reference/expressions.html#expression-precedence). This is facilitated by `next_operator_can_continue_expr`.

Relatedly, it is the thing that enables `(return) - 1` to require parentheses while `return + 1` does not, despite `+` and `-` having exactly the same precedence. This is facilitated by `next_operator_can_begin_expr`.

**Example:**

```rust
macro_rules! repro {
    ($e:expr) => {
        $e - $e;
        $e + $e;
    };
}

fn main() {
    repro!{return}
    repro!{return 1}
}
```

`-Zunpretty=expanded` **Before:**

```console
fn main() {
    (return) - (return);
    (return) + (return);
    (return 1) - (return 1);
    (return 1) + (return 1);
}
```

**After:**

```console
fn main() {
    (return) - return;
    return + return;
    (return 1) - return 1;
    (return 1) + return 1;
}
```
2025-06-13 05:16:54 +02:00
Deadbeef
5f0dd44b3b avoid &mut P<T> in visit_expr etc methods 2025-06-12 17:36:03 +08:00
Deadbeef
5e7185583f remove visit_clobber and move DummyAstNode to rustc_expand
`visit_clobber` is not really useful except for one niche purpose
involving generic code. We should just use the replace logic where we
can.
2025-05-29 12:46:26 +08:00
David Tolnay
6cca4ca82b
Implement asymmetrical precedence for closures and jumps 2025-05-03 23:27:29 -07:00
David Tolnay
0a09252866
Rollup merge of #134834 - dtolnay:unnamedcall, r=compiler-errors
Skip parenthesis around tuple struct field calls

The pretty-printer previously did not distinguish between named vs unnamed fields when printing a function call containing a struct field. It would print the call as `(self.fun)()` for a named field which is correct, and `(self.0)()` for an unnamed field which is redundant.

This PR changes function calls of tuple struct fields to print without parens.

**Before:**

```rust
struct Tuple(fn());

fn main() {
    let tuple = Tuple(|| {});
    (tuple.0)();
}
```

**After:**

```rust
struct Tuple(fn());

fn main() {
    let tuple = Tuple(|| {});
    tuple.0();
}
```
2024-12-27 18:43:05 -08:00
David Tolnay
26bb4e6464
Skip parenthesis around tuple struct field calls 2024-12-27 14:33:56 -08:00
David Tolnay
c95f9f50de
Add pretty-printer test of tuple field function call 2024-12-27 14:18:39 -08:00
David Tolnay
e67fe3698b
Skip parenthesis if . makes statement boundary unambiguous 2024-12-27 13:53:02 -08:00
David Tolnay
fef8ec5ad9
Add test of dot after eager statement boundary expr 2024-12-27 13:41:46 -08:00
Matthias Krüger
87be70e2b4
Rollup merge of #134599 - dtolnay:fulldepsparser, r=fmease
Detect invalid exprs in parser used by pretty-printer tests

This PR fixes a bug in https://github.com/rust-lang/rust/pull/133730 inherited from https://github.com/rust-lang/rust/pull/43742. Before this fix, the test might silently only operate on a prefix of some of the test cases in this table:

13170cd787/tests/ui-fulldeps/pprust-parenthesis-insertion.rs (L57)

For example, adding the test case `1 .. 2 .. 3` (a syntactically invalid expression) into the table would unexpectedly succeed the test instead of crashing at this unwrap:

13170cd787/tests/ui-fulldeps/pprust-parenthesis-insertion.rs (L199-L200)

because `parse_expr` would successfully parse just `1 .. 2` and disregard the last `.. 3`.

This PR adds a check that `parse_expr` reaches `Eof`, ensuring all the test cases actually test the whole expression they look like they are supposed to.
2024-12-22 09:12:11 +01:00
David Tolnay
1f2028f930
Show which test case was found to be meaningless 2024-12-21 18:50:11 -08:00
Matthias Krüger
bcdde4ea5b
Rollup merge of #134601 - dtolnay:dynstar, r=compiler-errors
Support pretty-printing `dyn*` trait objects

- Tracking issue: https://github.com/rust-lang/rust/issues/102425
2024-12-22 03:49:43 +01:00
David Tolnay
65ba6ac9a3
Extract ui-fulldeps expression parser into module 2024-12-21 18:48:13 -08:00
David Tolnay
23a250738b
Relocate dyn* test out of parenthesis insertion test 2024-12-20 21:31:21 -08:00
David Tolnay
1cc8289791
Support pretty-printing dyn* trait objects 2024-12-20 21:31:21 -08:00
David Tolnay
fe65e886f3
Change comparison operators to have Fixity::None 2024-12-20 20:12:22 -08:00
David Tolnay
d748d1d953
Add some parenthesization test cases with operators that are not left-associative 2024-12-20 20:12:20 -08:00
David Tolnay
3f98f76d70
Check that pretty-printer parenthesis test operates on the whole test case 2024-12-20 19:59:10 -08:00
Matthias Krüger
f3b19f54fa
Rollup merge of #133782 - dtolnay:closuresjumps, r=spastorino,traviscross
Precedence improvements: closures and jumps

This PR fixes some cases where rustc's pretty printers would redundantly parenthesize expressions that didn't need it.

<table>
<tr><th>Before</th><th>After</th></tr>
<tr><td><code>return (|x: i32| x)</code></td><td><code>return |x: i32| x</code></td></tr>
<tr><td><code>(|| -> &mut () { std::process::abort() }).clone()</code></td><td><code>|| -> &mut () { std::process::abort() }.clone()</code></td></tr>
<tr><td><code>(continue) + 1</code></td><td><code>continue + 1</code></td></tr>
</table>

Tested by `echo "fn main() { let _ = $AFTER; }" | rustc -Zunpretty=expanded /dev/stdin`.

The pretty-printer aims to render the syntax tree as it actually exists in rustc, as faithfully as possible, in Rust syntax. It can insert parentheses where forced by Rust's grammar in order to preserve the meaning of a macro-generated syntax tree, for example in the case of `a * $rhs` where $rhs is `b + c`. But for any expression parsed from source code, without a macro involved, there should never be a reason for inserting additional parentheses not present in the original.

For closures and jumps (return, break, continue, yield, do yeet, become) the unneeded parentheses came from the precedence of some of these expressions being misidentified. In the same order as the table above:

- Jumps and closures are supposed to have equal precedence. The [Rust Reference](https://doc.rust-lang.org/1.83.0/reference/expressions.html#expression-precedence) says so, and in Syn they do. There is no Rust syntax that would require making a precedence distinction between jumps and closures. But in rustc these were previously 2 distinct levels with the closure being lower, hence the parentheses around a closure inside a jump (but not a jump inside a closure).

- When a closure is written with an explicit return type, the grammar [requires](https://doc.rust-lang.org/1.83.0/reference/expressions/closure-expr.html) that the closure body consists of exactly one block expression, not any other arbitrary expression as usual for closures. Parsing of the closure body does not continue after the block expression. So in `|| { 0 }.clone()` the clone is inside the closure body and applies to `{ 0 }`, whereas in `|| -> _ { 0 }.clone()` the clone is outside and applies to the closure as a whole.

- Continue never needs parentheses. It was previously marked as having the lowest possible precedence but it should have been the highest, next to paths and loops and function calls, not next to jumps.
2024-12-21 01:30:15 +01:00
David Tolnay
a3cfe2fd08
Visit Stmt span in MutVisitor::flat_map_stmt 2024-12-03 07:03:26 -08:00
David Tolnay
a2eca35c15
Visit BinOp span in MutVisitor::visit_expr 2024-12-03 07:03:26 -08:00
David Tolnay
72ac961616
Raise precedence of closure that has explicit return type 2024-12-02 17:48:16 -08:00
David Tolnay
4df47a09a4
Add test of closure precedence with return type 2024-12-02 17:37:03 -08:00
David Tolnay
193d82797c
Squash closures and jumps into a single precedence level 2024-12-02 17:33:20 -08:00
David Tolnay
f3ac64ac34
Add test of closure vs jump precedence 2024-12-02 17:28:42 -08:00
David Tolnay
267dcb28c2
Add pretty-printer parenthesis insertion test 2024-12-01 21:31:02 -08:00