@@ -941,6 +941,28 @@ fn join_bounds_inner(
941941 ast:: GenericBound :: Trait ( ..) => last_line_extendable ( s) ,
942942 } ;
943943
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+
944966 let result = items. iter ( ) . enumerate ( ) . try_fold (
945967 ( String :: new ( ) , None , false ) ,
946968 |( strs, prev_trailing_span, prev_extendable) , ( i, item) | {
@@ -1035,10 +1057,23 @@ fn join_bounds_inner(
10351057 } ,
10361058 ) ?;
10371059
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 {
10421077 join_bounds_inner ( context, shape, items, need_indent, true )
10431078 } else {
10441079 Some ( result. 0 )
0 commit comments