rust-lang / rust-lang/rust

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

Open
#153,282 15 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-inference A-trait-system C-bug T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling the provided src/lib.rs reproducer with the stable and nightly rustc versions listed, on macOS and Linux, focusing on <*const _>::from(&self).cast::<u8>(). Trace the compiler's handling of raw-pointer type inference and From<&Foo>, then add regression coverage showing the expected compilation behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.