rust-lang / rust-lang/rust

Conflicting implementations with `fn_traits` and `From`

Open
#142,037 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-trait-system C-bug F-unboxed_closures T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

#![feature(unboxed_closures)]
#![feature(fn_traits)]
#![allow(unused)]
use std::rc::Rc;

struct MyFn {
    cb: Rc<dyn Fn()>,
}

impl FnOnce<()> for MyFn {
    type Output = ();

    extern "rust-call" fn call_once(self, _args: ()) -> Self::Output {
        (self.cb)()
    }
}

impl FnMut<()> for MyFn {
    extern "rust-call" fn call_mut(&mut self, _args: ()) {
        (self.cb)()
    }
}

// // Uncomment this to see the conflict From<F> implementation makes
// impl Fn<()> for MyFn {
//     extern "rust-call" fn call(&self, _args: ()) {
//         (self.cb)()
//     }
// }

// When MyFn implements Fn<()>
// From<F> will also 
// impl From<MyFn> for MyFn {}
impl<F> From<F> for MyFn
where
    F: Fn() + 'static, 
{
    fn from(f: F) -> Self {
        MyFn {
            cb: Rc::new(f),
        }
    }
}

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 minimal reproducer in the issue with and without the Fn implementation, then inspect the compiler's conflicting-implementation diagnostic. Determine the intended coherence behavior for the Fn and From implementations; done means the issue's expected behavior is established and covered by an appropriate compiler 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
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.