From 50c86db4facf516d2248b81d56f7ee4e8381b6da Mon Sep 17 00:00:00 2001 From: kadmin Date: Sat, 1 May 2021 04:39:50 +0000 Subject: [PATCH] Add help message for unused type param --- compiler/rustc_typeck/src/check/wfcheck.rs | 15 ++++++++++++--- ...t-param-type-depends-on-type-param.full.stderr | 1 + ...st-param-type-depends-on-type-param.min.stderr | 1 + .../ui/const-generics/issue-67375.full.stderr | 1 + src/test/ui/const-generics/issue-67375.min.stderr | 1 + .../ui/const-generics/issue-67945-1.full.stderr | 1 + .../ui/const-generics/issue-67945-1.min.stderr | 1 + .../ui/const-generics/issue-67945-2.full.stderr | 1 + .../ui/const-generics/issue-67945-2.min.stderr | 1 + .../unused-type-param-suggestion.rs | 4 ++++ .../unused-type-param-suggestion.stderr | 12 ++++++++++++ .../issue-70453-generics-in-discr-ice.stderr | 1 + src/test/ui/enum/issue-67945-1.stderr | 1 + src/test/ui/enum/issue-67945-2.stderr | 1 + src/test/ui/error-codes/E0392.stderr | 1 + src/test/ui/inner-static-type-parameter.stderr | 1 + src/test/ui/issues/issue-17904-2.stderr | 1 + src/test/ui/issues/issue-20413.stderr | 1 + src/test/ui/issues/issue-36299.stderr | 1 + src/test/ui/issues/issue-36638.stderr | 1 + src/test/ui/issues/issue-37534.stderr | 1 + .../ui/variance/variance-unused-type-param.stderr | 3 +++ 22 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 src/test/ui/const-generics/unused-type-param-suggestion.rs create mode 100644 src/test/ui/const-generics/unused-type-param-suggestion.stderr diff --git a/compiler/rustc_typeck/src/check/wfcheck.rs b/compiler/rustc_typeck/src/check/wfcheck.rs index d25dd9a6e8302..4914f196afbb5 100644 --- a/compiler/rustc_typeck/src/check/wfcheck.rs +++ b/compiler/rustc_typeck/src/check/wfcheck.rs @@ -1298,12 +1298,14 @@ fn check_variances_for_type_defn<'tcx>( match param.name { hir::ParamName::Error => {} - _ => report_bivariance(tcx, param.span, param.name.ident().name), + _ => report_bivariance(tcx, param), } } } -fn report_bivariance(tcx: TyCtxt<'_>, span: Span, param_name: Symbol) { +fn report_bivariance(tcx: TyCtxt<'_>, param: &rustc_hir::GenericParam<'_>) { + let span = param.span; + let param_name = param.name.ident().name; let mut err = error_392(tcx, span, param_name); let suggested_marker_id = tcx.lang_items().phantom_data(); @@ -1318,7 +1320,14 @@ fn report_bivariance(tcx: TyCtxt<'_>, span: Span, param_name: Symbol) { format!("consider removing `{}` or referring to it in a field", param_name) }; err.help(&msg); - err.emit(); + + if matches!(param.kind, rustc_hir::GenericParamKind::Type { .. }) { + err.help(&format!( + "if you intended `{0}` to be a const parameter, use `const {0}: usize` instead", + param_name + )); + } + err.emit() } /// Feature gates RFC 2056 -- trivial bounds, checking for global bounds that diff --git a/src/test/ui/const-generics/const-param-type-depends-on-type-param.full.stderr b/src/test/ui/const-generics/const-param-type-depends-on-type-param.full.stderr index a83ee627187b5..32f7dea82639b 100644 --- a/src/test/ui/const-generics/const-param-type-depends-on-type-param.full.stderr +++ b/src/test/ui/const-generics/const-param-type-depends-on-type-param.full.stderr @@ -11,6 +11,7 @@ LL | pub struct Dependent([(); X]); | ^ unused parameter | = help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData` + = help: if you intended `T` to be a const parameter, use `const T: usize` instead error: aborting due to 2 previous errors diff --git a/src/test/ui/const-generics/const-param-type-depends-on-type-param.min.stderr b/src/test/ui/const-generics/const-param-type-depends-on-type-param.min.stderr index a83ee627187b5..32f7dea82639b 100644 --- a/src/test/ui/const-generics/const-param-type-depends-on-type-param.min.stderr +++ b/src/test/ui/const-generics/const-param-type-depends-on-type-param.min.stderr @@ -11,6 +11,7 @@ LL | pub struct Dependent([(); X]); | ^ unused parameter | = help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData` + = help: if you intended `T` to be a const parameter, use `const T: usize` instead error: aborting due to 2 previous errors diff --git a/src/test/ui/const-generics/issue-67375.full.stderr b/src/test/ui/const-generics/issue-67375.full.stderr index 2f004f75de56c..0fe65272f1b78 100644 --- a/src/test/ui/const-generics/issue-67375.full.stderr +++ b/src/test/ui/const-generics/issue-67375.full.stderr @@ -15,6 +15,7 @@ LL | struct Bug { | ^ unused parameter | = help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData` + = help: if you intended `T` to be a const parameter, use `const T: usize` instead error: aborting due to previous error; 1 warning emitted diff --git a/src/test/ui/const-generics/issue-67375.min.stderr b/src/test/ui/const-generics/issue-67375.min.stderr index 337e7bc14099d..be81fa921297b 100644 --- a/src/test/ui/const-generics/issue-67375.min.stderr +++ b/src/test/ui/const-generics/issue-67375.min.stderr @@ -14,6 +14,7 @@ LL | struct Bug { | ^ unused parameter | = help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData` + = help: if you intended `T` to be a const parameter, use `const T: usize` instead error: aborting due to 2 previous errors diff --git a/src/test/ui/const-generics/issue-67945-1.full.stderr b/src/test/ui/const-generics/issue-67945-1.full.stderr index 5cdcefe35015f..63c50b5ca5423 100644 --- a/src/test/ui/const-generics/issue-67945-1.full.stderr +++ b/src/test/ui/const-generics/issue-67945-1.full.stderr @@ -19,6 +19,7 @@ LL | struct Bug { | ^ unused parameter | = help: consider removing `S`, referring to it in a field, or using a marker such as `PhantomData` + = help: if you intended `S` to be a const parameter, use `const S: usize` instead error: aborting due to 2 previous errors diff --git a/src/test/ui/const-generics/issue-67945-1.min.stderr b/src/test/ui/const-generics/issue-67945-1.min.stderr index a3e086ea95486..074d36c8ef3ed 100644 --- a/src/test/ui/const-generics/issue-67945-1.min.stderr +++ b/src/test/ui/const-generics/issue-67945-1.min.stderr @@ -23,6 +23,7 @@ LL | struct Bug { | ^ unused parameter | = help: consider removing `S`, referring to it in a field, or using a marker such as `PhantomData` + = help: if you intended `S` to be a const parameter, use `const S: usize` instead error: aborting due to 3 previous errors diff --git a/src/test/ui/const-generics/issue-67945-2.full.stderr b/src/test/ui/const-generics/issue-67945-2.full.stderr index 4d96058b395b2..b900406023117 100644 --- a/src/test/ui/const-generics/issue-67945-2.full.stderr +++ b/src/test/ui/const-generics/issue-67945-2.full.stderr @@ -19,6 +19,7 @@ LL | struct Bug { | ^ unused parameter | = help: consider removing `S`, referring to it in a field, or using a marker such as `PhantomData` + = help: if you intended `S` to be a const parameter, use `const S: usize` instead error: aborting due to 2 previous errors diff --git a/src/test/ui/const-generics/issue-67945-2.min.stderr b/src/test/ui/const-generics/issue-67945-2.min.stderr index 860be4a9b6ac9..c06df79f8428c 100644 --- a/src/test/ui/const-generics/issue-67945-2.min.stderr +++ b/src/test/ui/const-generics/issue-67945-2.min.stderr @@ -23,6 +23,7 @@ LL | struct Bug { | ^ unused parameter | = help: consider removing `S`, referring to it in a field, or using a marker such as `PhantomData` + = help: if you intended `S` to be a const parameter, use `const S: usize` instead error: aborting due to 3 previous errors diff --git a/src/test/ui/const-generics/unused-type-param-suggestion.rs b/src/test/ui/const-generics/unused-type-param-suggestion.rs new file mode 100644 index 0000000000000..2251512c4596e --- /dev/null +++ b/src/test/ui/const-generics/unused-type-param-suggestion.rs @@ -0,0 +1,4 @@ +#![crate_type="lib"] + +struct Example; +//~^ ERROR parameter diff --git a/src/test/ui/const-generics/unused-type-param-suggestion.stderr b/src/test/ui/const-generics/unused-type-param-suggestion.stderr new file mode 100644 index 0000000000000..807065ca109e5 --- /dev/null +++ b/src/test/ui/const-generics/unused-type-param-suggestion.stderr @@ -0,0 +1,12 @@ +error[E0392]: parameter `N` is never used + --> $DIR/unused-type-param-suggestion.rs:3:16 + | +LL | struct Example; + | ^ unused parameter + | + = help: consider removing `N`, referring to it in a field, or using a marker such as `PhantomData` + = help: if you intended `N` to be a const parameter, use `const N: usize` instead + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0392`. diff --git a/src/test/ui/enum-discriminant/issue-70453-generics-in-discr-ice.stderr b/src/test/ui/enum-discriminant/issue-70453-generics-in-discr-ice.stderr index 1d43903928bef..f89be630eeb30 100644 --- a/src/test/ui/enum-discriminant/issue-70453-generics-in-discr-ice.stderr +++ b/src/test/ui/enum-discriminant/issue-70453-generics-in-discr-ice.stderr @@ -14,6 +14,7 @@ LL | enum MyWeirdOption { | ^ unused parameter | = help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData` + = help: if you intended `T` to be a const parameter, use `const T: usize` instead error: aborting due to 2 previous errors diff --git a/src/test/ui/enum/issue-67945-1.stderr b/src/test/ui/enum/issue-67945-1.stderr index 32ca94203e630..227899e75350f 100644 --- a/src/test/ui/enum/issue-67945-1.stderr +++ b/src/test/ui/enum/issue-67945-1.stderr @@ -14,6 +14,7 @@ LL | enum Bug { | ^ unused parameter | = help: consider removing `S`, referring to it in a field, or using a marker such as `PhantomData` + = help: if you intended `S` to be a const parameter, use `const S: usize` instead error: aborting due to 2 previous errors diff --git a/src/test/ui/enum/issue-67945-2.stderr b/src/test/ui/enum/issue-67945-2.stderr index a738d3b15a557..5a90f00c346d0 100644 --- a/src/test/ui/enum/issue-67945-2.stderr +++ b/src/test/ui/enum/issue-67945-2.stderr @@ -14,6 +14,7 @@ LL | enum Bug { | ^ unused parameter | = help: consider removing `S`, referring to it in a field, or using a marker such as `PhantomData` + = help: if you intended `S` to be a const parameter, use `const S: usize` instead error: aborting due to 2 previous errors diff --git a/src/test/ui/error-codes/E0392.stderr b/src/test/ui/error-codes/E0392.stderr index 860bf68f0189b..622402999c348 100644 --- a/src/test/ui/error-codes/E0392.stderr +++ b/src/test/ui/error-codes/E0392.stderr @@ -5,6 +5,7 @@ LL | enum Foo { Bar } | ^ unused parameter | = help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData` + = help: if you intended `T` to be a const parameter, use `const T: usize` instead error: aborting due to previous error diff --git a/src/test/ui/inner-static-type-parameter.stderr b/src/test/ui/inner-static-type-parameter.stderr index 990e4649a5817..e4e449e4159b6 100644 --- a/src/test/ui/inner-static-type-parameter.stderr +++ b/src/test/ui/inner-static-type-parameter.stderr @@ -13,6 +13,7 @@ LL | enum Bar { What } | ^ unused parameter | = help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData` + = help: if you intended `T` to be a const parameter, use `const T: usize` instead error: aborting due to 2 previous errors diff --git a/src/test/ui/issues/issue-17904-2.stderr b/src/test/ui/issues/issue-17904-2.stderr index 62b7b79538c61..259e029113d50 100644 --- a/src/test/ui/issues/issue-17904-2.stderr +++ b/src/test/ui/issues/issue-17904-2.stderr @@ -5,6 +5,7 @@ LL | struct Foo where T: Copy; | ^ unused parameter | = help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData` + = help: if you intended `T` to be a const parameter, use `const T: usize` instead error: aborting due to previous error diff --git a/src/test/ui/issues/issue-20413.stderr b/src/test/ui/issues/issue-20413.stderr index 7fb1e3f2bba4f..6bcff7aff2dc4 100644 --- a/src/test/ui/issues/issue-20413.stderr +++ b/src/test/ui/issues/issue-20413.stderr @@ -5,6 +5,7 @@ LL | struct NoData; | ^ unused parameter | = help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData` + = help: if you intended `T` to be a const parameter, use `const T: usize` instead error[E0275]: overflow evaluating the requirement `NoData>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>: Foo` --> $DIR/issue-20413.rs:8:36 diff --git a/src/test/ui/issues/issue-36299.stderr b/src/test/ui/issues/issue-36299.stderr index 8e29a925d8044..dc24fb353f408 100644 --- a/src/test/ui/issues/issue-36299.stderr +++ b/src/test/ui/issues/issue-36299.stderr @@ -13,6 +13,7 @@ LL | struct Foo<'a, A> {} | ^ unused parameter | = help: consider removing `A`, referring to it in a field, or using a marker such as `PhantomData` + = help: if you intended `A` to be a const parameter, use `const A: usize` instead error: aborting due to 2 previous errors diff --git a/src/test/ui/issues/issue-36638.stderr b/src/test/ui/issues/issue-36638.stderr index e5d6f8ec7adeb..733fc4af534f7 100644 --- a/src/test/ui/issues/issue-36638.stderr +++ b/src/test/ui/issues/issue-36638.stderr @@ -17,6 +17,7 @@ LL | struct Foo(Self); | ^^^^ unused parameter | = help: consider removing `Self`, referring to it in a field, or using a marker such as `PhantomData` + = help: if you intended `Self` to be a const parameter, use `const Self: usize` instead error: aborting due to 3 previous errors diff --git a/src/test/ui/issues/issue-37534.stderr b/src/test/ui/issues/issue-37534.stderr index 895479986f1d1..82bb51028c977 100644 --- a/src/test/ui/issues/issue-37534.stderr +++ b/src/test/ui/issues/issue-37534.stderr @@ -22,6 +22,7 @@ LL | struct Foo { } | ^ unused parameter | = help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData` + = help: if you intended `T` to be a const parameter, use `const T: usize` instead error: aborting due to 2 previous errors; 1 warning emitted diff --git a/src/test/ui/variance/variance-unused-type-param.stderr b/src/test/ui/variance/variance-unused-type-param.stderr index a4d636bc03c76..270233c0c9772 100644 --- a/src/test/ui/variance/variance-unused-type-param.stderr +++ b/src/test/ui/variance/variance-unused-type-param.stderr @@ -5,6 +5,7 @@ LL | struct SomeStruct { x: u32 } | ^ unused parameter | = help: consider removing `A`, referring to it in a field, or using a marker such as `PhantomData` + = help: if you intended `A` to be a const parameter, use `const A: usize` instead error[E0392]: parameter `A` is never used --> $DIR/variance-unused-type-param.rs:9:15 @@ -13,6 +14,7 @@ LL | enum SomeEnum { Nothing } | ^ unused parameter | = help: consider removing `A`, referring to it in a field, or using a marker such as `PhantomData` + = help: if you intended `A` to be a const parameter, use `const A: usize` instead error[E0392]: parameter `T` is never used --> $DIR/variance-unused-type-param.rs:13:15 @@ -21,6 +23,7 @@ LL | enum ListCell { | ^ unused parameter | = help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData` + = help: if you intended `T` to be a const parameter, use `const T: usize` instead error: aborting due to 3 previous errors