Skip to content

PhantomData behavior is very surprising w.r.t. lack of Drop implications #102810

Closed
@SoniEx2

Description

@SoniEx2

I tried this code:

use core::cell::Cell;
use core::marker::PhantomData as PDOrBox;
//use std::boxed::Box as PDOrBox;
struct Foo<'a> {
  selfref: Cell<Option<&'a Foo<'a>>>,
}
impl<'a> Drop for Foo<'a> {
  fn drop(&mut self) {
  }
}
fn make_selfref<'a>(x: &'a PDOrBox<Foo<'a>>) {}
fn make_pdorbox<'a>() -> PDOrBox<Foo<'a>> {
    unimplemented!()
}
fn main() {
  let x = make_pdorbox();
  make_selfref(&x);
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=14dee41511095d6e940069db08cdc23f

I expected to see this happen: since a PhantomData claims to act like a T including for the purposes of borrowck/dropck then this should absolutely compile-fail. just as it does if you use a Box, or T directly!

Instead, this happened: it doesn't!

this is clearly unsound.

Meta

rustc --version --verbose:

playground
Backtrace

<backtrace>

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.T-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions