Skip to content

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

@lowlevl

Description

@lowlevl

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:

#[binrw]
struct Zoo<'bar>(std::marker::PhantomData<&'bar ()>);

This snippet compiles fine, even with the 'bar lifetime.

#[binrw]
struct Foo<'bar> { 
    zoo: Zoo<'bar>,
}

But this one fails.

#[binrw]
struct Foo<'bar> {
    #[br(count = 1)]
    zoos: Vec<Zoo<'bar>>,
}

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 =)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions