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

Add help message to suggest const for unused type param #84784

Merged
merged 1 commit into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 12 additions & 3 deletions compiler/rustc_typeck/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ LL | pub struct Dependent<T, const X: T>([(); 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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ LL | pub struct Dependent<T, const X: T>([(); 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

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/const-generics/issue-67375.full.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ LL | struct Bug<T> {
| ^ 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

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/const-generics/issue-67375.min.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ LL | struct Bug<T> {
| ^ 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

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/const-generics/issue-67945-1.full.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ LL | struct Bug<S> {
| ^ 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

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/const-generics/issue-67945-1.min.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ LL | struct Bug<S> {
| ^ 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

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/const-generics/issue-67945-2.full.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ LL | struct Bug<S> {
| ^ 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

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/const-generics/issue-67945-2.min.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ LL | struct Bug<S> {
| ^ 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

Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/const-generics/unused-type-param-suggestion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#![crate_type="lib"]

struct Example<N>;
//~^ ERROR parameter
12 changes: 12 additions & 0 deletions src/test/ui/const-generics/unused-type-param-suggestion.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0392]: parameter `N` is never used
--> $DIR/unused-type-param-suggestion.rs:3:16
|
LL | struct Example<N>;
| ^ 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`.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ LL | enum MyWeirdOption<T> {
| ^ 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

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/enum/issue-67945-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ LL | enum Bug<S> {
| ^ 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

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/enum/issue-67945-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ LL | enum Bug<S> {
| ^ 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

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/error-codes/E0392.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | enum Foo<T> { 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

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/inner-static-type-parameter.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ LL | enum Bar<T> { 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

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/issues/issue-17904-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | struct Foo<T> 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

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/issues/issue-20413.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | struct NoData<T>;
| ^ 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<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`
--> $DIR/issue-20413.rs:8:36
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/issues/issue-36299.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/issues/issue-36638.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ LL | struct Foo<Self>(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
JulianKnodt marked this conversation as resolved.
Show resolved Hide resolved

error: aborting due to 3 previous errors

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/issues/issue-37534.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ LL | struct Foo<T: ?Hash> { }
| ^ 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

Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/variance/variance-unused-type-param.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | struct SomeStruct<A> { 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
Expand All @@ -13,6 +14,7 @@ LL | enum SomeEnum<A> { 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
Expand All @@ -21,6 +23,7 @@ LL | enum ListCell<T> {
| ^ 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

Expand Down