@@ -5,7 +5,6 @@ use ast::{ForLoopKind, MatchKind};
55use itertools:: { Itertools , Position } ;
66use rustc_ast:: ptr:: P ;
77use rustc_ast:: token;
8- use rustc_ast:: util:: classify;
98use rustc_ast:: util:: literal:: escape_byte_str_symbol;
109use rustc_ast:: util:: parser:: { self , AssocOp , Fixity } ;
1110use rustc_ast:: { self as ast, BlockCheckMode } ;
@@ -65,9 +64,7 @@ impl<'a> State<'a> {
6564 /// Prints an expr using syntax that's acceptable in a condition position, such as the `cond` in
6665 /// `if cond { ... }`.
6766 fn print_expr_as_cond ( & mut self , expr : & ast:: Expr ) {
68- let fixup =
69- FixupContext { parenthesize_exterior_struct_lit : true , ..FixupContext :: default ( ) } ;
70- self . print_expr_cond_paren ( expr, Self :: cond_needs_par ( expr) , fixup)
67+ self . print_expr_cond_paren ( expr, Self :: cond_needs_par ( expr) , FixupContext :: new_cond ( ) )
7168 }
7269
7370 /// Does `expr` need parentheses when printed in a condition position?
@@ -239,15 +236,7 @@ impl<'a> State<'a> {
239236 // because the latter is valid syntax but with the incorrect meaning.
240237 // It's a match-expression followed by tuple-expression, not a function
241238 // call.
242- self . print_expr_maybe_paren (
243- func,
244- prec,
245- FixupContext {
246- stmt : false ,
247- leftmost_subexpression_in_stmt : fixup. stmt || fixup. leftmost_subexpression_in_stmt ,
248- ..fixup
249- } ,
250- ) ;
239+ self . print_expr_maybe_paren ( func, prec, fixup. leftmost_subexpression ( ) ) ;
251240
252241 self . print_call_post ( args)
253242 }
@@ -316,33 +305,17 @@ impl<'a> State<'a> {
316305 _ => left_prec,
317306 } ;
318307
319- self . print_expr_maybe_paren (
320- lhs,
321- left_prec,
322- FixupContext {
323- stmt : false ,
324- leftmost_subexpression_in_stmt : fixup. stmt || fixup. leftmost_subexpression_in_stmt ,
325- ..fixup
326- } ,
327- ) ;
308+ self . print_expr_maybe_paren ( lhs, left_prec, fixup. leftmost_subexpression ( ) ) ;
328309
329310 self . space ( ) ;
330311 self . word_space ( op. node . as_str ( ) ) ;
331312
332- self . print_expr_maybe_paren (
333- rhs,
334- right_prec,
335- FixupContext { stmt : false , leftmost_subexpression_in_stmt : false , ..fixup } ,
336- ) ;
313+ self . print_expr_maybe_paren ( rhs, right_prec, fixup. subsequent_subexpression ( ) ) ;
337314 }
338315
339316 fn print_expr_unary ( & mut self , op : ast:: UnOp , expr : & ast:: Expr , fixup : FixupContext ) {
340317 self . word ( op. as_str ( ) ) ;
341- self . print_expr_maybe_paren (
342- expr,
343- parser:: PREC_PREFIX ,
344- FixupContext { stmt : false , leftmost_subexpression_in_stmt : false , ..fixup } ,
345- ) ;
318+ self . print_expr_maybe_paren ( expr, parser:: PREC_PREFIX , fixup. subsequent_subexpression ( ) ) ;
346319 }
347320
348321 fn print_expr_addr_of (
@@ -360,11 +333,7 @@ impl<'a> State<'a> {
360333 self . print_mutability ( mutability, true ) ;
361334 }
362335 }
363- self . print_expr_maybe_paren (
364- expr,
365- parser:: PREC_PREFIX ,
366- FixupContext { stmt : false , leftmost_subexpression_in_stmt : false , ..fixup } ,
367- ) ;
336+ self . print_expr_maybe_paren ( expr, parser:: PREC_PREFIX , fixup. subsequent_subexpression ( ) ) ;
368337 }
369338
370339 pub ( super ) fn print_expr ( & mut self , expr : & ast:: Expr , fixup : FixupContext ) {
@@ -399,8 +368,7 @@ impl<'a> State<'a> {
399368 //
400369 // Same applies to a small set of other expression kinds which eagerly
401370 // terminate a statement which opens with them.
402- let needs_par =
403- fixup. leftmost_subexpression_in_stmt && !classify:: expr_requires_semi_to_be_stmt ( expr) ;
371+ let needs_par = fixup. would_cause_statement_boundary ( expr) ;
404372 if needs_par {
405373 self . popen ( ) ;
406374 fixup = FixupContext :: default ( ) ;
@@ -448,16 +416,7 @@ impl<'a> State<'a> {
448416 }
449417 ast:: ExprKind :: Cast ( expr, ty) => {
450418 let prec = AssocOp :: As . precedence ( ) as i8 ;
451- self . print_expr_maybe_paren (
452- expr,
453- prec,
454- FixupContext {
455- stmt : false ,
456- leftmost_subexpression_in_stmt : fixup. stmt
457- || fixup. leftmost_subexpression_in_stmt ,
458- ..fixup
459- } ,
460- ) ;
419+ self . print_expr_maybe_paren ( expr, prec, fixup. leftmost_subexpression ( ) ) ;
461420 self . space ( ) ;
462421 self . word_space ( "as" ) ;
463422 self . print_type ( ty) ;
@@ -589,70 +548,34 @@ impl<'a> State<'a> {
589548 self . print_block_with_attrs ( blk, attrs) ;
590549 }
591550 ast:: ExprKind :: Await ( expr, _) => {
592- // Same fixups as ExprKind::MethodCall.
593551 self . print_expr_maybe_paren ( expr, parser:: PREC_POSTFIX , fixup) ;
594552 self . word ( ".await" ) ;
595553 }
596554 ast:: ExprKind :: Assign ( lhs, rhs, _) => {
597- // Same fixups as ExprKind::Binary.
598555 let prec = AssocOp :: Assign . precedence ( ) as i8 ;
599- self . print_expr_maybe_paren (
600- lhs,
601- prec + 1 ,
602- FixupContext {
603- stmt : false ,
604- leftmost_subexpression_in_stmt : fixup. stmt
605- || fixup. leftmost_subexpression_in_stmt ,
606- ..fixup
607- } ,
608- ) ;
556+ self . print_expr_maybe_paren ( lhs, prec + 1 , fixup. leftmost_subexpression ( ) ) ;
609557 self . space ( ) ;
610558 self . word_space ( "=" ) ;
611- self . print_expr_maybe_paren (
612- rhs,
613- prec,
614- FixupContext { stmt : false , leftmost_subexpression_in_stmt : false , ..fixup } ,
615- ) ;
559+ self . print_expr_maybe_paren ( rhs, prec, fixup. subsequent_subexpression ( ) ) ;
616560 }
617561 ast:: ExprKind :: AssignOp ( op, lhs, rhs) => {
618- // Same fixups as ExprKind::Binary.
619562 let prec = AssocOp :: Assign . precedence ( ) as i8 ;
620- self . print_expr_maybe_paren (
621- lhs,
622- prec + 1 ,
623- FixupContext {
624- stmt : false ,
625- leftmost_subexpression_in_stmt : fixup. stmt
626- || fixup. leftmost_subexpression_in_stmt ,
627- ..fixup
628- } ,
629- ) ;
563+ self . print_expr_maybe_paren ( lhs, prec + 1 , fixup. leftmost_subexpression ( ) ) ;
630564 self . space ( ) ;
631565 self . word ( op. node . as_str ( ) ) ;
632566 self . word_space ( "=" ) ;
633- self . print_expr_maybe_paren (
634- rhs,
635- prec,
636- FixupContext { stmt : false , leftmost_subexpression_in_stmt : false , ..fixup } ,
637- ) ;
567+ self . print_expr_maybe_paren ( rhs, prec, fixup. subsequent_subexpression ( ) ) ;
638568 }
639569 ast:: ExprKind :: Field ( expr, ident) => {
640- // Same fixups as ExprKind::MethodCall.
641570 self . print_expr_maybe_paren ( expr, parser:: PREC_POSTFIX , fixup) ;
642571 self . word ( "." ) ;
643572 self . print_ident ( * ident) ;
644573 }
645574 ast:: ExprKind :: Index ( expr, index, _) => {
646- // Same fixups as ExprKind::Call.
647575 self . print_expr_maybe_paren (
648576 expr,
649577 parser:: PREC_POSTFIX ,
650- FixupContext {
651- stmt : false ,
652- leftmost_subexpression_in_stmt : fixup. stmt
653- || fixup. leftmost_subexpression_in_stmt ,
654- ..fixup
655- } ,
578+ fixup. leftmost_subexpression ( ) ,
656579 ) ;
657580 self . word ( "[" ) ;
658581 self . print_expr ( index, FixupContext :: default ( ) ) ;
@@ -665,31 +588,14 @@ impl<'a> State<'a> {
665588 // a "normal" binop gets parenthesized. (`LOr` is the lowest-precedence binop.)
666589 let fake_prec = AssocOp :: LOr . precedence ( ) as i8 ;
667590 if let Some ( e) = start {
668- self . print_expr_maybe_paren (
669- e,
670- fake_prec,
671- FixupContext {
672- stmt : false ,
673- leftmost_subexpression_in_stmt : fixup. stmt
674- || fixup. leftmost_subexpression_in_stmt ,
675- ..fixup
676- } ,
677- ) ;
591+ self . print_expr_maybe_paren ( e, fake_prec, fixup. leftmost_subexpression ( ) ) ;
678592 }
679593 match limits {
680594 ast:: RangeLimits :: HalfOpen => self . word ( ".." ) ,
681595 ast:: RangeLimits :: Closed => self . word ( "..=" ) ,
682596 }
683597 if let Some ( e) = end {
684- self . print_expr_maybe_paren (
685- e,
686- fake_prec,
687- FixupContext {
688- stmt : false ,
689- leftmost_subexpression_in_stmt : false ,
690- ..fixup
691- } ,
692- ) ;
598+ self . print_expr_maybe_paren ( e, fake_prec, fixup. subsequent_subexpression ( ) ) ;
693599 }
694600 }
695601 ast:: ExprKind :: Underscore => self . word ( "_" ) ,
@@ -706,11 +612,7 @@ impl<'a> State<'a> {
706612 self . print_expr_maybe_paren (
707613 expr,
708614 parser:: PREC_JUMP ,
709- FixupContext {
710- stmt : false ,
711- leftmost_subexpression_in_stmt : false ,
712- ..fixup
713- } ,
615+ fixup. subsequent_subexpression ( ) ,
714616 ) ;
715617 }
716618 }
@@ -728,11 +630,7 @@ impl<'a> State<'a> {
728630 self . print_expr_maybe_paren (
729631 expr,
730632 parser:: PREC_JUMP ,
731- FixupContext {
732- stmt : false ,
733- leftmost_subexpression_in_stmt : false ,
734- ..fixup
735- } ,
633+ fixup. subsequent_subexpression ( ) ,
736634 ) ;
737635 }
738636 }
@@ -745,11 +643,7 @@ impl<'a> State<'a> {
745643 self . print_expr_maybe_paren (
746644 expr,
747645 parser:: PREC_JUMP ,
748- FixupContext {
749- stmt : false ,
750- leftmost_subexpression_in_stmt : false ,
751- ..fixup
752- } ,
646+ fixup. subsequent_subexpression ( ) ,
753647 ) ;
754648 }
755649 }
@@ -759,7 +653,7 @@ impl<'a> State<'a> {
759653 self . print_expr_maybe_paren (
760654 result,
761655 parser:: PREC_JUMP ,
762- FixupContext { stmt : false , leftmost_subexpression_in_stmt : false , .. fixup } ,
656+ fixup. subsequent_subexpression ( ) ,
763657 ) ;
764658 }
765659 ast:: ExprKind :: InlineAsm ( a) => {
@@ -813,16 +707,11 @@ impl<'a> State<'a> {
813707 self . print_expr_maybe_paren (
814708 expr,
815709 parser:: PREC_JUMP ,
816- FixupContext {
817- stmt : false ,
818- leftmost_subexpression_in_stmt : false ,
819- ..fixup
820- } ,
710+ fixup. subsequent_subexpression ( ) ,
821711 ) ;
822712 }
823713 }
824714 ast:: ExprKind :: Try ( e) => {
825- // Same fixups as ExprKind::MethodCall.
826715 self . print_expr_maybe_paren ( e, parser:: PREC_POSTFIX , fixup) ;
827716 self . word ( "?" )
828717 }
@@ -890,7 +779,7 @@ impl<'a> State<'a> {
890779 }
891780 _ => {
892781 self . end ( ) ; // Close the ibox for the pattern.
893- self . print_expr ( body, FixupContext { stmt : true , .. FixupContext :: default ( ) } ) ;
782+ self . print_expr ( body, FixupContext :: new_stmt ( ) ) ;
894783 self . word ( "," ) ;
895784 }
896785 }
0 commit comments