@@ -3287,17 +3287,35 @@ pub(crate) mod printing {
32873287 }
32883288
32893289 #[ cfg( feature = "full" ) ]
3290- fn print_expr_assign ( e : & ExprAssign , tokens : & mut TokenStream , fixup : FixupContext ) {
3290+ fn print_expr_assign ( e : & ExprAssign , tokens : & mut TokenStream , mut fixup : FixupContext ) {
32913291 outer_attrs_to_tokens ( & e. attrs , tokens) ;
3292- let ( left_prec, left_fixup) =
3293- fixup. leftmost_subexpression_with_operator ( & e. left , false , false , Precedence :: Assign ) ;
3294- print_subexpression ( & e. left , left_prec <= Precedence :: Range , tokens, left_fixup) ;
3295- e. eq_token . to_tokens ( tokens) ;
3296- print_expr (
3297- & e. right ,
3298- tokens,
3299- fixup. rightmost_subexpression_fixup ( false , false , Precedence :: Assign ) ,
3300- ) ;
3292+
3293+ let needs_group = !e. attrs . is_empty ( ) ;
3294+ if needs_group {
3295+ fixup = FixupContext :: NONE ;
3296+ }
3297+
3298+ let do_print_expr = |tokens : & mut TokenStream | {
3299+ let ( left_prec, left_fixup) = fixup. leftmost_subexpression_with_operator (
3300+ & e. left ,
3301+ false ,
3302+ false ,
3303+ Precedence :: Assign ,
3304+ ) ;
3305+ print_subexpression ( & e. left , left_prec <= Precedence :: Range , tokens, left_fixup) ;
3306+ e. eq_token . to_tokens ( tokens) ;
3307+ print_expr (
3308+ & e. right ,
3309+ tokens,
3310+ fixup. rightmost_subexpression_fixup ( false , false , Precedence :: Assign ) ,
3311+ ) ;
3312+ } ;
3313+
3314+ if needs_group {
3315+ token:: Paren :: default ( ) . surround ( tokens, do_print_expr) ;
3316+ } else {
3317+ do_print_expr ( tokens) ;
3318+ }
33013319 }
33023320
33033321 #[ cfg( feature = "full" ) ]
@@ -3340,51 +3358,64 @@ pub(crate) mod printing {
33403358 }
33413359 }
33423360
3343- fn print_expr_binary ( e : & ExprBinary , tokens : & mut TokenStream , fixup : FixupContext ) {
3361+ fn print_expr_binary ( e : & ExprBinary , tokens : & mut TokenStream , mut fixup : FixupContext ) {
33443362 outer_attrs_to_tokens ( & e. attrs , tokens) ;
33453363
3346- let binop_prec = Precedence :: of_binop ( & e. op ) ;
3347- let ( left_prec, left_fixup) = fixup. leftmost_subexpression_with_operator (
3348- & e. left ,
3349- #[ cfg( feature = "full" ) ]
3350- match & e. op {
3351- BinOp :: Sub ( _)
3352- | BinOp :: Mul ( _)
3353- | BinOp :: And ( _)
3354- | BinOp :: Or ( _)
3355- | BinOp :: BitAnd ( _)
3356- | BinOp :: BitOr ( _)
3357- | BinOp :: Shl ( _)
3358- | BinOp :: Lt ( _) => true ,
3359- _ => false ,
3360- } ,
3361- match & e. op {
3362- BinOp :: Shl ( _) | BinOp :: Lt ( _) => true ,
3363- _ => false ,
3364- } ,
3365- #[ cfg( feature = "full" ) ]
3366- binop_prec,
3367- ) ;
3368- let left_needs_group = match binop_prec {
3369- Precedence :: Assign => left_prec <= Precedence :: Range ,
3370- Precedence :: Compare => left_prec <= binop_prec,
3371- _ => left_prec < binop_prec,
3372- } ;
3364+ let needs_group = !e. attrs . is_empty ( ) ;
3365+ if needs_group {
3366+ fixup = FixupContext :: NONE ;
3367+ }
33733368
3374- let right_fixup = fixup. rightmost_subexpression_fixup (
3375- #[ cfg( feature = "full" ) ]
3376- false ,
3377- #[ cfg( feature = "full" ) ]
3378- false ,
3379- #[ cfg( feature = "full" ) ]
3380- binop_prec,
3381- ) ;
3382- let right_needs_group = binop_prec != Precedence :: Assign
3383- && right_fixup. rightmost_subexpression_precedence ( & e. right ) <= binop_prec;
3369+ let do_print_expr = |tokens : & mut TokenStream | {
3370+ let binop_prec = Precedence :: of_binop ( & e. op ) ;
3371+ let ( left_prec, left_fixup) = fixup. leftmost_subexpression_with_operator (
3372+ & e. left ,
3373+ #[ cfg( feature = "full" ) ]
3374+ match & e. op {
3375+ BinOp :: Sub ( _)
3376+ | BinOp :: Mul ( _)
3377+ | BinOp :: And ( _)
3378+ | BinOp :: Or ( _)
3379+ | BinOp :: BitAnd ( _)
3380+ | BinOp :: BitOr ( _)
3381+ | BinOp :: Shl ( _)
3382+ | BinOp :: Lt ( _) => true ,
3383+ _ => false ,
3384+ } ,
3385+ match & e. op {
3386+ BinOp :: Shl ( _) | BinOp :: Lt ( _) => true ,
3387+ _ => false ,
3388+ } ,
3389+ #[ cfg( feature = "full" ) ]
3390+ binop_prec,
3391+ ) ;
3392+ let left_needs_group = match binop_prec {
3393+ Precedence :: Assign => left_prec <= Precedence :: Range ,
3394+ Precedence :: Compare => left_prec <= binop_prec,
3395+ _ => left_prec < binop_prec,
3396+ } ;
33843397
3385- print_subexpression ( & e. left , left_needs_group, tokens, left_fixup) ;
3386- e. op . to_tokens ( tokens) ;
3387- print_subexpression ( & e. right , right_needs_group, tokens, right_fixup) ;
3398+ let right_fixup = fixup. rightmost_subexpression_fixup (
3399+ #[ cfg( feature = "full" ) ]
3400+ false ,
3401+ #[ cfg( feature = "full" ) ]
3402+ false ,
3403+ #[ cfg( feature = "full" ) ]
3404+ binop_prec,
3405+ ) ;
3406+ let right_needs_group = binop_prec != Precedence :: Assign
3407+ && right_fixup. rightmost_subexpression_precedence ( & e. right ) <= binop_prec;
3408+
3409+ print_subexpression ( & e. left , left_needs_group, tokens, left_fixup) ;
3410+ e. op . to_tokens ( tokens) ;
3411+ print_subexpression ( & e. right , right_needs_group, tokens, right_fixup) ;
3412+ } ;
3413+
3414+ if needs_group {
3415+ token:: Paren :: default ( ) . surround ( tokens, do_print_expr) ;
3416+ } else {
3417+ do_print_expr ( tokens) ;
3418+ }
33883419 }
33893420
33903421 #[ cfg( feature = "full" ) ]
@@ -3462,19 +3493,33 @@ pub(crate) mod printing {
34623493 }
34633494 }
34643495
3465- fn print_expr_cast ( e : & ExprCast , tokens : & mut TokenStream , fixup : FixupContext ) {
3496+ fn print_expr_cast ( e : & ExprCast , tokens : & mut TokenStream , mut fixup : FixupContext ) {
34663497 outer_attrs_to_tokens ( & e. attrs , tokens) ;
3467- let ( left_prec, left_fixup) = fixup. leftmost_subexpression_with_operator (
3468- & e. expr ,
3469- #[ cfg( feature = "full" ) ]
3470- false ,
3471- false ,
3472- #[ cfg( feature = "full" ) ]
3473- Precedence :: Cast ,
3474- ) ;
3475- print_subexpression ( & e. expr , left_prec < Precedence :: Cast , tokens, left_fixup) ;
3476- e. as_token . to_tokens ( tokens) ;
3477- e. ty . to_tokens ( tokens) ;
3498+
3499+ let needs_group = !e. attrs . is_empty ( ) ;
3500+ if needs_group {
3501+ fixup = FixupContext :: NONE ;
3502+ }
3503+
3504+ let do_print_expr = |tokens : & mut TokenStream | {
3505+ let ( left_prec, left_fixup) = fixup. leftmost_subexpression_with_operator (
3506+ & e. expr ,
3507+ #[ cfg( feature = "full" ) ]
3508+ false ,
3509+ false ,
3510+ #[ cfg( feature = "full" ) ]
3511+ Precedence :: Cast ,
3512+ ) ;
3513+ print_subexpression ( & e. expr , left_prec < Precedence :: Cast , tokens, left_fixup) ;
3514+ e. as_token . to_tokens ( tokens) ;
3515+ e. ty . to_tokens ( tokens) ;
3516+ } ;
3517+
3518+ if needs_group {
3519+ token:: Paren :: default ( ) . surround ( tokens, do_print_expr) ;
3520+ } else {
3521+ do_print_expr ( tokens) ;
3522+ }
34783523 }
34793524
34803525 #[ cfg( feature = "full" ) ]
0 commit comments