Skip to content

Commit

Permalink
Fixing bad suggestion for _ in const type when a function #81885
Browse files Browse the repository at this point in the history
  • Loading branch information
kper committed Feb 9, 2021
1 parent 921ec4b commit 09d5d07
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 142 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_typeck/src/astconv/mod.rs
Expand Up @@ -2327,6 +2327,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
&generics.params[..],
visitor.0,
true,
true
);
}

Expand Down
22 changes: 16 additions & 6 deletions compiler/rustc_typeck/src/collect.rs
Expand Up @@ -141,6 +141,7 @@ crate fn placeholder_type_error(
generics: &[hir::GenericParam<'_>],
placeholder_types: Vec<Span>,
suggest: bool,
is_fn: bool,
) {
if placeholder_types.is_empty() {
return;
Expand Down Expand Up @@ -171,7 +172,9 @@ crate fn placeholder_type_error(
}

let mut err = bad_placeholder_type(tcx, placeholder_types);
if suggest {

// Suggest, but only if it is not a function
if suggest && !is_fn {
err.multipart_suggestion(
"use type parameters instead",
sugg,
Expand All @@ -198,7 +201,14 @@ fn reject_placeholder_type_signatures_in_item(tcx: TyCtxt<'tcx>, item: &'tcx hir
let mut visitor = PlaceholderHirTyCollector::default();
visitor.visit_item(item);

placeholder_type_error(tcx, Some(generics.span), &generics.params[..], visitor.0, suggest);
placeholder_type_error(
tcx,
Some(generics.span),
&generics.params[..],
visitor.0,
suggest,
false
);
}

impl Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
Expand Down Expand Up @@ -743,7 +753,7 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::HirId) {
// Account for `const C: _;`.
let mut visitor = PlaceholderHirTyCollector::default();
visitor.visit_trait_item(trait_item);
placeholder_type_error(tcx, None, &[], visitor.0, false);
placeholder_type_error(tcx, None, &[], visitor.0, false, false);
}

hir::TraitItemKind::Type(_, Some(_)) => {
Expand All @@ -752,7 +762,7 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::HirId) {
// Account for `type T = _;`.
let mut visitor = PlaceholderHirTyCollector::default();
visitor.visit_trait_item(trait_item);
placeholder_type_error(tcx, None, &[], visitor.0, false);
placeholder_type_error(tcx, None, &[], visitor.0, false, false);
}

hir::TraitItemKind::Type(_, None) => {
Expand All @@ -761,7 +771,7 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::HirId) {
// even if there is no concrete type.
let mut visitor = PlaceholderHirTyCollector::default();
visitor.visit_trait_item(trait_item);
placeholder_type_error(tcx, None, &[], visitor.0, false);
placeholder_type_error(tcx, None, &[], visitor.0, false, false);
}
};

Expand All @@ -782,7 +792,7 @@ fn convert_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::HirId) {
// Account for `type T = _;`
let mut visitor = PlaceholderHirTyCollector::default();
visitor.visit_impl_item(impl_item);
placeholder_type_error(tcx, None, &[], visitor.0, false);
placeholder_type_error(tcx, None, &[], visitor.0, false, false);
}
hir::ImplItemKind::Const(..) => {}
}
Expand Down
20 changes: 0 additions & 20 deletions src/test/ui/did_you_mean/bad-assoc-ty.stderr
Expand Up @@ -129,33 +129,18 @@ LL | fn foo<X: K<_, _>>(x: X) {}
| ^ ^ not allowed in type signatures
| |
| not allowed in type signatures
|
help: use type parameters instead
|
LL | fn foo<X: K<T, T>, T>(x: X) {}
| ^ ^ ^^^

error[E0121]: the type placeholder `_` is not allowed within types on item signatures
--> $DIR/bad-assoc-ty.rs:52:34
|
LL | fn bar<F>(_: F) where F: Fn() -> _ {}
| ^ not allowed in type signatures
|
help: use type parameters instead
|
LL | fn bar<F, T>(_: F) where F: Fn() -> T {}
| ^^^ ^

error[E0121]: the type placeholder `_` is not allowed within types on item signatures
--> $DIR/bad-assoc-ty.rs:55:19
|
LL | fn baz<F: Fn() -> _>(_: F) {}
| ^ not allowed in type signatures
|
help: use type parameters instead
|
LL | fn baz<F: Fn() -> T, T>(_: F) {}
| ^^^^

error[E0121]: the type placeholder `_` is not allowed within types on item signatures
--> $DIR/bad-assoc-ty.rs:58:33
Expand Down Expand Up @@ -217,11 +202,6 @@ error[E0121]: the type placeholder `_` is not allowed within types on item signa
|
LL | fn foo<F>(_: F) where F: Fn() -> _ {}
| ^ not allowed in type signatures
|
help: use type parameters instead
|
LL | fn foo<F, T>(_: F) where F: Fn() -> T {}
| ^^^ ^

error: aborting due to 28 previous errors

Expand Down
5 changes: 1 addition & 4 deletions src/test/ui/issues/issue-74086.stderr
Expand Up @@ -2,10 +2,7 @@ error[E0121]: the type placeholder `_` is not allowed within types on item signa
--> $DIR/issue-74086.rs:2:20
|
LL | static BUG: fn(_) -> u8 = |_| 8;
| ^
| |
| not allowed in type signatures
| help: use type parameters instead: `T`
| ^ not allowed in type signatures

error: aborting due to previous error

Expand Down
10 changes: 10 additions & 0 deletions src/test/ui/issues/issue-81885.rs
@@ -0,0 +1,10 @@
const TEST4: fn() -> _ = 42;
//~^ ERROR the type placeholder `_` is not allowed within types on item
//signatures

fn main() {
const TEST5: fn() -> _ = 42;
//~^ ERROR the type placeholder `_` is not allowed within types on item
//signatures

}
15 changes: 15 additions & 0 deletions src/test/ui/issues/issue-81885.stderr
@@ -0,0 +1,15 @@
error[E0121]: the type placeholder `_` is not allowed within types on item signatures
--> $DIR/issue-81885.rs:1:22
|
LL | const TEST4: fn() -> _ = 42;
| ^ not allowed in type signatures

error[E0121]: the type placeholder `_` is not allowed within types on item signatures
--> $DIR/issue-81885.rs:6:26
|
LL | const TEST5: fn() -> _ = 42;
| ^ not allowed in type signatures

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0121`.
10 changes: 0 additions & 10 deletions src/test/ui/self/self-infer.stderr
Expand Up @@ -3,22 +3,12 @@ error[E0121]: the type placeholder `_` is not allowed within types on item signa
|
LL | fn f(self: _) {}
| ^ not allowed in type signatures
|
help: use type parameters instead
|
LL | fn f<T>(self: T) {}
| ^^^ ^

error[E0121]: the type placeholder `_` is not allowed within types on item signatures
--> $DIR/self-infer.rs:5:17
|
LL | fn g(self: &_) {}
| ^ not allowed in type signatures
|
help: use type parameters instead
|
LL | fn g<T>(self: &T) {}
| ^^^ ^

error: aborting due to 2 previous errors

Expand Down

0 comments on commit 09d5d07

Please sign in to comment.