@@ -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 , AssocOp , ExprPrecedence , Fixity } ;
9
9
use rustc_ast:: {
10
- self as ast, BlockCheckMode , FormatAlignment , FormatArgPosition , FormatArgsPiece , FormatCount ,
11
- FormatDebugHex , FormatSign , FormatTrait , token,
10
+ self as ast, BinOpKind , BlockCheckMode , FormatAlignment , FormatArgPosition , FormatArgsPiece ,
11
+ FormatCount , FormatDebugHex , FormatSign , FormatTrait , 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,10 +280,25 @@ impl<'a> State<'a> {
279
280
rhs : & ast:: Expr ,
280
281
fixup : FixupContext ,
281
282
) {
283
+ let operator_can_begin_expr = match op. node {
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 assoc_op = AssocOp :: from_ast_binop ( op. node ) ;
283
299
let binop_prec = assoc_op. precedence ( ) ;
284
- let left_prec = lhs . precedence ( ) ;
285
- let right_prec = rhs . precedence ( ) ;
300
+ let left_prec = left_fixup . precedence ( lhs ) ;
301
+ let right_prec = fixup . precedence ( rhs ) ;
286
302
287
303
let ( mut left_needs_paren, right_needs_paren) = match assoc_op. fixity ( ) {
288
304
Fixity :: Left => ( left_prec < binop_prec, right_prec <= binop_prec) ,
@@ -311,18 +327,18 @@ impl<'a> State<'a> {
311
327
_ => { }
312
328
}
313
329
314
- self . print_expr_cond_paren ( lhs, left_needs_paren, fixup . leftmost_subexpression ( ) ) ;
330
+ self . print_expr_cond_paren ( lhs, left_needs_paren, left_fixup ) ;
315
331
self . space ( ) ;
316
332
self . word_space ( op. node . as_str ( ) ) ;
317
- self . print_expr_cond_paren ( rhs, right_needs_paren, fixup. subsequent_subexpression ( ) ) ;
333
+ self . print_expr_cond_paren ( rhs, right_needs_paren, fixup. rightmost_subexpression ( ) ) ;
318
334
}
319
335
320
336
fn print_expr_unary ( & mut self , op : ast:: UnOp , expr : & ast:: Expr , fixup : FixupContext ) {
321
337
self . word ( op. as_str ( ) ) ;
322
338
self . print_expr_cond_paren (
323
339
expr,
324
- expr . precedence ( ) < ExprPrecedence :: Prefix ,
325
- fixup. subsequent_subexpression ( ) ,
340
+ fixup . precedence ( expr ) < ExprPrecedence :: Prefix ,
341
+ fixup. rightmost_subexpression ( ) ,
326
342
) ;
327
343
}
328
344
@@ -343,8 +359,8 @@ impl<'a> State<'a> {
343
359
}
344
360
self . print_expr_cond_paren (
345
361
expr,
346
- expr . precedence ( ) < ExprPrecedence :: Prefix ,
347
- fixup. subsequent_subexpression ( ) ,
362
+ fixup . precedence ( expr ) < ExprPrecedence :: Prefix ,
363
+ fixup. rightmost_subexpression ( ) ,
348
364
) ;
349
365
}
350
366
@@ -587,8 +603,8 @@ impl<'a> State<'a> {
587
603
self . word_space ( "=" ) ;
588
604
self . print_expr_cond_paren (
589
605
rhs,
590
- rhs . precedence ( ) < ExprPrecedence :: Assign ,
591
- fixup. subsequent_subexpression ( ) ,
606
+ fixup . precedence ( rhs ) < ExprPrecedence :: Assign ,
607
+ fixup. rightmost_subexpression ( ) ,
592
608
) ;
593
609
}
594
610
ast:: ExprKind :: AssignOp ( op, lhs, rhs) => {
@@ -602,8 +618,8 @@ impl<'a> State<'a> {
602
618
self . word_space ( "=" ) ;
603
619
self . print_expr_cond_paren (
604
620
rhs,
605
- rhs . precedence ( ) < ExprPrecedence :: Assign ,
606
- fixup. subsequent_subexpression ( ) ,
621
+ fixup . precedence ( rhs ) < ExprPrecedence :: Assign ,
622
+ fixup. rightmost_subexpression ( ) ,
607
623
) ;
608
624
}
609
625
ast:: ExprKind :: Field ( expr, ident) => {
@@ -616,10 +632,11 @@ impl<'a> State<'a> {
616
632
self . print_ident ( * ident) ;
617
633
}
618
634
ast:: ExprKind :: Index ( expr, index, _) => {
635
+ let expr_fixup = fixup. leftmost_subexpression_with_operator ( true ) ;
619
636
self . print_expr_cond_paren (
620
637
expr,
621
- expr . precedence ( ) < ExprPrecedence :: Unambiguous ,
622
- fixup . leftmost_subexpression ( ) ,
638
+ expr_fixup . precedence ( expr ) < ExprPrecedence :: Unambiguous ,
639
+ expr_fixup ,
623
640
) ;
624
641
self . word ( "[" ) ;
625
642
self . print_expr ( index, FixupContext :: default ( ) ) ;
@@ -632,10 +649,11 @@ impl<'a> State<'a> {
632
649
// a "normal" binop gets parenthesized. (`LOr` is the lowest-precedence binop.)
633
650
let fake_prec = ExprPrecedence :: LOr ;
634
651
if let Some ( e) = start {
652
+ let start_fixup = fixup. leftmost_subexpression_with_operator ( true ) ;
635
653
self . print_expr_cond_paren (
636
654
e,
637
- e . precedence ( ) < fake_prec,
638
- fixup . leftmost_subexpression ( ) ,
655
+ start_fixup . precedence ( e ) < fake_prec,
656
+ start_fixup ,
639
657
) ;
640
658
}
641
659
match limits {
@@ -645,8 +663,8 @@ impl<'a> State<'a> {
645
663
if let Some ( e) = end {
646
664
self . print_expr_cond_paren (
647
665
e,
648
- e . precedence ( ) < fake_prec,
649
- fixup. subsequent_subexpression ( ) ,
666
+ fixup . precedence ( e ) < fake_prec,
667
+ fixup. rightmost_subexpression ( ) ,
650
668
) ;
651
669
}
652
670
}
@@ -663,11 +681,10 @@ impl<'a> State<'a> {
663
681
self . space ( ) ;
664
682
self . print_expr_cond_paren (
665
683
expr,
666
- // Parenthesize if required by precedence, or in the
667
- // case of `break 'inner: loop { break 'inner 1 } + 1`
668
- expr. precedence ( ) < ExprPrecedence :: Jump
669
- || ( opt_label. is_none ( ) && classify:: leading_labeled_expr ( expr) ) ,
670
- fixup. subsequent_subexpression ( ) ,
684
+ // Parenthesize `break 'inner: loop { break 'inner 1 } + 1`
685
+ // ^---------------------------------^
686
+ opt_label. is_none ( ) && classify:: leading_labeled_expr ( expr) ,
687
+ fixup. rightmost_subexpression ( ) ,
671
688
) ;
672
689
}
673
690
}
@@ -682,11 +699,7 @@ impl<'a> State<'a> {
682
699
self . word ( "return" ) ;
683
700
if let Some ( expr) = result {
684
701
self . word ( " " ) ;
685
- self . print_expr_cond_paren (
686
- expr,
687
- expr. precedence ( ) < ExprPrecedence :: Jump ,
688
- fixup. subsequent_subexpression ( ) ,
689
- ) ;
702
+ self . print_expr ( expr, fixup. rightmost_subexpression ( ) ) ;
690
703
}
691
704
}
692
705
ast:: ExprKind :: Yeet ( result) => {
@@ -695,21 +708,13 @@ impl<'a> State<'a> {
695
708
self . word ( "yeet" ) ;
696
709
if let Some ( expr) = result {
697
710
self . word ( " " ) ;
698
- self . print_expr_cond_paren (
699
- expr,
700
- expr. precedence ( ) < ExprPrecedence :: Jump ,
701
- fixup. subsequent_subexpression ( ) ,
702
- ) ;
711
+ self . print_expr ( expr, fixup. rightmost_subexpression ( ) ) ;
703
712
}
704
713
}
705
714
ast:: ExprKind :: Become ( result) => {
706
715
self . word ( "become" ) ;
707
716
self . word ( " " ) ;
708
- self . print_expr_cond_paren (
709
- result,
710
- result. precedence ( ) < ExprPrecedence :: Jump ,
711
- fixup. subsequent_subexpression ( ) ,
712
- ) ;
717
+ self . print_expr ( result, fixup. rightmost_subexpression ( ) ) ;
713
718
}
714
719
ast:: ExprKind :: InlineAsm ( a) => {
715
720
// FIXME: Print `builtin # asm` once macro `asm` uses `builtin_syntax`.
@@ -759,11 +764,7 @@ impl<'a> State<'a> {
759
764
760
765
if let Some ( expr) = e {
761
766
self . space ( ) ;
762
- self . print_expr_cond_paren (
763
- expr,
764
- expr. precedence ( ) < ExprPrecedence :: Jump ,
765
- fixup. subsequent_subexpression ( ) ,
766
- ) ;
767
+ self . print_expr ( expr, fixup. rightmost_subexpression ( ) ) ;
767
768
}
768
769
}
769
770
ast:: ExprKind :: Try ( e) => {
0 commit comments