@@ -145,6 +145,7 @@ crate fn placeholder_type_error(
145
145
placeholder_types : Vec < Span > ,
146
146
suggest : bool ,
147
147
hir_ty : Option < & hir:: Ty < ' _ > > ,
148
+ kind : & ' static str ,
148
149
) {
149
150
if placeholder_types. is_empty ( ) {
150
151
return ;
@@ -174,7 +175,7 @@ crate fn placeholder_type_error(
174
175
) ) ;
175
176
}
176
177
177
- let mut err = bad_placeholder_type ( tcx, placeholder_types) ;
178
+ let mut err = bad_placeholder_type ( tcx, placeholder_types, kind ) ;
178
179
179
180
// Suggest, but only if it is not a function in const or static
180
181
if suggest {
@@ -236,7 +237,15 @@ fn reject_placeholder_type_signatures_in_item(tcx: TyCtxt<'tcx>, item: &'tcx hir
236
237
let mut visitor = PlaceholderHirTyCollector :: default ( ) ;
237
238
visitor. visit_item ( item) ;
238
239
239
- placeholder_type_error ( tcx, Some ( generics. span ) , generics. params , visitor. 0 , suggest, None ) ;
240
+ placeholder_type_error (
241
+ tcx,
242
+ Some ( generics. span ) ,
243
+ generics. params ,
244
+ visitor. 0 ,
245
+ suggest,
246
+ None ,
247
+ item. kind . descr ( ) ,
248
+ ) ;
240
249
}
241
250
242
251
impl Visitor < ' tcx > for CollectItemTypesVisitor < ' tcx > {
@@ -302,13 +311,17 @@ impl Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
302
311
fn bad_placeholder_type (
303
312
tcx : TyCtxt < ' tcx > ,
304
313
mut spans : Vec < Span > ,
314
+ kind : & ' static str ,
305
315
) -> rustc_errors:: DiagnosticBuilder < ' tcx > {
316
+ let kind = if kind. ends_with ( 's' ) { format ! ( "{}es" , kind) } else { format ! ( "{}s" , kind) } ;
317
+
306
318
spans. sort ( ) ;
307
319
let mut err = struct_span_err ! (
308
320
tcx. sess,
309
321
spans. clone( ) ,
310
322
E0121 ,
311
- "the type placeholder `_` is not allowed within types on item signatures" ,
323
+ "the type placeholder `_` is not allowed within types on item signatures for {}" ,
324
+ kind
312
325
) ;
313
326
for span in spans {
314
327
err. span_label ( span, "not allowed in type signatures" ) ;
@@ -382,7 +395,7 @@ impl AstConv<'tcx> for ItemCtxt<'tcx> {
382
395
_: Option < & ty:: GenericParamDef > ,
383
396
span : Span ,
384
397
) -> & ' tcx Const < ' tcx > {
385
- bad_placeholder_type ( self . tcx ( ) , vec ! [ span] ) . emit ( ) ;
398
+ bad_placeholder_type ( self . tcx ( ) , vec ! [ span] , "generic" ) . emit ( ) ;
386
399
// Typeck doesn't expect erased regions to be returned from `type_of`.
387
400
let ty = self . tcx . fold_regions ( ty, & mut false , |r, _| match r {
388
401
ty:: ReErased => self . tcx . lifetimes . re_static ,
@@ -746,7 +759,15 @@ fn convert_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
746
759
hir:: ForeignItemKind :: Static ( ..) => {
747
760
let mut visitor = PlaceholderHirTyCollector :: default ( ) ;
748
761
visitor. visit_foreign_item ( item) ;
749
- placeholder_type_error ( tcx, None , & [ ] , visitor. 0 , false , None ) ;
762
+ placeholder_type_error (
763
+ tcx,
764
+ None ,
765
+ & [ ] ,
766
+ visitor. 0 ,
767
+ false ,
768
+ None ,
769
+ "static variable" ,
770
+ ) ;
750
771
}
751
772
_ => ( ) ,
752
773
}
@@ -818,7 +839,15 @@ fn convert_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
818
839
if let hir:: TyKind :: TraitObject ( ..) = ty. kind {
819
840
let mut visitor = PlaceholderHirTyCollector :: default ( ) ;
820
841
visitor. visit_item ( it) ;
821
- placeholder_type_error ( tcx, None , & [ ] , visitor. 0 , false , None ) ;
842
+ placeholder_type_error (
843
+ tcx,
844
+ None ,
845
+ & [ ] ,
846
+ visitor. 0 ,
847
+ false ,
848
+ None ,
849
+ it. kind . descr ( ) ,
850
+ ) ;
822
851
}
823
852
}
824
853
_ => ( ) ,
@@ -846,7 +875,7 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
846
875
// Account for `const C: _;`.
847
876
let mut visitor = PlaceholderHirTyCollector :: default ( ) ;
848
877
visitor. visit_trait_item ( trait_item) ;
849
- placeholder_type_error ( tcx, None , & [ ] , visitor. 0 , false , None ) ;
878
+ placeholder_type_error ( tcx, None , & [ ] , visitor. 0 , false , None , "constant" ) ;
850
879
}
851
880
852
881
hir:: TraitItemKind :: Type ( _, Some ( _) ) => {
@@ -855,7 +884,7 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
855
884
// Account for `type T = _;`.
856
885
let mut visitor = PlaceholderHirTyCollector :: default ( ) ;
857
886
visitor. visit_trait_item ( trait_item) ;
858
- placeholder_type_error ( tcx, None , & [ ] , visitor. 0 , false , None ) ;
887
+ placeholder_type_error ( tcx, None , & [ ] , visitor. 0 , false , None , "associated type" ) ;
859
888
}
860
889
861
890
hir:: TraitItemKind :: Type ( _, None ) => {
@@ -865,7 +894,7 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
865
894
let mut visitor = PlaceholderHirTyCollector :: default ( ) ;
866
895
visitor. visit_trait_item ( trait_item) ;
867
896
868
- placeholder_type_error ( tcx, None , & [ ] , visitor. 0 , false , None ) ;
897
+ placeholder_type_error ( tcx, None , & [ ] , visitor. 0 , false , None , "associated type" ) ;
869
898
}
870
899
} ;
871
900
@@ -887,7 +916,7 @@ fn convert_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::ImplItemId) {
887
916
let mut visitor = PlaceholderHirTyCollector :: default ( ) ;
888
917
visitor. visit_impl_item ( impl_item) ;
889
918
890
- placeholder_type_error ( tcx, None , & [ ] , visitor. 0 , false , None ) ;
919
+ placeholder_type_error ( tcx, None , & [ ] , visitor. 0 , false , None , "associated type" ) ;
891
920
}
892
921
hir:: ImplItemKind :: Const ( ..) => { }
893
922
}
@@ -1711,7 +1740,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
1711
1740
1712
1741
let mut visitor = PlaceholderHirTyCollector :: default ( ) ;
1713
1742
visitor. visit_ty ( ty) ;
1714
- let mut diag = bad_placeholder_type ( tcx, visitor. 0 ) ;
1743
+ let mut diag = bad_placeholder_type ( tcx, visitor. 0 , "return type" ) ;
1715
1744
let ret_ty = fn_sig. output ( ) ;
1716
1745
if ret_ty != tcx. ty_error ( ) {
1717
1746
if !ret_ty. is_closure ( ) {
0 commit comments