Open
Description
This comes from the user forum, playground, moved from #27964:
use std::error::Error;
fn library_function<F1, E1>(f1: F1) -> Result<(), E1>
where
F1: FnOnce() -> Result<(), E1>,
E1: Error, // <-remove this line -> compile
{
f1()
}
fn main() -> Result<(), Box<dyn Error>>
{
library_function( || Ok(()) )
}
Error:
error[E0277]: the size for values of type `dyn std::error::Error` cannot be known at compilation time
--> src/main.rs:13:5
|
3 | fn library_function<F1, E1>(f1: F1) -> Result<(), E1>
| ----------------
...
6 | E1: Error, // <-remove this line -> compile
| ----- required by this bound in `library_function`
...
13 | library_function( || Ok(()) )
| ^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `dyn std::error::Error`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= note: required because of the requirements on the impl of `std::error::Error` for `std::boxed::Box<dyn std::error::Error>`
Go figure why "library_function doesn't have a size known at compile time".
Analysis:
There is a blanket impl for Error
on Box<T>
, so quickly looking it up in the docs makes you think that it should implement Error, but it's not on Box<T: ?Sized>
. credit to @yandros
I feel the main issue here is that we have a trait bound on Error that isn't satisfied, so probably it would be nice to replace library_function
not being sized by: Box<dyn Error> does not implement Error. The following Impls where found: impl<T: Error> Error for Box<T>, but that requires T: Sized
.