@@ -276,21 +276,22 @@ impl<'a> State<'a> {
276
276
fixup : FixupContext ,
277
277
) {
278
278
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) ,
286
287
} ;
287
288
288
- let left_prec = match ( & lhs. kind , op. node ) {
289
+ match ( & lhs. kind , op. node ) {
289
290
// These cases need parens: `x as i32 < y` has the parser thinking that `i32 < y` is
290
291
// the beginning of a path type. It starts trying to parse `x as (i32 < y ...` instead
291
292
// of `(x as i32) < ...`. We need to convince it _not_ to do that.
292
293
( & ast:: ExprKind :: Cast { .. } , ast:: BinOpKind :: Lt | ast:: BinOpKind :: Shl ) => {
293
- parser :: PREC_FORCE_PAREN
294
+ left_needs_paren = true ;
294
295
}
295
296
// We are given `(let _ = a) OP b`.
296
297
//
@@ -300,26 +301,16 @@ impl<'a> State<'a> {
300
301
// - Otherwise, e.g. when we have `(let a = b) < c` in AST,
301
302
// parens are required since the parser would interpret `let a = b < c` as
302
303
// `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 ;
305
306
}
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
+ }
314
309
310
+ self . print_expr_cond_paren ( lhs, left_needs_paren, fixup. leftmost_subexpression ( ) ) ;
315
311
self . space ( ) ;
316
312
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 ( ) ) ;
323
314
}
324
315
325
316
fn print_expr_unary ( & mut self , op : ast:: UnOp , expr : & ast:: Expr , fixup : FixupContext ) {
0 commit comments