about summary refs log tree commit diff
path: root/tests/pretty
diff options
context:
space:
mode:
authorMatthias Krüger <476013+matthiaskrgr@users.noreply.github.com>2025-06-13 05:16:54 +0200
committerGitHub <noreply@github.com>2025-06-13 05:16:54 +0200
commitb12bb2530b12fedd93a2a54b806a4f8dc2e199c4 (patch)
tree458e216f7c8522dd44a1a5014e3fa6ac3c9df22e /tests/pretty
parentbb3a3c530c23147a12ec7ec479e191571ccbe92b (diff)
parent6cca4ca82b03f86df63b022a122377c5e95c9a2b (diff)
downloadrust-b12bb2530b12fedd93a2a54b806a4f8dc2e199c4.tar.gz
rust-b12bb2530b12fedd93a2a54b806a4f8dc2e199c4.zip
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;
}
```
Diffstat (limited to 'tests/pretty')
-rw-r--r--tests/pretty/postfix-match/precedence.pp2
1 files changed, 1 insertions, 1 deletions
diff --git a/tests/pretty/postfix-match/precedence.pp b/tests/pretty/postfix-match/precedence.pp
index 967aa7bc39e..2052b445a2b 100644
--- a/tests/pretty/postfix-match/precedence.pp
+++ b/tests/pretty/postfix-match/precedence.pp
@@ -26,7 +26,7 @@ pub fn main() {
         _ => {}
     };
     (4 as usize).match { _ => {} };
-    (return).match { _ => {} };
+    return.match { _ => {} };
     (a = 42).match { _ => {} };
     (|| {}).match { _ => {} };
     (42..101).match { _ => {} };