You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#![feature(coerce_unsized)]use std::ops::CoerceUnsized;use std::any::Any;fnbox_to_any<T:Any + ?Sized>(x:Box<T>) -> Box<dynAny>whereT:CoerceUnsized<dynAny>,{
x asBox<dynAny>}
with:
error[E0277]: the size for values of type T cannot be known at compilation time
note: required for the cast from Box<T> to Box<(dyn Any + 'static)>
Dyn upcasting adds support for upcasting trait objects dyn A where A: Any to dyn Any, but not just any type (e.g. str cannot be cast to dyn Any).
For whatever reason, the bound T: Any + ?Sized is not enough to support the cast: [i32] implements Any (strange) yet cannot be cast to dyn Any (if it could this would break memory safety since the impl of Any returns a single TypeId for all [i32] regardless of length).
The additional T: CoerceUnsized<dyn Any>looks like it should be enough to satisfy the coercion in the method, yet it is not. It should be?
I guess this should be categorised as a bug in #18598, but it could be a bug in #65991 or even a feature request.
The text was updated successfully, but these errors were encountered:
The correct bound is T: Unsize<dyn Any>. T: CoerceUnsized<U> does not imply Box<T>: CoerceUnsized<Box<U>>, and Box<T>: CoerceUnsized<Box<U>> is implemented only if T: Unsize<U>.
saethlin
added
C-discussion
Category: Discussion or questions that doesn't represent real issues.
and removed
needs-triage
This issue may need triage. Remove it if it has been sufficiently triaged.
labels
Dec 29, 2023
This fails:
with:
Dyn upcasting adds support for upcasting trait objects
dyn A
whereA: Any
todyn Any
, but not just any type (e.g.str
cannot be cast todyn Any
).For whatever reason, the bound
T: Any + ?Sized
is not enough to support the cast:[i32]
implementsAny
(strange) yet cannot be cast todyn Any
(if it could this would break memory safety since the impl ofAny
returns a singleTypeId
for all[i32]
regardless of length).The additional
T: CoerceUnsized<dyn Any>
looks like it should be enough to satisfy the coercion in the method, yet it is not. It should be?I guess this should be categorised as a bug in #18598, but it could be a bug in #65991 or even a feature request.
The text was updated successfully, but these errors were encountered: