Skip to content

Commit d828ead

Browse files
committed
Remove invalid suggestions for assoc consts on placeholder type error
1 parent 52b2286 commit d828ead

File tree

3 files changed

+51
-10
lines changed

3 files changed

+51
-10
lines changed

compiler/rustc_typeck/src/collect.rs

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

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

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

204210
// if function is wrapped around a const or static,
205211
// then don't show the suggestion
206-
if !(is_fn && (is_const || is_static)) {
212+
if !(is_fn && is_const_or_static) {
207213
err.multipart_suggestion(
208214
"use type parameters instead",
209215
sugg,
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)