rust-lang / rust-lang/rust

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

Open
#145,868 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-const-eval A-temporary-lifetime-extension C-bug T-compiler T-lang
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

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)

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

Reproduce the example in the Rust Playground using the reported nightly version, then compare the const-based code with the inline &Thing form. Read the Rust Reference section on destructor lifetime extension into statics and determine whether the differing drop behavior should be rejected; done means the language behavior and intended diagnostic are agreed and covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.