@@ -4,7 +4,6 @@ use ast::{ForLoopKind, MatchKind};
44use itertools:: { Itertools , Position } ;
55use rustc_ast:: ptr:: P ;
66use rustc_ast:: token;
7- use rustc_ast:: util:: classify;
87use rustc_ast:: util:: literal:: escape_byte_str_symbol;
98use rustc_ast:: util:: parser:: { self , AssocOp , Fixity } ;
109use rustc_ast:: { self as ast, BlockCheckMode } ;
@@ -64,9 +63,7 @@ impl<'a> State<'a> {
6463 /// Prints an expr using syntax that's acceptable in a condition position, such as the `cond` in
6564 /// `if cond { ... }`.
6665 fn print_expr_as_cond ( & mut self , expr : & ast:: Expr ) {
67- let fixup =
68- FixupContext { parenthesize_exterior_struct_lit : true , ..FixupContext :: default ( ) } ;
69- self . print_expr_cond_paren ( expr, Self :: cond_needs_par ( expr) , fixup)
66+ self . print_expr_cond_paren ( expr, Self :: cond_needs_par ( expr) , FixupContext :: new_cond ( ) )
7067 }
7168
7269 /// Does `expr` need parentheses when printed in a condition position?
@@ -238,15 +235,7 @@ impl<'a> State<'a> {
238235 // because the latter is valid syntax but with the incorrect meaning.
239236 // It's a match-expression followed by tuple-expression, not a function
240237 // call.
241- self . print_expr_maybe_paren (
242- func,
243- prec,
244- FixupContext {
245- stmt : false ,
246- leftmost_subexpression_in_stmt : fixup. stmt || fixup. leftmost_subexpression_in_stmt ,
247- ..fixup
248- } ,
249- ) ;
238+ self . print_expr_maybe_paren ( func, prec, fixup. leftmost_subexpression ( ) ) ;
250239
251240 self . print_call_post ( args)
252241 }
@@ -315,33 +304,17 @@ impl<'a> State<'a> {
315304 _ => left_prec,
316305 } ;
317306
318- self . print_expr_maybe_paren (
319- lhs,
320- left_prec,
321- FixupContext {
322- stmt : false ,
323- leftmost_subexpression_in_stmt : fixup. stmt || fixup. leftmost_subexpression_in_stmt ,
324- ..fixup
325- } ,
326- ) ;
307+ self . print_expr_maybe_paren ( lhs, left_prec, fixup. leftmost_subexpression ( ) ) ;
327308
328309 self . space ( ) ;
329310 self . word_space ( op. node . as_str ( ) ) ;
330311
331- self . print_expr_maybe_paren (
332- rhs,
333- right_prec,
334- FixupContext { stmt : false , leftmost_subexpression_in_stmt : false , ..fixup } ,
335- ) ;
312+ self . print_expr_maybe_paren ( rhs, right_prec, fixup. subsequent_subexpression ( ) ) ;
336313 }
337314
338315 fn print_expr_unary ( & mut self , op : ast:: UnOp , expr : & ast:: Expr , fixup : FixupContext ) {
339316 self . word ( op. as_str ( ) ) ;
340- self . print_expr_maybe_paren (
341- expr,
342- parser:: PREC_PREFIX ,
343- FixupContext { stmt : false , leftmost_subexpression_in_stmt : false , ..fixup } ,
344- ) ;
317+ self . print_expr_maybe_paren ( expr, parser:: PREC_PREFIX , fixup. subsequent_subexpression ( ) ) ;
345318 }
346319
347320 fn print_expr_addr_of (
@@ -359,11 +332,7 @@ impl<'a> State<'a> {
359332 self . print_mutability ( mutability, true ) ;
360333 }
361334 }
362- self . print_expr_maybe_paren (
363- expr,
364- parser:: PREC_PREFIX ,
365- FixupContext { stmt : false , leftmost_subexpression_in_stmt : false , ..fixup } ,
366- ) ;
335+ self . print_expr_maybe_paren ( expr, parser:: PREC_PREFIX , fixup. subsequent_subexpression ( ) ) ;
367336 }
368337
369338 pub ( super ) fn print_expr ( & mut self , expr : & ast:: Expr , fixup : FixupContext ) {
@@ -398,8 +367,7 @@ impl<'a> State<'a> {
398367 //
399368 // Same applies to a small set of other expression kinds which eagerly
400369 // terminate a statement which opens with them.
401- let needs_par =
402- fixup. leftmost_subexpression_in_stmt && !classify:: expr_requires_semi_to_be_stmt ( expr) ;
370+ let needs_par = fixup. would_cause_statement_boundary ( expr) ;
403371 if needs_par {
404372 self . popen ( ) ;
405373 fixup = FixupContext :: default ( ) ;
@@ -447,16 +415,7 @@ impl<'a> State<'a> {
447415 }
448416 ast:: ExprKind :: Cast ( expr, ty) => {
449417 let prec = AssocOp :: As . precedence ( ) as i8 ;
450- self . print_expr_maybe_paren (
451- expr,
452- prec,
453- FixupContext {
454- stmt : false ,
455- leftmost_subexpression_in_stmt : fixup. stmt
456- || fixup. leftmost_subexpression_in_stmt ,
457- ..fixup
458- } ,
459- ) ;
418+ self . print_expr_maybe_paren ( expr, prec, fixup. leftmost_subexpression ( ) ) ;
460419 self . space ( ) ;
461420 self . word_space ( "as" ) ;
462421 self . print_type ( ty) ;
@@ -588,70 +547,34 @@ impl<'a> State<'a> {
588547 self . print_block_with_attrs ( blk, attrs) ;
589548 }
590549 ast:: ExprKind :: Await ( expr, _) => {
591- // Same fixups as ExprKind::MethodCall.
592550 self . print_expr_maybe_paren ( expr, parser:: PREC_POSTFIX , fixup) ;
593551 self . word ( ".await" ) ;
594552 }
595553 ast:: ExprKind :: Assign ( lhs, rhs, _) => {
596- // Same fixups as ExprKind::Binary.
597554 let prec = AssocOp :: Assign . precedence ( ) as i8 ;
598- self . print_expr_maybe_paren (
599- lhs,
600- prec + 1 ,
601- FixupContext {
602- stmt : false ,
603- leftmost_subexpression_in_stmt : fixup. stmt
604- || fixup. leftmost_subexpression_in_stmt ,
605- ..fixup
606- } ,
607- ) ;
555+ self . print_expr_maybe_paren ( lhs, prec + 1 , fixup. leftmost_subexpression ( ) ) ;
608556 self . space ( ) ;
609557 self . word_space ( "=" ) ;
610- self . print_expr_maybe_paren (
611- rhs,
612- prec,
613- FixupContext { stmt : false , leftmost_subexpression_in_stmt : false , ..fixup } ,
614- ) ;
558+ self . print_expr_maybe_paren ( rhs, prec, fixup. subsequent_subexpression ( ) ) ;
615559 }
616560 ast:: ExprKind :: AssignOp ( op, lhs, rhs) => {
617- // Same fixups as ExprKind::Binary.
618561 let prec = AssocOp :: Assign . precedence ( ) as i8 ;
619- self . print_expr_maybe_paren (
620- lhs,
621- prec + 1 ,
622- FixupContext {
623- stmt : false ,
624- leftmost_subexpression_in_stmt : fixup. stmt
625- || fixup. leftmost_subexpression_in_stmt ,
626- ..fixup
627- } ,
628- ) ;
562+ self . print_expr_maybe_paren ( lhs, prec + 1 , fixup. leftmost_subexpression ( ) ) ;
629563 self . space ( ) ;
630564 self . word ( op. node . as_str ( ) ) ;
631565 self . word_space ( "=" ) ;
632- self . print_expr_maybe_paren (
633- rhs,
634- prec,
635- FixupContext { stmt : false , leftmost_subexpression_in_stmt : false , ..fixup } ,
636- ) ;
566+ self . print_expr_maybe_paren ( rhs, prec, fixup. subsequent_subexpression ( ) ) ;
637567 }
638568 ast:: ExprKind :: Field ( expr, ident) => {
639- // Same fixups as ExprKind::MethodCall.
640569 self . print_expr_maybe_paren ( expr, parser:: PREC_POSTFIX , fixup) ;
641570 self . word ( "." ) ;
642571 self . print_ident ( * ident) ;
643572 }
644573 ast:: ExprKind :: Index ( expr, index, _) => {
645- // Same fixups as ExprKind::Call.
646574 self . print_expr_maybe_paren (
647575 expr,
648576 parser:: PREC_POSTFIX ,
649- FixupContext {
650- stmt : false ,
651- leftmost_subexpression_in_stmt : fixup. stmt
652- || fixup. leftmost_subexpression_in_stmt ,
653- ..fixup
654- } ,
577+ fixup. leftmost_subexpression ( ) ,
655578 ) ;
656579 self . word ( "[" ) ;
657580 self . print_expr ( index, FixupContext :: default ( ) ) ;
@@ -664,31 +587,14 @@ impl<'a> State<'a> {
664587 // a "normal" binop gets parenthesized. (`LOr` is the lowest-precedence binop.)
665588 let fake_prec = AssocOp :: LOr . precedence ( ) as i8 ;
666589 if let Some ( e) = start {
667- self . print_expr_maybe_paren (
668- e,
669- fake_prec,
670- FixupContext {
671- stmt : false ,
672- leftmost_subexpression_in_stmt : fixup. stmt
673- || fixup. leftmost_subexpression_in_stmt ,
674- ..fixup
675- } ,
676- ) ;
590+ self . print_expr_maybe_paren ( e, fake_prec, fixup. leftmost_subexpression ( ) ) ;
677591 }
678592 match limits {
679593 ast:: RangeLimits :: HalfOpen => self . word ( ".." ) ,
680594 ast:: RangeLimits :: Closed => self . word ( "..=" ) ,
681595 }
682596 if let Some ( e) = end {
683- self . print_expr_maybe_paren (
684- e,
685- fake_prec,
686- FixupContext {
687- stmt : false ,
688- leftmost_subexpression_in_stmt : false ,
689- ..fixup
690- } ,
691- ) ;
597+ self . print_expr_maybe_paren ( e, fake_prec, fixup. subsequent_subexpression ( ) ) ;
692598 }
693599 }
694600 ast:: ExprKind :: Underscore => self . word ( "_" ) ,
@@ -705,11 +611,7 @@ impl<'a> State<'a> {
705611 self . print_expr_maybe_paren (
706612 expr,
707613 parser:: PREC_JUMP ,
708- FixupContext {
709- stmt : false ,
710- leftmost_subexpression_in_stmt : false ,
711- ..fixup
712- } ,
614+ fixup. subsequent_subexpression ( ) ,
713615 ) ;
714616 }
715617 }
@@ -727,11 +629,7 @@ impl<'a> State<'a> {
727629 self . print_expr_maybe_paren (
728630 expr,
729631 parser:: PREC_JUMP ,
730- FixupContext {
731- stmt : false ,
732- leftmost_subexpression_in_stmt : false ,
733- ..fixup
734- } ,
632+ fixup. subsequent_subexpression ( ) ,
735633 ) ;
736634 }
737635 }
@@ -744,11 +642,7 @@ impl<'a> State<'a> {
744642 self . print_expr_maybe_paren (
745643 expr,
746644 parser:: PREC_JUMP ,
747- FixupContext {
748- stmt : false ,
749- leftmost_subexpression_in_stmt : false ,
750- ..fixup
751- } ,
645+ fixup. subsequent_subexpression ( ) ,
752646 ) ;
753647 }
754648 }
@@ -758,7 +652,7 @@ impl<'a> State<'a> {
758652 self . print_expr_maybe_paren (
759653 result,
760654 parser:: PREC_JUMP ,
761- FixupContext { stmt : false , leftmost_subexpression_in_stmt : false , .. fixup } ,
655+ fixup. subsequent_subexpression ( ) ,
762656 ) ;
763657 }
764658 ast:: ExprKind :: InlineAsm ( a) => {
@@ -812,16 +706,11 @@ impl<'a> State<'a> {
812706 self . print_expr_maybe_paren (
813707 expr,
814708 parser:: PREC_JUMP ,
815- FixupContext {
816- stmt : false ,
817- leftmost_subexpression_in_stmt : false ,
818- ..fixup
819- } ,
709+ fixup. subsequent_subexpression ( ) ,
820710 ) ;
821711 }
822712 }
823713 ast:: ExprKind :: Try ( e) => {
824- // Same fixups as ExprKind::MethodCall.
825714 self . print_expr_maybe_paren ( e, parser:: PREC_POSTFIX , fixup) ;
826715 self . word ( "?" )
827716 }
@@ -889,7 +778,7 @@ impl<'a> State<'a> {
889778 }
890779 _ => {
891780 self . end ( ) ; // Close the ibox for the pattern.
892- self . print_expr ( body, FixupContext { stmt : true , .. FixupContext :: default ( ) } ) ;
781+ self . print_expr ( body, FixupContext :: new_stmt ( ) ) ;
893782 self . word ( "," ) ;
894783 }
895784 }
0 commit comments