Skip to content

In consts, values with destructors can be lifetime-extended into statics. #145868

@theemathas

Description

@theemathas

I'm not sure if this is a bug or not.

struct Thing;

impl Drop for Thing {
    fn drop(&mut self) {
        println!("drop");
    }
}

const X: &Thing = &Thing;

fn main() {
    let _a = X;
}

The above code prints nothing. It seems that the Thing value is lifetime-extended into an implicit static.

I believe that the design intention of const is that replacing a const with its definition should not change its behavior. However, in the above code, replacing X with &Thing causes it to print drop. Therefore, I think this code should probably produce a compile error.

Meta

Reproducible on the playground with version 1.91.0-nightly (2025-08-25 54c581243c977c7662c9)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-temporary-lifetime-extensionArea: temporary lifetime extensionC-bugCategory: This is a bug.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