Skip to content

Constant promotion doesn't work in certain situations #153129

@abacabadabacaba

Description

@abacabadabacaba

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 859951e).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-constant-promotionArea: constant promotionA-temporary-lifetime-extensionArea: temporary lifetime extensionC-discussionCategory: Discussion or questions that doesn't represent real issues.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions