Skip to content

Replace ItemCtxt::report_placeholder_type_error match with a call to TyCtxt::def_descr #143234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3141,15 +3141,6 @@ pub enum TraitItemKind<'hir> {
/// type.
Type(GenericBounds<'hir>, Option<&'hir Ty<'hir>>),
}
impl TraitItemKind<'_> {
pub fn descr(&self) -> &'static str {
match self {
TraitItemKind::Const(..) => "associated constant",
TraitItemKind::Fn(..) => "function",
TraitItemKind::Type(..) => "associated type",
}
}
}

// The bodies for items are stored "out of line", in a separate
// hashmap in the `Crate`. Here we just record the hir-id of the item
Expand Down Expand Up @@ -3211,15 +3202,6 @@ pub enum ImplItemKind<'hir> {
/// An associated type.
Type(&'hir Ty<'hir>),
}
impl ImplItemKind<'_> {
pub fn descr(&self) -> &'static str {
match self {
ImplItemKind::Const(..) => "associated constant",
ImplItemKind::Fn(..) => "function",
ImplItemKind::Type(..) => "associated type",
}
}
}

/// A constraint on an associated item.
///
Expand Down Expand Up @@ -4545,16 +4527,6 @@ pub enum ForeignItemKind<'hir> {
Type,
}

impl ForeignItemKind<'_> {
pub fn descr(&self) -> &'static str {
match self {
ForeignItemKind::Fn(..) => "function",
ForeignItemKind::Static(..) => "static variable",
ForeignItemKind::Type => "type",
}
}
}

/// A variable captured by a closure.
#[derive(Debug, Copy, Clone, HashStable_Generic)]
pub struct Upvar {
Expand Down
12 changes: 1 addition & 11 deletions compiler/rustc_hir_analysis/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,17 +277,7 @@ impl<'tcx> ItemCtxt<'tcx> {
}
_ => self.item_def_id,
};
// FIXME: just invoke `tcx.def_descr` instead of going through the HIR
// Can also remove most `descr` methods then.
let kind = match self.tcx.hir_node_by_def_id(kind_id) {
Node::Item(it) => it.kind.descr(),
Node::ImplItem(it) => it.kind.descr(),
Node::TraitItem(it) => it.kind.descr(),
Node::ForeignItem(it) => it.kind.descr(),
Node::OpaqueTy(_) => "opaque type",
Node::Synthetic => self.tcx.def_descr(kind_id.into()),
node => todo!("{node:#?}"),
};
let kind = self.tcx.def_descr(kind_id.into());
let mut diag = placeholder_type_error_diag(
self,
generics,
Expand Down
4 changes: 2 additions & 2 deletions tests/rustdoc-ui/invalid_infered_static_and_const.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constant items
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
--> $DIR/invalid_infered_static_and_const.rs:1:24
|
LL | const FOO: dyn Fn() -> _ = "";
| ^ not allowed in type signatures

error[E0121]: the placeholder `_` is not allowed within types on item signatures for static items
error[E0121]: the placeholder `_` is not allowed within types on item signatures for statics
--> $DIR/invalid_infered_static_and_const.rs:2:25
|
LL | static BOO: dyn Fn() -> _ = "";
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/closures/missing-body.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Checks that the compiler complains about the missing closure body and does not
// crash.
// This is a regression test for <https://github.com/rust-lang/rust/issues/143128>.

fn main() { |b: [str; _]| {}; }
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for closures
//~| ERROR the size for values of type `str` cannot be known at compilation time
19 changes: 19 additions & 0 deletions tests/ui/closures/missing-body.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error[E0121]: the placeholder `_` is not allowed within types on item signatures for closures
--> $DIR/missing-body.rs:5:23
|
LL | fn main() { |b: [str; _]| {}; }
| ^ not allowed in type signatures

error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/missing-body.rs:5:17
|
LL | fn main() { |b: [str; _]| {}; }
| ^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
= note: slice and array elements must have `Sized` type

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0121, E0277.
For more information about an error, try `rustc --explain E0121`.
2 changes: 1 addition & 1 deletion tests/ui/did_you_mean/bad-assoc-ty.edition2015.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures
LL | trait P<F> where F: Fn() -> _ {
| ^ not allowed in type signatures

error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated functions
--> $DIR/bad-assoc-ty.rs:88:38
|
LL | fn foo<F>(_: F) where F: Fn() -> _ {}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/did_you_mean/bad-assoc-ty.edition2021.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures
LL | trait P<F> where F: Fn() -> _ {
| ^ not allowed in type signatures

error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated functions
--> $DIR/bad-assoc-ty.rs:88:38
|
LL | fn foo<F>(_: F) where F: Fn() -> _ {}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/did_you_mean/bad-assoc-ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ trait P<F> where F: Fn() -> _ {

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

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ trait Foo<T>: Sized {

impl Foo<usize> for () {
fn bar(i: i32, t: usize, s: &()) -> (usize, i32) {
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated functions
//~| ERROR type annotations needed
(1, 2)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/did_you_mean/replace-impl-infer-ty-from-trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ trait Foo<T>: Sized {

impl Foo<usize> for () {
fn bar(i: _, t: _, s: _) -> _ {
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated functions
//~| ERROR type annotations needed
(1, 2)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated functions
--> $DIR/replace-impl-infer-ty-from-trait.rs:9:15
|
LL | fn bar(i: _, t: _, s: _) -> _ {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/fn/error-recovery-mismatch.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ LL | fn fold<T>(&self, _: T, &self._) {}
= note: for more information, see issue #41686 <https://github.com/rust-lang/rust/issues/41686>
= note: `#[warn(anonymous_parameters)]` on by default

error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
error[E0121]: the placeholder `_` is not allowed within types on item signatures for methods
--> $DIR/error-recovery-mismatch.rs:11:35
|
LL | fn fold<T>(&self, _: T, &self._) {}
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/self/self-infer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
struct S;

impl S {
fn f(self: _) {} //~ERROR the placeholder `_` is not allowed within types on item signatures for functions
fn g(self: &_) {} //~ERROR the placeholder `_` is not allowed within types on item signatures for functions
fn f(self: _) {} //~ERROR the placeholder `_` is not allowed within types on item signatures for methods
fn g(self: &_) {} //~ERROR the placeholder `_` is not allowed within types on item signatures for methods
}

fn main() {}
4 changes: 2 additions & 2 deletions tests/ui/self/self-infer.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
error[E0121]: the placeholder `_` is not allowed within types on item signatures for methods
--> $DIR/self-infer.rs:4:16
|
LL | fn f(self: _) {}
| ^ not allowed in type signatures

error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
error[E0121]: the placeholder `_` is not allowed within types on item signatures for methods
--> $DIR/self-infer.rs:5:17
|
LL | fn g(self: &_) {}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/suggestions/bad-infer-in-trait-impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ trait Foo {

impl Foo for () {
fn bar(s: _) {}
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated functions
//~| ERROR has 1 parameter but the declaration in trait `Foo::bar` has 0
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/suggestions/bad-infer-in-trait-impl.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated functions
--> $DIR/bad-infer-in-trait-impl.rs:6:15
|
LL | fn bar(s: _) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ LL | impl<T> const FromResidual for T {
= note: implementing a foreign trait is only possible if at least one of the types for which it is implemented is local
= note: only traits defined in the current crate can be implemented for a type parameter

error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated functions
--> $DIR/ice-119717-constant-lifetime.rs:9:31
|
LL | fn from_residual(t: T) -> _ {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated functions
--> $DIR/method-argument-mismatch-variance-ice-119867.rs:8:23
|
LL | fn deserialize(s: _) {}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/typeck/issue-74086.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fn main() {
static BUG: fn(_) -> u8 = |_| 8;
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for static items
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for statics
}
2 changes: 1 addition & 1 deletion tests/ui/typeck/issue-74086.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0121]: the placeholder `_` is not allowed within types on item signatures for static items
error[E0121]: the placeholder `_` is not allowed within types on item signatures for statics
--> $DIR/issue-74086.rs:2:20
|
LL | static BUG: fn(_) -> u8 = |_| 8;
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/typeck/issue-75889.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constant items
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
--> $DIR/issue-75889.rs:3:24
|
LL | const FOO: dyn Fn() -> _ = "";
| ^ not allowed in type signatures

error[E0121]: the placeholder `_` is not allowed within types on item signatures for static items
error[E0121]: the placeholder `_` is not allowed within types on item signatures for statics
--> $DIR/issue-75889.rs:4:25
|
LL | static BOO: dyn Fn() -> _ = "";
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/typeck/issue-81885.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const TEST4: fn() -> _ = 42;
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for constant items
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for constants

fn main() {
const TEST5: fn() -> _ = 42;
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for constant items
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for constants
}
4 changes: 2 additions & 2 deletions tests/ui/typeck/issue-81885.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constant items
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
--> $DIR/issue-81885.rs:1:22
|
LL | const TEST4: fn() -> _ = 42;
| ^ not allowed in type signatures

error[E0121]: the placeholder `_` is not allowed within types on item signatures for constant items
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
--> $DIR/issue-81885.rs:5:26
|
LL | const TEST5: fn() -> _ = 42;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0121]: the placeholder `_` is not allowed within types on item signatures for static variables
error[E0121]: the placeholder `_` is not allowed within types on item signatures for statics
--> $DIR/issue-83621-placeholder-static-in-extern.rs:4:15
|
LL | static x: _;
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/typeck/issue-88643.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ use std::collections::HashMap;
pub trait T {}

static CALLBACKS: HashMap<*const dyn T, dyn FnMut(&mut _) + 'static> = HashMap::new();
//~^ ERROR: the placeholder `_` is not allowed within types on item signatures for static items [E0121]
//~^ ERROR: the placeholder `_` is not allowed within types on item signatures for statics [E0121]

static CALLBACKS2: Vec<dyn Fn(& _)> = Vec::new();
//~^ ERROR: the placeholder `_` is not allowed within types on item signatures for static items [E0121]
//~^ ERROR: the placeholder `_` is not allowed within types on item signatures for statics [E0121]

static CALLBACKS3: Option<dyn Fn(& _)> = None;
//~^ ERROR: the placeholder `_` is not allowed within types on item signatures for static items [E0121]
//~^ ERROR: the placeholder `_` is not allowed within types on item signatures for statics [E0121]

fn main() {}
6 changes: 3 additions & 3 deletions tests/ui/typeck/issue-88643.stderr
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
error[E0121]: the placeholder `_` is not allowed within types on item signatures for static items
error[E0121]: the placeholder `_` is not allowed within types on item signatures for statics
--> $DIR/issue-88643.rs:10:56
|
LL | static CALLBACKS: HashMap<*const dyn T, dyn FnMut(&mut _) + 'static> = HashMap::new();
| ^ not allowed in type signatures

error[E0121]: the placeholder `_` is not allowed within types on item signatures for static items
error[E0121]: the placeholder `_` is not allowed within types on item signatures for statics
--> $DIR/issue-88643.rs:13:33
|
LL | static CALLBACKS2: Vec<dyn Fn(& _)> = Vec::new();
| ^ not allowed in type signatures

error[E0121]: the placeholder `_` is not allowed within types on item signatures for static items
error[E0121]: the placeholder `_` is not allowed within types on item signatures for statics
--> $DIR/issue-88643.rs:16:36
|
LL | static CALLBACKS3: Option<dyn Fn(& _)> = None;
Expand Down
28 changes: 14 additions & 14 deletions tests/ui/typeck/typeck_type_placeholder_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Test9 {
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types

fn test10(&self, _x : _) { }
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for methods
}

fn test11(x: &usize) -> &_ {
Expand All @@ -56,10 +56,10 @@ unsafe fn test12(x: *const usize) -> *const *const _ {

impl Clone for Test9 {
fn clone(&self) -> _ { Test9 }
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for methods

fn clone_from(&mut self, other: _) { *self = Test9; }
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for methods
}

struct Test10 {
Expand Down Expand Up @@ -108,15 +108,15 @@ pub fn main() {
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types

fn fn_test10(&self, _x : _) { }
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for methods
}

impl Clone for FnTest9 {
fn clone(&self) -> _ { FnTest9 }
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for methods

fn clone_from(&mut self, other: _) { *self = FnTest9; }
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for methods
}

struct FnTest10 {
Expand All @@ -140,19 +140,19 @@ pub fn main() {

trait T {
fn method_test1(&self, x: _);
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for methods
fn method_test2(&self, x: _) -> _;
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
//~| ERROR the placeholder `_` is not allowed within types on item signatures for functions
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for methods
//~| ERROR the placeholder `_` is not allowed within types on item signatures for methods
fn method_test3(&self) -> _;
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for methods
fn assoc_fn_test1(x: _);
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated functions
fn assoc_fn_test2(x: _) -> _;
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
//~| ERROR the placeholder `_` is not allowed within types on item signatures for functions
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated functions
//~| ERROR the placeholder `_` is not allowed within types on item signatures for associated functions
fn assoc_fn_test3() -> _;
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated functions
}

struct BadStruct<_>(_);
Expand Down
Loading
Loading