Skip to content

Instantly share code, notes, and snippets.

@little-dude
Created February 1, 2022 09:21
Show Gist options
  • Save little-dude/2b309f2b683d2713e3ea6d37deb3a627 to your computer and use it in GitHub Desktop.
Save little-dude/2b309f2b683d2713e3ea6d37deb3a627 to your computer and use it in GitHub Desktop.
// This runs into https://github.com/rust-lang/rust/issues/56105
pub trait MyTrait<T> {
fn method(&self, _:T);
}
impl<T> MyTrait<T> for Box<dyn MyTrait<T>> {
fn method(&self, t: T) {
(**self).method(t)
}
}
// Why does this conflict with the impl above?
impl<'a, T> MyTrait<&'a T> for Box<dyn for<'t> MyTrait<&'t T>> {
fn method(&self, t: &'a T) {
(**self).method(t)
}
}
// For the sake of the example, define
struct Foo;
impl<'t> MyTrait<&'t u32> for Foo {
fn method(&self, _: &'t u32) {}
}
fn func<T>(_: T)
where
T: for<'t> MyTrait<&'t u32>,
{
}
fn main() {
let foo = Foo;
func(foo);
let boxed_foo: Box<dyn for<'t> MyTrait<&'t u32>> = Box::new(Foo);
func(boxed_foo);
}
@little-dude
Copy link
Author

   Compiling playground v0.0.1 (/playground)
warning: conflicting implementations of trait `MyTrait<&_>` for type `std::boxed::Box<(dyn MyTrait<&_> + 'static)>`
  --> src/main.rs:12:1
   |
5  | impl<T> MyTrait<T> for Box<dyn MyTrait<T>> {
   | ------------------------------------------ first implementation here
...
12 | impl<'a, T> MyTrait<&'a T> for Box<dyn for<'t> MyTrait<&'t T>> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `std::boxed::Box<(dyn MyTrait<&_> + 'static)>`
   |
   = note: `#[warn(coherence_leak_check)]` on by default
   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
   = note: for more information, see issue #56105 <https://github.com/rust-lang/rust/issues/56105>
   = note: this behavior recently changed as a result of a bug fix; see rust-lang/rust#56105 for details

warning: `playground` (bin "playground") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.97s
     Running `target/debug/playground`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment