@@ -102,6 +102,7 @@ pub struct ItemCtxt<'tcx> {
102
102
103
103
///////////////////////////////////////////////////////////////////////////
104
104
105
+ #[ derive( Default ) ]
105
106
crate struct PlaceholderHirTyCollector ( crate Vec < Span > ) ;
106
107
107
108
impl < ' v > Visitor < ' v > for PlaceholderHirTyCollector {
@@ -116,16 +117,13 @@ impl<'v> Visitor<'v> for PlaceholderHirTyCollector {
116
117
}
117
118
}
118
119
119
- impl PlaceholderHirTyCollector {
120
- pub fn new ( ) -> PlaceholderHirTyCollector {
121
- PlaceholderHirTyCollector ( vec ! [ ] )
122
- }
123
- }
124
-
125
120
struct CollectItemTypesVisitor < ' tcx > {
126
121
tcx : TyCtxt < ' tcx > ,
127
122
}
128
123
124
+ /// If there are any placeholder types (`_`), emit an error explaining that this is not allowed
125
+ /// and suggest adding type parameters in the appropriate place, taking into consideration any and
126
+ /// all already existing generic type parameters to avoid suggesting a name that is already in use.
129
127
crate fn placeholder_type_error (
130
128
tcx : TyCtxt < ' tcx > ,
131
129
ident_span : Span ,
@@ -136,6 +134,7 @@ crate fn placeholder_type_error(
136
134
if placeholder_types. is_empty ( ) {
137
135
return ;
138
136
}
137
+ // This is the whitelist of possible parameter names that we might suggest.
139
138
let possible_names = [ "T" , "K" , "L" , "A" , "B" , "C" ] ;
140
139
let used_names = generics
141
140
. iter ( )
@@ -181,7 +180,7 @@ fn reject_placeholder_type_signatures_in_item(tcx: TyCtxt<'tcx>, item: &'tcx hir
181
180
_ => return ,
182
181
} ;
183
182
184
- let mut visitor = PlaceholderHirTyCollector :: new ( ) ;
183
+ let mut visitor = PlaceholderHirTyCollector :: default ( ) ;
185
184
visitor. visit_item ( item) ;
186
185
187
186
placeholder_type_error ( tcx, item. ident . span , generics, visitor. 0 , suggest) ;
@@ -1796,15 +1795,7 @@ fn is_suggestable_infer_ty(ty: &hir::Ty<'_>) -> bool {
1796
1795
match & ty. kind {
1797
1796
hir:: TyKind :: Infer => true ,
1798
1797
hir:: TyKind :: Slice ( ty) | hir:: TyKind :: Array ( ty, _) => is_suggestable_infer_ty ( ty) ,
1799
- hir:: TyKind :: Tup ( tys)
1800
- if !tys. is_empty ( )
1801
- && tys. iter ( ) . any ( |ty| match ty. kind {
1802
- hir:: TyKind :: Infer => true ,
1803
- _ => false ,
1804
- } ) =>
1805
- {
1806
- true
1807
- }
1798
+ hir:: TyKind :: Tup ( tys) => tys. iter ( ) . any ( |ty| is_suggestable_infer_ty ( ty) ) ,
1808
1799
_ => false ,
1809
1800
}
1810
1801
}
@@ -1838,7 +1829,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
1838
1829
match get_infer_ret_ty ( & sig. decl . output ) {
1839
1830
Some ( ty) => {
1840
1831
let fn_sig = tcx. typeck_tables_of ( def_id) . liberated_fn_sigs ( ) [ hir_id] ;
1841
- let mut visitor = PlaceholderHirTyCollector :: new ( ) ;
1832
+ let mut visitor = PlaceholderHirTyCollector :: default ( ) ;
1842
1833
visitor. visit_ty ( ty) ;
1843
1834
let mut diag = bad_placeholder_type ( tcx, visitor. 0 ) ;
1844
1835
let ret_ty = fn_sig. output ( ) ;
0 commit comments