rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
96.89k stars 12.52k forks source link

Confusing error message when using `min_specialization` with `dyn Trait` #130102

Open oskgo opened 1 week ago

oskgo commented 1 week ago

I tried this code:

#![feature(min_specialization)]
pub trait Foo {
    fn foo(&self);
}

impl<T> Foo for T {
    default fn foo(&self) {}
}

impl Foo for Box<dyn Foo> {
    fn foo(&self) {}
}

This failed to compile with

error: cannot specialize on `'static` lifetime
  --> src/lib.rs:15:1
   |
15 | impl Foo for Box<dyn Foo> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^

This seems weird since there isn't a visible lifetime anywhere. The error should explain that the 'static lifetime is implicit on dyn Foo and suggest adding a lifetime parameter.

ChayimFriedman2 commented 1 week ago

You can definitely get a Box<dyn Foo> with an incompatible lifetime: Box<dyn Foo + 'a>.

oskgo commented 1 week ago

Using that instead makes it compile. Guess this is a diagnostics issue, not a bug then.