Skip to content

Commit 0d282f3

Browse files
committed
Parenthesize break values containing leading label
1 parent 5f37433 commit 0d282f3

File tree

3 files changed

+86
-5
lines changed

3 files changed

+86
-5
lines changed

compiler/rustc_ast/src/util/classify.rs

+78-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//! Routines the parser and pretty-printer use to classify AST nodes.
22
33
use crate::ast::ExprKind::*;
4-
use crate::{ast, token::Delimiter};
4+
use crate::ast::{self, MatchKind};
5+
use crate::token::Delimiter;
56

67
/// This classification determines whether various syntactic positions break out
78
/// of parsing the current expression (true) or continue parsing more of the
@@ -81,6 +82,82 @@ pub fn expr_requires_semi_to_be_stmt(e: &ast::Expr) -> bool {
8182
}
8283
}
8384

85+
/// Returns whether the leftmost token of the given expression is the label of a
86+
/// labeled loop or block, such as in `'inner: loop { break 'inner 1 } + 1`.
87+
///
88+
/// Such expressions are not allowed as the value of an unlabeled break.
89+
///
90+
/// ```ignore (illustrative)
91+
/// 'outer: {
92+
/// break 'inner: loop { break 'inner 1 } + 1; // invalid syntax
93+
///
94+
/// break 'outer 'inner: loop { break 'inner 1 } + 1; // okay
95+
///
96+
/// break ('inner: loop { break 'inner 1 } + 1); // okay
97+
///
98+
/// break ('inner: loop { break 'inner 1 }) + 1; // okay
99+
/// }
100+
/// ```
101+
pub fn leading_labeled_expr(mut expr: &ast::Expr) -> bool {
102+
loop {
103+
match &expr.kind {
104+
Block(_, label) | ForLoop { label, .. } | Loop(_, label, _) | While(_, _, label) => {
105+
return label.is_some();
106+
}
107+
108+
Assign(e, _, _)
109+
| AssignOp(_, e, _)
110+
| Binary(_, e, _)
111+
| Call(e, _)
112+
| Cast(e, _)
113+
| Field(e, _)
114+
| Index(e, _, _)
115+
| Match(e, _, MatchKind::Postfix)
116+
| Range(Some(e), _, _) => {
117+
expr = e;
118+
}
119+
MethodCall(method_call) => {
120+
expr = &method_call.receiver;
121+
}
122+
123+
AddrOf(..)
124+
| Array(..)
125+
| Await(..)
126+
| Become(..)
127+
| Break(..)
128+
| Closure(..)
129+
| ConstBlock(..)
130+
| Continue(..)
131+
| FormatArgs(..)
132+
| Gen(..)
133+
| If(..)
134+
| IncludedBytes(..)
135+
| InlineAsm(..)
136+
| Let(..)
137+
| Lit(..)
138+
| MacCall(..)
139+
| Match(_, _, MatchKind::Prefix)
140+
| OffsetOf(..)
141+
| Paren(..)
142+
| Path(..)
143+
| Range(None, _, _)
144+
| Repeat(..)
145+
| Ret(..)
146+
| Struct(..)
147+
| Try(..)
148+
| TryBlock(..)
149+
| Tup(..)
150+
| Type(..)
151+
| Unary(..)
152+
| Underscore
153+
| Yeet(..)
154+
| Yield(..)
155+
| Err(..)
156+
| Dummy => return false,
157+
}
158+
}
159+
}
160+
84161
pub enum TrailingBrace<'a> {
85162
/// Trailing brace in a macro call, like the one in `x as *const brace! {}`.
86163
/// We will suggest changing the macro call to a different delimiter.

compiler/rustc_ast_pretty/src/pprust/state/expr.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use ast::{ForLoopKind, MatchKind};
55
use itertools::{Itertools, Position};
66
use rustc_ast::ptr::P;
77
use rustc_ast::token;
8+
use rustc_ast::util::classify;
89
use rustc_ast::util::literal::escape_byte_str_symbol;
910
use rustc_ast::util::parser::{self, AssocOp, Fixity};
1011
use rustc_ast::{self as ast, BlockCheckMode};
@@ -610,9 +611,12 @@ impl<'a> State<'a> {
610611
}
611612
if let Some(expr) = opt_expr {
612613
self.space();
613-
self.print_expr_maybe_paren(
614+
self.print_expr_cond_paren(
614615
expr,
615-
parser::PREC_JUMP,
616+
// Parenthesize if required by precedence, or in the
617+
// case of `break 'inner: loop { break 'inner 1 } + 1`
618+
expr.precedence().order() < parser::PREC_JUMP
619+
|| (opt_label.is_none() && classify::leading_labeled_expr(expr)),
616620
fixup.subsequent_subexpression(),
617621
);
618622
}

tests/ui/unpretty/expanded-interpolation.stdout

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ fn break_labeled_loop() {
2525
'outer: loop { break 'outer 'inner: loop { break 'inner 1; } + 1; };
2626

2727
let paren_around_break_value =
28-
'outer: loop { break 'inner: loop { break 'inner 1; } + 1; };
28+
'outer: loop { break ('inner: loop { break 'inner 1; } + 1); };
2929

3030
macro_rules! breaking { ($value:expr) => { break $value }; }
3131

3232
let paren_around_break_value =
33-
loop { break 'inner: loop { break 'inner 1; } + 1; };
33+
loop { break ('inner: loop { break 'inner 1; } + 1); };
3434
}
3535

3636
fn if_let() {

0 commit comments

Comments
 (0)