Skip to content

bytes!() is now hard to use in a lot of cases due to lifetime issues #11641

Closed
@lilyball

Description

@lilyball

With #3511 landed, the lifetime of bytes!() is now problematic. Specifically, the following used to work and now fails:

let b = match true {
    true => bytes!("test"),
    false => unreachable!()
};

The lifetime of bytes!("test") is now limited to that branch of the match, so it doesn't live long enough to be assigned to b.

I'm not sure if this means lifetimes need to change. The other option is that bytes!() needs to return a &'static [u8]. Unfortunately, the only way to do that right now is to make it equivalent to {static b: &'static [u8] = &[...]; b }, which makes it no longer usable as the value of a constant (e.g. static foo: &'static [u8] = bytes!("foo")).

I'm thinking the right solution is to make it legal for me to say &'static [1u8]. I can type that right now, but the 'static lifetime seems to be wholly ignored, as I get the exact same error with the following as I do with the bytes!() example above:

let b = match true {
    true => &'static [1u8],
    false => unreachable!()
};

If &'static [1u8] becomes a valid expression with the 'static lifetime, then bytes!() could start evaluating to that and everything should work as expected. Furthermore, it would be nice (but perhaps not required) if &[1u8], where all elements are compile-time constants, would implicitly be given the 'static lifetime.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions