Skip to content

Raw pointer casts in enum Ord impl can cause type inference failures #36

@sfackler

Description

@sfackler

If a trait implementation like the following is defined in a crate's dependency graph, educe-generated Ord implementations will fail to compile:

struct PoisonTypeInference;

impl From<&PoisonTypeInference> for *const () {
    fn from(value: &PoisonTypeInference) -> Self {
        value as *const PoisonTypeInference as *const ()
    }
}
error[E0277]: the trait bound `*const _: From<&Enum2>` is not satisfied
  --> tests/ord_enum.rs:15:29
   |
15 |     #[derive(PartialEq, Eq, Educe)]
   |                             ^^^^^ the trait `From<&Enum2>` is not implemented for `*const _`
   |
   = note: this error originates in the derive macro `Educe` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0282]: type annotations needed
  --> tests/ord_enum.rs:15:29
   |
15 |     #[derive(PartialEq, Eq, Educe)]
   |                             ^^^^^
   |                             |
   |                             cannot infer type
   |                             cannot call a method on a raw pointer with an unknown pointee type
   |
   = note: this error originates in the derive macro `Educe` (in Nightly builds, run with -Z macro-backtrace for more info)

The failures come from these casts:

::core::cmp::Ord::cmp(&*<*const _>::from(self).cast::<#discriminant_type>(), &*<*const _>::from(other).cast::<#discriminant_type>())

::core::cmp::Ord::cmp(&*<*const _>::from(self).cast::<#discriminant_type>(), &*<*const _>::from(other).cast::<#discriminant_type>())

I think it should be fixable by explicitly specifying *const Self instead of *const _.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions