Skip to content

E0277: the trait bound *const _: From<&Foo> is not satisfied #153282

@tyilo

Description

@tyilo

I tried this code:

[package]
name = "foo"
version = "0.1.0"
edition = "2024"

[dependencies]
security-framework-sys = { version = "=2.17.0", default-features = false }
#[allow(unused)]
use security_framework_sys;

#[repr(u8)]
pub enum Foo {
    A,
    B,
    C,
}

impl Foo {
    pub fn discriminant(self) -> u8 {
        // SAFETY: Because `Foo` is marked `repr(u8)`, its layout is a `repr(C)`
        // `union` between `repr(C)` structs, each of which has the `u8`
        // discriminant as its first field, so we can read the discriminant
        // without offsetting the pointer.
        unsafe { *<*const _>::from(&self).cast::<u8>() }
    }
}

I expected to see this happen: The code compiles.

Instead, this happened: The fails to compile with the following error:

error[E0277]: the trait bound `*const _: From<&Foo>` is not satisfied
  --> src/lib.rs:17:20
   |
17 |         unsafe { *<*const _>::from(&self).cast::<u8>() }
   |                    ^^^^^^^^ the trait `From<&Foo>` is not implemented for `*const _`

error[E0282]: type annotations needed
  --> src/lib.rs:17:19
   |
17 |         unsafe { *<*const _>::from(&self).cast::<u8>() }
   |                   ^^^^^^^^^^^^^^^^^^^^^^^ ---- cannot call a method on a raw pointer with an unknown pointee type
   |                   |
   |                   cannot infer type

Some errors have detailed explanations: E0277, E0282.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `aaa` (lib) due to 2 previous errors

On nightly I only get the first error:

error[E0277]: the trait bound `*const _: From<&Foo>` is not satisfied
  --> src/lib.rs:17:20
   |
17 |         unsafe { *<*const _>::from(&self).cast::<u8>() }
   |                    ^^^^^^^^ the trait `From<&Foo>` is not implemented for `*const _`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `aaa` (lib) due to 1 previous error

Meta

rustc --version --verbose:

rustc 1.93.1 (01f6ddf75 2026-02-11)
binary: rustc
commit-hash: 01f6ddf7588f42ae2d7eb0a2f21d44e8e96674cf
commit-date: 2026-02-11
host: aarch64-apple-darwin
release: 1.93.1
LLVM version: 21.1.8

rustc +nightly --version --verbose

rustc 1.96.0-nightly (80381278a 2026-03-01)
binary: rustc
commit-hash: 80381278a08582356c13b0f52af92d27c567c230
commit-date: 2026-03-01
host: aarch64-apple-darwin
release: 1.96.0-nightly
LLVM version: 22.1.0

I can also reproduce it on Linux:
rustc --version --verbose:

rustc 1.93.1 (01f6ddf75 2026-02-11)
binary: rustc
commit-hash: 01f6ddf7588f42ae2d7eb0a2f21d44e8e96674cf
commit-date: 2026-02-11
host: x86_64-unknown-linux-gnu
release: 1.93.1
LLVM version: 21.1.8

rustc +nightly --version --verbose

rustc 1.96.0-nightly (80381278a 2026-03-01)
binary: rustc
commit-hash: 80381278a08582356c13b0f52af92d27c567c230
commit-date: 2026-03-01
host: x86_64-unknown-linux-gnu
release: 1.96.0-nightly
LLVM version: 22.1.0

I originally discovered this in a project that uses reqwest. The relevant code have nothing to do with reqwest and the compile error only happened on macOS, not on Linux.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceA-trait-systemArea: Trait systemC-bugCategory: This is a bug.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    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