@@ -7,8 +7,8 @@ use rustc_ast::util::classify;
7
7
use rustc_ast:: util:: literal:: escape_byte_str_symbol;
8
8
use rustc_ast:: util:: parser:: { self , ExprPrecedence , Fixity } ;
9
9
use rustc_ast:: {
10
- self as ast, BlockCheckMode , FormatAlignment , FormatArgPosition , FormatArgsPiece , FormatCount ,
11
- FormatDebugHex , FormatSign , FormatTrait , YieldKind , token,
10
+ self as ast, BinOpKind , BlockCheckMode , FormatAlignment , FormatArgPosition , FormatArgsPiece ,
11
+ FormatCount , FormatDebugHex , FormatSign , FormatTrait , YieldKind , token,
12
12
} ;
13
13
14
14
use crate :: pp:: Breaks :: Inconsistent ;
@@ -212,13 +212,6 @@ impl<'a> State<'a> {
212
212
}
213
213
214
214
fn print_expr_call ( & mut self , func : & ast:: Expr , args : & [ P < ast:: Expr > ] , fixup : FixupContext ) {
215
- let needs_paren = match func. kind {
216
- // In order to call a named field, needs parens: `(self.fun)()`
217
- // But not for an unnamed field: `self.0()`
218
- ast:: ExprKind :: Field ( _, name) => !name. is_numeric ( ) ,
219
- _ => func. precedence ( ) < ExprPrecedence :: Unambiguous ,
220
- } ;
221
-
222
215
// Independent of parenthesization related to precedence, we must
223
216
// parenthesize `func` if this is a statement context in which without
224
217
// parentheses, a statement boundary would occur inside `func` or
@@ -235,8 +228,16 @@ impl<'a> State<'a> {
235
228
// because the latter is valid syntax but with the incorrect meaning.
236
229
// It's a match-expression followed by tuple-expression, not a function
237
230
// call.
238
- self . print_expr_cond_paren ( func, needs_paren, fixup. leftmost_subexpression ( ) ) ;
231
+ let func_fixup = fixup. leftmost_subexpression_with_operator ( true ) ;
232
+
233
+ let needs_paren = match func. kind {
234
+ // In order to call a named field, needs parens: `(self.fun)()`
235
+ // But not for an unnamed field: `self.0()`
236
+ ast:: ExprKind :: Field ( _, name) => !name. is_numeric ( ) ,
237
+ _ => func_fixup. precedence ( func) < ExprPrecedence :: Unambiguous ,
238
+ } ;
239
239
240
+ self . print_expr_cond_paren ( func, needs_paren, func_fixup) ;
240
241
self . print_call_post ( args)
241
242
}
242
243
@@ -279,9 +280,24 @@ impl<'a> State<'a> {
279
280
rhs : & ast:: Expr ,
280
281
fixup : FixupContext ,
281
282
) {
283
+ let operator_can_begin_expr = match op {
284
+ | BinOpKind :: Sub // -x
285
+ | BinOpKind :: Mul // *x
286
+ | BinOpKind :: And // &&x
287
+ | BinOpKind :: Or // || x
288
+ | BinOpKind :: BitAnd // &x
289
+ | BinOpKind :: BitOr // |x| x
290
+ | BinOpKind :: Shl // <<T as Trait>::Type as Trait>::CONST
291
+ | BinOpKind :: Lt // <T as Trait>::CONST
292
+ => true ,
293
+ _ => false ,
294
+ } ;
295
+
296
+ let left_fixup = fixup. leftmost_subexpression_with_operator ( operator_can_begin_expr) ;
297
+
282
298
let binop_prec = op. precedence ( ) ;
283
- let left_prec = lhs . precedence ( ) ;
284
- let right_prec = rhs . precedence ( ) ;
299
+ let left_prec = left_fixup . precedence ( lhs ) ;
300
+ let right_prec = fixup . precedence ( rhs ) ;
285
301
286
302
let ( mut left_needs_paren, right_needs_paren) = match op. fixity ( ) {
287
303
Fixity :: Left => ( left_prec < binop_prec, right_prec <= binop_prec) ,
@@ -310,18 +326,18 @@ impl<'a> State<'a> {
310
326
_ => { }
311
327
}
312
328
313
- self . print_expr_cond_paren ( lhs, left_needs_paren, fixup . leftmost_subexpression ( ) ) ;
329
+ self . print_expr_cond_paren ( lhs, left_needs_paren, left_fixup ) ;
314
330
self . space ( ) ;
315
331
self . word_space ( op. as_str ( ) ) ;
316
- self . print_expr_cond_paren ( rhs, right_needs_paren, fixup. subsequent_subexpression ( ) ) ;
332
+ self . print_expr_cond_paren ( rhs, right_needs_paren, fixup. rightmost_subexpression ( ) ) ;
317
333
}
318
334
319
335
fn print_expr_unary ( & mut self , op : ast:: UnOp , expr : & ast:: Expr , fixup : FixupContext ) {
320
336
self . word ( op. as_str ( ) ) ;
321
337
self . print_expr_cond_paren (
322
338
expr,
323
- expr . precedence ( ) < ExprPrecedence :: Prefix ,
324
- fixup. subsequent_subexpression ( ) ,
339
+ fixup . precedence ( expr ) < ExprPrecedence :: Prefix ,
340
+ fixup. rightmost_subexpression ( ) ,
325
341
) ;
326
342
}
327
343
@@ -342,8 +358,8 @@ impl<'a> State<'a> {
342
358
}
343
359
self . print_expr_cond_paren (
344
360
expr,
345
- expr . precedence ( ) < ExprPrecedence :: Prefix ,
346
- fixup. subsequent_subexpression ( ) ,
361
+ fixup . precedence ( expr ) < ExprPrecedence :: Prefix ,
362
+ fixup. rightmost_subexpression ( ) ,
347
363
) ;
348
364
}
349
365
@@ -594,8 +610,8 @@ impl<'a> State<'a> {
594
610
self . word_space ( "=" ) ;
595
611
self . print_expr_cond_paren (
596
612
rhs,
597
- rhs . precedence ( ) < ExprPrecedence :: Assign ,
598
- fixup. subsequent_subexpression ( ) ,
613
+ fixup . precedence ( rhs ) < ExprPrecedence :: Assign ,
614
+ fixup. rightmost_subexpression ( ) ,
599
615
) ;
600
616
}
601
617
ast:: ExprKind :: AssignOp ( op, lhs, rhs) => {
@@ -608,8 +624,8 @@ impl<'a> State<'a> {
608
624
self . word_space ( op. node . as_str ( ) ) ;
609
625
self . print_expr_cond_paren (
610
626
rhs,
611
- rhs . precedence ( ) < ExprPrecedence :: Assign ,
612
- fixup. subsequent_subexpression ( ) ,
627
+ fixup . precedence ( rhs ) < ExprPrecedence :: Assign ,
628
+ fixup. rightmost_subexpression ( ) ,
613
629
) ;
614
630
}
615
631
ast:: ExprKind :: Field ( expr, ident) => {
@@ -622,10 +638,11 @@ impl<'a> State<'a> {
622
638
self . print_ident ( * ident) ;
623
639
}
624
640
ast:: ExprKind :: Index ( expr, index, _) => {
641
+ let expr_fixup = fixup. leftmost_subexpression_with_operator ( true ) ;
625
642
self . print_expr_cond_paren (
626
643
expr,
627
- expr . precedence ( ) < ExprPrecedence :: Unambiguous ,
628
- fixup . leftmost_subexpression ( ) ,
644
+ expr_fixup . precedence ( expr ) < ExprPrecedence :: Unambiguous ,
645
+ expr_fixup ,
629
646
) ;
630
647
self . word ( "[" ) ;
631
648
self . print_expr ( index, FixupContext :: default ( ) ) ;
@@ -638,10 +655,11 @@ impl<'a> State<'a> {
638
655
// a "normal" binop gets parenthesized. (`LOr` is the lowest-precedence binop.)
639
656
let fake_prec = ExprPrecedence :: LOr ;
640
657
if let Some ( e) = start {
658
+ let start_fixup = fixup. leftmost_subexpression_with_operator ( true ) ;
641
659
self . print_expr_cond_paren (
642
660
e,
643
- e . precedence ( ) < fake_prec,
644
- fixup . leftmost_subexpression ( ) ,
661
+ start_fixup . precedence ( e ) < fake_prec,
662
+ start_fixup ,
645
663
) ;
646
664
}
647
665
match limits {
@@ -651,8 +669,8 @@ impl<'a> State<'a> {
651
669
if let Some ( e) = end {
652
670
self . print_expr_cond_paren (
653
671
e,
654
- e . precedence ( ) < fake_prec,
655
- fixup. subsequent_subexpression ( ) ,
672
+ fixup . precedence ( e ) < fake_prec,
673
+ fixup. rightmost_subexpression ( ) ,
656
674
) ;
657
675
}
658
676
}
@@ -669,11 +687,10 @@ impl<'a> State<'a> {
669
687
self . space ( ) ;
670
688
self . print_expr_cond_paren (
671
689
expr,
672
- // Parenthesize if required by precedence, or in the
673
- // case of `break 'inner: loop { break 'inner 1 } + 1`
674
- expr. precedence ( ) < ExprPrecedence :: Jump
675
- || ( opt_label. is_none ( ) && classify:: leading_labeled_expr ( expr) ) ,
676
- fixup. subsequent_subexpression ( ) ,
690
+ // Parenthesize `break 'inner: loop { break 'inner 1 } + 1`
691
+ // ^---------------------------------^
692
+ opt_label. is_none ( ) && classify:: leading_labeled_expr ( expr) ,
693
+ fixup. rightmost_subexpression ( ) ,
677
694
) ;
678
695
}
679
696
}
@@ -688,11 +705,7 @@ impl<'a> State<'a> {
688
705
self . word ( "return" ) ;
689
706
if let Some ( expr) = result {
690
707
self . word ( " " ) ;
691
- self . print_expr_cond_paren (
692
- expr,
693
- expr. precedence ( ) < ExprPrecedence :: Jump ,
694
- fixup. subsequent_subexpression ( ) ,
695
- ) ;
708
+ self . print_expr ( expr, fixup. rightmost_subexpression ( ) ) ;
696
709
}
697
710
}
698
711
ast:: ExprKind :: Yeet ( result) => {
@@ -701,21 +714,13 @@ impl<'a> State<'a> {
701
714
self . word ( "yeet" ) ;
702
715
if let Some ( expr) = result {
703
716
self . word ( " " ) ;
704
- self . print_expr_cond_paren (
705
- expr,
706
- expr. precedence ( ) < ExprPrecedence :: Jump ,
707
- fixup. subsequent_subexpression ( ) ,
708
- ) ;
717
+ self . print_expr ( expr, fixup. rightmost_subexpression ( ) ) ;
709
718
}
710
719
}
711
720
ast:: ExprKind :: Become ( result) => {
712
721
self . word ( "become" ) ;
713
722
self . word ( " " ) ;
714
- self . print_expr_cond_paren (
715
- result,
716
- result. precedence ( ) < ExprPrecedence :: Jump ,
717
- fixup. subsequent_subexpression ( ) ,
718
- ) ;
723
+ self . print_expr ( result, fixup. rightmost_subexpression ( ) ) ;
719
724
}
720
725
ast:: ExprKind :: InlineAsm ( a) => {
721
726
// FIXME: Print `builtin # asm` once macro `asm` uses `builtin_syntax`.
@@ -765,11 +770,7 @@ impl<'a> State<'a> {
765
770
766
771
if let Some ( expr) = e {
767
772
self . space ( ) ;
768
- self . print_expr_cond_paren (
769
- expr,
770
- expr. precedence ( ) < ExprPrecedence :: Jump ,
771
- fixup. subsequent_subexpression ( ) ,
772
- ) ;
773
+ self . print_expr ( expr, fixup. rightmost_subexpression ( ) ) ;
773
774
}
774
775
}
775
776
ast:: ExprKind :: Yield ( YieldKind :: Postfix ( e) ) => {
0 commit comments