@@ -11,7 +11,7 @@ use rustc_data_structures::stack::ensure_sufficient_stack;
11
11
use rustc_data_structures:: unord:: UnordMap ;
12
12
use rustc_errors:: codes:: * ;
13
13
use rustc_errors:: {
14
- Applicability , Diag , ErrorGuaranteed , MultiSpan , StashKey , Subdiagnostic , pluralize,
14
+ Applicability , Diag , ErrorGuaranteed , MultiSpan , StashKey , Subdiagnostic , listify , pluralize,
15
15
struct_span_code_err,
16
16
} ;
17
17
use rustc_hir:: def:: { CtorKind , DefKind , Res } ;
@@ -2190,13 +2190,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2190
2190
}
2191
2191
if !missing_mandatory_fields. is_empty ( ) {
2192
2192
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 ( ) ;
2200
2194
self . dcx ( )
2201
2195
. struct_span_err (
2202
2196
span. shrink_to_hi ( ) ,
@@ -2553,25 +2547,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2553
2547
. partition ( |field| field. 2 ) ;
2554
2548
err. span_labels ( used_private_fields. iter ( ) . map ( |( _, span, _) | * span) , "private field" ) ;
2555
2549
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
+ )
2569
2558
} ;
2570
2559
err. note ( format ! (
2571
2560
"{}private field{s} {names}that {were} not provided" ,
2572
2561
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 ( ) ) ,
2575
2564
) ) ;
2576
2565
}
2577
2566
0 commit comments