Skip to content

Commit 4e1e6d8

Browse files
authored
Rollup merge of rust-lang#86223 - fee1-dead:better-E0121, r=petrochenkov
Specify the kind of the item for E0121 Fixes rust-lang#86005
2 parents 4c27d8e + e66f241 commit 4e1e6d8

34 files changed

+455
-388
lines changed

compiler/rustc_hir/src/hir.rs

+21
Original file line numberDiff line numberDiff line change
@@ -2815,6 +2815,27 @@ impl ItemKind<'_> {
28152815
_ => return None,
28162816
})
28172817
}
2818+
2819+
pub fn descr(&self) -> &'static str {
2820+
match self {
2821+
ItemKind::ExternCrate(..) => "extern crate",
2822+
ItemKind::Use(..) => "`use` import",
2823+
ItemKind::Static(..) => "static item",
2824+
ItemKind::Const(..) => "constant item",
2825+
ItemKind::Fn(..) => "function",
2826+
ItemKind::Mod(..) => "module",
2827+
ItemKind::ForeignMod { .. } => "extern block",
2828+
ItemKind::GlobalAsm(..) => "global asm item",
2829+
ItemKind::TyAlias(..) => "type alias",
2830+
ItemKind::OpaqueTy(..) => "opaque type",
2831+
ItemKind::Enum(..) => "enum",
2832+
ItemKind::Struct(..) => "struct",
2833+
ItemKind::Union(..) => "union",
2834+
ItemKind::Trait(..) => "trait",
2835+
ItemKind::TraitAlias(..) => "trait alias",
2836+
ItemKind::Impl(..) => "implementation",
2837+
}
2838+
}
28182839
}
28192840

28202841
/// A reference from an trait to one of its associated items. This

compiler/rustc_typeck/src/astconv/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2418,6 +2418,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
24182418
visitor.0,
24192419
true,
24202420
hir_ty,
2421+
"function",
24212422
);
24222423
}
24232424

compiler/rustc_typeck/src/collect.rs

+40-11
Original file line numberDiff line numberDiff line change
@@ -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

242251
impl Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
@@ -302,13 +311,17 @@ impl Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
302311
fn 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
}
@@ -818,7 +839,15 @@ fn convert_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
818839
if let hir::TyKind::TraitObject(..) = ty.kind {
819840
let mut visitor = PlaceholderHirTyCollector::default();
820841
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+
);
822851
}
823852
}
824853
_ => (),
@@ -846,7 +875,7 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
846875
// Account for `const C: _;`.
847876
let mut visitor = PlaceholderHirTyCollector::default();
848877
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");
850879
}
851880

852881
hir::TraitItemKind::Type(_, Some(_)) => {
@@ -855,7 +884,7 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
855884
// Account for `type T = _;`.
856885
let mut visitor = PlaceholderHirTyCollector::default();
857886
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");
859888
}
860889

861890
hir::TraitItemKind::Type(_, None) => {
@@ -865,7 +894,7 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
865894
let mut visitor = PlaceholderHirTyCollector::default();
866895
visitor.visit_trait_item(trait_item);
867896

868-
placeholder_type_error(tcx, None, &[], visitor.0, false, None);
897+
placeholder_type_error(tcx, None, &[], visitor.0, false, None, "associated type");
869898
}
870899
};
871900

@@ -887,7 +916,7 @@ fn convert_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::ImplItemId) {
887916
let mut visitor = PlaceholderHirTyCollector::default();
888917
visitor.visit_impl_item(impl_item);
889918

890-
placeholder_type_error(tcx, None, &[], visitor.0, false, None);
919+
placeholder_type_error(tcx, None, &[], visitor.0, false, None, "associated type");
891920
}
892921
hir::ImplItemKind::Const(..) => {}
893922
}
@@ -1711,7 +1740,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
17111740

17121741
let mut visitor = PlaceholderHirTyCollector::default();
17131742
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");
17151744
let ret_ty = fn_sig.output();
17161745
if ret_ty != tcx.ty_error() {
17171746
if !ret_ty.is_closure() {

compiler/rustc_typeck/src/collect/type_of.rs

+28-9
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,9 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
285285
TraitItemKind::Const(ref ty, body_id) => body_id
286286
.and_then(|body_id| {
287287
if is_suggestable_infer_ty(ty) {
288-
Some(infer_placeholder_type(tcx, def_id, body_id, ty.span, item.ident))
288+
Some(infer_placeholder_type(
289+
tcx, def_id, body_id, ty.span, item.ident, "constant",
290+
))
289291
} else {
290292
None
291293
}
@@ -304,7 +306,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
304306
}
305307
ImplItemKind::Const(ref ty, body_id) => {
306308
if is_suggestable_infer_ty(ty) {
307-
infer_placeholder_type(tcx, def_id, body_id, ty.span, item.ident)
309+
infer_placeholder_type(tcx, def_id, body_id, ty.span, item.ident, "constant")
308310
} else {
309311
icx.to_ty(ty)
310312
}
@@ -320,9 +322,25 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
320322

321323
Node::Item(item) => {
322324
match item.kind {
323-
ItemKind::Static(ref ty, .., body_id) | ItemKind::Const(ref ty, body_id) => {
325+
ItemKind::Static(ref ty, .., body_id) => {
324326
if is_suggestable_infer_ty(ty) {
325-
infer_placeholder_type(tcx, def_id, body_id, ty.span, item.ident)
327+
infer_placeholder_type(
328+
tcx,
329+
def_id,
330+
body_id,
331+
ty.span,
332+
item.ident,
333+
"static variable",
334+
)
335+
} else {
336+
icx.to_ty(ty)
337+
}
338+
}
339+
ItemKind::Const(ref ty, body_id) => {
340+
if is_suggestable_infer_ty(ty) {
341+
infer_placeholder_type(
342+
tcx, def_id, body_id, ty.span, item.ident, "constant",
343+
)
326344
} else {
327345
icx.to_ty(ty)
328346
}
@@ -742,13 +760,14 @@ fn let_position_impl_trait_type(tcx: TyCtxt<'_>, opaque_ty_id: LocalDefId) -> Ty
742760
concrete_ty
743761
}
744762

745-
fn infer_placeholder_type(
746-
tcx: TyCtxt<'_>,
763+
fn infer_placeholder_type<'a>(
764+
tcx: TyCtxt<'a>,
747765
def_id: LocalDefId,
748766
body_id: hir::BodyId,
749767
span: Span,
750768
item_ident: Ident,
751-
) -> Ty<'_> {
769+
kind: &'static str,
770+
) -> Ty<'a> {
752771
// Attempts to make the type nameable by turning FnDefs into FnPtrs.
753772
struct MakeNameable<'tcx> {
754773
success: bool,
@@ -802,7 +821,7 @@ fn infer_placeholder_type(
802821
if let Some(sugg_ty) = sugg_ty {
803822
err.span_suggestion(
804823
span,
805-
"provide a type for the item",
824+
&format!("provide a type for the {item}", item = kind),
806825
format!("{}: {}", item_ident, sugg_ty),
807826
Applicability::MachineApplicable,
808827
);
@@ -816,7 +835,7 @@ fn infer_placeholder_type(
816835
err.emit_unless(ty.references_error());
817836
}
818837
None => {
819-
let mut diag = bad_placeholder_type(tcx, vec![span]);
838+
let mut diag = bad_placeholder_type(tcx, vec![span], kind);
820839

821840
if !ty.references_error() {
822841
let mut mk_nameable = MakeNameable::new(tcx);

src/test/ui/did_you_mean/bad-assoc-ty.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type D = (u8, u8)::AssocTy;
1616

1717
type E = _::AssocTy;
1818
//~^ ERROR missing angle brackets in associated item path
19-
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
19+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures for type aliases
2020

2121
type F = &'static (u8)::AssocTy;
2222
//~^ ERROR missing angle brackets in associated item path
@@ -47,37 +47,37 @@ type I = ty!()::AssocTy;
4747

4848
trait K<A, B> {}
4949
fn foo<X: K<_, _>>(x: X) {}
50-
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
50+
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures for functions
5151

5252
fn bar<F>(_: F) where F: Fn() -> _ {}
53-
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
53+
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures for functions
5454

5555
fn baz<F: Fn() -> _>(_: F) {}
56-
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
56+
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures for functions
5757

5858
struct L<F>(F) where F: Fn() -> _;
59-
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
59+
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures for structs
6060
struct M<F> where F: Fn() -> _ {
61-
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
61+
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures for structs
6262
a: F,
6363
}
6464
enum N<F> where F: Fn() -> _ {
65-
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
65+
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures for enums
6666
Foo(F),
6767
}
6868

6969
union O<F> where F: Fn() -> _ {
70-
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
70+
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures for unions
7171
foo: F,
7272
}
7373

7474
trait P<F> where F: Fn() -> _ {
75-
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
75+
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures for traits
7676
}
7777

7878
trait Q {
7979
fn foo<F>(_: F) where F: Fn() -> _ {}
80-
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
80+
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures for functions
8181
}
8282

8383
fn main() {}

0 commit comments

Comments
 (0)