Open
Description
@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
Labels
Area: Constant evaluation, covers all const contexts (static, const fn, ...)Area: Lifetimes / regionsCategory: This is a bug.Relevant to the compiler team, which will review and decide on the PR/issue.Relevant to the language teamThis issue may need triage. Remove it if it has been sufficiently triaged.