@@ -8,8 +8,8 @@ use rustc_ast::{ast, ptr};
88use rustc_span:: Span ;
99
1010use crate :: closures;
11- use crate :: config:: lists:: * ;
1211use crate :: config:: Version ;
12+ use crate :: config:: { lists:: * , Density } ;
1313use crate :: expr:: {
1414 can_be_overflowed_expr, is_every_expr_simple, is_method_call, is_nested_call, is_simple_expr,
1515 rewrite_cond,
@@ -252,6 +252,7 @@ pub(crate) fn rewrite_with_parens<'a, T: 'a + IntoOverflowableItem<'a>>(
252252 span : Span ,
253253 item_max_width : usize ,
254254 force_separator_tactic : Option < SeparatorTactic > ,
255+ force_list_tactic : Option < Density > ,
255256) -> Option < String > {
256257 Context :: new (
257258 context,
@@ -263,6 +264,7 @@ pub(crate) fn rewrite_with_parens<'a, T: 'a + IntoOverflowableItem<'a>>(
263264 ")" ,
264265 item_max_width,
265266 force_separator_tactic,
267+ force_list_tactic,
266268 None ,
267269 )
268270 . rewrite ( shape)
@@ -286,6 +288,7 @@ pub(crate) fn rewrite_with_angle_brackets<'a, T: 'a + IntoOverflowableItem<'a>>(
286288 context. config . max_width ( ) ,
287289 None ,
288290 None ,
291+ None ,
289292 )
290293 . rewrite ( shape)
291294}
@@ -314,6 +317,7 @@ pub(crate) fn rewrite_with_square_brackets<'a, T: 'a + IntoOverflowableItem<'a>>
314317 rhs,
315318 context. config . array_width ( ) ,
316319 force_separator_tactic,
320+ None ,
317321 Some ( ( "[" , "]" ) ) ,
318322 )
319323 . rewrite ( shape)
@@ -331,6 +335,7 @@ struct Context<'a> {
331335 item_max_width : usize ,
332336 one_line_width : usize ,
333337 force_separator_tactic : Option < SeparatorTactic > ,
338+ force_list_tactic : Option < Density > ,
334339 custom_delims : Option < ( & ' a str , & ' a str ) > ,
335340}
336341
@@ -345,6 +350,7 @@ impl<'a> Context<'a> {
345350 suffix : & ' static str ,
346351 item_max_width : usize ,
347352 force_separator_tactic : Option < SeparatorTactic > ,
353+ force_list_tactic : Option < Density > ,
348354 custom_delims : Option < ( & ' a str , & ' a str ) > ,
349355 ) -> Context < ' a > {
350356 let used_width = extra_offset ( ident, shape) ;
@@ -369,6 +375,7 @@ impl<'a> Context<'a> {
369375 item_max_width,
370376 one_line_width,
371377 force_separator_tactic,
378+ force_list_tactic,
372379 custom_delims,
373380 }
374381 }
@@ -584,6 +591,34 @@ impl<'a> Context<'a> {
584591 _ => ( ) ,
585592 }
586593
594+ // we only care if the any element but the last has a sigle line comment
595+ let any_but_last_contains_line_comment = list_items
596+ . iter ( )
597+ . rev ( )
598+ . skip ( 1 )
599+ . any ( |item| item. has_single_line_comment ( ) ) ;
600+
601+ match self . force_list_tactic {
602+ Some ( Density :: Tall )
603+ if tactic == DefinitiveListTactic :: Mixed && any_but_last_contains_line_comment =>
604+ {
605+ // If we determined a `Mixed` layout, but we configured tall then force
606+ // the tactic to be vertical only if any of the items contain single line comments.
607+ // Otherwise, the tacitc was properly set above.
608+ tactic = DefinitiveListTactic :: Vertical
609+ }
610+ Some ( Density :: Compressed ) if tactic != DefinitiveListTactic :: Horizontal => {
611+ // Only force a mixed layout if we haven't already decided on going horizontal
612+ tactic = DefinitiveListTactic :: Mixed
613+ }
614+ // If we need to force a `Vertical` layout, we should only do so if there are
615+ // at least 2 items for us to format. Otherwise, use the tactic already determined.
616+ Some ( Density :: Vertical ) if self . items . len ( ) > 1 => {
617+ tactic = DefinitiveListTactic :: Vertical ;
618+ }
619+ _ => { }
620+ } ;
621+
587622 tactic
588623 }
589624
0 commit comments