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
When using cast() to cast to pointer to type of unknown size the compiler fails due to the target type being constrained as Sized, but casting with as works fine.
/// A newtype with alignment of at least `A` bytes#[repr(C)]pubstructAligned<A,T>whereT: ?Sized,{_alignment:[A;0],value:T,}fnfoo(ptr:*mut[u8]) -> *mutAligned<u16,[u8]>{
ptr as*mutAligned<u16,[u8]>// Compiles}fnbar(ptr:*mut[u8]) -> *mutAligned<u16,[u8]>{
ptr.cast()// Fails with: the size for values of type `[u8]` cannot be known at compilation time}
I suggest adding ?Sized to U in cast() signatures.
This isn't currently possible (#60602 (comment)). With this change applied, building the standard library gives you this error
error[E0606]: casting `*constT` as `*constU` is invalid
--> library/core/src/ptr/const_ptr.rs:61:9
|
61 | selfas_
| ^^^^^^^^^
|
= note:vtable kinds may not match
Casting works fine with as when not generic as the compiler knows how to construct the fat pointer
clubby789
added
C-discussion
Category: Discussion or questions that doesn't represent real issues.
and removed
C-bug
Category: This is a bug.
needs-triage
This issue may need triage. Remove it if it has been sufficiently triaged.
labels
Jan 17, 2024
Edit: This hack only supports thin->thin and fat->fat casts, but not fat->thin casts. Since .cast() can already do fat->thin casts, we can't replace it like this.
When using
cast()
to cast to pointer to type of unknown size the compiler fails due to the target type being constrained asSized
, but casting withas
works fine.I suggest adding
?Sized
toU
incast()
signatures.Meta
Tested with 1.75., 1.76.0-beta.5, 1.77.0-nightly in Rust Playground
The text was updated successfully, but these errors were encountered: