Constant promotion doesn't work in certain situations
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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