@@ -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 }
@@ -838,7 +859,7 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
838859 // Account for `const C: _;`.
839860 let mut visitor = PlaceholderHirTyCollector :: default ( ) ;
840861 visitor. visit_trait_item ( trait_item) ;
841- placeholder_type_error ( tcx, None , & [ ] , visitor. 0 , false , None ) ;
862+ placeholder_type_error ( tcx, None , & [ ] , visitor. 0 , false , None , "constant" ) ;
842863 }
843864
844865 hir:: TraitItemKind :: Type ( _, Some ( _) ) => {
@@ -847,7 +868,7 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
847868 // Account for `type T = _;`.
848869 let mut visitor = PlaceholderHirTyCollector :: default ( ) ;
849870 visitor. visit_trait_item ( trait_item) ;
850- placeholder_type_error ( tcx, None , & [ ] , visitor. 0 , false , None ) ;
871+ placeholder_type_error ( tcx, None , & [ ] , visitor. 0 , false , None , "associated type" ) ;
851872 }
852873
853874 hir:: TraitItemKind :: Type ( _, None ) => {
@@ -857,7 +878,7 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
857878 let mut visitor = PlaceholderHirTyCollector :: default ( ) ;
858879 visitor. visit_trait_item ( trait_item) ;
859880
860- placeholder_type_error ( tcx, None , & [ ] , visitor. 0 , false , None ) ;
881+ placeholder_type_error ( tcx, None , & [ ] , visitor. 0 , false , None , "associated type" ) ;
861882 }
862883 } ;
863884
@@ -879,7 +900,7 @@ fn convert_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::ImplItemId) {
879900 let mut visitor = PlaceholderHirTyCollector :: default ( ) ;
880901 visitor. visit_impl_item ( impl_item) ;
881902
882- placeholder_type_error ( tcx, None , & [ ] , visitor. 0 , false , None ) ;
903+ placeholder_type_error ( tcx, None , & [ ] , visitor. 0 , false , None , "associated type" ) ;
883904 }
884905 hir:: ImplItemKind :: Const ( ..) => { }
885906 }
@@ -1703,7 +1724,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
17031724
17041725 let mut visitor = PlaceholderHirTyCollector :: default ( ) ;
17051726 visitor. visit_ty ( ty) ;
1706- let mut diag = bad_placeholder_type ( tcx, visitor. 0 ) ;
1727+ let mut diag = bad_placeholder_type ( tcx, visitor. 0 , "return type" ) ;
17071728 let ret_ty = fn_sig. output ( ) ;
17081729 if ret_ty != tcx. ty_error ( ) {
17091730 if !ret_ty. is_closure ( ) {
0 commit comments