-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider relaxing the Sized
requirement in impl Error for Box<T>
#104485
Comments
@rustbot label +T-libs-api |
I may be wrong, but I think this is a known issue that can't be fixed because it would break backwards compatibility. But if I'm wrong will other people please chime in. (It's much quicker to get an answer on the internet by posting something incorrect than by asking a question 😛) |
@derekdreery I came across this comment while working on #113464 which supports your backwards compatibility claim: https://github.com/rust-lang/rust/blob/master/library/core/src/error.rs#L409
I think I just realized the issue here -- it's the downstream usages of Box (ie contexts receiving and using Box such as an error-reporting library like anyhow that rely on it having a size known at compile time) that potentially rely on the Sized bound which would be broken if it's removed, not the Box itself. |
I believe this is because It might still be acceptable to relax the bounds though, if a crater run shows no breakage. |
Okay I just tried this out:
But when I try compiling I get
So Is this happening because of monomorphization? At compile time because one possible known implementation of
And this would also be monomorphed from Is there a good way to work around this? Add a |
As for today the following blanket implementation exists:
rust/library/alloc/src/boxed.rs
Lines 2443 to 2458 in e702534
The problem with this is that
T
is required to beSized
, so, for example,Box<dyn Error>
doesn't implementError
. It looks like a straightforward and intuitive assumption thatBox<dyn Error>
should implementError
trait, but it doesn't.Going even further, there is an inconsistency between the blanket impls for
Arc
as well, because there exists an analogous impl forArc
that has relaxedSized?
requirement:rust/library/alloc/src/sync.rs
Lines 2769 to 2788 in e702534
Use Case
The use case where I stumbled with this problem is where I wanted to put
Box<dyn Error>
as asource
error inthiserror
error enum, but I couldn't because that type doesn't implement Error trait.Workarounds
The workaround for this problem is to use
Arc<T>
instead ofBox<T>
when an Error impl is required forSized?
type.The text was updated successfully, but these errors were encountered: