diff options
| author | bors <bors@rust-lang.org> | 2024-05-12 22:06:34 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-05-12 22:06:34 +0000 |
| commit | ecbe3fd550fccd2cba17ea7e86539bf3e0bfc618 (patch) | |
| tree | c3fe72599d5318f82b14fe186bdf7a6b67935dcb | |
| parent | ef0027897d2e9014766fb47dce9ddbb925d2f540 (diff) | |
| parent | 94cc82c088b9301dc4dcf84ce127a64bbd77dddf (diff) | |
| download | rust-ecbe3fd550fccd2cba17ea7e86539bf3e0bfc618.tar.gz rust-ecbe3fd550fccd2cba17ea7e86539bf3e0bfc618.zip | |
Auto merge of #125051 - dtolnay:printletelse, r=compiler-errors
Pretty-print let-else with added parenthesization when needed
Rustc used to produce invalid syntax for the following code, which is problematic because it means we cannot apply rustfmt to the output of `-Zunpretty=expanded`.
```rust
macro_rules! expr {
($e:expr) => { $e };
}
fn main() {
let _ = expr!(loop {}) else { return; };
}
```
```console
$ rustc repro.rs -Zunpretty=expanded | rustfmt
error: `loop...else` loops are not supported
--> <stdin>:9:29
|
9 | fn main() { let _ = loop {} else { return; }; }
| ---- ^^^^^^^^^^^^^^^^
| |
| `else` is attached to this loop
|
= note: consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run
```
| -rw-r--r-- | compiler/rustc_ast_pretty/src/pprust/state.rs | 6 | ||||
| -rw-r--r-- | tests/ui/macros/stringify.rs | 15 | ||||
| -rw-r--r-- | tests/ui/unpretty/let-else-hir.rs (renamed from tests/ui/unpretty/pretty-let-else.rs) | 0 | ||||
| -rw-r--r-- | tests/ui/unpretty/let-else-hir.stdout (renamed from tests/ui/unpretty/pretty-let-else.stdout) | 0 | ||||
| -rw-r--r-- | tests/ui/unpretty/let-else.rs | 11 | ||||
| -rw-r--r-- | tests/ui/unpretty/let-else.stdout | 15 |
6 files changed, 46 insertions, 1 deletions
diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs index 2c176828c84..b7bdb2e14a6 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state.rs @@ -1238,7 +1238,11 @@ impl<'a> State<'a> { if let Some((init, els)) = loc.kind.init_else_opt() { self.nbsp(); self.word_space("="); - self.print_expr(init, FixupContext::default()); + self.print_expr_cond_paren( + init, + els.is_some() && classify::expr_trailing_brace(init).is_some(), + FixupContext::default(), + ); if let Some(els) = els { self.cbox(INDENT_UNIT); self.ibox(INDENT_UNIT); diff --git a/tests/ui/macros/stringify.rs b/tests/ui/macros/stringify.rs index 472cb4d417b..6b215ba525d 100644 --- a/tests/ui/macros/stringify.rs +++ b/tests/ui/macros/stringify.rs @@ -675,6 +675,11 @@ fn test_stmt() { "let (a, b): (u32, u32) = (1, 2);", "let (a, b): (u32, u32) = (1, 2)" ); + c2!(stmt, + [ let _ = f() else { return; } ], + "let _ = f() else { return; };", + "let _ = f() else { return; }", + ); macro_rules! c2_let_expr_minus_one { ([ $expr:expr ], $stmt_expected:expr, $tokens_expected:expr $(,)?) => { c2!(stmt, [ let _ = $expr - 1 ], $stmt_expected, $tokens_expected); @@ -685,6 +690,16 @@ fn test_stmt() { "let _ = match void {} - 1;", "let _ = match void {} - 1", ); + macro_rules! c2_let_expr_else_return { + ([ $expr:expr ], $stmt_expected:expr, $tokens_expected:expr $(,)?) => { + c2!(stmt, [ let _ = $expr else { return; } ], $stmt_expected, $tokens_expected); + }; + } + c2_let_expr_else_return!( + [ f() ], + "let _ = f() else { return; };", + "let _ = f() else { return; }", + ); // StmtKind::Item c1!(stmt, [ struct S; ], "struct S;"); diff --git a/tests/ui/unpretty/pretty-let-else.rs b/tests/ui/unpretty/let-else-hir.rs index 9c231189659..9c231189659 100644 --- a/tests/ui/unpretty/pretty-let-else.rs +++ b/tests/ui/unpretty/let-else-hir.rs diff --git a/tests/ui/unpretty/pretty-let-else.stdout b/tests/ui/unpretty/let-else-hir.stdout index ed55f293876..ed55f293876 100644 --- a/tests/ui/unpretty/pretty-let-else.stdout +++ b/tests/ui/unpretty/let-else-hir.stdout diff --git a/tests/ui/unpretty/let-else.rs b/tests/ui/unpretty/let-else.rs new file mode 100644 index 00000000000..4db6eca99b1 --- /dev/null +++ b/tests/ui/unpretty/let-else.rs @@ -0,0 +1,11 @@ +//@ compile-flags: -Zunpretty=expanded +//@ check-pass + +macro_rules! expr { + ($e:expr) => { $e }; +} + +fn main() { + let _ = expr!(1 + 1) else { return; }; + let _ = expr!(loop {}) else { return; }; +} diff --git a/tests/ui/unpretty/let-else.stdout b/tests/ui/unpretty/let-else.stdout new file mode 100644 index 00000000000..4bc4d9e085f --- /dev/null +++ b/tests/ui/unpretty/let-else.stdout @@ -0,0 +1,15 @@ +#![feature(prelude_import)] +#![no_std] +#[prelude_import] +use ::std::prelude::rust_2015::*; +#[macro_use] +extern crate std; +//@ compile-flags: -Zunpretty=expanded +//@ check-pass + +macro_rules! expr { ($e:expr) => { $e }; } + +fn main() { + let _ = 1 + 1 else { return; }; + let _ = (loop {}) else { return; }; +} |
