Skip to content
/ rust Public
forked from rust-lang/rust

Commit ecbe3fd

Browse files
committed
Auto merge of rust-lang#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 ```
2 parents ef00278 + 94cc82c commit ecbe3fd

File tree

6 files changed

+46
-1
lines changed

6 files changed

+46
-1
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,11 @@ impl<'a> State<'a> {
12381238
if let Some((init, els)) = loc.kind.init_else_opt() {
12391239
self.nbsp();
12401240
self.word_space("=");
1241-
self.print_expr(init, FixupContext::default());
1241+
self.print_expr_cond_paren(
1242+
init,
1243+
els.is_some() && classify::expr_trailing_brace(init).is_some(),
1244+
FixupContext::default(),
1245+
);
12421246
if let Some(els) = els {
12431247
self.cbox(INDENT_UNIT);
12441248
self.ibox(INDENT_UNIT);

tests/ui/macros/stringify.rs

+15
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,11 @@ fn test_stmt() {
675675
"let (a, b): (u32, u32) = (1, 2);",
676676
"let (a, b): (u32, u32) = (1, 2)"
677677
);
678+
c2!(stmt,
679+
[ let _ = f() else { return; } ],
680+
"let _ = f() else { return; };",
681+
"let _ = f() else { return; }",
682+
);
678683
macro_rules! c2_let_expr_minus_one {
679684
([ $expr:expr ], $stmt_expected:expr, $tokens_expected:expr $(,)?) => {
680685
c2!(stmt, [ let _ = $expr - 1 ], $stmt_expected, $tokens_expected);
@@ -685,6 +690,16 @@ fn test_stmt() {
685690
"let _ = match void {} - 1;",
686691
"let _ = match void {} - 1",
687692
);
693+
macro_rules! c2_let_expr_else_return {
694+
([ $expr:expr ], $stmt_expected:expr, $tokens_expected:expr $(,)?) => {
695+
c2!(stmt, [ let _ = $expr else { return; } ], $stmt_expected, $tokens_expected);
696+
};
697+
}
698+
c2_let_expr_else_return!(
699+
[ f() ],
700+
"let _ = f() else { return; };",
701+
"let _ = f() else { return; }",
702+
);
688703

689704
// StmtKind::Item
690705
c1!(stmt, [ struct S; ], "struct S;");

tests/ui/unpretty/let-else.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@ compile-flags: -Zunpretty=expanded
2+
//@ check-pass
3+
4+
macro_rules! expr {
5+
($e:expr) => { $e };
6+
}
7+
8+
fn main() {
9+
let _ = expr!(1 + 1) else { return; };
10+
let _ = expr!(loop {}) else { return; };
11+
}

tests/ui/unpretty/let-else.stdout

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![feature(prelude_import)]
2+
#![no_std]
3+
#[prelude_import]
4+
use ::std::prelude::rust_2015::*;
5+
#[macro_use]
6+
extern crate std;
7+
//@ compile-flags: -Zunpretty=expanded
8+
//@ check-pass
9+
10+
macro_rules! expr { ($e:expr) => { $e }; }
11+
12+
fn main() {
13+
let _ = 1 + 1 else { return; };
14+
let _ = (loop {}) else { return; };
15+
}

0 commit comments

Comments
 (0)