Skip to content

Commit aff7994

Browse files
authored
Rollup merge of #86355 - JohnTitor:issue-82158, r=estebank
Remove invalid suggestions for assoc consts on placeholder type error Fixes #82158 This also moves some tests to typeck. r? ``@estebank``
2 parents afe70ee + fb06d9e commit aff7994

10 files changed

+51
-10
lines changed

compiler/rustc_typeck/src/collect.rs

+16-10
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,7 @@ crate fn placeholder_type_error(
179179
// Suggest, but only if it is not a function in const or static
180180
if suggest {
181181
let mut is_fn = false;
182-
let mut is_const = false;
183-
let mut is_static = false;
182+
let mut is_const_or_static = false;
184183

185184
if let Some(hir_ty) = hir_ty {
186185
if let hir::TyKind::BareFn(_) = hir_ty.kind {
@@ -190,19 +189,26 @@ crate fn placeholder_type_error(
190189
let parent_id = tcx.hir().get_parent_node(hir_ty.hir_id);
191190
let parent_node = tcx.hir().get(parent_id);
192191

193-
if let hir::Node::Item(item) = parent_node {
194-
if let hir::ItemKind::Const(_, _) = item.kind {
195-
is_const = true;
196-
} else if let hir::ItemKind::Static(_, _, _) = item.kind {
197-
is_static = true;
198-
}
199-
}
192+
is_const_or_static = match parent_node {
193+
Node::Item(&hir::Item {
194+
kind: hir::ItemKind::Const(..) | hir::ItemKind::Static(..),
195+
..
196+
})
197+
| Node::TraitItem(&hir::TraitItem {
198+
kind: hir::TraitItemKind::Const(..),
199+
..
200+
})
201+
| Node::ImplItem(&hir::ImplItem {
202+
kind: hir::ImplItemKind::Const(..), ..
203+
}) => true,
204+
_ => false,
205+
};
200206
}
201207
}
202208

203209
// if function is wrapped around a const or static,
204210
// then don't show the suggestion
205-
if !(is_fn && (is_const || is_static)) {
211+
if !(is_fn && is_const_or_static) {
206212
err.multipart_suggestion(
207213
"use type parameters instead",
208214
sugg,
File renamed without changes.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
struct MyStruct;
2+
3+
trait Test {
4+
const TEST: fn() -> _;
5+
//~^ ERROR: the type placeholder `_` is not allowed within types on item signatures [E0121]
6+
//~| ERROR: the type placeholder `_` is not allowed within types on item signatures [E0121]
7+
}
8+
9+
impl Test for MyStruct {
10+
const TEST: fn() -> _ = 42;
11+
//~^ ERROR: the type placeholder `_` is not allowed within types on item signatures [E0121]
12+
}
13+
14+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0121]: the type placeholder `_` is not allowed within types on item signatures
2+
--> $DIR/type-placeholder-fn-in-const.rs:4:25
3+
|
4+
LL | const TEST: fn() -> _;
5+
| ^ not allowed in type signatures
6+
7+
error[E0121]: the type placeholder `_` is not allowed within types on item signatures
8+
--> $DIR/type-placeholder-fn-in-const.rs:4:25
9+
|
10+
LL | const TEST: fn() -> _;
11+
| ^ not allowed in type signatures
12+
13+
error[E0121]: the type placeholder `_` is not allowed within types on item signatures
14+
--> $DIR/type-placeholder-fn-in-const.rs:10:25
15+
|
16+
LL | const TEST: fn() -> _ = 42;
17+
| ^ not allowed in type signatures
18+
19+
error: aborting due to 3 previous errors
20+
21+
For more information about this error, try `rustc --explain E0121`.

0 commit comments

Comments
 (0)