@@ -1162,32 +1162,33 @@ impl<'a> State<'a> {
1162
1162
1163
1163
fn print_expr_binary ( & mut self , op : hir:: BinOp , lhs : & hir:: Expr < ' _ > , rhs : & hir:: Expr < ' _ > ) {
1164
1164
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) ,
1172
1173
} ;
1173
1174
1174
- let left_prec = match ( & lhs. kind , op. node ) {
1175
+ match ( & lhs. kind , op. node ) {
1175
1176
// These cases need parens: `x as i32 < y` has the parser thinking that `i32 < y` is
1176
1177
// the beginning of a path type. It starts trying to parse `x as (i32 < y ...` instead
1177
1178
// of `(x as i32) < ...`. We need to convince it _not_ to do that.
1178
1179
( & hir:: ExprKind :: Cast { .. } , hir:: BinOpKind :: Lt | hir:: BinOpKind :: Shl ) => {
1179
- parser :: PREC_FORCE_PAREN
1180
+ left_needs_paren = true ;
1180
1181
}
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 ;
1183
1184
}
1184
- _ => left_prec ,
1185
- } ;
1185
+ _ => { }
1186
+ }
1186
1187
1187
- self . print_expr_cond_paren ( lhs, lhs . precedence ( ) < left_prec ) ;
1188
+ self . print_expr_cond_paren ( lhs, left_needs_paren ) ;
1188
1189
self . space ( ) ;
1189
1190
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 ) ;
1191
1192
}
1192
1193
1193
1194
fn print_expr_unary ( & mut self , op : hir:: UnOp , expr : & hir:: Expr < ' _ > ) {
0 commit comments