`Box<dyn Error>` doesn't implement `Error`

I was very surprised to see that Box<dyn Error> does not implement Error because of a missing ?Sized bound. In the standard library we've got this:

#[stable(feature = "box_error", since = "1.8.0")]
impl<T: Error> Error for Box<T> {
    #[allow(deprecated, deprecated_in_future)]
    fn description(&self) -> &str {
        Error::description(&**self)
    }

    #[allow(deprecated)]
    fn cause(&self) -> Option<&dyn Error> {
        Error::cause(&**self)
    }

    fn source(&self) -> Option<&(dyn Error + 'static)> {
        Error::source(&**self)
    }
}

Given that Box can hold ?Sized types, I'd expect it to be:

impl<T: Error + ?Sized> Error for Box<T> {
    #[allow(deprecated, deprecated_in_future)]
    fn description(&self) -> &str {
        Error::description(&**self)
    }

    #[allow(deprecated)]
    fn cause(&self) -> Option<&dyn Error> {
        Error::cause(&**self)
    }

    fn source(&self) -> Option<&(dyn Error + 'static)> {
        Error::source(&**self)
    }
}

Would this change be breaking?

Nevermind, I was pointed out that this causes a coherence error between Box<dyn Error>: From<Box<dyn Error>> vs. Box<dyn Error>: From<impl Error+Sized>.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.