Skip to content

Adding helper to report_unused_parameter #108252

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
11 changes: 10 additions & 1 deletion compiler/rustc_hir_analysis/src/impl_wf_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::constrained_generic_params as cgp;
use min_specialization::check_min_specialization;

use rustc_data_structures::fx::FxHashSet;
use rustc_errors::struct_span_err;
use rustc_errors::{struct_span_err, Applicability};
use rustc_hir::def::DefKind;
use rustc_hir::def_id::LocalDefId;
use rustc_middle::ty::query::Providers;
Expand Down Expand Up @@ -181,6 +181,15 @@ fn report_unused_parameter(tcx: TyCtxt<'_>, span: Span, kind: &str, name: Symbol
name
);
err.span_label(span, format!("unconstrained {} parameter", kind));
err.span_suggestion(
span,
format!(
"Either remove the type parameter '{}' or make use of it, for example ` for S<{}>`.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Make use of it" is a little misleading, the user might already use it in a where clause. I'm not sure what the best wording for this would be.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe just removing the "make" and leave it Either remove the type parameter or use it?
I am not sure on the words as well... =(

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea that sounds better

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: our messages start lowercase

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should not just suggest S<T>. Either load the trait or type name and append the generics to that or write prosa explaining what's going on without naming the trait or type.

name, name
),
"",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's a good idea to give a suggestion to remove it. Simply removing the parameter is usually not the solution to this error in my experience.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I based myself on the "desired output" on #107295
What would be a good explanation? :)

Applicability::MaybeIncorrect,
);
if kind == "const" {
err.note(
"expressions using a const parameter must map each value to a distinct output value",
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/async-await/issues/issue-78654.full.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ LL | impl<const H: feature> Foo {
|
= note: expressions using a const parameter must map each value to a distinct output value
= note: proving the result of expressions other than the parameter are unique is not supported
help: Either remove the type parameter 'H' or make use of it, for example ` for S<H>`.
|
LL - impl<const H: feature> Foo {
LL + impl<> Foo {
|

error: aborting due to 2 previous errors

Expand Down
5 changes: 5 additions & 0 deletions tests/ui/async-await/issues/issue-78654.min.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ LL | impl<const H: feature> Foo {
|
= note: expressions using a const parameter must map each value to a distinct output value
= note: proving the result of expressions other than the parameter are unique is not supported
help: Either remove the type parameter 'H' or make use of it, for example ` for S<H>`.
|
LL - impl<const H: feature> Foo {
LL + impl<> Foo {
|

error: aborting due to 2 previous errors

Expand Down
10 changes: 10 additions & 0 deletions tests/ui/const-generics/issues/issue-68366.full.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ LL | impl <const N: usize> Collatz<{Some(N)}> {}
|
= note: expressions using a const parameter must map each value to a distinct output value
= note: proving the result of expressions other than the parameter are unique is not supported
help: Either remove the type parameter 'N' or make use of it, for example ` for S<N>`.
|
LL - impl <const N: usize> Collatz<{Some(N)}> {}
LL + impl <> Collatz<{Some(N)}> {}
|
Comment on lines +9 to +13
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this case the suggestion is wrong, removing it is never the right thing if it is used, but in a way that isn't legal.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we know it in this case (we are emitting the two notes above after all), you can re-use the condition for that and not emit the suggestion in that case


error[E0207]: the const parameter `N` is not constrained by the impl trait, self type, or predicates
--> $DIR/issue-68366.rs:17:6
Expand All @@ -15,6 +20,11 @@ LL | impl<const N: usize> Foo {}
|
= note: expressions using a const parameter must map each value to a distinct output value
= note: proving the result of expressions other than the parameter are unique is not supported
help: Either remove the type parameter 'N' or make use of it, for example ` for S<N>`.
|
LL - impl<const N: usize> Foo {}
LL + impl<> Foo {}
|

error: aborting due to 2 previous errors

Expand Down
10 changes: 10 additions & 0 deletions tests/ui/const-generics/issues/issue-68366.min.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ LL | impl <const N: usize> Collatz<{Some(N)}> {}
|
= note: expressions using a const parameter must map each value to a distinct output value
= note: proving the result of expressions other than the parameter are unique is not supported
help: Either remove the type parameter 'N' or make use of it, for example ` for S<N>`.
|
LL - impl <const N: usize> Collatz<{Some(N)}> {}
LL + impl <> Collatz<{Some(N)}> {}
|

error[E0207]: the const parameter `N` is not constrained by the impl trait, self type, or predicates
--> $DIR/issue-68366.rs:17:6
Expand All @@ -24,6 +29,11 @@ LL | impl<const N: usize> Foo {}
|
= note: expressions using a const parameter must map each value to a distinct output value
= note: proving the result of expressions other than the parameter are unique is not supported
help: Either remove the type parameter 'N' or make use of it, for example ` for S<N>`.
|
LL - impl<const N: usize> Foo {}
LL + impl<> Foo {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should remove the generic parameters entirely instead of leaving empty ones around

|

error: aborting due to 3 previous errors

Expand Down
6 changes: 6 additions & 0 deletions tests/ui/duplicate/duplicate-type-parameter.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self
|
LL | impl<T,T> Qux<T,T> for Option<T> {}
| ^ unconstrained type parameter
|
help: Either remove the type parameter 'T' or make use of it, for example ` for S<T>`.
|
LL - impl<T,T> Qux<T,T> for Option<T> {}
LL + impl<T,> Qux<T,T> for Option<T> {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stray comma in the suggestion

|

error: aborting due to 8 previous errors

Expand Down
6 changes: 6 additions & 0 deletions tests/ui/error-codes/E0207.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self
|
LL | impl<T: Default> Foo {
| ^ unconstrained type parameter
|
help: Either remove the type parameter 'T' or make use of it, for example ` for S<T>`.
|
LL - impl<T: Default> Foo {
LL + impl<: Default> Foo {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to remove the bounds, too

|

error: aborting due to previous error

Expand Down
6 changes: 6 additions & 0 deletions tests/ui/generic-associated-types/bugs/issue-87735.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ error[E0207]: the type parameter `U` is not constrained by the impl trait, self
|
LL | impl<'b, T, U> AsRef2 for Foo<T>
| ^ unconstrained type parameter
|
help: Either remove the type parameter 'U' or make use of it, for example ` for S<U>`.
|
LL - impl<'b, T, U> AsRef2 for Foo<T>
LL + impl<'b, T, > AsRef2 for Foo<T>
|

error: aborting due to previous error

Expand Down
6 changes: 6 additions & 0 deletions tests/ui/generic-associated-types/bugs/issue-88526.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ error[E0207]: the type parameter `I` is not constrained by the impl trait, self
|
LL | impl<'q, Q, I, F> A for TestB<Q, F>
| ^ unconstrained type parameter
|
help: Either remove the type parameter 'I' or make use of it, for example ` for S<I>`.
|
LL - impl<'q, Q, I, F> A for TestB<Q, F>
LL + impl<'q, Q, , F> A for TestB<Q, F>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stray comma

|

error: aborting due to previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ error[E0207]: the type parameter `T1` is not constrained by the impl trait, self
|
LL | impl <T, T1> Foo for T {
| ^^ unconstrained type parameter
|
help: Either remove the type parameter 'T1' or make use of it, for example ` for S<T1>`.
|
LL - impl <T, T1> Foo for T {
LL + impl <T, > Foo for T {
|

error: aborting due to 3 previous errors

Expand Down
6 changes: 6 additions & 0 deletions tests/ui/impl-trait/issues/issue-87340.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self
|
LL | impl<T> X for () {
| ^ unconstrained type parameter
|
help: Either remove the type parameter 'T' or make use of it, for example ` for S<T>`.
|
LL - impl<T> X for () {
LL + impl<> X for () {
|

error: aborting due to previous error

Expand Down
6 changes: 6 additions & 0 deletions tests/ui/impl-unused-rps-in-assoc-type.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait,
|
LL | impl<'a> Fun for Holder {
| ^^ unconstrained lifetime parameter
|
help: Either remove the type parameter ''a' or make use of it, for example ` for S<'a>`.
|
LL - impl<'a> Fun for Holder {
LL + impl<> Fun for Holder {
|

error: aborting due to previous error

Expand Down
12 changes: 12 additions & 0 deletions tests/ui/impl-unused-tps-inherent.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,24 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self
|
LL | impl<T> MyType {
| ^ unconstrained type parameter
|
help: Either remove the type parameter 'T' or make use of it, for example ` for S<T>`.
|
LL - impl<T> MyType {
LL + impl<> MyType {
|

error[E0207]: the type parameter `U` is not constrained by the impl trait, self type, or predicates
--> $DIR/impl-unused-tps-inherent.rs:17:8
|
LL | impl<T,U> MyType1<T> {
| ^ unconstrained type parameter
|
help: Either remove the type parameter 'U' or make use of it, for example ` for S<U>`.
|
LL - impl<T,U> MyType1<T> {
LL + impl<T,> MyType1<T> {
|

error: aborting due to 2 previous errors

Expand Down
30 changes: 30 additions & 0 deletions tests/ui/impl-unused-tps.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,60 @@ error[E0207]: the type parameter `U` is not constrained by the impl trait, self
|
LL | impl<T,U> Foo<T> for [isize;1] {
| ^ unconstrained type parameter
|
help: Either remove the type parameter 'U' or make use of it, for example ` for S<U>`.
|
LL - impl<T,U> Foo<T> for [isize;1] {
LL + impl<T,> Foo<T> for [isize;1] {
|

error[E0207]: the type parameter `U` is not constrained by the impl trait, self type, or predicates
--> $DIR/impl-unused-tps.rs:30:8
|
LL | impl<T,U> Bar for T {
| ^ unconstrained type parameter
|
help: Either remove the type parameter 'U' or make use of it, for example ` for S<U>`.
|
LL - impl<T,U> Bar for T {
LL + impl<T,> Bar for T {
|

error[E0207]: the type parameter `U` is not constrained by the impl trait, self type, or predicates
--> $DIR/impl-unused-tps.rs:38:8
|
LL | impl<T,U> Bar for T
| ^ unconstrained type parameter
|
help: Either remove the type parameter 'U' or make use of it, for example ` for S<U>`.
|
LL - impl<T,U> Bar for T
LL + impl<T,> Bar for T
|

error[E0207]: the type parameter `U` is not constrained by the impl trait, self type, or predicates
--> $DIR/impl-unused-tps.rs:46:8
|
LL | impl<T,U,V> Foo<T> for T
| ^ unconstrained type parameter
|
help: Either remove the type parameter 'U' or make use of it, for example ` for S<U>`.
|
LL - impl<T,U,V> Foo<T> for T
LL + impl<T,,V> Foo<T> for T
|

error[E0207]: the type parameter `V` is not constrained by the impl trait, self type, or predicates
--> $DIR/impl-unused-tps.rs:46:10
|
LL | impl<T,U,V> Foo<T> for T
| ^ unconstrained type parameter
|
help: Either remove the type parameter 'V' or make use of it, for example ` for S<V>`.
|
LL - impl<T,U,V> Foo<T> for T
LL + impl<T,U,> Foo<T> for T
|

error: aborting due to 5 previous errors

Expand Down
6 changes: 6 additions & 0 deletions tests/ui/issues/issue-16562.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self
|
LL | impl<T, M: MatrixShape> Collection for Col<M, usize> {
| ^ unconstrained type parameter
|
help: Either remove the type parameter 'T' or make use of it, for example ` for S<T>`.
|
LL - impl<T, M: MatrixShape> Collection for Col<M, usize> {
LL + impl<, M: MatrixShape> Collection for Col<M, usize> {
|

error: aborting due to previous error

Expand Down
6 changes: 6 additions & 0 deletions tests/ui/issues/issue-22886.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait,
|
LL | impl<'a> Iterator for Newtype {
| ^^ unconstrained lifetime parameter
|
help: Either remove the type parameter ''a' or make use of it, for example ` for S<'a>`.
|
LL - impl<'a> Iterator for Newtype {
LL + impl<> Iterator for Newtype {
|

error: aborting due to previous error

Expand Down
12 changes: 12 additions & 0 deletions tests/ui/issues/issue-26262.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,24 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self
|
LL | impl<T: Tr> S<T::Assoc> {
| ^ unconstrained type parameter
|
help: Either remove the type parameter 'T' or make use of it, for example ` for S<T>`.
|
LL - impl<T: Tr> S<T::Assoc> {
LL + impl<: Tr> S<T::Assoc> {
|

error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates
--> $DIR/issue-26262.rs:17:6
|
LL | impl<'a,T: Trait2<'a>> Trait1<<T as Trait2<'a>>::Foo> for T {
| ^^ unconstrained lifetime parameter
|
help: Either remove the type parameter ''a' or make use of it, for example ` for S<'a>`.
|
LL - impl<'a,T: Trait2<'a>> Trait1<<T as Trait2<'a>>::Foo> for T {
LL + impl<,T: Trait2<'a>> Trait1<<T as Trait2<'a>>::Foo> for T {
|

error: aborting due to 2 previous errors

Expand Down
6 changes: 6 additions & 0 deletions tests/ui/issues/issue-29861.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait,
|
LL | impl<'a, T: 'a> MakeRef2 for T {
| ^^ unconstrained lifetime parameter
|
help: Either remove the type parameter ''a' or make use of it, for example ` for S<'a>`.
|
LL - impl<'a, T: 'a> MakeRef2 for T {
LL + impl<, T: 'a> MakeRef2 for T {
|

error: aborting due to previous error

Expand Down
6 changes: 6 additions & 0 deletions tests/ui/issues/issue-35139.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait,
|
LL | impl<'a> MethodType for MTFn {
| ^^ unconstrained lifetime parameter
|
help: Either remove the type parameter ''a' or make use of it, for example ` for S<'a>`.
|
LL - impl<'a> MethodType for MTFn {
LL + impl<> MethodType for MTFn {
|

error: aborting due to previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait,
|
LL | impl<'a, I> UnwrapItemsExt for I {
| ^^ unconstrained lifetime parameter
|
help: Either remove the type parameter ''a' or make use of it, for example ` for S<'a>`.
|
LL - impl<'a, I> UnwrapItemsExt for I {
LL + impl<, I> UnwrapItemsExt for I {
|

error: aborting due to previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self
|
LL | impl<T> X for () {
| ^ unconstrained type parameter
|
help: Either remove the type parameter 'T' or make use of it, for example ` for S<T>`.
|
LL - impl<T> X for () {
LL + impl<> X for () {
|

error: aborting due to previous error

Expand Down
6 changes: 6 additions & 0 deletions tests/ui/type-alias-impl-trait/issue-74244.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self
|
LL | impl<T> Allocator for DefaultAllocator {
| ^ unconstrained type parameter
|
help: Either remove the type parameter 'T' or make use of it, for example ` for S<T>`.
|
LL - impl<T> Allocator for DefaultAllocator {
LL + impl<> Allocator for DefaultAllocator {
|

error: aborting due to previous error

Expand Down
Loading