Skip to content

PhantomData not behaving as advertised #22579

@dylanede

Description

@dylanede

As far as I can tell, from the point of view of lifetimes, PhantomData<T> should behave identically to T. This is unfortunately not the case in practice.

This function causes a mismatched type error on x:

fn cast_phantom<'a, 'b, T>(x: PhantomData<&'a T>) -> PhantomData<&'b T> where 'a : 'b {
    x
}

Whereas this similar function does not:

fn cast_actual<'a, 'b, T>(x: &'a T) -> &'b T where 'a : 'b {
    x
}

The variance rules of lifetimes should make both functions valid.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions