diff --git a/compiler/rustc_typeck/src/collect.rs b/compiler/rustc_typeck/src/collect.rs index ee3ac3b62d9ec..05a0a5a6cc04e 100644 --- a/compiler/rustc_typeck/src/collect.rs +++ b/compiler/rustc_typeck/src/collect.rs @@ -180,8 +180,7 @@ crate fn placeholder_type_error( // Suggest, but only if it is not a function in const or static if suggest { let mut is_fn = false; - let mut is_const = false; - let mut is_static = false; + let mut is_const_or_static = false; if let Some(hir_ty) = hir_ty { if let hir::TyKind::BareFn(_) = hir_ty.kind { @@ -191,19 +190,26 @@ crate fn placeholder_type_error( let parent_id = tcx.hir().get_parent_node(hir_ty.hir_id); let parent_node = tcx.hir().get(parent_id); - if let hir::Node::Item(item) = parent_node { - if let hir::ItemKind::Const(_, _) = item.kind { - is_const = true; - } else if let hir::ItemKind::Static(_, _, _) = item.kind { - is_static = true; - } - } + is_const_or_static = match parent_node { + Node::Item(&hir::Item { + kind: hir::ItemKind::Const(..) | hir::ItemKind::Static(..), + .. + }) + | Node::TraitItem(&hir::TraitItem { + kind: hir::TraitItemKind::Const(..), + .. + }) + | Node::ImplItem(&hir::ImplItem { + kind: hir::ImplItemKind::Const(..), .. + }) => true, + _ => false, + }; } } // if function is wrapped around a const or static, // then don't show the suggestion - if !(is_fn && (is_const || is_static)) { + if !(is_fn && is_const_or_static) { err.multipart_suggestion( "use type parameters instead", sugg, diff --git a/src/test/ui/issues/issue-74086.rs b/src/test/ui/typeck/issue-74086.rs similarity index 100% rename from src/test/ui/issues/issue-74086.rs rename to src/test/ui/typeck/issue-74086.rs diff --git a/src/test/ui/issues/issue-74086.stderr b/src/test/ui/typeck/issue-74086.stderr similarity index 100% rename from src/test/ui/issues/issue-74086.stderr rename to src/test/ui/typeck/issue-74086.stderr diff --git a/src/test/ui/issues/issue-81885.rs b/src/test/ui/typeck/issue-81885.rs similarity index 100% rename from src/test/ui/issues/issue-81885.rs rename to src/test/ui/typeck/issue-81885.rs diff --git a/src/test/ui/issues/issue-81885.stderr b/src/test/ui/typeck/issue-81885.stderr similarity index 100% rename from src/test/ui/issues/issue-81885.stderr rename to src/test/ui/typeck/issue-81885.stderr diff --git a/src/test/ui/typeck/type-placeholder-fn-in-const.rs b/src/test/ui/typeck/type-placeholder-fn-in-const.rs new file mode 100644 index 0000000000000..c27edc8485b92 --- /dev/null +++ b/src/test/ui/typeck/type-placeholder-fn-in-const.rs @@ -0,0 +1,14 @@ +struct MyStruct; + +trait Test { + const TEST: fn() -> _; + //~^ ERROR: the type placeholder `_` is not allowed within types on item signatures [E0121] + //~| ERROR: the type placeholder `_` is not allowed within types on item signatures [E0121] +} + +impl Test for MyStruct { + const TEST: fn() -> _ = 42; + //~^ ERROR: the type placeholder `_` is not allowed within types on item signatures [E0121] +} + +fn main() {} diff --git a/src/test/ui/typeck/type-placeholder-fn-in-const.stderr b/src/test/ui/typeck/type-placeholder-fn-in-const.stderr new file mode 100644 index 0000000000000..662871779a10e --- /dev/null +++ b/src/test/ui/typeck/type-placeholder-fn-in-const.stderr @@ -0,0 +1,21 @@ +error[E0121]: the type placeholder `_` is not allowed within types on item signatures + --> $DIR/type-placeholder-fn-in-const.rs:4:25 + | +LL | const TEST: fn() -> _; + | ^ not allowed in type signatures + +error[E0121]: the type placeholder `_` is not allowed within types on item signatures + --> $DIR/type-placeholder-fn-in-const.rs:4:25 + | +LL | const TEST: fn() -> _; + | ^ not allowed in type signatures + +error[E0121]: the type placeholder `_` is not allowed within types on item signatures + --> $DIR/type-placeholder-fn-in-const.rs:10:25 + | +LL | const TEST: fn() -> _ = 42; + | ^ not allowed in type signatures + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0121`. diff --git a/src/test/ui/typeck-closure-to-unsafe-fn-ptr.rs b/src/test/ui/typeck/typeck-closure-to-unsafe-fn-ptr.rs similarity index 100% rename from src/test/ui/typeck-closure-to-unsafe-fn-ptr.rs rename to src/test/ui/typeck/typeck-closure-to-unsafe-fn-ptr.rs diff --git a/src/test/ui/typeck-fn-to-unsafe-fn-ptr.rs b/src/test/ui/typeck/typeck-fn-to-unsafe-fn-ptr.rs similarity index 100% rename from src/test/ui/typeck-fn-to-unsafe-fn-ptr.rs rename to src/test/ui/typeck/typeck-fn-to-unsafe-fn-ptr.rs diff --git a/src/test/ui/typeck_type_placeholder_1.rs b/src/test/ui/typeck/typeck_type_placeholder_1.rs similarity index 100% rename from src/test/ui/typeck_type_placeholder_1.rs rename to src/test/ui/typeck/typeck_type_placeholder_1.rs