Skip to content

&raw const can sometimes do temporary lifetime extension #145813

@theemathas

Description

@theemathas

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

struct LoudDrop;

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

fn main() {
    let _ptr = &raw const Box::new((LoudDrop,)).0;
    println!("after let");
}

This code compiles, and prints after let, then prints drop. This contradicts the following comment in the compiler, which says that the above code creates an immediately-dangling pointer:

// This is maybe too permissive, since it allows
// `let u = &raw const Box::new((1,)).0`, which creates an
// immediately dangling raw pointer.

@rustbot labels +T-lang +T-compiler +A-raw-pointers

Meta

Reproducible on the playground with version 1.91.0-nightly (2025-08-23 69b76df90c7ea63b5350)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-raw-pointersArea: raw pointers, MaybeUninit, NonNullA-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