Skip to content

Commit

Permalink
Rollup merge of #86355 - JohnTitor:issue-82158, r=estebank
Browse files Browse the repository at this point in the history
Remove invalid suggestions for assoc consts on placeholder type error

Fixes #82158
This also moves some tests to typeck.
r? ``@estebank``
  • Loading branch information
JohnTitor committed Jun 17, 2021
2 parents afe70ee + fb06d9e commit aff7994
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 10 deletions.
26 changes: 16 additions & 10 deletions compiler/rustc_typeck/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,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 @@ -190,19 +189,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.
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

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`.
File renamed without changes.
File renamed without changes.

0 comments on commit aff7994

Please sign in to comment.