Skip to content

Vec's const_make_global method erroring when Vec is empty #153158

@izagawd

Description

@izagawd

I tried this code:

#![feature(const_heap)]
fn main() {
    const {
        let mut inner_vec = Vec::<i32>::new();
        let inner_slice = inner_vec.const_make_global();
    }
}

I expected const_make_global() to just return an empty 'static slice

Instead, it gave me this error:

error[E0080]: pointer not dereferenceable: pointer must point to some allocation, but got 0x4[noalloc] which is a dangling pointer (it has no provenance)
   --> src\main.rs:6:27
    |
  6 |         let inner_slice = inner_vec.const_make_global();
    |                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `main::{constant#0}` failed inside this call
    |
note: inside `Vec::<i32>::const_make_global`
   --> C:\Users\theiz\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\alloc\src\vec\mod.rs:897:18
    |
897 |         unsafe { core::intrinsics::const_make_global(self.as_mut_ptr().cast()) };
    |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the failure occurred here

But this code:

#![feature(const_heap)]
#![feature(const_trait_impl)]
fn main() {
    const {
        let mut inner_vec = Vec::<i32>::new();
        inner_vec.push(4);
        let inner_slice = inner_vec.const_make_global();
    }
}

works just fine

Meta

rustc --version --verbose:

rustc 1.95.0-nightly (859951e3c 2026-02-24)
binary: rustc
commit-hash: 859951e3c7c9d0322c39bad49221937455bdffcd
commit-date: 2026-02-24
host: x86_64-pc-windows-msvc
release: 1.95.0-nightly
LLVM version: 22.1.0

I am assuming it was because the Vec was empty

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-collectionsArea: `std::collections`C-bugCategory: This is a bug.F-const_heap`#[feature(const_heap)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions