Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
sfackler opened this issue Feb 7, 2025 · 0 comments · May be fixed by #37
Open

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

sfackler opened this issue Feb 7, 2025 · 0 comments · May be fixed by #37

Comments

@sfackler
Copy link

sfackler commented Feb 7, 2025

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 _.

@sfackler sfackler linked a pull request Feb 7, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant