Skip to content

Commit 34a65f2

Browse files
committed
Eliminate precedence arithmetic from rustc_hir_pretty
1 parent c02032c commit 34a65f2

File tree

1 file changed

+16
-15
lines changed
  • compiler/rustc_hir_pretty/src

1 file changed

+16
-15
lines changed

compiler/rustc_hir_pretty/src/lib.rs

+16-15
Original file line numberDiff line numberDiff line change
@@ -1162,32 +1162,33 @@ impl<'a> State<'a> {
11621162

11631163
fn print_expr_binary(&mut self, op: hir::BinOp, lhs: &hir::Expr<'_>, rhs: &hir::Expr<'_>) {
11641164
let assoc_op = AssocOp::from_ast_binop(op.node);
1165-
let prec = assoc_op.precedence() as i8;
1166-
let fixity = assoc_op.fixity();
1167-
1168-
let (left_prec, right_prec) = match fixity {
1169-
Fixity::Left => (prec, prec + 1),
1170-
Fixity::Right => (prec + 1, prec),
1171-
Fixity::None => (prec + 1, prec + 1),
1165+
let binop_prec = assoc_op.precedence() as i8;
1166+
let left_prec = lhs.precedence();
1167+
let right_prec = rhs.precedence();
1168+
1169+
let (mut left_needs_paren, right_needs_paren) = match assoc_op.fixity() {
1170+
Fixity::Left => (left_prec < binop_prec, right_prec <= binop_prec),
1171+
Fixity::Right => (left_prec <= binop_prec, right_prec < binop_prec),
1172+
Fixity::None => (left_prec <= binop_prec, right_prec <= binop_prec),
11721173
};
11731174

1174-
let left_prec = match (&lhs.kind, op.node) {
1175+
match (&lhs.kind, op.node) {
11751176
// These cases need parens: `x as i32 < y` has the parser thinking that `i32 < y` is
11761177
// the beginning of a path type. It starts trying to parse `x as (i32 < y ...` instead
11771178
// of `(x as i32) < ...`. We need to convince it _not_ to do that.
11781179
(&hir::ExprKind::Cast { .. }, hir::BinOpKind::Lt | hir::BinOpKind::Shl) => {
1179-
parser::PREC_FORCE_PAREN
1180+
left_needs_paren = true;
11801181
}
1181-
(&hir::ExprKind::Let { .. }, _) if !parser::needs_par_as_let_scrutinee(prec) => {
1182-
parser::PREC_FORCE_PAREN
1182+
(&hir::ExprKind::Let { .. }, _) if !parser::needs_par_as_let_scrutinee(binop_prec) => {
1183+
left_needs_paren = true;
11831184
}
1184-
_ => left_prec,
1185-
};
1185+
_ => {}
1186+
}
11861187

1187-
self.print_expr_cond_paren(lhs, lhs.precedence() < left_prec);
1188+
self.print_expr_cond_paren(lhs, left_needs_paren);
11881189
self.space();
11891190
self.word_space(op.node.as_str());
1190-
self.print_expr_cond_paren(rhs, rhs.precedence() < right_prec)
1191+
self.print_expr_cond_paren(rhs, right_needs_paren);
11911192
}
11921193

11931194
fn print_expr_unary(&mut self, op: hir::UnOp, expr: &hir::Expr<'_>) {

0 commit comments

Comments
 (0)