Skip to content

Commit 50c86db

Browse files
committed
Add help message for unused type param
1 parent b52769b commit 50c86db

22 files changed

+49
-3
lines changed

compiler/rustc_typeck/src/check/wfcheck.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -1298,12 +1298,14 @@ fn check_variances_for_type_defn<'tcx>(
12981298

12991299
match param.name {
13001300
hir::ParamName::Error => {}
1301-
_ => report_bivariance(tcx, param.span, param.name.ident().name),
1301+
_ => report_bivariance(tcx, param),
13021302
}
13031303
}
13041304
}
13051305

1306-
fn report_bivariance(tcx: TyCtxt<'_>, span: Span, param_name: Symbol) {
1306+
fn report_bivariance(tcx: TyCtxt<'_>, param: &rustc_hir::GenericParam<'_>) {
1307+
let span = param.span;
1308+
let param_name = param.name.ident().name;
13071309
let mut err = error_392(tcx, span, param_name);
13081310

13091311
let suggested_marker_id = tcx.lang_items().phantom_data();
@@ -1318,7 +1320,14 @@ fn report_bivariance(tcx: TyCtxt<'_>, span: Span, param_name: Symbol) {
13181320
format!("consider removing `{}` or referring to it in a field", param_name)
13191321
};
13201322
err.help(&msg);
1321-
err.emit();
1323+
1324+
if matches!(param.kind, rustc_hir::GenericParamKind::Type { .. }) {
1325+
err.help(&format!(
1326+
"if you intended `{0}` to be a const parameter, use `const {0}: usize` instead",
1327+
param_name
1328+
));
1329+
}
1330+
err.emit()
13221331
}
13231332

13241333
/// Feature gates RFC 2056 -- trivial bounds, checking for global bounds that

src/test/ui/const-generics/const-param-type-depends-on-type-param.full.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ LL | pub struct Dependent<T, const X: T>([(); X]);
1111
| ^ unused parameter
1212
|
1313
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
14+
= help: if you intended `T` to be a const parameter, use `const T: usize` instead
1415

1516
error: aborting due to 2 previous errors
1617

src/test/ui/const-generics/const-param-type-depends-on-type-param.min.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ LL | pub struct Dependent<T, const X: T>([(); X]);
1111
| ^ unused parameter
1212
|
1313
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
14+
= help: if you intended `T` to be a const parameter, use `const T: usize` instead
1415

1516
error: aborting due to 2 previous errors
1617

src/test/ui/const-generics/issue-67375.full.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ LL | struct Bug<T> {
1515
| ^ unused parameter
1616
|
1717
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
18+
= help: if you intended `T` to be a const parameter, use `const T: usize` instead
1819

1920
error: aborting due to previous error; 1 warning emitted
2021

src/test/ui/const-generics/issue-67375.min.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ LL | struct Bug<T> {
1414
| ^ unused parameter
1515
|
1616
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
17+
= help: if you intended `T` to be a const parameter, use `const T: usize` instead
1718

1819
error: aborting due to 2 previous errors
1920

src/test/ui/const-generics/issue-67945-1.full.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ LL | struct Bug<S> {
1919
| ^ unused parameter
2020
|
2121
= help: consider removing `S`, referring to it in a field, or using a marker such as `PhantomData`
22+
= help: if you intended `S` to be a const parameter, use `const S: usize` instead
2223

2324
error: aborting due to 2 previous errors
2425

src/test/ui/const-generics/issue-67945-1.min.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ LL | struct Bug<S> {
2323
| ^ unused parameter
2424
|
2525
= help: consider removing `S`, referring to it in a field, or using a marker such as `PhantomData`
26+
= help: if you intended `S` to be a const parameter, use `const S: usize` instead
2627

2728
error: aborting due to 3 previous errors
2829

src/test/ui/const-generics/issue-67945-2.full.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ LL | struct Bug<S> {
1919
| ^ unused parameter
2020
|
2121
= help: consider removing `S`, referring to it in a field, or using a marker such as `PhantomData`
22+
= help: if you intended `S` to be a const parameter, use `const S: usize` instead
2223

2324
error: aborting due to 2 previous errors
2425

src/test/ui/const-generics/issue-67945-2.min.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ LL | struct Bug<S> {
2323
| ^ unused parameter
2424
|
2525
= help: consider removing `S`, referring to it in a field, or using a marker such as `PhantomData`
26+
= help: if you intended `S` to be a const parameter, use `const S: usize` instead
2627

2728
error: aborting due to 3 previous errors
2829

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#![crate_type="lib"]
2+
3+
struct Example<N>;
4+
//~^ ERROR parameter
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0392]: parameter `N` is never used
2+
--> $DIR/unused-type-param-suggestion.rs:3:16
3+
|
4+
LL | struct Example<N>;
5+
| ^ unused parameter
6+
|
7+
= help: consider removing `N`, referring to it in a field, or using a marker such as `PhantomData`
8+
= help: if you intended `N` to be a const parameter, use `const N: usize` instead
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0392`.

src/test/ui/enum-discriminant/issue-70453-generics-in-discr-ice.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ LL | enum MyWeirdOption<T> {
1414
| ^ unused parameter
1515
|
1616
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
17+
= help: if you intended `T` to be a const parameter, use `const T: usize` instead
1718

1819
error: aborting due to 2 previous errors
1920

src/test/ui/enum/issue-67945-1.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ LL | enum Bug<S> {
1414
| ^ unused parameter
1515
|
1616
= help: consider removing `S`, referring to it in a field, or using a marker such as `PhantomData`
17+
= help: if you intended `S` to be a const parameter, use `const S: usize` instead
1718

1819
error: aborting due to 2 previous errors
1920

src/test/ui/enum/issue-67945-2.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ LL | enum Bug<S> {
1414
| ^ unused parameter
1515
|
1616
= help: consider removing `S`, referring to it in a field, or using a marker such as `PhantomData`
17+
= help: if you intended `S` to be a const parameter, use `const S: usize` instead
1718

1819
error: aborting due to 2 previous errors
1920

src/test/ui/error-codes/E0392.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | enum Foo<T> { Bar }
55
| ^ unused parameter
66
|
77
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
8+
= help: if you intended `T` to be a const parameter, use `const T: usize` instead
89

910
error: aborting due to previous error
1011

src/test/ui/inner-static-type-parameter.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ LL | enum Bar<T> { What }
1313
| ^ unused parameter
1414
|
1515
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
16+
= help: if you intended `T` to be a const parameter, use `const T: usize` instead
1617

1718
error: aborting due to 2 previous errors
1819

src/test/ui/issues/issue-17904-2.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | struct Foo<T> where T: Copy;
55
| ^ unused parameter
66
|
77
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
8+
= help: if you intended `T` to be a const parameter, use `const T: usize` instead
89

910
error: aborting due to previous error
1011

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

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | struct NoData<T>;
55
| ^ unused parameter
66
|
77
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
8+
= help: if you intended `T` to be a const parameter, use `const T: usize` instead
89

910
error[E0275]: overflow evaluating the requirement `NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<NoData<T>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>: Foo`
1011
--> $DIR/issue-20413.rs:8:36

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

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ LL | struct Foo<'a, A> {}
1313
| ^ unused parameter
1414
|
1515
= help: consider removing `A`, referring to it in a field, or using a marker such as `PhantomData`
16+
= help: if you intended `A` to be a const parameter, use `const A: usize` instead
1617

1718
error: aborting due to 2 previous errors
1819

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

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ LL | struct Foo<Self>(Self);
1717
| ^^^^ unused parameter
1818
|
1919
= help: consider removing `Self`, referring to it in a field, or using a marker such as `PhantomData`
20+
= help: if you intended `Self` to be a const parameter, use `const Self: usize` instead
2021

2122
error: aborting due to 3 previous errors
2223

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

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ LL | struct Foo<T: ?Hash> { }
2222
| ^ unused parameter
2323
|
2424
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
25+
= help: if you intended `T` to be a const parameter, use `const T: usize` instead
2526

2627
error: aborting due to 2 previous errors; 1 warning emitted
2728

src/test/ui/variance/variance-unused-type-param.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | struct SomeStruct<A> { x: u32 }
55
| ^ unused parameter
66
|
77
= help: consider removing `A`, referring to it in a field, or using a marker such as `PhantomData`
8+
= help: if you intended `A` to be a const parameter, use `const A: usize` instead
89

910
error[E0392]: parameter `A` is never used
1011
--> $DIR/variance-unused-type-param.rs:9:15
@@ -13,6 +14,7 @@ LL | enum SomeEnum<A> { Nothing }
1314
| ^ unused parameter
1415
|
1516
= help: consider removing `A`, referring to it in a field, or using a marker such as `PhantomData`
17+
= help: if you intended `A` to be a const parameter, use `const A: usize` instead
1618

1719
error[E0392]: parameter `T` is never used
1820
--> $DIR/variance-unused-type-param.rs:13:15
@@ -21,6 +23,7 @@ LL | enum ListCell<T> {
2123
| ^ unused parameter
2224
|
2325
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
26+
= help: if you intended `T` to be a const parameter, use `const T: usize` instead
2427

2528
error: aborting due to 3 previous errors
2629

0 commit comments

Comments
 (0)