@@ -145,6 +145,7 @@ crate fn placeholder_type_error(
145145 placeholder_types : Vec < Span > ,
146146 suggest : bool ,
147147 hir_ty : Option < & hir:: Ty < ' _ > > ,
148+ kind : & ' static str ,
148149) {
149150 if placeholder_types. is_empty ( ) {
150151 return ;
@@ -174,7 +175,7 @@ crate fn placeholder_type_error(
174175 ) ) ;
175176 }
176177
177- let mut err = bad_placeholder_type ( tcx, placeholder_types) ;
178+ let mut err = bad_placeholder_type ( tcx, placeholder_types, kind ) ;
178179
179180 // Suggest, but only if it is not a function in const or static
180181 if suggest {
@@ -236,7 +237,15 @@ fn reject_placeholder_type_signatures_in_item(tcx: TyCtxt<'tcx>, item: &'tcx hir
236237 let mut visitor = PlaceholderHirTyCollector :: default ( ) ;
237238 visitor. visit_item ( item) ;
238239
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+ ) ;
240249}
241250
242251impl Visitor < ' tcx > for CollectItemTypesVisitor < ' tcx > {
@@ -302,13 +311,17 @@ impl Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
302311fn bad_placeholder_type (
303312 tcx : TyCtxt < ' tcx > ,
304313 mut spans : Vec < Span > ,
314+ kind : & ' static str ,
305315) -> rustc_errors:: DiagnosticBuilder < ' tcx > {
316+ let kind = if kind. ends_with ( 's' ) { format ! ( "{}es" , kind) } else { format ! ( "{}s" , kind) } ;
317+
306318 spans. sort ( ) ;
307319 let mut err = struct_span_err ! (
308320 tcx. sess,
309321 spans. clone( ) ,
310322 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
312325 ) ;
313326 for span in spans {
314327 err. span_label ( span, "not allowed in type signatures" ) ;
@@ -382,7 +395,7 @@ impl AstConv<'tcx> for ItemCtxt<'tcx> {
382395 _: Option < & ty:: GenericParamDef > ,
383396 span : Span ,
384397 ) -> & ' tcx Const < ' tcx > {
385- bad_placeholder_type ( self . tcx ( ) , vec ! [ span] ) . emit ( ) ;
398+ bad_placeholder_type ( self . tcx ( ) , vec ! [ span] , "generic" ) . emit ( ) ;
386399 // Typeck doesn't expect erased regions to be returned from `type_of`.
387400 let ty = self . tcx . fold_regions ( ty, & mut false , |r, _| match r {
388401 ty:: ReErased => self . tcx . lifetimes . re_static ,
@@ -746,7 +759,15 @@ fn convert_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
746759 hir:: ForeignItemKind :: Static ( ..) => {
747760 let mut visitor = PlaceholderHirTyCollector :: default ( ) ;
748761 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+ ) ;
750771 }
751772 _ => ( ) ,
752773 }
@@ -846,7 +867,7 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
846867 // Account for `const C: _;`.
847868 let mut visitor = PlaceholderHirTyCollector :: default ( ) ;
848869 visitor. visit_trait_item ( trait_item) ;
849- placeholder_type_error ( tcx, None , & [ ] , visitor. 0 , false , None ) ;
870+ placeholder_type_error ( tcx, None , & [ ] , visitor. 0 , false , None , "constant" ) ;
850871 }
851872
852873 hir:: TraitItemKind :: Type ( _, Some ( _) ) => {
@@ -855,7 +876,7 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
855876 // Account for `type T = _;`.
856877 let mut visitor = PlaceholderHirTyCollector :: default ( ) ;
857878 visitor. visit_trait_item ( trait_item) ;
858- placeholder_type_error ( tcx, None , & [ ] , visitor. 0 , false , None ) ;
879+ placeholder_type_error ( tcx, None , & [ ] , visitor. 0 , false , None , "associated type" ) ;
859880 }
860881
861882 hir:: TraitItemKind :: Type ( _, None ) => {
@@ -865,7 +886,7 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
865886 let mut visitor = PlaceholderHirTyCollector :: default ( ) ;
866887 visitor. visit_trait_item ( trait_item) ;
867888
868- placeholder_type_error ( tcx, None , & [ ] , visitor. 0 , false , None ) ;
889+ placeholder_type_error ( tcx, None , & [ ] , visitor. 0 , false , None , "associated type" ) ;
869890 }
870891 } ;
871892
@@ -887,7 +908,7 @@ fn convert_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::ImplItemId) {
887908 let mut visitor = PlaceholderHirTyCollector :: default ( ) ;
888909 visitor. visit_impl_item ( impl_item) ;
889910
890- placeholder_type_error ( tcx, None , & [ ] , visitor. 0 , false , None ) ;
911+ placeholder_type_error ( tcx, None , & [ ] , visitor. 0 , false , None , "associated type" ) ;
891912 }
892913 hir:: ImplItemKind :: Const ( ..) => { }
893914 }
@@ -1711,7 +1732,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
17111732
17121733 let mut visitor = PlaceholderHirTyCollector :: default ( ) ;
17131734 visitor. visit_ty ( ty) ;
1714- let mut diag = bad_placeholder_type ( tcx, visitor. 0 ) ;
1735+ let mut diag = bad_placeholder_type ( tcx, visitor. 0 , "return type" ) ;
17151736 let ret_ty = fn_sig. output ( ) ;
17161737 if ret_ty != tcx. ty_error ( ) {
17171738 if !ret_ty. is_closure ( ) {
0 commit comments