rust-lang / rust-lang/rust

Constant promotion doesn't work in certain situations

Open
#153,129 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-const-eval A-constant-promotion A-temporary-lifetime-extension C-discussion T-compiler T-lang
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

This code:

const C: *const *const i32 = [
    &{
        let x: i32 = 0;
        x
    } as *const i32,
]
.as_ptr();

fails to build with an error: error: encountered dangling pointer in final value of constant.

My belief is that the subexpression { let x: i32 = 0; x } should be promoted to a constant. It works if the final .as_ptr() is removed:

const C: [*const i32; 1] = [
    &{
        let x: i32 = 0;
        x
    } as *const i32,
];

If the inner subexpression is replaced with zero, it works as well:

const C: *const *const i32 = [
    &{
        0
    } as *const i32,
]
.as_ptr();

Tested in Rust 1.95.0-nightly (2026-02-24 859951e3c7c9d0322c39).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository file or test is named. Reproduce the reported snippets on the stated nightly, then trace Rust's constant-promotion handling and the dangling-pointer final-value diagnostic. Done means the first example is accepted consistently with the working variants, with regression coverage for the reported case.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.