diff --git a/compiler/rustc_typeck/src/collect.rs b/compiler/rustc_typeck/src/collect.rs index d6985f3bd4d63..fdab5afd9aca1 100644 --- a/compiler/rustc_typeck/src/collect.rs +++ b/compiler/rustc_typeck/src/collect.rs @@ -710,6 +710,14 @@ fn convert_item(tcx: TyCtxt<'_>, item_id: hir::HirId) { if let hir::ItemKind::Fn(..) = it.kind { tcx.ensure().fn_sig(def_id); } + // Account for the `_` placeholder. + // We only check on `static` or `const` to avoid too many duplicated errors + // (see https://github.com/rust-lang/rust/issues/77428). + if matches!(it.kind, hir::ItemKind::Static(..) | hir::ItemKind::Const(..)) { + let mut visitor = PlaceholderHirTyCollector::default(); + visitor.visit_item(it); + placeholder_type_error(tcx, None, &[], visitor.0, false); + } } } } diff --git a/src/test/ui/error-codes/E0121.rs b/src/test/ui/error-codes/E0121.rs index f8b4d61b32301..1b68be4ea96c0 100644 --- a/src/test/ui/error-codes/E0121.rs +++ b/src/test/ui/error-codes/E0121.rs @@ -1,6 +1,10 @@ fn foo() -> _ { 5 } //~ ERROR E0121 static BAR: _ = "test"; //~ ERROR E0121 +//~^ ERROR E0121 -fn main() { -} +const FOO: dyn Fn() -> _ = ""; //~ ERROR E0121 + +static BOO: dyn Fn() -> _ = ""; //~ ERROR E0121 + +fn main() {} diff --git a/src/test/ui/error-codes/E0121.stderr b/src/test/ui/error-codes/E0121.stderr index ad854837ae5bd..b46383b4a0ca4 100644 --- a/src/test/ui/error-codes/E0121.stderr +++ b/src/test/ui/error-codes/E0121.stderr @@ -16,6 +16,24 @@ LL | static BAR: _ = "test"; | not allowed in type signatures | help: replace `_` with the correct type: `&str` -error: aborting due to 2 previous errors +error[E0121]: the type placeholder `_` is not allowed within types on item signatures + --> $DIR/E0121.rs:3:13 + | +LL | static BAR: _ = "test"; + | ^ not allowed in type signatures + +error[E0121]: the type placeholder `_` is not allowed within types on item signatures + --> $DIR/E0121.rs:6:24 + | +LL | const FOO: dyn Fn() -> _ = ""; + | ^ not allowed in type signatures + +error[E0121]: the type placeholder `_` is not allowed within types on item signatures + --> $DIR/E0121.rs:8:25 + | +LL | static BOO: dyn Fn() -> _ = ""; + | ^ not allowed in type signatures + +error: aborting due to 5 previous errors For more information about this error, try `rustc --explain E0121`.