Confusing rustc error message on generic struct with misleading suggestion
- Dominant language
- Rust
- Stars
- 853
- Forks
- 56
- PR merge metrics
- No merged PRs in 30d
Description
I have the following:
```rust
#[derive(Debug, BinRead)]
pub struct Array
where
T: for<'a> BinRead = ()>,
{
_count: u32,
#[br(count = _count)]
pub data: Vec,
}
```
For this, rustc complains about a missing lifetime bound on `T`, but does so in an _interesting_ manner (see expando at the end for the full message):

The correct solution here is to either add a `'static` bound to `T` (if we're fine with disallowing a borrowed T), or to add a lifetime bound to the struct (and a PhantomData). Perhaps that could be made clearer.
Full message
```
error[E0310]: the parameter type `T` may not live long enough
--> src\base_binrw.rs:159:17
|
159 | #[derive(Debug, BinRead)]
| ^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
|
= note: this error originates in the derive macro `BinRead` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider adding an explicit lifetime bound...
|
162 | T: for<'a> BinRead = ()> + 'static,
| +++++++++
error[E0311]: the parameter type `T` may not live long enough
--> src\base_binrw.rs:159:17
|
159 | #[derive(Debug, BinRead)]
| ^^^^^^^
|
note: the parameter type `T` must be valid for the anonymous lifetime defined here...
--> src\base_binrw.rs:159:17
|
159 | #[derive(Debug, BinRead)]
| ^^^^^^^
note: ...so that the type `T` will meet its required lifetime bounds
--> src\base_binrw.rs:159:17
|
159 | #[derive(Debug, BinRead)]
| ^^^^^^^
= note: this error originates in the derive macro `BinRead` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider adding an explicit lifetime bound...
|
159 ~ #[derive(Debug, 'a, BinRead)]
160 | pub struct Array
161 | where
162 ~ T: for<'a> BinRead = ()> + 'a,
|
```
Contributor guide
Research direction
Start with the generic Array example and the full rustc diagnostics reported at src\base_binrw.rs:159, focusing on the BinRead derive macro. Compare the suggested 'static and lifetime-bound fixes, then define wording that makes the applicable choice clear. Done means the diagnostic no longer presents a misleading suggestion for this case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100