jam1garner / jam1garner/binrw

Issue with lifetimes and Vec (lifetime may not live long enough requires that `'bar` must outlive `'static`)

Open
#298 6 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Rust
Stars
853
Forks
56
PR merge metrics
No merged PRs in 30d

Description

Hi there,

Upon rewriting some of my `binrw`-using code with lifetimes, I stumbled upon some interesting issue with what the macro generates about `Vec` and lifetimes, mainly that they seem incompatible, while the same struct without the `Vec` seems to compile fine.
I managed to make a minimal reproduction of the issue.

So considering a struct `Zoo<'bar>`, defined as such:
```rust
#[binrw]
struct Zoo<'bar>(std::marker::PhantomData<&'bar ()>);
```

This snippet compiles fine, even with the `'bar` lifetime.
```rust
#[binrw]
struct Foo<'bar> {
zoo: Zoo<'bar>,
}
```

But this one fails.
```rust
#[binrw]
struct Foo<'bar> {
#[br(count = 1)]
zoos: Vec>,
}
```
Here is the compiling error:
```
error: lifetime may not live long enough
|
249 | #[binrw]
| ^^^^^^^^ requires that `'bar` must outlive `'static`
250 | struct Foo<'bar> {
| ---- lifetime `'bar` defined here
|
= note: this error originates in the attribute macro `binrw` (in Nightly builds, run with -Z macro-backtrace for more info)
```

I started investigating the macro expansion, but unfortunately was unable to pinpoint an exact cause for this.

Let me know if you need any more context or help !
Thanks =)

Contributor guide

Open the contributing guide

Research direction

Start by expanding the #[binrw] attribute macro for the two minimal examples, comparing the direct Zoo<'bar> field with Vec> using #[br(count = 1)]. Trace where the generated code introduces the 'static requirement, then confirm the fix by compiling both reproductions without requiring 'bar to outlive 'static.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.