Commit 8751016
authored
Rollup merge of rust-lang#142476 - dtolnay:attrbinop, r=fmease
Insert parentheses around binary operation with attribute
Fixes the bug found by `@fmease` in rust-lang#134661 (review).
Previously, `-Zunpretty=expanded` would expand this program as follows:
```rust
#![feature(stmt_expr_attributes)]
#![allow(unused_attributes)]
macro_rules! group {
($e:expr) => {
$e
};
}
macro_rules! extra {
($e:expr) => {
#[allow()] $e
};
}
fn main() {
let _ = #[allow()] 1 + 1;
let _ = group!(#[allow()] 1) + 1;
let _ = 1 + group!(#[allow()] 1);
let _ = extra!({ 0 }) + 1;
let _ = extra!({ 0 } + 1);
}
```
```console
let _ = #[allow()] 1 + 1;
let _ = #[allow()] 1 + 1;
let _ = 1 + #[allow()] 1;
let _ = #[allow()] { 0 } + 1;
let _ = #[allow()] { 0 } + 1;
```
The first 4 statements are the correct expansion, but the last one is not. The attribute is supposed to apply to the entire binary operation, not only to the left operand.
After this PR, the 5th statement will expand to:
```console
let _ = #[allow()] ({ 0 } + 1);
```
In the future, as some subset of `stmt_expr_attributes` approaches stabilization, it is possible that we will need to do parenthesization for a number of additional cases depending on the outcome of rust-lang#127436. But for now, at least this PR makes the pretty-printer align with the current behavior of the parser.
r? fmeaseFile tree
2 files changed
+58
-12
lines changed- compiler/rustc_ast_pretty/src/pprust/state
- tests/ui-fulldeps
2 files changed
+58
-12
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
386 | 386 | | |
387 | 387 | | |
388 | 388 | | |
389 | | - | |
390 | | - | |
391 | | - | |
392 | | - | |
393 | | - | |
394 | | - | |
395 | | - | |
396 | | - | |
397 | | - | |
398 | | - | |
399 | | - | |
400 | | - | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
401 | 427 | | |
402 | 428 | | |
403 | 429 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
92 | 92 | | |
93 | 93 | | |
94 | 94 | | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
95 | 110 | | |
96 | 111 | | |
97 | 112 | | |
| |||
158 | 173 | | |
159 | 174 | | |
160 | 175 | | |
| 176 | + | |
161 | 177 | | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
162 | 182 | | |
163 | 183 | | |
164 | 184 | | |
| |||
0 commit comments