Skip to content

super let with &mut [T; 0] in const has inconsistent borrow-checking behavior #142766

Open
@theemathas

Description

@theemathas

It is possible to create a &mut reference pointing to specifically an empty array in const. See #140126.

In the following code, the constants TUPLE, REBORROW, and INFER are slight variations of the constant FAILS.

#![feature(super_let)]

type Ref = &'static mut [i32; 0];

const fn make() -> Ref {
    &mut []
}

const FAILS: [Ref; 2] = {
    super let x: Ref = make();
    [x, x]
};

const TUPLE: (Ref, Ref) = {
    super let x: Ref = make();
    (x, x)
};

const REBORROW: [Ref; 2] = {
    super let x: Ref = make();
    [x, &mut *x]
};

const INFER: [Ref; 2] = {
    // Don't annotate as 'static
    super let x: &mut [i32; 0] = make();
    [x, x]
};

I expected all four constants to either all compile or all produce an error. Instead, the FAILS constant produces an error, but the other three compile fine.

error[E0505]: cannot move out of `x` because it is borrowed
  --> src/lib.rs:11:9
   |
10 |     super let x: Ref = make();
   |               - binding `x` declared here
11 |     [x, x]
   |     ----^-
   |     ||  |
   |     ||  move out of `x` occurs here
   |     |borrow of `*x` occurs here
   |     using this value as a constant requires that `*x` is borrowed for `'static`

For more information about this error, try `rustc --explain E0505`.
error: could not compile `playground` (lib) due to 1 previous error

Maybe related to #142607.

Meta

Reproducible on the playground with version 1.89.0-nightly (2025-06-11 e703dff8fe220b78195c)

@rustbot labels +F-super_let +A-const-eval +A-borrow-checker

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerA-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-bugCategory: This is a bug.F-super_letit's super, let's go!needs-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