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

Parameter considered not used in recursive types #105740

Closed
Patryk27 opened this issue Dec 15, 2022 · 0 comments · Fixed by #127871
Closed

Parameter considered not used in recursive types #105740

Patryk27 opened this issue Dec 15, 2022 · 0 comments · Fixed by #127871
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Patryk27
Copy link
Contributor

Patryk27 commented Dec 15, 2022

Given the following code:

struct Foo<T> {
    val: Box<Foo<T>>,
}

(playground)

... the current output is:

error[E0392]: parameter `T` is never used
 --> src/lib.rs:1:12
  |
1 | struct Foo<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

... and it's a bit misleading, since one could argue that the parameter is actually used - there's Box<Foo<T>>, after all! 😇

Maybe something like this would work?

error[E0392]: parameter `T` is never used
 --> src/lib.rs:1:12
  |
1 | struct Foo<T> {
  |            ^ unused parameter
  |
note: `T` is used here, but it needs to be used outside of `Foo<...>` to determine its variance
  |
2 |     val: Box<Foo<T>>,
  |              ------
  |
help: use `PhantomData`
  |
+ |     _marker: PhantomData<T>,
  |     ++++++++++++++++++++++++

(note that generating _marker: ... wouldn't work for enums, though)

We could also "prove" that the parameter is not actually used by pretty-printing a Self-substituted version of the offending type, i.e.:

note: if you replace `Foo<T>` with `Self`, you can see that `T` is not actually used
  |     struct Foo<T> {
  |         val: Box<Self>,
  |                  ----
  |     }

... but I'm not sure if that's worth it 👀

@Patryk27 Patryk27 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 15, 2022
@bors bors closed this as completed in 65de5d0 Jul 19, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jul 19, 2024
Rollup merge of rust-lang#127871 - compiler-errors:recursive, r=estebank

Mention that type parameters are used recursively on bivariance error

Right now when a type parameter is used recursively, even with indirection (so it has a finite size) we say that the type parameter is unused:

```
struct B<T>(Box<B<T>>);
```

This is confusing, because the type parameter is *used*, it just doesn't have its variance constrained. This PR tweaks that message to mention that it must be used *non-recursively*.

Not sure if we should actually mention "variance" here, but also I'd somewhat prefer we don't keep the power users in the dark w.r.t the real underlying issue, which is that the variance isn't constrained. That technical detail is reserved for a note, though.

cc `@fee1-dead`

Fixes rust-lang#118976
Fixes rust-lang#26283
Fixes rust-lang#53191
Fixes rust-lang#105740
Fixes rust-lang#110466
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant