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

Fixing bad suggestion for _ in const type when a function #81885 #81914

Merged
merged 5 commits into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions compiler/rustc_typeck/src/astconv/mod.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 {
estebank marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
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 {}
| ^^^ ^
estebank marked this conversation as resolved.
Show resolved Hide resolved

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Loading