Skip to content

Lifetime extension in consts/statics sometimes makes *less* code compile #143129

Open
@RalfJung

Description

@RalfJung

@traviscross found this fun example:

const C1: &[u8] = { let x: &'static mut [u8] = &mut []; x }; //~ OK
const C2: &[u8] = { &mut [] }; //~ ERROR

C1 compiles because empty arrays get promoted even behind mutable references.

C2 does not compile because that borrow is in lifetime extension position, and then the const-checks reject it due to being a non-transient mutable borrow. If we didn't do lifetime extension here, then the code would work, since then const-checks would consider the borrow to be transient, and finally promotion would make the rest work.

Not sure if this is worth fixing, but it's definitely odd. If we did want to fix it, we would have to either

  • ensure that const-checks accept non-transient borrows that will later get promoted (duplicating the same logic... ugh)
  • or run promotion before const-checks (which seems to be the opposite of what @oli-obk has planned)

@rust-lang/wg-const-eval

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language teamneeds-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions