@@ -941,6 +941,28 @@ fn join_bounds_inner(
941
941
ast:: GenericBound :: Trait ( ..) => last_line_extendable ( s) ,
942
942
} ;
943
943
944
+ // Whether a GenericBound item is a PathSegment segment that includes internal array
945
+ // that contains more than one item
946
+ let is_item_with_multi_items_array = |item : & ast:: GenericBound | match item {
947
+ ast:: GenericBound :: Trait ( ref poly_trait_ref, ..) => {
948
+ let segments = & poly_trait_ref. trait_ref . path . segments ;
949
+ if segments. len ( ) > 1 {
950
+ true
951
+ } else {
952
+ if let Some ( args_in) = & segments[ 0 ] . args {
953
+ matches ! (
954
+ args_in. deref( ) ,
955
+ ast:: GenericArgs :: AngleBracketed ( bracket_args)
956
+ if bracket_args. args. len( ) > 1
957
+ )
958
+ } else {
959
+ false
960
+ }
961
+ }
962
+ }
963
+ _ => false ,
964
+ } ;
965
+
944
966
let result = items. iter ( ) . enumerate ( ) . try_fold (
945
967
( String :: new ( ) , None , false ) ,
946
968
|( strs, prev_trailing_span, prev_extendable) , ( i, item) | {
@@ -1035,10 +1057,23 @@ fn join_bounds_inner(
1035
1057
} ,
1036
1058
) ?;
1037
1059
1038
- if !force_newline
1039
- && items. len ( ) > 1
1040
- && ( result. 0 . contains ( '\n' ) || result. 0 . len ( ) > shape. width )
1041
- {
1060
+ // Whether retry the function with forced newline is needed:
1061
+ // Only if result is not already multiline and did not exceed line width,
1062
+ // and either there is more than one item;
1063
+ // or the single item is of type `Trait`,
1064
+ // and any of the internal arrays contains more than one item;
1065
+ let retry_with_force_newline =
1066
+ if force_newline || ( !result. 0 . contains ( '\n' ) && result. 0 . len ( ) <= shape. width ) {
1067
+ false
1068
+ } else {
1069
+ if items. len ( ) > 1 {
1070
+ true
1071
+ } else {
1072
+ is_item_with_multi_items_array ( & items[ 0 ] )
1073
+ }
1074
+ } ;
1075
+
1076
+ if retry_with_force_newline {
1042
1077
join_bounds_inner ( context, shape, items, need_indent, true )
1043
1078
} else {
1044
1079
Some ( result. 0 )
0 commit comments