Skip to content

Commit 09d5d07

Browse files
committed
Fixing bad suggestion for _ in const type when a function #81885
1 parent 921ec4b commit 09d5d07

File tree

9 files changed

+46
-142
lines changed

9 files changed

+46
-142
lines changed

compiler/rustc_typeck/src/astconv/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2327,6 +2327,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
23272327
&generics.params[..],
23282328
visitor.0,
23292329
true,
2330+
true
23302331
);
23312332
}
23322333

compiler/rustc_typeck/src/collect.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ crate fn placeholder_type_error(
141141
generics: &[hir::GenericParam<'_>],
142142
placeholder_types: Vec<Span>,
143143
suggest: bool,
144+
is_fn: bool,
144145
) {
145146
if placeholder_types.is_empty() {
146147
return;
@@ -171,7 +172,9 @@ crate fn placeholder_type_error(
171172
}
172173

173174
let mut err = bad_placeholder_type(tcx, placeholder_types);
174-
if suggest {
175+
176+
// Suggest, but only if it is not a function
177+
if suggest && !is_fn {
175178
err.multipart_suggestion(
176179
"use type parameters instead",
177180
sugg,
@@ -198,7 +201,14 @@ fn reject_placeholder_type_signatures_in_item(tcx: TyCtxt<'tcx>, item: &'tcx hir
198201
let mut visitor = PlaceholderHirTyCollector::default();
199202
visitor.visit_item(item);
200203

201-
placeholder_type_error(tcx, Some(generics.span), &generics.params[..], visitor.0, suggest);
204+
placeholder_type_error(
205+
tcx,
206+
Some(generics.span),
207+
&generics.params[..],
208+
visitor.0,
209+
suggest,
210+
false
211+
);
202212
}
203213

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

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

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

@@ -782,7 +792,7 @@ fn convert_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::HirId) {
782792
// Account for `type T = _;`
783793
let mut visitor = PlaceholderHirTyCollector::default();
784794
visitor.visit_impl_item(impl_item);
785-
placeholder_type_error(tcx, None, &[], visitor.0, false);
795+
placeholder_type_error(tcx, None, &[], visitor.0, false, false);
786796
}
787797
hir::ImplItemKind::Const(..) => {}
788798
}

src/test/ui/did_you_mean/bad-assoc-ty.stderr

-20
Original file line numberDiff line numberDiff line change
@@ -129,33 +129,18 @@ LL | fn foo<X: K<_, _>>(x: X) {}
129129
| ^ ^ not allowed in type signatures
130130
| |
131131
| not allowed in type signatures
132-
|
133-
help: use type parameters instead
134-
|
135-
LL | fn foo<X: K<T, T>, T>(x: X) {}
136-
| ^ ^ ^^^
137132

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

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

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

226206
error: aborting due to 28 previous errors
227207

src/test/ui/issues/issue-74086.stderr

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ error[E0121]: the type placeholder `_` is not allowed within types on item signa
22
--> $DIR/issue-74086.rs:2:20
33
|
44
LL | static BUG: fn(_) -> u8 = |_| 8;
5-
| ^
6-
| |
7-
| not allowed in type signatures
8-
| help: use type parameters instead: `T`
5+
| ^ not allowed in type signatures
96

107
error: aborting due to previous error
118

src/test/ui/issues/issue-81885.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const TEST4: fn() -> _ = 42;
2+
//~^ ERROR the type placeholder `_` is not allowed within types on item
3+
//signatures
4+
5+
fn main() {
6+
const TEST5: fn() -> _ = 42;
7+
//~^ ERROR the type placeholder `_` is not allowed within types on item
8+
//signatures
9+
10+
}

src/test/ui/issues/issue-81885.stderr

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0121]: the type placeholder `_` is not allowed within types on item signatures
2+
--> $DIR/issue-81885.rs:1:22
3+
|
4+
LL | const TEST4: fn() -> _ = 42;
5+
| ^ not allowed in type signatures
6+
7+
error[E0121]: the type placeholder `_` is not allowed within types on item signatures
8+
--> $DIR/issue-81885.rs:6:26
9+
|
10+
LL | const TEST5: fn() -> _ = 42;
11+
| ^ not allowed in type signatures
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0121`.

src/test/ui/self/self-infer.stderr

-10
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,12 @@ error[E0121]: the type placeholder `_` is not allowed within types on item signa
33
|
44
LL | fn f(self: _) {}
55
| ^ not allowed in type signatures
6-
|
7-
help: use type parameters instead
8-
|
9-
LL | fn f<T>(self: T) {}
10-
| ^^^ ^
116

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

2313
error: aborting due to 2 previous errors
2414

0 commit comments

Comments
 (0)