Skip to content

Commit c02032c

Browse files
committed
Eliminate precedence arithmetic from rustc_ast_pretty
1 parent 7442931 commit c02032c

File tree

1 file changed

+16
-25
lines changed
  • compiler/rustc_ast_pretty/src/pprust/state

1 file changed

+16
-25
lines changed

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

+16-25
Original file line numberDiff line numberDiff line change
@@ -276,21 +276,22 @@ impl<'a> State<'a> {
276276
fixup: FixupContext,
277277
) {
278278
let assoc_op = AssocOp::from_ast_binop(op.node);
279-
let prec = assoc_op.precedence() as i8;
280-
let fixity = assoc_op.fixity();
281-
282-
let (left_prec, right_prec) = match fixity {
283-
Fixity::Left => (prec, prec + 1),
284-
Fixity::Right => (prec + 1, prec),
285-
Fixity::None => (prec + 1, prec + 1),
279+
let binop_prec = assoc_op.precedence() as i8;
280+
let left_prec = lhs.precedence();
281+
let right_prec = rhs.precedence();
282+
283+
let (mut left_needs_paren, right_needs_paren) = match assoc_op.fixity() {
284+
Fixity::Left => (left_prec < binop_prec, right_prec <= binop_prec),
285+
Fixity::Right => (left_prec <= binop_prec, right_prec < binop_prec),
286+
Fixity::None => (left_prec <= binop_prec, right_prec <= binop_prec),
286287
};
287288

288-
let left_prec = match (&lhs.kind, op.node) {
289+
match (&lhs.kind, op.node) {
289290
// These cases need parens: `x as i32 < y` has the parser thinking that `i32 < y` is
290291
// the beginning of a path type. It starts trying to parse `x as (i32 < y ...` instead
291292
// of `(x as i32) < ...`. We need to convince it _not_ to do that.
292293
(&ast::ExprKind::Cast { .. }, ast::BinOpKind::Lt | ast::BinOpKind::Shl) => {
293-
parser::PREC_FORCE_PAREN
294+
left_needs_paren = true;
294295
}
295296
// We are given `(let _ = a) OP b`.
296297
//
@@ -300,26 +301,16 @@ impl<'a> State<'a> {
300301
// - Otherwise, e.g. when we have `(let a = b) < c` in AST,
301302
// parens are required since the parser would interpret `let a = b < c` as
302303
// `let a = (b < c)`. To achieve this, we force parens.
303-
(&ast::ExprKind::Let { .. }, _) if !parser::needs_par_as_let_scrutinee(prec) => {
304-
parser::PREC_FORCE_PAREN
304+
(&ast::ExprKind::Let { .. }, _) if !parser::needs_par_as_let_scrutinee(binop_prec) => {
305+
left_needs_paren = true;
305306
}
306-
_ => left_prec,
307-
};
308-
309-
self.print_expr_cond_paren(
310-
lhs,
311-
lhs.precedence() < left_prec,
312-
fixup.leftmost_subexpression(),
313-
);
307+
_ => {}
308+
}
314309

310+
self.print_expr_cond_paren(lhs, left_needs_paren, fixup.leftmost_subexpression());
315311
self.space();
316312
self.word_space(op.node.as_str());
317-
318-
self.print_expr_cond_paren(
319-
rhs,
320-
rhs.precedence() < right_prec,
321-
fixup.subsequent_subexpression(),
322-
);
313+
self.print_expr_cond_paren(rhs, right_needs_paren, fixup.subsequent_subexpression());
323314
}
324315

325316
fn print_expr_unary(&mut self, op: ast::UnOp, expr: &ast::Expr, fixup: FixupContext) {

0 commit comments

Comments
 (0)