Skip to content
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

Remove invalid suggestions for assoc consts on placeholder type error #86355

Merged
merged 2 commits into from
Jun 17, 2021
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
26 changes: 16 additions & 10 deletions compiler/rustc_typeck/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions src/test/ui/typeck/type-placeholder-fn-in-const.rs
Original file line number Diff line number Diff line change
@@ -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() {}
21 changes: 21 additions & 0 deletions src/test/ui/typeck/type-placeholder-fn-in-const.stderr
Original file line number Diff line number Diff line change
@@ -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
Comment on lines +1 to +11
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unrelated to this PR but it seems we call placeholder_type_error twice for assoc consts of traits.


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`.