Skip to content

Commit 6292b2a

Browse files
committedJul 2, 2024
Auto merge of #127244 - matthiaskrgr:rollup-px1vahe, r=matthiaskrgr
Rollup of 10 pull requests Successful merges: - #126883 (Parenthesize break values containing leading label) - #127136 (Fix `FnMut::call_mut`/`Fn::call` shim for async closures that capture references) - #127146 (Uplift fast rejection to new solver) - #127152 (Bootstrap: Try renaming the file if removing fails) - #127168 (Use the aligned size for alloca at args/ret when the pass mode is cast) - #127203 (Fix import suggestion error when path segment failed not from starting) - #127212 (Update books) - #127224 (Make `FloatTy` checks exhaustive in pretty print) - #127230 (chore: remove duplicate words) - #127243 (Add test for adt_const_params) r? `@ghost` `@rustbot` modify labels: rollup
·
1.88.01.81.0
2 parents 49ff390 + 6407580 commit 6292b2a

File tree

79 files changed

+1301
-546
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+1301
-546
lines changed
 

‎compiler/rustc_ast/src/util/classify.rs

Lines changed: 78 additions & 1 deletion
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+
| Await(e, _)
111+
| Binary(_, e, _)
112+
| Call(e, _)
113+
| Cast(e, _)
114+
| Field(e, _)
115+
| Index(e, _, _)
116+
| Match(e, _, MatchKind::Postfix)
117+
| Range(Some(e), _, _)
118+
| Try(e) => {
119+
expr = e;
120+
}
121+
MethodCall(method_call) => {
122+
expr = &method_call.receiver;
123+
}
124+
125+
AddrOf(..)
126+
| Array(..)
127+
| Become(..)
128+
| Break(..)
129+
| Closure(..)
130+
| ConstBlock(..)
131+
| Continue(..)
132+
| FormatArgs(..)
133+
| Gen(..)
134+
| If(..)
135+
| IncludedBytes(..)
136+
| InlineAsm(..)
137+
| Let(..)
138+
| Lit(..)
139+
| MacCall(..)
140+
| Match(_, _, MatchKind::Prefix)
141+
| OffsetOf(..)
142+
| Paren(..)
143+
| Path(..)
144+
| Range(None, _, _)
145+
| Repeat(..)
146+
| Ret(..)
147+
| Struct(..)
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

Lines changed: 6 additions & 2 deletions
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
}

0 commit comments

Comments
 (0)
Please sign in to comment.