@@ -473,7 +473,7 @@ fn rewrite_bounded_lifetime(
473
473
"{}{}{}" ,
474
474
result,
475
475
colon,
476
- join_bounds( context, shape. sub_width( overhead) ?, bounds, true ) ?
476
+ join_bounds( context, shape. sub_width( overhead) ?, bounds, true , false ) ?
477
477
) ;
478
478
Some ( result)
479
479
}
@@ -515,13 +515,7 @@ impl Rewrite for ast::GenericBounds {
515
515
} else {
516
516
shape
517
517
} ;
518
- join_bounds ( context, bounds_shape, self , true ) . map ( |s| {
519
- if has_paren {
520
- format ! ( "({})" , s)
521
- } else {
522
- s
523
- }
524
- } )
518
+ join_bounds ( context, bounds_shape, self , true , has_paren)
525
519
}
526
520
}
527
521
@@ -757,17 +751,47 @@ fn join_bounds(
757
751
shape : Shape ,
758
752
items : & [ ast:: GenericBound ] ,
759
753
need_indent : bool ,
754
+ has_paren : bool ,
760
755
) -> Option < String > {
756
+ debug_assert ! ( !items. is_empty( ) ) ;
757
+
761
758
// Try to join types in a single line
762
759
let joiner = match context. config . type_punctuation_density ( ) {
763
760
TypeDensity :: Compressed => "+" ,
764
761
TypeDensity :: Wide => " + " ,
765
762
} ;
766
763
let type_strs = items
767
764
. iter ( )
768
- . map ( |item| item. rewrite ( context, shape) )
765
+ . map ( |item| {
766
+ item. rewrite (
767
+ context,
768
+ if has_paren {
769
+ shape. sub_width ( 1 ) ?. offset_left ( 1 ) ?
770
+ } else {
771
+ shape
772
+ } ,
773
+ )
774
+ } )
769
775
. collect :: < Option < Vec < _ > > > ( ) ?;
770
- let result = type_strs. join ( joiner) ;
776
+ let mut result = String :: with_capacity ( 128 ) ;
777
+ let mut closing_paren = has_paren;
778
+ if has_paren {
779
+ result. push ( '(' ) ;
780
+ }
781
+ result. push_str ( & type_strs[ 0 ] ) ;
782
+ if has_paren && type_strs. len ( ) == 1 {
783
+ result. push ( ')' ) ;
784
+ }
785
+ for ( i, type_str) in type_strs[ 1 ..] . iter ( ) . enumerate ( ) {
786
+ if closing_paren {
787
+ if let ast:: GenericBound :: Outlives ( ..) = items[ i + 1 ] {
788
+ result. push ( ')' ) ;
789
+ closing_paren = false ;
790
+ }
791
+ }
792
+ result. push_str ( joiner) ;
793
+ result. push_str ( type_str) ;
794
+ }
771
795
if items. len ( ) <= 1 || ( !result. contains ( '\n' ) && result. len ( ) <= shape. width ) {
772
796
return Some ( result) ;
773
797
}
@@ -790,10 +814,20 @@ fn join_bounds(
790
814
ast:: GenericBound :: Trait ( ..) => last_line_extendable ( s) ,
791
815
} ;
792
816
let mut result = String :: with_capacity ( 128 ) ;
817
+ let mut closing_paren = has_paren;
818
+ if has_paren {
819
+ result. push ( '(' ) ;
820
+ }
793
821
result. push_str ( & type_strs[ 0 ] ) ;
794
822
let mut can_be_put_on_the_same_line = is_bound_extendable ( & result, & items[ 0 ] ) ;
795
823
let generic_bounds_in_order = is_generic_bounds_in_order ( items) ;
796
824
for ( bound, bound_str) in items[ 1 ..] . iter ( ) . zip ( type_strs[ 1 ..] . iter ( ) ) {
825
+ if closing_paren {
826
+ if let ast:: GenericBound :: Outlives ( ..) = bound {
827
+ closing_paren = false ;
828
+ result. push ( ')' ) ;
829
+ }
830
+ }
797
831
if generic_bounds_in_order && can_be_put_on_the_same_line {
798
832
result. push_str ( joiner) ;
799
833
} else {
0 commit comments