@@ -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 ;
@@ -214,13 +214,6 @@ impl<'a> State<'a> {
214
214
}
215
215
216
216
fn print_expr_call ( & mut self , func : & ast:: Expr , args : & [ P < ast:: Expr > ] , fixup : FixupContext ) {
217
- let needs_paren = match func. kind {
218
- // In order to call a named field, needs parens: `(self.fun)()`
219
- // But not for an unnamed field: `self.0()`
220
- ast:: ExprKind :: Field ( _, name) => !name. is_numeric ( ) ,
221
- _ => func. precedence ( ) < ExprPrecedence :: Unambiguous ,
222
- } ;
223
-
224
217
// Independent of parenthesization related to precedence, we must
225
218
// parenthesize `func` if this is a statement context in which without
226
219
// parentheses, a statement boundary would occur inside `func` or
@@ -237,8 +230,16 @@ impl<'a> State<'a> {
237
230
// because the latter is valid syntax but with the incorrect meaning.
238
231
// It's a match-expression followed by tuple-expression, not a function
239
232
// call.
240
- self . print_expr_cond_paren ( func, needs_paren, fixup. leftmost_subexpression ( ) ) ;
233
+ let func_fixup = fixup. leftmost_subexpression_with_operator ( true ) ;
234
+
235
+ let needs_paren = match func. kind {
236
+ // In order to call a named field, needs parens: `(self.fun)()`
237
+ // But not for an unnamed field: `self.0()`
238
+ ast:: ExprKind :: Field ( _, name) => !name. is_numeric ( ) ,
239
+ _ => func_fixup. precedence ( func) < ExprPrecedence :: Unambiguous ,
240
+ } ;
241
241
242
+ self . print_expr_cond_paren ( func, needs_paren, func_fixup) ;
242
243
self . print_call_post ( args)
243
244
}
244
245
@@ -281,9 +282,24 @@ impl<'a> State<'a> {
281
282
rhs : & ast:: Expr ,
282
283
fixup : FixupContext ,
283
284
) {
285
+ let operator_can_begin_expr = match op {
286
+ | BinOpKind :: Sub // -x
287
+ | BinOpKind :: Mul // *x
288
+ | BinOpKind :: And // &&x
289
+ | BinOpKind :: Or // || x
290
+ | BinOpKind :: BitAnd // &x
291
+ | BinOpKind :: BitOr // |x| x
292
+ | BinOpKind :: Shl // <<T as Trait>::Type as Trait>::CONST
293
+ | BinOpKind :: Lt // <T as Trait>::CONST
294
+ => true ,
295
+ _ => false ,
296
+ } ;
297
+
298
+ let left_fixup = fixup. leftmost_subexpression_with_operator ( operator_can_begin_expr) ;
299
+
284
300
let binop_prec = op. precedence ( ) ;
285
- let left_prec = lhs . precedence ( ) ;
286
- let right_prec = rhs . precedence ( ) ;
301
+ let left_prec = left_fixup . precedence ( lhs ) ;
302
+ let right_prec = fixup . precedence ( rhs ) ;
287
303
288
304
let ( mut left_needs_paren, right_needs_paren) = match op. fixity ( ) {
289
305
Fixity :: Left => ( left_prec < binop_prec, right_prec <= binop_prec) ,
@@ -312,18 +328,18 @@ impl<'a> State<'a> {
312
328
_ => { }
313
329
}
314
330
315
- self . print_expr_cond_paren ( lhs, left_needs_paren, fixup . leftmost_subexpression ( ) ) ;
331
+ self . print_expr_cond_paren ( lhs, left_needs_paren, left_fixup ) ;
316
332
self . space ( ) ;
317
333
self . word_space ( op. as_str ( ) ) ;
318
- self . print_expr_cond_paren ( rhs, right_needs_paren, fixup. subsequent_subexpression ( ) ) ;
334
+ self . print_expr_cond_paren ( rhs, right_needs_paren, fixup. rightmost_subexpression ( ) ) ;
319
335
}
320
336
321
337
fn print_expr_unary ( & mut self , op : ast:: UnOp , expr : & ast:: Expr , fixup : FixupContext ) {
322
338
self . word ( op. as_str ( ) ) ;
323
339
self . print_expr_cond_paren (
324
340
expr,
325
- expr . precedence ( ) < ExprPrecedence :: Prefix ,
326
- fixup. subsequent_subexpression ( ) ,
341
+ fixup . precedence ( expr ) < ExprPrecedence :: Prefix ,
342
+ fixup. rightmost_subexpression ( ) ,
327
343
) ;
328
344
}
329
345
@@ -344,8 +360,8 @@ impl<'a> State<'a> {
344
360
}
345
361
self . print_expr_cond_paren (
346
362
expr,
347
- expr . precedence ( ) < ExprPrecedence :: Prefix ,
348
- fixup. subsequent_subexpression ( ) ,
363
+ fixup . precedence ( expr ) < ExprPrecedence :: Prefix ,
364
+ fixup. rightmost_subexpression ( ) ,
349
365
) ;
350
366
}
351
367
@@ -590,8 +606,8 @@ impl<'a> State<'a> {
590
606
self . word_space ( "=" ) ;
591
607
self . print_expr_cond_paren (
592
608
rhs,
593
- rhs . precedence ( ) < ExprPrecedence :: Assign ,
594
- fixup. subsequent_subexpression ( ) ,
609
+ fixup . precedence ( rhs ) < ExprPrecedence :: Assign ,
610
+ fixup. rightmost_subexpression ( ) ,
595
611
) ;
596
612
}
597
613
ast:: ExprKind :: AssignOp ( op, lhs, rhs) => {
@@ -604,8 +620,8 @@ impl<'a> State<'a> {
604
620
self . word_space ( op. node . as_str ( ) ) ;
605
621
self . print_expr_cond_paren (
606
622
rhs,
607
- rhs . precedence ( ) < ExprPrecedence :: Assign ,
608
- fixup. subsequent_subexpression ( ) ,
623
+ fixup . precedence ( rhs ) < ExprPrecedence :: Assign ,
624
+ fixup. rightmost_subexpression ( ) ,
609
625
) ;
610
626
}
611
627
ast:: ExprKind :: Field ( expr, ident) => {
@@ -618,10 +634,11 @@ impl<'a> State<'a> {
618
634
self . print_ident ( * ident) ;
619
635
}
620
636
ast:: ExprKind :: Index ( expr, index, _) => {
637
+ let expr_fixup = fixup. leftmost_subexpression_with_operator ( true ) ;
621
638
self . print_expr_cond_paren (
622
639
expr,
623
- expr . precedence ( ) < ExprPrecedence :: Unambiguous ,
624
- fixup . leftmost_subexpression ( ) ,
640
+ expr_fixup . precedence ( expr ) < ExprPrecedence :: Unambiguous ,
641
+ expr_fixup ,
625
642
) ;
626
643
self . word ( "[" ) ;
627
644
self . print_expr ( index, FixupContext :: default ( ) ) ;
@@ -634,10 +651,11 @@ impl<'a> State<'a> {
634
651
// a "normal" binop gets parenthesized. (`LOr` is the lowest-precedence binop.)
635
652
let fake_prec = ExprPrecedence :: LOr ;
636
653
if let Some ( e) = start {
654
+ let start_fixup = fixup. leftmost_subexpression_with_operator ( true ) ;
637
655
self . print_expr_cond_paren (
638
656
e,
639
- e . precedence ( ) < fake_prec,
640
- fixup . leftmost_subexpression ( ) ,
657
+ start_fixup . precedence ( e ) < fake_prec,
658
+ start_fixup ,
641
659
) ;
642
660
}
643
661
match limits {
@@ -647,8 +665,8 @@ impl<'a> State<'a> {
647
665
if let Some ( e) = end {
648
666
self . print_expr_cond_paren (
649
667
e,
650
- e . precedence ( ) < fake_prec,
651
- fixup. subsequent_subexpression ( ) ,
668
+ fixup . precedence ( e ) < fake_prec,
669
+ fixup. rightmost_subexpression ( ) ,
652
670
) ;
653
671
}
654
672
}
@@ -665,11 +683,10 @@ impl<'a> State<'a> {
665
683
self . space ( ) ;
666
684
self . print_expr_cond_paren (
667
685
expr,
668
- // Parenthesize if required by precedence, or in the
669
- // case of `break 'inner: loop { break 'inner 1 } + 1`
670
- expr. precedence ( ) < ExprPrecedence :: Jump
671
- || ( opt_label. is_none ( ) && classify:: leading_labeled_expr ( expr) ) ,
672
- fixup. subsequent_subexpression ( ) ,
686
+ // Parenthesize `break 'inner: loop { break 'inner 1 } + 1`
687
+ // ^---------------------------------^
688
+ opt_label. is_none ( ) && classify:: leading_labeled_expr ( expr) ,
689
+ fixup. rightmost_subexpression ( ) ,
673
690
) ;
674
691
}
675
692
}
@@ -684,11 +701,7 @@ impl<'a> State<'a> {
684
701
self . word ( "return" ) ;
685
702
if let Some ( expr) = result {
686
703
self . word ( " " ) ;
687
- self . print_expr_cond_paren (
688
- expr,
689
- expr. precedence ( ) < ExprPrecedence :: Jump ,
690
- fixup. subsequent_subexpression ( ) ,
691
- ) ;
704
+ self . print_expr ( expr, fixup. rightmost_subexpression ( ) ) ;
692
705
}
693
706
}
694
707
ast:: ExprKind :: Yeet ( result) => {
@@ -697,21 +710,13 @@ impl<'a> State<'a> {
697
710
self . word ( "yeet" ) ;
698
711
if let Some ( expr) = result {
699
712
self . word ( " " ) ;
700
- self . print_expr_cond_paren (
701
- expr,
702
- expr. precedence ( ) < ExprPrecedence :: Jump ,
703
- fixup. subsequent_subexpression ( ) ,
704
- ) ;
713
+ self . print_expr ( expr, fixup. rightmost_subexpression ( ) ) ;
705
714
}
706
715
}
707
716
ast:: ExprKind :: Become ( result) => {
708
717
self . word ( "become" ) ;
709
718
self . word ( " " ) ;
710
- self . print_expr_cond_paren (
711
- result,
712
- result. precedence ( ) < ExprPrecedence :: Jump ,
713
- fixup. subsequent_subexpression ( ) ,
714
- ) ;
719
+ self . print_expr ( result, fixup. rightmost_subexpression ( ) ) ;
715
720
}
716
721
ast:: ExprKind :: InlineAsm ( a) => {
717
722
// FIXME: Print `builtin # asm` once macro `asm` uses `builtin_syntax`.
@@ -761,11 +766,7 @@ impl<'a> State<'a> {
761
766
762
767
if let Some ( expr) = e {
763
768
self . space ( ) ;
764
- self . print_expr_cond_paren (
765
- expr,
766
- expr. precedence ( ) < ExprPrecedence :: Jump ,
767
- fixup. subsequent_subexpression ( ) ,
768
- ) ;
769
+ self . print_expr ( expr, fixup. rightmost_subexpression ( ) ) ;
769
770
}
770
771
}
771
772
ast:: ExprKind :: Yield ( YieldKind :: Postfix ( e) ) => {
0 commit comments