rust-lang / rust-lang/rust

Trait bound From<fn()> is not satisfied even with a correct value

Open
#142,513 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics C-bug T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

fn main() {
    UnsafeValue::from(foo);
}

fn foo(_: &Context) -> Result<(), Box<dyn core::error::Error>> {
    Ok(())
}

struct UnsafeValue;

impl From<fn(&Context) -> Result<(), Box<dyn core::error::Error>>> for UnsafeValue {
    fn from(_: fn(&Context) -> Result<(), Box<dyn core::error::Error>>) -> Self {
        Self
    }
}

struct Context {}

I expected to see this happen: The code compile.

Instead, this happened:

error[E0277]: the trait bound `UnsafeValue: From<for<'a> fn(&'a Context) -> Result<(), Box<(dyn std::error::Error + 'static)>> {foo}>` is not satisfied
 --> src/main.rs:2:5
  |
2 |     UnsafeValue::from(foo);
  |     ^^^^^^^^^^^ the trait `From<for<'a> fn(&'a Context) -> Result<(), Box<(dyn std::error::Error + 'static)>> {foo}>` is not implemented for `UnsafeValue`
  |
  = help: the trait `From<for<'a> fn(&'a Context) -> Result<_, _> {foo}>` is not implemented for `UnsafeValue`
          but trait `From<for<'a> fn(&'a Context) -> Result<_, _>>` is implemented for it
  = help: for that trait implementation, expected `for<'a> fn(&'a Context) -> Result<(), Box<(dyn std::error::Error + 'static)>>`, found `for<'a> fn(&'a Context) -> Result<(), Box<(dyn std::error::Error + 'static)>> {foo}`

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

rustc --version --verbose:

rustc 1.87.0 (17067e9ac 2025-05-09)
binary: rustc
commit-hash: 17067e9ac6d7ecb70e50f92c1944e545188d2359
commit-date: 2025-05-09
host: x86_64-unknown-linux-gnu
release: 1.87.0
LLVM version: 20.1.1

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 with the minimal reproduction in src/main.rs and compile it using the reported rustc 1.87.0 environment. Read the E0277 explanation and trace how the compiler compares the function item in foo with the declared From function-pointer implementation. Done means the behavior is either corrected or clearly documented with a focused regression test.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.