@@ -11,7 +11,7 @@ use rustc_data_structures::stack::ensure_sufficient_stack;
1111use rustc_data_structures:: unord:: UnordMap ;
1212use rustc_errors:: codes:: * ;
1313use rustc_errors:: {
14- Applicability , Diag , ErrorGuaranteed , MultiSpan , StashKey , Subdiagnostic , pluralize,
14+ Applicability , Diag , ErrorGuaranteed , MultiSpan , StashKey , Subdiagnostic , listify , pluralize,
1515 struct_span_code_err,
1616} ;
1717use rustc_hir:: def:: { CtorKind , DefKind , Res } ;
@@ -2190,13 +2190,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21902190 }
21912191 if !missing_mandatory_fields. is_empty ( ) {
21922192 let s = pluralize ! ( missing_mandatory_fields. len( ) ) ;
2193- let fields: Vec < _ > =
2194- missing_mandatory_fields. iter ( ) . map ( |f| format ! ( "`{f}`" ) ) . collect ( ) ;
2195- let fields = match & fields[ ..] {
2196- [ ] => unreachable ! ( ) ,
2197- [ only] => only. to_string ( ) ,
2198- [ start @ .., last] => format ! ( "{} and {last}" , start. join( ", " ) ) ,
2199- } ;
2193+ let fields = listify ( & missing_mandatory_fields, |f| format ! ( "`{f}`" ) ) . unwrap ( ) ;
22002194 self . dcx ( )
22012195 . struct_span_err (
22022196 span. shrink_to_hi ( ) ,
@@ -2553,25 +2547,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
25532547 . partition ( |field| field. 2 ) ;
25542548 err. span_labels ( used_private_fields. iter ( ) . map ( |( _, span, _) | * span) , "private field" ) ;
25552549 if !remaining_private_fields. is_empty ( ) {
2556- let remaining_private_fields_len = remaining_private_fields. len ( ) ;
2557- let names = match & remaining_private_fields
2558- . iter ( )
2559- . map ( |( name, _, _) | name)
2560- . collect :: < Vec < _ > > ( ) [ ..]
2561- {
2562- _ if remaining_private_fields_len > 6 => String :: new ( ) ,
2563- [ name] => format ! ( "`{name}` " ) ,
2564- [ names @ .., last] => {
2565- let names = names. iter ( ) . map ( |name| format ! ( "`{name}`" ) ) . collect :: < Vec < _ > > ( ) ;
2566- format ! ( "{} and `{last}` " , names. join( ", " ) )
2567- }
2568- [ ] => bug ! ( "expected at least one private field to report" ) ,
2550+ let names = if remaining_private_fields. len ( ) > 6 {
2551+ String :: new ( )
2552+ } else {
2553+ format ! (
2554+ "{} " ,
2555+ listify( & remaining_private_fields, |( name, _, _) | format!( "`{name}`" ) )
2556+ . expect( "expected at least one private field to report" )
2557+ )
25692558 } ;
25702559 err. note ( format ! (
25712560 "{}private field{s} {names}that {were} not provided" ,
25722561 if used_fields. is_empty( ) { "" } else { "...and other " } ,
2573- s = pluralize!( remaining_private_fields_len ) ,
2574- were = pluralize!( "was" , remaining_private_fields_len ) ,
2562+ s = pluralize!( remaining_private_fields . len ( ) ) ,
2563+ were = pluralize!( "was" , remaining_private_fields . len ( ) ) ,
25752564 ) ) ;
25762565 }
25772566
0 commit comments