From e09696aa1d7688052ef504a00751dc68fd1a8bb6 Mon Sep 17 00:00:00 2001 From: Jason Newcomb Date: Sat, 25 Mar 2023 18:06:35 -0400 Subject: [PATCH] Add conversion traits for unified type casts: * `Reinterpret` * `TryReinterpret` * `ReinterpretInner` * `TryReinterpretInner` --- .editorconfig | 9 + .github/workflows/rust.yml | 10 + .gitignore | 1 + Cargo.toml | 15 +- src/cast.rs | 1456 +++++++++++++++++ src/lib.rs | 7 + tests/unified_cast_alloc_tests.rs | 285 ++++ tests/unified_cast_tests.rs | 383 +++++ uitest/Cargo.toml | 15 + uitest/src/main.rs | 1 + uitest/tests/ui.rs | 8 + uitest/tests/ui/reinterpret_incompatible.rs | 24 + .../tests/ui/reinterpret_incompatible.stderr | 98 ++ uitest/tests/ui/reinterpret_inner_arc.rs | 21 + uitest/tests/ui/reinterpret_inner_arc.stderr | 252 +++ uitest/tests/ui/reinterpret_inner_arc_weak.rs | 12 + .../ui/reinterpret_inner_arc_weak.stderr | 126 ++ .../tests/ui/reinterpret_inner_atomic_ptr.rs | 12 + .../ui/reinterpret_inner_atomic_ptr.stderr | 107 ++ uitest/tests/ui/reinterpret_inner_box.rs | 20 + uitest/tests/ui/reinterpret_inner_box.stderr | 252 +++ .../tests/ui/reinterpret_inner_const_ptr.rs | 11 + .../ui/reinterpret_inner_const_ptr.stderr | 101 ++ uitest/tests/ui/reinterpret_inner_cow.rs | 27 + uitest/tests/ui/reinterpret_inner_cow.stderr | 242 +++ uitest/tests/ui/reinterpret_inner_mut.rs | 20 + uitest/tests/ui/reinterpret_inner_mut.stderr | 236 +++ uitest/tests/ui/reinterpret_inner_mut_ptr.rs | 11 + .../tests/ui/reinterpret_inner_mut_ptr.stderr | 107 ++ uitest/tests/ui/reinterpret_inner_non_null.rs | 28 + .../ui/reinterpret_inner_non_null.stderr | 236 +++ uitest/tests/ui/reinterpret_inner_option.rs | 9 + .../tests/ui/reinterpret_inner_option.stderr | 104 ++ uitest/tests/ui/reinterpret_inner_pin.rs | 10 + uitest/tests/ui/reinterpret_inner_pin.stderr | 98 ++ uitest/tests/ui/reinterpret_inner_rc.rs | 21 + uitest/tests/ui/reinterpret_inner_rc.stderr | 252 +++ uitest/tests/ui/reinterpret_inner_rc_weak.rs | 12 + .../tests/ui/reinterpret_inner_rc_weak.stderr | 126 ++ uitest/tests/ui/reinterpret_inner_ref.rs | 20 + uitest/tests/ui/reinterpret_inner_ref.stderr | 224 +++ uitest/tests/ui/reinterpret_inner_vec.rs | 12 + uitest/tests/ui/reinterpret_inner_vec.stderr | 153 ++ uitest/tests/ui/reinterpret_wrong_size.rs | 7 + uitest/tests/ui/reinterpret_wrong_size.stderr | 53 + uitest/tests/ui/try_reinterpret_inner_arc.rs | 18 + .../tests/ui/try_reinterpret_inner_arc.stderr | 180 ++ .../ui/try_reinterpret_inner_arc_weak.rs | 18 + .../ui/try_reinterpret_inner_arc_weak.stderr | 107 ++ .../ui/try_reinterpret_inner_atomic_ptr.rs | 11 + .../try_reinterpret_inner_atomic_ptr.stderr | 59 + uitest/tests/ui/try_reinterpret_inner_box.rs | 17 + .../tests/ui/try_reinterpret_inner_box.stderr | 180 ++ .../ui/try_reinterpret_inner_const_ptr.rs | 9 + .../ui/try_reinterpret_inner_const_ptr.stderr | 53 + uitest/tests/ui/try_reinterpret_inner_cow.rs | 18 + .../tests/ui/try_reinterpret_inner_cow.stderr | 89 + uitest/tests/ui/try_reinterpret_inner_mut.rs | 9 + .../tests/ui/try_reinterpret_inner_mut.stderr | 83 + .../tests/ui/try_reinterpret_inner_mut_ptr.rs | 9 + .../ui/try_reinterpret_inner_mut_ptr.stderr | 59 + .../ui/try_reinterpret_inner_non_null.rs | 14 + .../ui/try_reinterpret_inner_non_null.stderr | 83 + .../tests/ui/try_reinterpret_inner_option.rs | 11 + .../ui/try_reinterpret_inner_option.stderr | 89 + uitest/tests/ui/try_reinterpret_inner_pin.rs | 12 + .../tests/ui/try_reinterpret_inner_pin.stderr | 83 + uitest/tests/ui/try_reinterpret_inner_rc.rs | 18 + .../tests/ui/try_reinterpret_inner_rc.stderr | 180 ++ .../tests/ui/try_reinterpret_inner_rc_weak.rs | 18 + .../ui/try_reinterpret_inner_rc_weak.stderr | 107 ++ uitest/tests/ui/try_reinterpret_inner_ref.rs | 8 + .../tests/ui/try_reinterpret_inner_ref.stderr | 71 + uitest/tests/ui/try_reinterpret_inner_vec.rs | 9 + .../tests/ui/try_reinterpret_inner_vec.stderr | 77 + uitest/tests/ui/try_reinterpret_wrong_size.rs | 7 + .../ui/try_reinterpret_wrong_size.stderr | 50 + 77 files changed, 6988 insertions(+), 2 deletions(-) create mode 100644 .editorconfig create mode 100644 src/cast.rs create mode 100644 tests/unified_cast_alloc_tests.rs create mode 100644 tests/unified_cast_tests.rs create mode 100644 uitest/Cargo.toml create mode 100644 uitest/src/main.rs create mode 100644 uitest/tests/ui.rs create mode 100644 uitest/tests/ui/reinterpret_incompatible.rs create mode 100644 uitest/tests/ui/reinterpret_incompatible.stderr create mode 100644 uitest/tests/ui/reinterpret_inner_arc.rs create mode 100644 uitest/tests/ui/reinterpret_inner_arc.stderr create mode 100644 uitest/tests/ui/reinterpret_inner_arc_weak.rs create mode 100644 uitest/tests/ui/reinterpret_inner_arc_weak.stderr create mode 100644 uitest/tests/ui/reinterpret_inner_atomic_ptr.rs create mode 100644 uitest/tests/ui/reinterpret_inner_atomic_ptr.stderr create mode 100644 uitest/tests/ui/reinterpret_inner_box.rs create mode 100644 uitest/tests/ui/reinterpret_inner_box.stderr create mode 100644 uitest/tests/ui/reinterpret_inner_const_ptr.rs create mode 100644 uitest/tests/ui/reinterpret_inner_const_ptr.stderr create mode 100644 uitest/tests/ui/reinterpret_inner_cow.rs create mode 100644 uitest/tests/ui/reinterpret_inner_cow.stderr create mode 100644 uitest/tests/ui/reinterpret_inner_mut.rs create mode 100644 uitest/tests/ui/reinterpret_inner_mut.stderr create mode 100644 uitest/tests/ui/reinterpret_inner_mut_ptr.rs create mode 100644 uitest/tests/ui/reinterpret_inner_mut_ptr.stderr create mode 100644 uitest/tests/ui/reinterpret_inner_non_null.rs create mode 100644 uitest/tests/ui/reinterpret_inner_non_null.stderr create mode 100644 uitest/tests/ui/reinterpret_inner_option.rs create mode 100644 uitest/tests/ui/reinterpret_inner_option.stderr create mode 100644 uitest/tests/ui/reinterpret_inner_pin.rs create mode 100644 uitest/tests/ui/reinterpret_inner_pin.stderr create mode 100644 uitest/tests/ui/reinterpret_inner_rc.rs create mode 100644 uitest/tests/ui/reinterpret_inner_rc.stderr create mode 100644 uitest/tests/ui/reinterpret_inner_rc_weak.rs create mode 100644 uitest/tests/ui/reinterpret_inner_rc_weak.stderr create mode 100644 uitest/tests/ui/reinterpret_inner_ref.rs create mode 100644 uitest/tests/ui/reinterpret_inner_ref.stderr create mode 100644 uitest/tests/ui/reinterpret_inner_vec.rs create mode 100644 uitest/tests/ui/reinterpret_inner_vec.stderr create mode 100644 uitest/tests/ui/reinterpret_wrong_size.rs create mode 100644 uitest/tests/ui/reinterpret_wrong_size.stderr create mode 100644 uitest/tests/ui/try_reinterpret_inner_arc.rs create mode 100644 uitest/tests/ui/try_reinterpret_inner_arc.stderr create mode 100644 uitest/tests/ui/try_reinterpret_inner_arc_weak.rs create mode 100644 uitest/tests/ui/try_reinterpret_inner_arc_weak.stderr create mode 100644 uitest/tests/ui/try_reinterpret_inner_atomic_ptr.rs create mode 100644 uitest/tests/ui/try_reinterpret_inner_atomic_ptr.stderr create mode 100644 uitest/tests/ui/try_reinterpret_inner_box.rs create mode 100644 uitest/tests/ui/try_reinterpret_inner_box.stderr create mode 100644 uitest/tests/ui/try_reinterpret_inner_const_ptr.rs create mode 100644 uitest/tests/ui/try_reinterpret_inner_const_ptr.stderr create mode 100644 uitest/tests/ui/try_reinterpret_inner_cow.rs create mode 100644 uitest/tests/ui/try_reinterpret_inner_cow.stderr create mode 100644 uitest/tests/ui/try_reinterpret_inner_mut.rs create mode 100644 uitest/tests/ui/try_reinterpret_inner_mut.stderr create mode 100644 uitest/tests/ui/try_reinterpret_inner_mut_ptr.rs create mode 100644 uitest/tests/ui/try_reinterpret_inner_mut_ptr.stderr create mode 100644 uitest/tests/ui/try_reinterpret_inner_non_null.rs create mode 100644 uitest/tests/ui/try_reinterpret_inner_non_null.stderr create mode 100644 uitest/tests/ui/try_reinterpret_inner_option.rs create mode 100644 uitest/tests/ui/try_reinterpret_inner_option.stderr create mode 100644 uitest/tests/ui/try_reinterpret_inner_pin.rs create mode 100644 uitest/tests/ui/try_reinterpret_inner_pin.stderr create mode 100644 uitest/tests/ui/try_reinterpret_inner_rc.rs create mode 100644 uitest/tests/ui/try_reinterpret_inner_rc.stderr create mode 100644 uitest/tests/ui/try_reinterpret_inner_rc_weak.rs create mode 100644 uitest/tests/ui/try_reinterpret_inner_rc_weak.stderr create mode 100644 uitest/tests/ui/try_reinterpret_inner_ref.rs create mode 100644 uitest/tests/ui/try_reinterpret_inner_ref.stderr create mode 100644 uitest/tests/ui/try_reinterpret_inner_vec.rs create mode 100644 uitest/tests/ui/try_reinterpret_inner_vec.stderr create mode 100644 uitest/tests/ui/try_reinterpret_wrong_size.rs create mode 100644 uitest/tests/ui/try_reinterpret_wrong_size.stderr diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..933ec94 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*] +end_of_line = lf +insert_final_newline = true +indent_style = space +indent_size = 2 +trim_trailing_whitespace = true +charset = utf-8 diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index df8cd7b..e603962 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -78,6 +78,16 @@ jobs: - run: cargo miri test --verbose --all-features - run: cd derive && rm -fr target && cargo miri test --verbose --all-features + ui-test: + name: Test compiler errors + runs-on: ubuntu-latest + steps: + - uses: hecrj/setup-rust-action@v1 + with: + rust-version: stable + - uses: actions/checkout@v3 + - run: cargo test -p uitest + sanitizer-test: name: Test with -Zsanitizer=${{ matrix.sanitizer }} runs-on: ubuntu-latest diff --git a/.gitignore b/.gitignore index fb74370..968eb30 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ Cargo.lock /derive/target/ /derive/.vscode/ +/uitest/wip/ diff --git a/Cargo.toml b/Cargo.toml index 9d6be11..5708a5c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,9 @@ edition = "2018" license = "Zlib OR Apache-2.0 OR MIT" exclude = ["/pedantic.bat"] +[workspace] +members = ["uitest", "derive"] + [features] # In v2 we'll fix these names to be more "normal". derive = ["bytemuck_derive"] @@ -19,8 +22,12 @@ extern_crate_std = ["extern_crate_alloc"] zeroable_maybe_uninit = [] zeroable_atomics = [] min_const_generics = [] -wasm_simd = [] # Until >= 1.54.0 is MSRV this is an off-by-default feature. -aarch64_simd = [] # Until >= 1.59.0 is MSRV this is an off-by-default feature. + +# Off-by-default until bytemuck's MSRV is raised. +wasm_simd = [] # Requires MSRV >= 1.54.0. +unified_cast = [] # Requires MSRV >= 1.57.0. +aarch64_simd = [] # Requires MSRV >= 1.59.0. +non_null_slice_cast = [] # Requires MSRV >= 1.63.0. # Do not use if you can avoid it, because this is unsound. unsound_ptr_pod_impl = [] @@ -42,6 +49,8 @@ features = [ "zeroable_atomics", "min_const_generics", "wasm_simd", + "unified_cast", + "non_null_slice_cast", ] [package.metadata.playground] @@ -54,4 +63,6 @@ features = [ "zeroable_atomics", "min_const_generics", "wasm_simd", + "unified_cast", + "non_null_slice_cast", ] diff --git a/src/cast.rs b/src/cast.rs new file mode 100644 index 0000000..24394b1 --- /dev/null +++ b/src/cast.rs @@ -0,0 +1,1456 @@ +//! Safe byte-wise conversion between types. +//! +//! The public interface consist of a few traits: +//! * `Reinterpret` and `TryReinterpret` implement by-value conversion. +//! * `ReinterpretInner` and `TryReinterpretInner` implement in-place conversion +//! for various container types. +//! +//! `ReinterpretInner` is backed by several helper traits: +//! * `Container` is a concrete container with a pointer to zero or more items. +//! This trait handles several things: +//! * Holding a tag type representing the containers type class. This prevents +//! casts between incompatible containers and allows any additional +//! constraints required by the container not handled by it's raw +//! representation. +//! * Conversion to and from a container's raw form. This allows the actual +//! cast implementation to be shared between multiple containers. +//! * Whether the original value should be returned in case of an error. This +//! allows simplifying the return type of `try_reinterpret_inner` for `Copy` +//! types. +//! Examples of containers include references, raw pointers, `Box` and +//! `Vec`. +//! * `AssertClassContraints` handles any additional constraints a container +//! places on its items. +//! * `RawPointer` Gives a more unified interface for handling `T`, `[T]` and +//! `str` for `Container` impls. +//! * `CastRaw` implements the conversion between a container's raw forms. This +//! is responsible for verifying that the container's item types are +//! compatible and the item's size/alignment constraints are met. +//! * `TryCastRaw` is the falliable form of `CastRaw` + +use core::{ + marker::{PhantomData, Unpin}, + mem::{align_of, size_of, transmute_copy, ManuallyDrop}, + ops::Deref, + pin::Pin, + ptr::{self, NonNull}, + sync::atomic::AtomicPtr, +}; + +#[cfg(feature = "extern_crate_alloc")] +use alloc::{ + borrow::{Cow, ToOwned}, + boxed::Box, + rc::{Rc, Weak as RcWeak}, + string::String, + sync::{Arc, Weak as ArcWeak}, + vec::Vec, +}; + +use crate::{AnyBitPattern, CheckedBitPattern, NoUninit, PodCastError}; + +/// Safe byte-wise conversion of a value. +/// +/// This requires the `unified_cast` feature to be enabled and a rust version +/// `>=1.57`. +pub trait Reinterpret: Sized { + #[doc(hidden)] + const ASSERT: (); + /// Performs the conversion. + fn reinterpret(self) -> Dst; +} +impl Reinterpret for Src +where + Src: NoUninit, + Dst: AnyBitPattern, +{ + const ASSERT: () = assert!( + size_of::() == size_of::(), + "attempted conversion between types of different sizes" + ); + + #[allow(path_statements, clippy::no_effect)] + fn reinterpret(self) -> Dst { + >::ASSERT; + // SAFETY: + // There are no uninitialized bytes in the source type. + // All bit patterns are accepted by the target type. + // Both types have the same size. + unsafe { + transmute_copy::, Dst>(&ManuallyDrop::new(self)) + } + } +} + +/// Safe byte-wise conversion of a value which may fail due to the source value +/// not being a valid bit pattern for the target type. +/// +/// This requires the `unified_cast` feature to be enabled and a rust version +/// `>=1.57`. +pub trait TryReinterpret: Sized { + #[doc(hidden)] + const ASSERT: (); + /// Performs the conversion. + fn try_reinterpret(self) -> Option; +} +impl TryReinterpret for Src +where + Src: NoUninit, + Dst: CheckedBitPattern, +{ + const ASSERT: () = { + assert!( + size_of::() == size_of::(), + "attempted conversion between types of different sizes" + ); + assert!( + size_of::() == size_of::(), + "target type has an invalid implementation of `CheckedBitPattern`" + ) + }; + + #[allow(path_statements, clippy::no_effect)] + fn try_reinterpret(self) -> Option { + >::ASSERT; + let bits = unsafe { + transmute_copy::, Dst::Bits>(&ManuallyDrop::new(self)) + }; + if Dst::is_valid_bit_pattern(&bits) { + // SAFETY: + // There are no uninitialized bytes in the source type. + // The value has been confirmed to be a valid bit pattern for the target type. + // Both types have the same size. + Some(unsafe { + transmute_copy::, Dst>(&ManuallyDrop::new(bits)) + }) + } else { + None + } + } +} + +pub trait ItemLayout { + const SIZE: usize; + const ALIGN: usize; +} +impl ItemLayout for T { + const SIZE: usize = size_of::(); + const ALIGN: usize = align_of::(); +} +impl ItemLayout for [T] { + const SIZE: usize = size_of::(); + const ALIGN: usize = align_of::(); +} +impl ItemLayout for str { + const SIZE: usize = 1; + const ALIGN: usize = 1; +} + +// Container classes. +pub struct RefT; +pub struct PtrT; +pub struct NonNullT; +pub struct AtomicPtrT; +pub struct OptionT(PhantomData); +pub struct PinT(PhantomData); +#[cfg(feature = "extern_crate_alloc")] +pub struct BoxT; +#[cfg(feature = "extern_crate_alloc")] +pub struct CowT; +#[cfg(feature = "extern_crate_alloc")] +pub struct RcT; +#[cfg(feature = "extern_crate_alloc")] +pub struct RcWeakT; +#[cfg(feature = "extern_crate_alloc")] +pub struct ArcT; +#[cfg(feature = "extern_crate_alloc")] +pub struct ArcWeakT; +#[cfg(feature = "extern_crate_alloc")] +pub struct VecT; + +/// Policy trait for whether the original value should be returned with the +/// error. +pub trait CastErrWithValue { + /// The error type returned. + type Err; + /// Combines the original value with the error. + fn cast_error_with_value(err: PodCastError, value: T) -> Self::Err; +} +/// Return the error without the original value. +pub struct OnlyErr; +impl CastErrWithValue for OnlyErr { + type Err = PodCastError; + fn cast_error_with_value(err: PodCastError, _: T) -> Self::Err { + err + } +} +/// Return both the error and the original value. +pub struct ErrWithValue; +impl CastErrWithValue for ErrWithValue { + type Err = (PodCastError, T); + fn cast_error_with_value(err: PodCastError, value: T) -> Self::Err { + (err, value) + } +} + +/// Like `*const [T]`, but the length can be retrieved safely. +pub struct RawSlice { + data: *const T, + len: usize, +} +impl Clone for RawSlice { + fn clone(&self) -> Self { + *self + } +} +impl Copy for RawSlice {} + +/// Like `*mut [T]`, but the length can be retrieved safely. +pub struct RawMutSlice { + data: *mut T, + len: usize, +} +impl Clone for RawMutSlice { + fn clone(&self) -> Self { + *self + } +} +impl Copy for RawMutSlice {} + +/// A single byte from a `str` slice. +#[derive(Clone, Copy)] +#[repr(transparent)] +pub struct StrByte(u8); +// SAFETY: A transparent wrapper over a single byte has no uninitialized bytes. +unsafe impl NoUninit for StrByte {} + +/// Converts between a pointer type and it's raw form. This allows treating +/// various DST pointers similarly. +/// +/// # Safety +/// Converting a pointer to and from it's raw form must result in the same +/// pointer. +pub unsafe trait RawPtr { + /// The raw form of the pointer. + type Raw: Copy; + + /// Performs the conversion to the raw form. + /// + /// # Safety + /// For DST targets the pointer must be safe to materialize. If the + /// `non_null_slice_cast` feature is enabled, then it must only be non-null. + unsafe fn into_raw_ptr(self) -> Self::Raw; + + /// Performs the conversion from the raw form. + /// + /// # Safety + /// The value must have been created from `into_raw_ptr` and possibly + /// converted to a different type using `CastRaw` or `TryCastRaw`. + unsafe fn from_raw_ptr(raw: Self::Raw) -> Self; +} +unsafe impl RawPtr for *const T { + type Raw = *const T; + unsafe fn into_raw_ptr(self) -> Self::Raw { + self + } + unsafe fn from_raw_ptr(raw: Self::Raw) -> Self { + raw + } +} +unsafe impl RawPtr for *mut T { + type Raw = *mut T; + unsafe fn into_raw_ptr(self) -> Self::Raw { + self + } + unsafe fn from_raw_ptr(raw: Self::Raw) -> Self { + raw + } +} +unsafe impl RawPtr for *const [T] { + type Raw = RawSlice; + #[cfg(feature = "non_null_slice_cast")] + unsafe fn into_raw_ptr(self) -> Self::Raw { + let len = NonNull::new_unchecked(self as *mut [T]).len(); + RawSlice { data: self as *const T, len } + } + #[cfg(not(feature = "non_null_slice_cast"))] + unsafe fn into_raw_ptr(self) -> Self::Raw { + let len = (*self).len(); + RawSlice { data: self as *const T, len } + } + unsafe fn from_raw_ptr(raw: Self::Raw) -> Self { + ptr::slice_from_raw_parts(raw.data, raw.len) + } +} +unsafe impl RawPtr for *mut [T] { + type Raw = RawMutSlice; + #[cfg(feature = "non_null_slice_cast")] + unsafe fn into_raw_ptr(self) -> Self::Raw { + let len = NonNull::new_unchecked(self).len(); + RawMutSlice { data: self as *mut T, len } + } + #[cfg(not(feature = "non_null_slice_cast"))] + unsafe fn into_raw_ptr(self) -> Self::Raw { + let len = (*self).len(); + RawMutSlice { data: self as *mut T, len } + } + unsafe fn from_raw_ptr(raw: Self::Raw) -> Self { + ptr::slice_from_raw_parts_mut(raw.data, raw.len) + } +} +unsafe impl RawPtr for *const str { + type Raw = RawSlice; + unsafe fn into_raw_ptr(self) -> Self::Raw { + (self as *const [StrByte]).into_raw_ptr() + } + unsafe fn from_raw_ptr(raw: Self::Raw) -> Self { + <*const [StrByte]>::from_raw_ptr(raw) as *const str + } +} +unsafe impl RawPtr for *mut str { + type Raw = RawMutSlice; + unsafe fn into_raw_ptr(self) -> Self::Raw { + (self as *mut [StrByte]).into_raw_ptr() + } + unsafe fn from_raw_ptr(raw: Self::Raw) -> Self { + <*mut [StrByte]>::from_raw_ptr(raw) as *mut str + } +} + +/// A concrete container type. e.g `&'a T` or `Box` +/// +/// # Safety +/// * `Class` must be set such that calling `CastRaw` on this containers raw +/// form is valid. +/// * `Item` must match the contained item type. +/// * `into_raw` -> `CastRaw`/`TryCastRaw` -> `from_raw` must be valid. +pub unsafe trait Container<'a>: Sized { + /// The type class of this container. Used to limit which raw casts should be + type Class: AssertClassContraints; + /// The item type held within this container. + type Item: 'a + ?Sized + ItemLayout; + /// The 'raw' form of this container. Used to allow different containers to + /// share the same `CastRaw` and `TryCastRaw` impls. + type Raw: 'a + Copy; + /// Whether the cast should return the original value along with the error. + type CastErr: CastErrWithValue; + + /// Converts the container into it's raw form. + fn into_raw(self) -> Self::Raw; + + /// Reconstructs the container from it's raw form. + /// + /// # Safety + /// The values must have to come from `into_parts` of the same container + /// class. Casting to a different item type must meet the following + /// constraints: + /// * Casting between zero-sized types and non-zero-sized types is forbidden. + /// * The data pointer's alignment must meet the alignment constraints of it's + /// item type. + /// * Size and alignment requirements of the container class must be met. + /// * The additional data must be adjusted for the new type. + unsafe fn from_raw(raw: Self::Raw) -> Self; +} + +unsafe impl<'a, T> Container<'a> for &'a T +where + T: 'a + ?Sized + ItemLayout, + *const T: RawPtr, +{ + type Class = RefT; + type Item = T; + type Raw = <*const T as RawPtr>::Raw; + type CastErr = OnlyErr; + + fn into_raw(self) -> Self::Raw { + // SAFETY: Materializing pointers backed by a reference is safe. + unsafe { (self as *const T).into_raw_ptr() } + } + unsafe fn from_raw(raw: Self::Raw) -> Self { + &*<*const T>::from_raw_ptr(raw) + } +} + +unsafe impl<'a, T> Container<'a> for &'a mut T +where + T: 'a + ?Sized + ItemLayout, + *mut T: RawPtr, +{ + type Class = RefT; + type Item = T; + type Raw = <*mut T as RawPtr>::Raw; + type CastErr = OnlyErr; + + fn into_raw(self) -> Self::Raw { + // SAFETY: Materializing pointers backed by a reference is safe. + unsafe { (self as *mut T).into_raw_ptr() } + } + unsafe fn from_raw(raw: Self::Raw) -> Self { + &mut *<*mut T>::from_raw_ptr(raw) + } +} + +// No safe way to get the length of a slice. Only implement for sized types. +unsafe impl<'a, T: 'a> Container<'a> for *const T { + type Class = PtrT; + type Item = T; + type Raw = Self; + type CastErr = OnlyErr; + + fn into_raw(self) -> Self::Raw { + self + } + unsafe fn from_raw(raw: Self::Raw) -> Self { + raw + } +} + +// No safe way to get the length of a slice. Only implement for sized types. +unsafe impl<'a, T: 'a> Container<'a> for *mut T { + type Class = PtrT; + type Item = T; + type Raw = Self; + type CastErr = OnlyErr; + + fn into_raw(self) -> Self::Raw { + self + } + unsafe fn from_raw(raw: Self::Raw) -> Self { + raw + } +} + +unsafe impl< + 'a, + #[cfg(feature = "non_null_slice_cast")] T: 'a + ?Sized + ItemLayout, + #[cfg(not(feature = "non_null_slice_cast"))] T: 'a, + > Container<'a> for NonNull +where + *mut T: RawPtr, +{ + type Class = NonNullT; + type Item = T; + type Raw = <*mut T as RawPtr>::Raw; + type CastErr = OnlyErr; + + fn into_raw(self) -> Self::Raw { + // SAFETY: We only attempt to get the length with the `non_null_slice_cast` + // feature. + unsafe { self.as_ptr().into_raw_ptr() } + } + unsafe fn from_raw(raw: Self::Raw) -> Self { + NonNull::new_unchecked(<*mut T>::from_raw_ptr(raw)) + } +} + +unsafe impl<'a, T: 'a> Container<'a> for AtomicPtr { + type Class = AtomicPtrT; + type Item = T; + type Raw = *mut T; + type CastErr = OnlyErr; + + fn into_raw(self) -> Self::Raw { + self.into_inner() + } + unsafe fn from_raw(raw: Self::Raw) -> Self { + Self::new(raw) + } +} + +unsafe impl<'a, C> Container<'a> for Option +where + C: Container<'a>, + C::CastErr: CastErrWithValue, +{ + type Class = OptionT; + type Item = C::Item; + type Raw = Option; + type CastErr = C::CastErr; + + fn into_raw(self) -> Self::Raw { + self.map(|x| x.into_raw()) + } + unsafe fn from_raw(raw: Self::Raw) -> Self { + raw.map(|raw| C::from_raw(raw)) + } +} + +// SAFETY: `Pin` has no safety requirements for types which deref to an `Unpin` +// type. +unsafe impl<'a, C> Container<'a> for Pin +where + C: Container<'a> + Deref>::Item>, + C::Item: Unpin, + C::CastErr: CastErrWithValue, +{ + type Class = PinT; + type Item = C::Item; + type Raw = C::Raw; + type CastErr = C::CastErr; + + fn into_raw(self) -> Self::Raw { + Self::into_inner(self).into_raw() + } + unsafe fn from_raw(raw: Self::Raw) -> Self { + Self::new(C::from_raw(raw)) + } +} + +#[cfg(feature = "extern_crate_alloc")] +unsafe impl<'a, T> Container<'a> for Box +where + T: 'a + ?Sized + ItemLayout, + *const T: RawPtr, +{ + type Class = BoxT; + type Item = T; + // Uses `*const T` as the old value can't be read after the conversion. + type Raw = <*const T as RawPtr>::Raw; + type CastErr = ErrWithValue; + + fn into_raw(self) -> Self::Raw { + // SAFETY: Materializing a pointer to an allocated box is safe. + unsafe { (Self::into_raw(self) as *const T).into_raw_ptr() } + } + unsafe fn from_raw(raw: Self::Raw) -> Self { + Self::from_raw(<*const T>::from_raw_ptr(raw) as *mut T) + } +} + +#[cfg(feature = "extern_crate_alloc")] +unsafe impl<'a, T> Container<'a> for Rc +where + T: 'a + ?Sized + ItemLayout, + *const T: RawPtr, +{ + type Class = RcT; + type Item = T; + type Raw = <*const T as RawPtr>::Raw; + type CastErr = ErrWithValue; + + fn into_raw(self) -> Self::Raw { + // SAFETY: Materializing a pointer to an allocated `Rc` is safe. + unsafe { Self::into_raw(self).into_raw_ptr() } + } + unsafe fn from_raw(raw: Self::Raw) -> Self { + Self::from_raw(<*const T>::from_raw_ptr(raw)) + } +} +#[cfg(feature = "extern_crate_alloc")] +unsafe impl<'a, T: 'a> Container<'a> for RcWeak { + type Class = RcWeakT; + type Item = T; + // `get_mut_unchecked` requires no other `Rc`s with a different type exist. + type Raw = *const T; + type CastErr = ErrWithValue; + + fn into_raw(self) -> Self::Raw { + Self::into_raw(self) + } + unsafe fn from_raw(raw: Self::Raw) -> Self { + Self::from_raw(raw) + } +} + +#[cfg(feature = "extern_crate_alloc")] +unsafe impl<'a, T> Container<'a> for Arc +where + T: 'a + ?Sized + ItemLayout, + *const T: RawPtr, +{ + type Class = ArcT; + type Item = T; + type Raw = <*const T as RawPtr>::Raw; + type CastErr = ErrWithValue; + + fn into_raw(self) -> Self::Raw { + // SAFETY: Materializing a pointer to an allocated `Arc` is safe. + unsafe { Self::into_raw(self).into_raw_ptr() } + } + unsafe fn from_raw(raw: Self::Raw) -> Self { + Self::from_raw(<*const T>::from_raw_ptr(raw)) + } +} +#[cfg(feature = "extern_crate_alloc")] +unsafe impl<'a, T: 'a> Container<'a> for ArcWeak { + type Class = ArcWeakT; + type Item = T; + type Raw = *const T; + type CastErr = ErrWithValue; + + fn into_raw(self) -> Self::Raw { + Self::into_raw(self) + } + unsafe fn from_raw(raw: Self::Raw) -> Self { + Self::from_raw(raw) + } +} + +/// The raw form of a vec. +#[cfg(feature = "extern_crate_alloc")] +#[derive(Clone, Copy)] +pub struct RawVec { + slice: RawSlice, + cap: usize, +} +#[cfg(feature = "extern_crate_alloc")] +unsafe impl<'a, T: 'a + Copy> Container<'a> for Vec { + type Class = VecT; + type Item = T; + type Raw = RawVec; + type CastErr = ErrWithValue; + + fn into_raw(self) -> Self::Raw { + let mut x = ManuallyDrop::new(self); + RawVec { + // Use `as_mut_ptr` to get the correct provenance. + slice: RawSlice { data: x.as_mut_ptr() as *const T, len: x.len() }, + cap: x.capacity(), + } + } + unsafe fn from_raw(raw: Self::Raw) -> Self { + Self::from_raw_parts(raw.slice.data as *mut T, raw.slice.len, raw.cap) + } +} + +/// Conversion between a types `Owned` type, and that type's raw form. +#[cfg(feature = "extern_crate_alloc")] +pub trait RawToOwned: ToOwned { + type RawOwned: Copy; + type CastErr: CastErrWithValue<*const Self>; + fn raw_from_owned(value: Self::Owned) -> Self::RawOwned; + unsafe fn owned_from_raw(raw: Self::RawOwned) -> Self::Owned; +} +#[cfg(feature = "extern_crate_alloc")] +impl RawToOwned for T { + type RawOwned = T; + type CastErr = OnlyErr; + fn raw_from_owned(value: Self::Owned) -> Self::RawOwned { + value + } + unsafe fn owned_from_raw(raw: Self::RawOwned) -> Self::Owned { + raw + } +} +#[cfg(feature = "extern_crate_alloc")] +impl RawToOwned for [T] { + type RawOwned = RawVec; + type CastErr = ErrWithValue; + fn raw_from_owned(value: Self::Owned) -> Self::RawOwned { + value.into_raw() + } + unsafe fn owned_from_raw(raw: Self::RawOwned) -> Self::Owned { + Vec::from_raw(raw) + } +} +#[cfg(feature = "extern_crate_alloc")] +impl RawToOwned for str { + type RawOwned = RawVec; + type CastErr = ErrWithValue; + fn raw_from_owned(value: Self::Owned) -> Self::RawOwned { + let raw = value.into_bytes().into_raw(); + RawVec { + slice: RawSlice { data: raw.slice.data.cast(), len: raw.slice.len }, + cap: raw.cap, + } + } + unsafe fn owned_from_raw(raw: Self::RawOwned) -> Self::Owned { + String::from_utf8_unchecked(Vec::from_raw(raw.cast_raw())) + } +} + +/// The raw form of a `Cow`. +#[cfg(feature = "extern_crate_alloc")] +pub enum RawCow +where + *const T: RawPtr, +{ + Borrowed(<*const T as RawPtr>::Raw), + Owned(::RawOwned), +} +#[cfg(feature = "extern_crate_alloc")] +impl Clone for RawCow +where + *const T: RawPtr, +{ + fn clone(&self) -> Self { + *self + } +} +#[cfg(feature = "extern_crate_alloc")] +impl Copy for RawCow where *const T: RawPtr {} + +#[cfg(feature = "extern_crate_alloc")] +unsafe impl<'a, T> Container<'a> for Cow<'a, T> +where + T: 'a + ?Sized + RawToOwned + ItemLayout, + ::CastErr: CastErrWithValue, + *const T: RawPtr, +{ + type Class = CowT; + type Item = T; + type Raw = RawCow; + type CastErr = ::CastErr; + + fn into_raw(self) -> Self::Raw { + match self { + Self::Borrowed(x) => { + // SAFETY: Materializing a pointer backed by a reference is safe. + RawCow::Borrowed(unsafe { (x as *const T).into_raw_ptr() }) + } + Self::Owned(x) => RawCow::Owned(::raw_from_owned(x)), + } + } + unsafe fn from_raw(raw: Self::Raw) -> Self { + match raw { + RawCow::Borrowed(x) => Self::Borrowed(&*<*const T>::from_raw_ptr(x)), + RawCow::Owned(x) => Self::Owned(::owned_from_raw(x)), + } + } +} + +/// Attempts to convert the pointer type. Will fail if the pointer is not +/// suitably aligned for the target type. +fn try_cast_ptr(ptr: *const T) -> Result<*const U, PodCastError> { + if align_of::() >= align_of::() || ptr as usize % align_of::() == 0 { + Ok(ptr.cast()) + } else { + Err(PodCastError::AlignmentMismatch) + } +} +/// Attempts to convert the pointer type. Will fail if the pointer is not +/// suitably aligned for the target type. +fn try_cast_mut_ptr(ptr: *mut T) -> Result<*mut U, PodCastError> { + Ok(try_cast_ptr::(ptr)? as *mut U) +} + +/// Attempts to convert the length of `[T]` to the length of `[U]`. Will fail if +/// there is no length for the target type which will occupy all the bytes of +/// the input. +fn try_cast_len(size: usize) -> Result { + if size_of::() == size_of::() { + Ok(size) + } else if size_of::() % size_of::() == 0 { + Ok(size * (size_of::() / size_of::())) + } else { + let byte_size = size * size_of::(); + if byte_size % size_of::() == 0 { + Ok(byte_size / size_of::()) + } else { + Err(PodCastError::OutputSliceWouldHaveSlop) + } + } +} + +#[test] +fn test_try_cast_len() { + assert_eq!(try_cast_len::<(), ()>(0), Ok(0)); + assert_eq!(try_cast_len::<(), ()>(1), Ok(1)); + assert_eq!(try_cast_len::<(), ()>(2), Ok(2)); + assert_eq!(try_cast_len::(0), Ok(0)); + assert_eq!(try_cast_len::(1), Ok(1)); + assert_eq!(try_cast_len::(2), Ok(2)); + assert_eq!(try_cast_len::(0), Ok(0)); + assert_eq!(try_cast_len::(2), Ok(1)); + assert_eq!(try_cast_len::(4), Ok(2)); + assert_eq!(try_cast_len::(0), Ok(0)); + assert_eq!(try_cast_len::(4), Ok(1)); + assert_eq!(try_cast_len::(8), Ok(2)); + assert_eq!(try_cast_len::(0), Ok(0)); + assert_eq!(try_cast_len::(1), Ok(1)); + assert_eq!(try_cast_len::(2), Ok(2)); + assert_eq!(try_cast_len::(0), Ok(0)); + assert_eq!(try_cast_len::(2), Ok(1)); + assert_eq!(try_cast_len::(4), Ok(2)); + assert_eq!(try_cast_len::(1), Ok(4)); + assert_eq!(try_cast_len::(2), Ok(8)); + assert_eq!(try_cast_len::(3), Ok(12)); + assert_eq!(try_cast_len::<[u8; 3], u16>(0), Ok(0)); + assert_eq!(try_cast_len::<[u8; 3], u16>(2), Ok(3)); + assert_eq!(try_cast_len::<[u8; 3], u16>(4), Ok(6)); + assert_eq!(try_cast_len::<[u8; 3], u32>(0), Ok(0)); + assert_eq!(try_cast_len::<[u8; 3], u32>(4), Ok(3)); + assert_eq!(try_cast_len::<[u8; 3], u32>(8), Ok(6)); + assert!(try_cast_len::(1).is_err()); + assert!(try_cast_len::(3).is_err()); + assert!(try_cast_len::(5).is_err()); + assert!(try_cast_len::(1).is_err()); + assert!(try_cast_len::(2).is_err()); + assert!(try_cast_len::(3).is_err()); + assert!(try_cast_len::<[u8; 3], [u8; 2]>(1).is_err()); + assert!(try_cast_len::<[u8; 3], [u8; 2]>(3).is_err()); + assert!(try_cast_len::<[u8; 3], [u8; 2]>(5).is_err()); +} + +/// A conversion from a container's raw type to a compatible container's raw +/// type. This is not required to uphold any container specific constraints +/// which would be upheld by `AssertClassContraints`. +/// +/// # Safety +/// Assuming `AssertClassContraints` succeeds, the resulting value must be safe +/// to use for the following two steps: +/// * `RawPtr::from_raw_ptr` if the input was created from `RawPtr::to_raw_ptr`. +/// * `Container::from_raw` for the resulting type's container. +/// +/// Possible constraints include, but are not limited to: +/// * Any contained pointers must point to the same location after the +/// conversion. +/// * Assuming an input pointer is aligned, the matching result pointer must be +/// suitably aligned for the target type. +/// * All bit patterns of any converted input type must be valid for the target +/// type. +/// * For any converted mutable pointers, the reverse is true as well. +/// * Any length field must be converted from it's input type to occupy the same +/// number of bytes in it's output type. +pub unsafe trait CastRaw: Copy { + const ASSERT: (); + /// Performs the conversion. + /// + /// # Safety + /// `ASSERT` must be materialized before calling this function. + unsafe fn cast_raw(self) -> Dst; +} + +unsafe impl CastRaw<*const Dst> for *const Src +where + Src: NoUninit, + Dst: AnyBitPattern, +{ + const ASSERT: () = { + assert!( + size_of::() == size_of::(), + "Attempted conversion between types of different sizes." + ); + assert!( + align_of::() >= align_of::(), + "Attempted conversion to a type with a stricter alignment" + ); + }; + unsafe fn cast_raw(self) -> *const Dst { + self as *const Dst + } +} +unsafe impl CastRaw<*const Dst> for *mut Src +where + *const Src: CastRaw<*const Dst>, +{ + const ASSERT: () = <*const Src as CastRaw<*const Dst>>::ASSERT; + unsafe fn cast_raw(self) -> *const Dst { + (self as *const Src).cast_raw() + } +} +unsafe impl CastRaw<*mut Dst> for *mut Src +where + Src: NoUninit + AnyBitPattern, + Dst: NoUninit + AnyBitPattern, +{ + const ASSERT: () = <*const Src as CastRaw<*const Dst>>::ASSERT; + unsafe fn cast_raw(self) -> *mut Dst { + self as *mut Dst + } +} + +unsafe impl CastRaw> for *const Src +where + Src: NoUninit, + Dst: AnyBitPattern, +{ + const ASSERT: () = { + assert!( + size_of::() == size_of::() + || (size_of::() != 0 && size_of::() % size_of::() == 0), + "Attempted conversion to a slice which cannot match the size of the item" + ); + }; + unsafe fn cast_raw(self) -> RawSlice { + let len = if size_of::() == 0 { + 1 + } else { + size_of::() / size_of::() + }; + RawSlice { data: self.cast(), len } + } +} +unsafe impl CastRaw> for *mut Src +where + *const Src: CastRaw>, +{ + const ASSERT: () = <*const Src as CastRaw>>::ASSERT; + unsafe fn cast_raw(self) -> RawSlice { + (self as *const Src).cast_raw() + } +} +unsafe impl CastRaw> for *mut Src +where + Src: NoUninit + AnyBitPattern, + Dst: NoUninit + AnyBitPattern, +{ + const ASSERT: () = <*const Src as CastRaw>>::ASSERT; + unsafe fn cast_raw(self) -> RawMutSlice { + let len = if size_of::() == 0 { + 1 + } else { + size_of::() / size_of::() + }; + RawMutSlice { data: self.cast(), len } + } +} + +unsafe impl CastRaw> for RawSlice +where + Src: NoUninit, + Dst: AnyBitPattern, +{ + const ASSERT: () = { + assert!( + size_of::() == size_of::() + || (size_of::() != 0 && size_of::() % size_of::() == 0), + "Attempted conversion between slice types which may not succeed" + ); + assert!( + align_of::() >= align_of::(), + "Attempted conversion to a type with a stricter alignment" + ) + }; + unsafe fn cast_raw(self) -> RawSlice { + let m = if size_of::() == 0 { + 1 + } else { + size_of::() / size_of::() + }; + RawSlice { data: self.data as *const Dst, len: self.len * m } + } +} +unsafe impl CastRaw> for RawMutSlice +where + RawSlice: CastRaw>, +{ + const ASSERT: () = as CastRaw>>::ASSERT; + unsafe fn cast_raw(self) -> RawSlice { + RawSlice { data: self.data as *const Src, len: self.len }.cast_raw() + } +} +unsafe impl CastRaw> for RawMutSlice +where + Src: NoUninit + AnyBitPattern, + Dst: NoUninit + AnyBitPattern, +{ + const ASSERT: () = as CastRaw>>::ASSERT; + unsafe fn cast_raw(self) -> RawMutSlice { + let m = if size_of::() == 0 { + 1 + } else { + size_of::() / size_of::() + }; + RawMutSlice { data: self.data as *mut Dst, len: self.len * m } + } +} + +unsafe impl CastRaw> for Option +where + Src: CastRaw, + Dst: Copy, +{ + const ASSERT: () = >::ASSERT; + unsafe fn cast_raw(self) -> Option { + self.map(|x| x.cast_raw()) + } +} + +#[cfg(feature = "extern_crate_alloc")] +unsafe impl CastRaw> for RawVec +where + Src: NoUninit, + Dst: AnyBitPattern, +{ + const ASSERT: () = as CastRaw>>::ASSERT; + unsafe fn cast_raw(self) -> RawVec { + let m = if size_of::() == 0 { + 1 + } else { + size_of::() / size_of::() + }; + RawVec { slice: self.slice.cast_raw(), cap: self.cap * m } + } +} + +#[cfg(feature = "extern_crate_alloc")] +unsafe impl CastRaw> for RawCow +where + Src: NoUninit, + Dst: AnyBitPattern, +{ + const ASSERT: () = <*const Src as CastRaw<*const Dst>>::ASSERT; + unsafe fn cast_raw(self) -> RawCow { + match self { + Self::Borrowed(x) => RawCow::Borrowed(x.cast_raw()), + Self::Owned(x) => RawCow::Owned( + transmute_copy::, Dst>(&ManuallyDrop::new(x)), + ), + } + } +} +#[cfg(feature = "extern_crate_alloc")] +unsafe impl CastRaw> for RawCow<[Src]> +where + Src: NoUninit, + Dst: AnyBitPattern, +{ + #[allow(path_statements, clippy::no_effect)] + const ASSERT: () = { + assert!( + align_of::() == align_of::(), + "Attempted conversion between `Cow<[_]>` types with different alignments" + ); + as CastRaw>>::ASSERT; + }; + unsafe fn cast_raw(self) -> RawCow<[Dst]> { + match self { + Self::Borrowed(x) => RawCow::Borrowed(x.cast_raw()), + Self::Owned(x) => RawCow::Owned(x.cast_raw()), + } + } +} + +/// An attempted conversion from a container's raw type to a compatible +/// container's raw type. This is not required to uphold any container specific +/// constraints which would be upheld by `AssertClassContraints`. +/// +/// # Safety +/// Assuming `AssertClassContraints` succeeds, the value resulting from a +/// successful conversion must be safe to use for the following two steps: +/// * `RawPtr::from_raw_ptr` if the input was created from `RawPtr::to_raw_ptr`. +/// * `Container::from_raw` for the resulting type's container. +/// +/// Possible constraints include, but are not limited to: +/// * Any contained pointers must point to the same location after the +/// conversion. +/// * Assuming an input pointer is aligned, the matching result pointer must be +/// suitably aligned for the target type. +/// * All bit patterns of any converted input type must be valid for the target +/// type. +/// * For any converted mutable pointers, the reverse is true as well. +/// * Any length field must be converted from it's input type to occupy the same +/// number of bytes in it's output type. +pub unsafe trait TryCastRaw: Copy { + const ASSERT: (); + /// Perform the cast. + /// + /// # Safety + /// `ASSERT` must be materialized before calling this function. + unsafe fn try_cast_raw(self) -> Result; +} + +unsafe impl TryCastRaw<*const Dst> for *const Src +where + Src: NoUninit, + Dst: AnyBitPattern, +{ + const ASSERT: () = assert!( + size_of::() == size_of::(), + "Attempted conversion between types of different sizes." + ); + unsafe fn try_cast_raw(self) -> Result<*const Dst, PodCastError> { + try_cast_ptr(self) + } +} +unsafe impl TryCastRaw<*const Dst> for *mut Src +where + *const Src: TryCastRaw<*const Dst>, +{ + const ASSERT: () = <*const Src as TryCastRaw<*const Dst>>::ASSERT; + unsafe fn try_cast_raw(self) -> Result<*const Dst, PodCastError> { + (self as *const Src).try_cast_raw() + } +} +unsafe impl TryCastRaw<*mut Dst> for *mut Src +where + Src: NoUninit + AnyBitPattern, + Dst: NoUninit + AnyBitPattern, +{ + const ASSERT: () = <*const Src as TryCastRaw<*const Dst>>::ASSERT; + unsafe fn try_cast_raw(self) -> Result<*mut Dst, PodCastError> { + try_cast_mut_ptr(self) + } +} + +unsafe impl TryCastRaw> for RawSlice +where + Src: NoUninit, + Dst: AnyBitPattern, +{ + const ASSERT: () = assert!( + (size_of::() == 0) == (size_of::() == 0), + "Attempted conversion between a zero-sized type and a non-zero-sized type" + ); + unsafe fn try_cast_raw(self) -> Result, PodCastError> { + Ok(RawSlice { + data: try_cast_ptr(self.data)?, + len: try_cast_len::(self.len)?, + }) + } +} +unsafe impl TryCastRaw> for RawMutSlice +where + RawSlice: TryCastRaw>, +{ + const ASSERT: () = as TryCastRaw>>::ASSERT; + unsafe fn try_cast_raw(self) -> Result, PodCastError> { + RawSlice { data: self.data as *const Src, len: self.len }.try_cast_raw() + } +} +unsafe impl TryCastRaw> for RawMutSlice +where + Src: NoUninit + AnyBitPattern, + Dst: NoUninit + AnyBitPattern, +{ + const ASSERT: () = as TryCastRaw>>::ASSERT; + unsafe fn try_cast_raw(self) -> Result, PodCastError> { + Ok(RawMutSlice { + data: try_cast_mut_ptr(self.data)?, + len: try_cast_len::(self.len)?, + }) + } +} + +unsafe impl TryCastRaw<*const Dst> for RawSlice +where + Src: NoUninit, + Dst: AnyBitPattern, +{ + const ASSERT: () = assert!( + size_of::() == size_of::() + || (size_of::() != 0 && size_of::() % size_of::() == 0), + "Attempted conversion from a slice which cannot match the size of the item" + ); + unsafe fn try_cast_raw(self) -> Result<*const Dst, PodCastError> { + let len = if size_of::() == 0 { + 1 + } else { + size_of::() / size_of::() + }; + if len == self.len { + Ok(try_cast_ptr(self.data)?) + } else { + Err(PodCastError::SizeMismatch) + } + } +} +unsafe impl TryCastRaw<*const Dst> for RawMutSlice +where + RawSlice: TryCastRaw<*const Dst>, +{ + const ASSERT: () = as TryCastRaw<*const Dst>>::ASSERT; + unsafe fn try_cast_raw(self) -> Result<*const Dst, PodCastError> { + RawSlice { data: self.data as *const Src, len: self.len }.try_cast_raw() + } +} +unsafe impl TryCastRaw<*mut Dst> for RawMutSlice +where + Src: NoUninit + AnyBitPattern, + Dst: NoUninit + AnyBitPattern, +{ + const ASSERT: () = as TryCastRaw<*const Dst>>::ASSERT; + unsafe fn try_cast_raw(self) -> Result<*mut Dst, PodCastError> { + let len = if size_of::() == 0 { + 1 + } else { + size_of::() / size_of::() + }; + if len == self.len { + Ok(try_cast_mut_ptr(self.data)?) + } else { + Err(PodCastError::SizeMismatch) + } + } +} + +unsafe impl TryCastRaw> for *const Src +where + Src: NoUninit, + Dst: AnyBitPattern, +{ + const ASSERT: () = assert!( + size_of::() == size_of::() + || (size_of::() != 0 && size_of::() % size_of::() == 0), + "Attempted conversion to a slice which cannot match the size of the item" + ); + unsafe fn try_cast_raw(self) -> Result, PodCastError> { + Ok(RawSlice { + data: try_cast_ptr(self)?, + len: if size_of::() == size_of::() { + 1 + } else { + size_of::() / size_of::() + }, + }) + } +} +unsafe impl TryCastRaw> for *mut Src +where + *const Src: TryCastRaw>, +{ + const ASSERT: () = <*const Src as TryCastRaw>>::ASSERT; + unsafe fn try_cast_raw(self) -> Result, PodCastError> { + (self as *const Src).try_cast_raw() + } +} +unsafe impl TryCastRaw> for *mut Src +where + Src: NoUninit + AnyBitPattern, + Dst: NoUninit + AnyBitPattern, +{ + const ASSERT: () = <*const Src as TryCastRaw>>::ASSERT; + unsafe fn try_cast_raw(self) -> Result, PodCastError> { + Ok(RawMutSlice { + data: try_cast_mut_ptr(self)?, + len: if size_of::() == size_of::() { + 1 + } else { + size_of::() / size_of::() + }, + }) + } +} + +unsafe impl TryCastRaw> for Option +where + Src: TryCastRaw, + Dst: Copy, +{ + const ASSERT: () = >::ASSERT; + unsafe fn try_cast_raw(self) -> Result, PodCastError> { + match self { + Some(x) => Ok(Some(x.try_cast_raw()?)), + None => Ok(None), + } + } +} + +#[cfg(feature = "extern_crate_alloc")] +unsafe impl TryCastRaw> for RawVec +where + Src: NoUninit, + Dst: AnyBitPattern, +{ + const ASSERT: () = as TryCastRaw>>::ASSERT; + unsafe fn try_cast_raw(self) -> Result, PodCastError> { + Ok(RawVec { + slice: self.slice.try_cast_raw()?, + cap: try_cast_len::(self.cap)?, + }) + } +} + +#[cfg(feature = "extern_crate_alloc")] +unsafe impl TryCastRaw> for RawCow +where + Src: NoUninit, + Dst: AnyBitPattern, +{ + const ASSERT: () = <*const Src as TryCastRaw<*const Dst>>::ASSERT; + unsafe fn try_cast_raw(self) -> Result, PodCastError> { + Ok(match self { + Self::Borrowed(x) => RawCow::Borrowed(x.try_cast_raw()?), + Self::Owned(x) => RawCow::Owned( + transmute_copy::, Dst>(&ManuallyDrop::new(x)), + ), + }) + } +} +#[cfg(feature = "extern_crate_alloc")] +unsafe impl TryCastRaw> for RawCow<[Src]> +where + Src: NoUninit, + Dst: AnyBitPattern, +{ + const ASSERT: () = as TryCastRaw>>::ASSERT; + unsafe fn try_cast_raw(self) -> Result, PodCastError> { + match self { + Self::Borrowed(x) => Ok(RawCow::Borrowed(x.try_cast_raw()?)), + Self::Owned(x) if align_of::() == align_of::() => { + Ok(RawCow::Owned(x.try_cast_raw()?)) + } + Self::Owned(_) => Err(PodCastError::AlignmentMismatch), + } + } +} + +/// Checks any constraints the container requires when casting between types. +pub trait AssertClassContraints { + const ASSERT: () = (); +} +impl AssertClassContraints for RefT {} +impl AssertClassContraints for PtrT {} +impl AssertClassContraints for NonNullT {} +impl AssertClassContraints for AtomicPtrT {} +impl AssertClassContraints for OptionT +where + C: AssertClassContraints, +{ + const ASSERT: () = C::ASSERT; +} +impl AssertClassContraints for PinT +where + C: AssertClassContraints, +{ + const ASSERT: () = C::ASSERT; +} +#[cfg(feature = "extern_crate_alloc")] +impl + AssertClassContraints for BoxT +{ + const ASSERT: () = assert!( + Src::ALIGN == Dst::ALIGN, + "Attempted conversion between `Box` types with different alignments" + ); +} +#[cfg(feature = "extern_crate_alloc")] +impl + AssertClassContraints for RcT +{ + const ASSERT: () = assert!( + Src::ALIGN == Dst::ALIGN, + "Attempted conversion between `Rc` types with different alignments" + ); +} +#[cfg(feature = "extern_crate_alloc")] +impl AssertClassContraints for RcWeakT { + const ASSERT: () = assert!( + Src::ALIGN == Dst::ALIGN, + "Attempted conversion between `rc::Weak` types with different alignments" + ); +} +#[cfg(feature = "extern_crate_alloc")] +impl + AssertClassContraints for ArcT +{ + const ASSERT: () = assert!( + Src::ALIGN == Dst::ALIGN, + "Attempted conversion between `Arc` types with different alignments" + ); +} +#[cfg(feature = "extern_crate_alloc")] +impl AssertClassContraints for ArcWeakT { + const ASSERT: () = assert!( + Src::ALIGN == Dst::ALIGN, + "Attempted conversion between `sync::Weak` types with different alignments" + ); +} +#[cfg(feature = "extern_crate_alloc")] +impl AssertClassContraints for VecT { + const ASSERT: () = assert!( + Src::ALIGN == Dst::ALIGN, + "Attempted conversion between `Vec` types with different alignments" + ); +} +#[cfg(feature = "extern_crate_alloc")] +impl< + Src: ?Sized + RawToOwned + ItemLayout, + Dst: ?Sized + RawToOwned + ItemLayout, + > AssertClassContraints for CowT +{ +} + +/// Safe byte-wise conversion between two values which contain some number of +/// values without allocation. This conversion should not fail for any reason. +/// +/// This supports the following conversions: +/// * `&[mut] T`/`&[mut] [T]` -> `&U`/`&[U]` +/// * `&mut T`/`&mut [T]` -> `&mut U`/`&mut [T]` +/// * `*[const|mut] T` -> `*const U` +/// * `*mut T` -> `*mut U` +/// * `NonNull`/`NonNull<[T]>` -> `NonNull`/`NonNull<[U]>` (slice version +/// requires the `non_null_slice_cast` feature) +/// * `AtomicPtr` -> `AtomicPtr` +/// * `Pin` -> `Pin` where `T` -> `U` is valid +/// * `Option` -> `Option` where `T` -> `U` is valid +/// +/// With the `extern_crate_alloc` feature the following are also supported: +/// `Box`/`Box<[T]>` -> `Box`/`Box` +/// `Rc`/`Rc<[T]>` -> `Rc`/`Rc` +/// `rc::Weak`/`rc::Weak<[T]>` -> `rc::Weak`/`rc::Weak` +/// `Arc`/`Arc<[T]>` -> `Arc`/`Arc` +/// `sync::Weak` -> `sync::Weak` +/// `Vec` -> `Vec` +/// `Cow` -> `Cow` +/// `Cow<[T]>` -> `Cow` +/// +/// This requires the `unified_cast` feature to be enabled and a rust version +/// `>=1.57`. +pub trait ReinterpretInner<'a, Dst: 'a>: 'a + Sized { + /// Performs the conversion. + fn reinterpret_inner(self) -> Dst; +} +impl<'a, Src: 'a, Dst: 'a> ReinterpretInner<'a, Dst> for Src +where + Src: Container<'a, Class = Dst::Class>, + Dst: Container<'a>, + Src::Class: AssertClassContraints, + Src::Raw: CastRaw, +{ + #[allow(path_statements, clippy::no_effect)] + fn reinterpret_inner(self) -> Dst { + >::ASSERT; + >::ASSERT; + unsafe { Dst::from_raw(self.into_raw().cast_raw()) } + } +} + +/// Attempt at a safe byte-wise conversion between two values which contain some +/// number of values. This conversion may fail due runtime conditions such as +/// size and alignment errors. +/// +/// This supports the following conversions: +/// * `&[mut] T`/`&[mut] [T]` -> `&U`/`&[U]` +/// * `&mut T`/`&mut [T]` -> `&mut U`/`&mut [T]` +/// * `*[const|mut] T` -> `*const U` +/// * `*mut T` -> `*mut U` +/// * `NonNull`/`NonNull<[T]>` -> `NonNull`/`NonNull<[U]>` (slice version +/// requires the `non_null_slice_cast` feature) +/// * `AtomicPtr` -> `AtomicPtr` +/// * `Pin` -> `Pin` where `T` -> `U` is valid +/// * `Option` -> `Option` where `T` -> `U` is valid +/// +/// With the `extern_crate_alloc` feature the following are also supported: +/// `Box`/`Box<[T]>` -> `Box`/`Box` +/// `Rc`/`Rc<[T]>` -> `Rc`/`Rc` +/// `rc::Weak`/`rc::Weak<[T]>` -> `rc::Weak`/`rc::Weak` +/// `Arc`/`Arc<[T]>` -> `Arc`/`Arc` +/// `sync::Weak` -> `sync::Weak` +/// `Vec` -> `Vec` +/// `Cow` -> `Cow` +/// `Cow<[T]>` -> `Cow` +/// +/// This requires the `unified_cast` feature to be enabled and a rust version +/// `>=1.57`. +pub trait TryReinterpretInner<'a, Dst: 'a>: 'a + Sized { + /// The type returned in the event of a conversion error. + type Error; + /// Perform the conversion. + fn try_reinterpret_inner(self) -> Result; +} +impl<'a, Src: 'a, Dst: 'a> TryReinterpretInner<'a, Dst> for Src +where + Src: Container<'a, Class = Dst::Class>, + Dst: Container<'a>, + Src::Class: AssertClassContraints, + Src::Raw: TryCastRaw, +{ + type Error = >::Err; + #[allow(path_statements, clippy::no_effect)] + fn try_reinterpret_inner(self) -> Result { + >::ASSERT; + >::ASSERT; + let raw = self.into_raw(); + match unsafe { raw.try_cast_raw() } { + Ok(raw) => Ok(unsafe { Dst::from_raw(raw) }), + Err(e) => { + Err(>::cast_error_with_value( + e, + unsafe { Src::from_raw(raw) }, + )) + } + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 7ee1f90..76e5689 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -125,6 +125,13 @@ pub use offset_of::*; mod transparent; pub use transparent::*; +#[cfg(feature = "unified_cast")] +mod cast; +#[cfg(feature = "unified_cast")] +pub use cast::{ + Reinterpret, ReinterpretInner, TryReinterpret, TryReinterpretInner, +}; + #[cfg(feature = "derive")] pub use bytemuck_derive::{ AnyBitPattern, ByteEq, ByteHash, CheckedBitPattern, Contiguous, NoUninit, diff --git a/tests/unified_cast_alloc_tests.rs b/tests/unified_cast_alloc_tests.rs new file mode 100644 index 0000000..d3acbeb --- /dev/null +++ b/tests/unified_cast_alloc_tests.rs @@ -0,0 +1,285 @@ +#![cfg(all(feature = "unified_cast", feature = "extern_crate_alloc"))] + +extern crate alloc; + +use alloc::{ + borrow::Cow, + boxed::Box, + rc::{Rc, Weak as RcWeak}, + sync::{Arc, Weak as ArcWeak}, + vec::Vec, +}; +use bytemuck::{ + PodCastError::{AlignmentMismatch, OutputSliceWouldHaveSlop, SizeMismatch}, + ReinterpretInner, TryReinterpretInner, +}; +use core::convert::identity; + +macro_rules! test_assert { + ($target:expr, $init:expr) => { + assert_eq!($target, $init); + }; + ($target:expr, $_init:expr, $e:expr) => { + assert_eq!($target, $e); + }; +} + +// Test both `ReinterpretInner` and `TryReinterpretInner` +macro_rules! test_reinterpret_inner { + ( + $init:expr => $ty:ty $(= $assert_val:expr)? + ) => {{ + test_assert!(identity::<$ty>($init.reinterpret_inner()), $init $(, $assert_val)?); + test_assert!(identity::<$ty>($init.try_reinterpret_inner().unwrap()), $init $(, $assert_val)?); + }}; +} + +// Test both `ReinterpretInner` and `TryReinterpretInner` +macro_rules! test_try_reinterpret_inner { + ( + $init:expr => $ty:ty $(= $assert_val:expr)? + ) => {{ + test_assert!(identity::>($init.try_reinterpret_inner()), $init $(, $assert_val)?); + }}; +} + +#[test] +fn reinterpret_inner_self() { + test_reinterpret_inner!(Cow::Borrowed(&1u32) => Cow); + test_reinterpret_inner!(Cow::::Owned(1u32) => Cow); + test_reinterpret_inner!(Cow::Borrowed([1u32].as_slice()) => Cow<[u32]>); + test_reinterpret_inner!(Cow::<[u32]>::Owned(vec![1u32]) => Cow<[u32]>); + test_reinterpret_inner!(Box::new(1u32) => Box); + test_reinterpret_inner!(vec![1u32].into_boxed_slice() => Box<[u32]>); + test_reinterpret_inner!(Rc::new(1u32) => Rc); + test_reinterpret_inner!(Rc::<[u32]>::from([1u32].as_slice()) => Rc<[u32]>); + let _: RcWeak = Rc::downgrade(&Rc::new(1u32)).reinterpret_inner(); + let _: RcWeak = + Rc::downgrade(&Rc::new(1u32)).try_reinterpret_inner().unwrap(); + test_reinterpret_inner!(Arc::new(1u32) => Arc); + test_reinterpret_inner!(Arc::<[u32]>::from([1u32].as_slice()) => Arc<[u32]>); + let _: ArcWeak = Arc::downgrade(&Arc::new(1u32)).reinterpret_inner(); + let _: ArcWeak = + Arc::downgrade(&Arc::new(1u32)).try_reinterpret_inner().unwrap(); + test_reinterpret_inner!(vec![1u32] => Vec); +} + +#[test] +fn reinterpret_inner_same_align() { + test_reinterpret_inner!(Cow::Borrowed(&1u32) => Cow = Cow::Borrowed(&1i32)); + test_reinterpret_inner!(Cow::::Owned(1u32) => Cow = Cow::::Owned(1i32)); + test_reinterpret_inner!( + Cow::Borrowed([1u32].as_slice()) => Cow<[i32]> + = Cow::Borrowed([1i32].as_slice()) + ); + test_reinterpret_inner!( + Cow::<[u32]>::Owned(vec![1u32]) => Cow<[i32]> + = Cow::<[i32]>::Owned(vec![1i32]) + ); + test_reinterpret_inner!(Box::new(1u32) => Box = Box::new(1i32)); + test_reinterpret_inner!( + vec![1u32].into_boxed_slice() => Box<[i32]> + = vec![1i32].into_boxed_slice() + ); + test_reinterpret_inner!(Rc::new(1u32) => Rc = Rc::new(1i32)); + test_reinterpret_inner!( + Rc::<[u32]>::from([1u32].as_slice()) => Rc<[i32]> + = Rc::<[i32]>::from([1i32].as_slice()) + ); + let _: RcWeak = Rc::downgrade(&Rc::new(1u32)).reinterpret_inner(); + let _: RcWeak = + Rc::downgrade(&Rc::new(1u32)).try_reinterpret_inner().unwrap(); + test_reinterpret_inner!(Arc::new(1u32) => Arc = Arc::new(1i32)); + test_reinterpret_inner!( + Arc::<[u32]>::from([1u32].as_slice()) => Arc<[i32]> + = Arc::<[i32]>::from([1i32].as_slice()) + ); + let _: ArcWeak = Arc::downgrade(&Arc::new(1u32)).reinterpret_inner(); + let _: ArcWeak = + Arc::downgrade(&Arc::new(1u32)).try_reinterpret_inner().unwrap(); + test_reinterpret_inner!(vec![1u32] => Vec = vec![1i32]); +} + +#[test] +fn reinterpret_inner_lesser_align() { + test_reinterpret_inner!(Cow::Borrowed(&0u32) => Cow<[u16; 2]> = Cow::Borrowed(&[0u16; 2])); + test_reinterpret_inner!( + Cow::::Owned(0u32) => Cow<[u16; 2]> + = Cow::<[u16; 2]>::Owned([0u16; 2]) + ); + test_try_reinterpret_inner!( + Cow::<[u32]>::Owned(vec![0u32; 2]) => Cow<[u8]> + = Err((AlignmentMismatch, Cow::<[u32]>::Owned(vec![0u32; 2]))) + ); +} + +#[test] +fn reinterpret_inner_no_uninit() { + test_reinterpret_inner!(Cow::Borrowed(&false) => Cow = Cow::Borrowed(&0)); + test_reinterpret_inner!( + Cow::::Owned(false) => Cow + = Cow::::Owned(0u8) + ); + test_reinterpret_inner!( + Cow::Borrowed([false].as_slice()) => Cow<[u8]> + = Cow::Borrowed([0u8].as_slice()) + ); + test_reinterpret_inner!( + Cow::<[bool]>::Owned(vec![false]) => Cow<[u8]> + = Cow::<[u8]>::Owned(vec![0u8]) + ); + test_reinterpret_inner!(Box::new(false) => Box = Box::new(0u8)); + test_reinterpret_inner!( + vec![false].into_boxed_slice() => Box<[u8]> + = vec![0u8].into_boxed_slice() + ); + test_reinterpret_inner!(Rc::new(false) => Rc = Rc::new(0u8)); + test_reinterpret_inner!( + Rc::<[bool]>::from([false].as_slice()) => Rc<[u8]> + = Rc::<[u8]>::from([0u8].as_slice()) + ); + let _: RcWeak = Rc::downgrade(&Rc::new(false)).reinterpret_inner(); + let _: RcWeak = + Rc::downgrade(&Rc::new(false)).try_reinterpret_inner().unwrap(); + test_reinterpret_inner!(Arc::new(false) => Arc = Arc::new(0u8)); + test_reinterpret_inner!( + Arc::<[bool]>::from([false].as_slice()) => Arc<[u8]> + = Arc::<[u8]>::from([0u8].as_slice()) + ); + let _: ArcWeak = Arc::downgrade(&Arc::new(false)).reinterpret_inner(); + let _: ArcWeak = + Arc::downgrade(&Arc::new(false)).try_reinterpret_inner().unwrap(); + test_reinterpret_inner!(vec![false] => Vec = vec![0u8]); +} + +#[test] +fn reinterpret_inner_unsize() { + test_reinterpret_inner!( + Box::new([0u32]) => Box<[u32]> + = vec!(0u32).into_boxed_slice() + ); + test_reinterpret_inner!( + Rc::new([0u32]) => Rc<[u32]> + = Rc::<[u32]>::from([0u32].as_slice()) + ); + test_reinterpret_inner!( + Arc::new([0u32]) => Arc<[u32]> + = Arc::<[u32]>::from([0u32].as_slice()) + ); +} + +#[test] +fn try_reinterpret_inner_misaligned() { + let x = [0u32; 2]; + let x: &[u8] = x.as_slice().reinterpret_inner(); + let x = &x[1..5]; + test_try_reinterpret_inner!( + Cow::<[u8; 4]>::Borrowed(x.try_reinterpret_inner().unwrap()) => Cow + = Err(AlignmentMismatch) + ); + test_try_reinterpret_inner!( + Cow::<[u8]>::Borrowed(x) => Cow<[u32]> + = Err((AlignmentMismatch, Cow::<[u8]>::Borrowed(x))) + ); +} + +#[test] +fn try_reinterpret_inner_change_align() { + let x = [0u32; 2]; + let x: &[u8; 8] = (&x).reinterpret_inner(); + test_try_reinterpret_inner!( + Cow::<[u8; 8]>::Borrowed(x) => Cow<[u32; 2]> + = Ok(Cow::<[u32; 2]>::Borrowed(&[0u32; 2])) + ); + test_try_reinterpret_inner!( + Cow::<[u8]>::Borrowed(x) => Cow<[u32]> + = Ok(Cow::<[u32]>::Borrowed([0u32; 2].as_slice())) + ); + test_try_reinterpret_inner!( + Cow::<[u8]>::Owned(vec![0u8; 4]) => Cow<[u32]> + = Err((AlignmentMismatch, Cow::<[u8]>::Owned(vec![0u8; 4]))) + ); +} + +#[test] +fn try_reinterpret_change_element_size() { + let x = [[0u16; 2]; 2].as_slice(); + let y = [0u16; 4].as_slice(); + test_try_reinterpret_inner!( + Cow::<[[u16; 2]]>::Borrowed(x) => Cow<[u16]> + = Ok(Cow::<[u16]>::Borrowed(y)) + ); + test_try_reinterpret_inner!( + Cow::<[[u16; 2]]>::Owned(Vec::from(x)) => Cow<[u16]> + = Ok(Cow::<[u16]>::Owned(Vec::from(y))) + ); + test_try_reinterpret_inner!( + Box::<[[u16; 2]]>::from(x) => Box<[u16]> + = Ok(Box::<[u16]>::from(y)) + ); + test_try_reinterpret_inner!( + Rc::<[[u16; 2]]>::from(x) => Rc<[u16]> + = Ok(Rc::<[u16]>::from(y)) + ); + test_try_reinterpret_inner!( + Arc::<[[u16; 2]]>::from(x) => Arc<[u16]> + = Ok(Arc::<[u16]>::from(y)) + ); + test_try_reinterpret_inner!( + Vec::<[u16; 2]>::from(x) => Vec + = Ok(Vec::::from(y)) + ); +} + +#[test] +fn try_reinterpret_wrong_element_size() { + let x = [[0u16; 2]; 2].as_slice(); + test_try_reinterpret_inner!( + Cow::<[[u16; 2]]>::Borrowed(x) => Cow<[[u16; 3]]> + = Err((OutputSliceWouldHaveSlop, Cow::<[[u16; 2]]>::Borrowed(x))) + ); + test_try_reinterpret_inner!( + Cow::<[[u16; 2]]>::Owned(Vec::from(x)) => Cow<[[u16; 3]]> + = Err((OutputSliceWouldHaveSlop, Cow::<[[u16; 2]]>::Owned(Vec::from(x)))) + ); + test_try_reinterpret_inner!( + Box::<[[u16; 2]]>::from(x) => Box<[[u16; 3]]> + = Err((OutputSliceWouldHaveSlop, Box::<[[u16; 2]]>::from(x))) + ); + test_try_reinterpret_inner!( + Rc::<[[u16; 2]]>::from(x) => Rc<[[u16; 3]]> + = Err((OutputSliceWouldHaveSlop, Rc::<[[u16; 2]]>::from(x))) + ); + test_try_reinterpret_inner!( + Arc::<[[u16; 2]]>::from(x) => Arc<[[u16; 3]]> + = Err((OutputSliceWouldHaveSlop, Arc::<[[u16; 2]]>::from(x))) + ); + test_try_reinterpret_inner!( + Vec::<[u16; 2]>::from(x) => Vec<[u16; 3]> + = Err((OutputSliceWouldHaveSlop, Vec::<[u16; 2]>::from(x))) + ); + let mut x = Vec::with_capacity(4); + x.extend([[0u16; 2]; 3]); + test_try_reinterpret_inner!( + x => Vec<[u16; 3]> + = Err((OutputSliceWouldHaveSlop, vec![[0u16; 2]; 3])) + ); +} + +#[test] +fn try_reinterpret_inner_resize() { + let x: Box<[u32]> = Box::new(0u32).reinterpret_inner(); + test_try_reinterpret_inner!(x => Box = Ok(Box::new(0u32))); + let x: Box<[u32]> = Box::new(0u32).reinterpret_inner(); + test_try_reinterpret_inner!(x => Box<[u32; 2]> = Err((SizeMismatch, Box::from([0u32].as_slice())))); + + let x: Rc<[u32]> = Rc::new(0u32).reinterpret_inner(); + test_try_reinterpret_inner!(x => Rc = Ok(Rc::new(0u32))); + let x: Rc<[u32]> = Rc::new(0u32).reinterpret_inner(); + test_try_reinterpret_inner!(x => Rc<[u32; 2]> = Err((SizeMismatch, Rc::from([0u32].as_slice())))); + + let x: Arc<[u32]> = Arc::new(0u32).reinterpret_inner(); + test_try_reinterpret_inner!(x => Arc = Ok(Arc::new(0u32))); + let x: Arc<[u32]> = Arc::new(0u32).reinterpret_inner(); + test_try_reinterpret_inner!(x => Arc<[u32; 2]> = Err((SizeMismatch, Arc::from([0u32].as_slice())))); +} diff --git a/tests/unified_cast_tests.rs b/tests/unified_cast_tests.rs new file mode 100644 index 0000000..2246155 --- /dev/null +++ b/tests/unified_cast_tests.rs @@ -0,0 +1,383 @@ +#![cfg(feature = "unified_cast")] + +use bytemuck::{ + PodCastError::{AlignmentMismatch, OutputSliceWouldHaveSlop, SizeMismatch}, + Reinterpret, ReinterpretInner, TryReinterpret, TryReinterpretInner, +}; +#[cfg(feature = "non_null_slice_cast")] +use core::ptr; +use core::{ + convert::identity, num::NonZeroI32, pin::Pin, ptr::NonNull, + sync::atomic::AtomicPtr, +}; + +macro_rules! test_assert { + ($target:expr, $init:expr) => { + assert_eq!($target, $init); + }; + ($target:expr, $_init:expr, $e:expr) => { + assert_eq!($target, $e); + }; +} + +// Test both `Reinterpret` and `TryReinterpret` +macro_rules! test_reinterpret { + ( + $init:expr => $ty:ty $(= $assert_val:expr)? + ) => {{ + test_assert!(identity::<$ty>($init.reinterpret()), $init $(, $assert_val)?); + test_assert!(identity::<$ty>($init.try_reinterpret().unwrap()), $init $(, $assert_val)?); + }}; +} + +// Test both `TryReinterpret` +macro_rules! test_try_reinterpret { + ( + $init:expr => $ty:ty $(= $assert_val:expr)? + ) => {{ + test_assert!(identity::>($init.try_reinterpret()), Some($init) $(, $assert_val)?); + }}; +} + +// Test both `ReinterpretInner` and `TryReinterpretInner` +macro_rules! test_reinterpret_inner { + ( + $init:expr => $ty:ty $(= $assert_val:expr)? + ) => {{ + test_assert!(identity::<$ty>($init.reinterpret_inner()), $init $(, $assert_val)?); + test_assert!(identity::<$ty>($init.try_reinterpret_inner().unwrap()), $init $(, $assert_val)?); + }}; +} + +// Test both `ReinterpretInner` and `TryReinterpretInner` +macro_rules! test_try_reinterpret_inner { + ( + $init:expr => $ty:ty $(= $assert_val:expr)? + ) => {{ + test_assert!(identity::>($init.try_reinterpret_inner()), $init $(, $assert_val)?); + }}; +} + +#[test] +fn reinterpret_self() { + test_reinterpret!(0u8 => u8); + test_reinterpret!(() => ()); + test_reinterpret!([0i32, 1i32, 2i32] => [i32; 3]); +} + +#[test] +fn reinterpret_same_align() { + test_reinterpret!(1u8 => i8 = 1i8); + test_reinterpret!( + [u32::MAX, 0, 1] => [i32; 3] + = [i32::from_ne_bytes(u32::MAX.to_ne_bytes()), 0, 1] + ); + test_reinterpret!(0u32 => Option = None); +} + +#[test] +fn reinterpret_lesser_align() { + test_reinterpret!(0u16 => [u8; 2] = [0u8; 2]); + test_reinterpret!([0u64; 2] => [u16; 8] = [0u16; 8]); +} + +#[test] +fn reinterpret_greater_align() { + test_reinterpret!([0u16; 2] => u32 = 0); +} + +#[test] +fn reinterpret_no_uninit() { + test_reinterpret!(true => u8 = 1); +} + +#[test] +fn try_reinterpret() { + test_try_reinterpret!(0u8 => bool = Some(false)); + test_try_reinterpret!(2u8 => bool = None); +} + +#[test] +fn reinterpret_inner_self() { + test_reinterpret_inner!(&() => &()); + test_reinterpret_inner!([0u32; 2].as_slice() => &[u32]); + test_reinterpret_inner!(&mut 1u32 => &mut u32); + test_reinterpret_inner!([1i32, 4i32].as_mut_slice() => &mut [i32]); + + let x = &[0u8; 2] as *const [u8; 2]; + test_reinterpret_inner!(x => *const [u8; 2]); + let x = &mut [0u8; 2] as *mut [u8; 2]; + test_reinterpret_inner!(x => *mut [u8; 2]); + let x = NonNull::from(&5u64); + test_reinterpret_inner!(x => NonNull); + + test_reinterpret_inner!(Some(&0u8) => Option<&u8>); + test_reinterpret_inner!(Some(Some([0u8; 4].as_slice())) => Option>); + test_reinterpret_inner!(Option::<&u8>::None => Option<&u8>); + + let x = Some(NonNull::from(&0i32)); + test_reinterpret_inner!(x => Option>); + + let _: AtomicPtr = AtomicPtr::new(&mut 1i8).reinterpret_inner(); + let _: AtomicPtr = + AtomicPtr::new(&mut 1i8).try_reinterpret_inner().unwrap(); + + test_reinterpret_inner!(Pin::new(&1u8) => Pin<&u8>); + test_reinterpret_inner!(Pin::new([0u16; 2].as_slice()) => Pin<&[u16]>); +} + +#[test] +fn reinterpret_inner_same_align() { + test_reinterpret_inner!(&50u8 => &i8 = &50); + test_reinterpret_inner!([0u32; 2].as_slice() => &[i32] = [0; 2]); + test_reinterpret_inner!( + &mut 1f32 => &mut u32 + = &mut u32::from_ne_bytes(1f32.to_ne_bytes()) + ); + test_reinterpret_inner!([1i32, 4i32].as_mut_slice() => &mut [u32] = [1u32, 4u32]); + + let x = &[0u8; 2] as *const [u8; 2]; + test_reinterpret_inner!(x => *const [i8; 2] = x.cast()); + let x = &mut [0u8; 2] as *mut [u8; 2]; + test_reinterpret_inner!(x => *mut [i8; 2] = x.cast()); + let x = NonNull::from(&5u64); + test_reinterpret_inner!(x => NonNull = x.cast()); + + test_reinterpret_inner!(Some(&0u8) => Option<&i8> = Some(&0i8)); + test_reinterpret_inner!( + Some(Some([127u8; 4].as_slice())) => Option> + = Some(Some([127i8; 4].as_slice())) + ); + test_reinterpret_inner!(Option::<&u8>::None => Option<&u8> = None); + + let x = Some(NonNull::from(&0i32)); + test_reinterpret_inner!(x => Option> = x.map(|x| x.cast())); + + let _: AtomicPtr = AtomicPtr::new(&mut 1i8).reinterpret_inner(); + let _: AtomicPtr = + AtomicPtr::new(&mut 1i8).try_reinterpret_inner().unwrap(); + + test_reinterpret_inner!(Pin::new(&1u8) => Pin<&i8> = Pin::new(&1i8)); + test_reinterpret_inner!( + Pin::new([0xFFFFu16; 2].as_slice()) => Pin<&[i16]> + = Pin::new([i16::from_ne_bytes(0xFFFFu16.to_ne_bytes()); 2].as_slice()) + ); +} + +#[test] +fn reinterpret_inner_lesser_align() { + test_reinterpret_inner!(&0xFF01u16 => &[u8; 2] = &0xFF01u16.to_ne_bytes()); + test_reinterpret_inner!([0u32; 2].as_slice() => &[[u16; 2]] = [[0; 2]; 2]); + test_reinterpret_inner!(&mut 1f32 => &mut [u8; 4] = &1f32.to_ne_bytes()); + test_reinterpret_inner!( + [1i32, 4i32].as_mut_slice() => &mut [[u8; 4]] + = &[1u32.to_ne_bytes(), 4u32.to_ne_bytes()] + ); + + let x = &[0u64; 2] as *const [u64; 2]; + test_reinterpret_inner!(x => *const [[u32; 2]; 2] = x.cast()); + let x = &mut 0u16 as *mut u16; + test_reinterpret_inner!(x => *mut [i8; 2] = x.cast()); + let x = NonNull::from(&5u64); + test_reinterpret_inner!(x => NonNull<[f32; 2]> = x.cast()); + + test_reinterpret_inner!(Some(&0u32) => Option<&[u8; 4]> = Some(&[0; 4])); + test_reinterpret_inner!( + Some(Some([127u16; 2].as_slice())) => Option> + = Some(Some([127u16.to_ne_bytes(); 2].as_slice())) + ); + test_reinterpret_inner!(Option::<&u16>::None => Option<&[u8; 2]> = None); + + let x = Some(NonNull::from(&0i32)); + test_reinterpret_inner!(x => Option> = x.map(|x| x.cast())); + + let _: AtomicPtr<[u8; 2]> = AtomicPtr::new(&mut 0u16).reinterpret_inner(); + let _: AtomicPtr<[u8; 2]> = + AtomicPtr::new(&mut 0u16).try_reinterpret_inner().unwrap(); + + test_reinterpret_inner!( + Pin::new(&1u16) => Pin<&[u8; 2]> + = Pin::new(&1u16.to_ne_bytes()) + ); + test_reinterpret_inner!( + Pin::new([0xFF00u16; 2].as_slice()) => Pin<&[[u8; 2]]> + = Pin::new([0xFF00u16.to_ne_bytes(); 2].as_slice()) + ); +} + +#[test] +fn reinterpret_inner_no_uninit() { + test_reinterpret_inner!(&true => &u8 = &1); + test_reinterpret_inner!([true, false].as_slice() => &[u8] = [1, 0]); + let x = &true as *const bool; + test_reinterpret_inner!(x => *const u8 = x.cast()); + test_reinterpret_inner!(Some(&true) => Option<&u8> = Some(&1)); + test_reinterpret_inner!(Some(Some(&true)) => Option> = Some(Some(&1))); + test_reinterpret_inner!(Pin::new(&true) => Pin<&u8> = Pin::new(&1)); +} + +#[test] +fn reinterpret_inner_unsize() { + test_reinterpret_inner!(&0u32 => &[u8] = [0; 4]); + test_reinterpret_inner!(&0u32 => &[u16] = [0; 2]); + test_reinterpret_inner!(&mut 0xFFEEDDCCu32 => &mut [u8] = 0xFFEEDDCCu32.to_ne_bytes()); + test_reinterpret_inner!(&mut 0u32 => &mut [u16] = [0; 2]); + #[cfg(feature = "non_null_slice_cast")] + { + let x = NonNull::from(&mut 0u32); + test_reinterpret_inner!( + x => NonNull<[u8]> + = NonNull::new(ptr::slice_from_raw_parts_mut(x.as_ptr().cast(), 4)).unwrap() + ); + test_reinterpret_inner!( + x => NonNull<[u16]> + = NonNull::new(ptr::slice_from_raw_parts_mut(x.as_ptr().cast(), 2)).unwrap() + ); + } +} + +#[test] +fn try_reinterpret_inner_misaligned() { + let x = [0u32, 0u32]; + let x: &[u16; 4] = (&x).reinterpret_inner(); + let x: &[u16; 2] = x[1..3].try_reinterpret_inner().unwrap(); + test_try_reinterpret_inner!(x => &u32 = Err(AlignmentMismatch)); + test_try_reinterpret_inner!(x.as_slice() => &[u32] = Err(AlignmentMismatch)); + test_try_reinterpret_inner!(Pin::new(x) => Pin<&u32> = Err(AlignmentMismatch)); + test_try_reinterpret_inner!(x as *const [u16; 2] => *const u32 = Err(AlignmentMismatch)); + test_try_reinterpret_inner!(Some(x) => Option<&u32> = Err(AlignmentMismatch)); + + let mut x = [0u32, 0u32]; + let x: &mut [u16; 4] = (&mut x).reinterpret_inner(); + let x: &mut [u16; 2] = (&mut x[1..3]).try_reinterpret_inner().unwrap(); + test_try_reinterpret_inner!(x => &mut u32 = Err(AlignmentMismatch)); + test_try_reinterpret_inner!(x.as_mut_slice() => &mut [u32] = Err(AlignmentMismatch)); + test_try_reinterpret_inner!(Pin::new(&mut *x) => Pin<&mut u32> = Err(AlignmentMismatch)); + test_try_reinterpret_inner!(x as *mut [u16; 2] => *mut u32 = Err(AlignmentMismatch)); + test_try_reinterpret_inner!(Some(&mut *x) => Option<&mut u32> = Err(AlignmentMismatch)); + test_try_reinterpret_inner!(NonNull::from(&mut *x) => NonNull = Err(AlignmentMismatch)); + let err: Result, _> = + AtomicPtr::new(&mut *x).try_reinterpret_inner(); + assert!(matches!(err, Err(AlignmentMismatch))); + #[cfg(feature = "non_null_slice_cast")] + { + test_try_reinterpret_inner!(NonNull::from(x.as_mut_slice()) => NonNull<[u32]> = Err(AlignmentMismatch)); + } +} + +#[test] +fn try_reinterpret_inner_greater_align() { + let x = 0u32; + let y: &[u16; 2] = (&x).reinterpret_inner(); + test_try_reinterpret_inner!(y => &u32 = Ok(&x)); + test_try_reinterpret_inner!(y.as_slice() => &[u32] = Ok([x].as_slice())); + test_try_reinterpret_inner!(Pin::new(y) => Pin<&u32> = Ok(Pin::new(&x))); + test_try_reinterpret_inner!(y as *const [u16; 2] => *const u32 = Ok(&x as *const u32)); + test_try_reinterpret_inner!(NonNull::from(y) => NonNull = Ok(NonNull::from(&x))); + test_try_reinterpret_inner!(Some(y) => Option<&u32> = Ok(Some(&x))); + + let mut x = 0u32; + let ptr = &mut x as *mut u32; + let y: &mut [u16; 2] = (&mut x).reinterpret_inner(); + test_try_reinterpret_inner!(y => &mut u32 = Ok(&mut 0)); + test_try_reinterpret_inner!(y.as_mut_slice() => &mut [u32] = Ok([0].as_mut_slice())); + test_try_reinterpret_inner!(Pin::new(&mut *y) => Pin<&mut u32> = Ok(Pin::new(&mut 0))); + test_try_reinterpret_inner!(y as *mut [u16; 2] => *mut u32 = Ok(ptr)); + test_try_reinterpret_inner!(Some(&mut *y) => Option<&mut u32> = Ok(Some(&mut 0))); + let _: AtomicPtr = + AtomicPtr::new(&mut *y).try_reinterpret_inner().unwrap(); + #[cfg(feature = "non_null_slice_cast")] + { + test_try_reinterpret_inner!( + NonNull::from(y.as_mut_slice()) => NonNull<[u32]> + = Ok(NonNull::new(ptr::slice_from_raw_parts_mut(ptr, 1)).unwrap()) + ); + } +} + +#[test] +fn try_reinterpret_change_element_size() { + test_try_reinterpret_inner!( + [0u32; 3].as_slice() => &[[u8; 3]] + = Ok([[0; 3]; 4].as_slice()) + ); + test_try_reinterpret_inner!( + [0u32; 1].as_slice() => &[[u8; 2]] + = Ok([[0; 2]; 2].as_slice()) + ); + test_try_reinterpret_inner!( + [0u32; 3].as_mut_slice() => &mut [[u8; 3]] + = Ok([[0; 3]; 4].as_mut_slice()) + ); + test_try_reinterpret_inner!( + [0u32; 1].as_mut_slice() => &mut [[u8; 2]] + = Ok([[0; 2]; 2].as_mut_slice()) + ); + test_try_reinterpret_inner!( + Some([0u32; 3].as_slice()) => Option<&[[u8; 3]]> + = Ok(Some([[0; 3]; 4].as_slice())) + ); + test_try_reinterpret_inner!( + Pin::new([0u32; 1].as_slice()) => Pin<&[[u8; 2]]> + = Ok(Pin::new([[0; 2]; 2].as_slice())) + ); + #[cfg(feature = "non_null_slice_cast")] + { + let mut x = [0u32; 3]; + test_try_reinterpret_inner!( + NonNull::from(x.as_mut_slice()) => NonNull<[[u8; 3]]> + = Ok(NonNull::new(ptr::slice_from_raw_parts_mut(x.as_mut_ptr().cast::<[u8; 3]>(), 4)).unwrap()) + ); + } +} + +#[test] +fn try_reinterpret_wrong_element_size() { + test_try_reinterpret_inner!( + [0u32; 3].as_slice() => &[[u8; 5]] + = Err(OutputSliceWouldHaveSlop) + ); + test_try_reinterpret_inner!( + [0u32; 1].as_slice() => &[[u32; 2]] + = Err(OutputSliceWouldHaveSlop) + ); + test_try_reinterpret_inner!( + [0u32; 3].as_mut_slice() => &mut [[u8; 5]] + = Err(OutputSliceWouldHaveSlop) + ); + test_try_reinterpret_inner!( + [0u32; 1].as_mut_slice() => &mut [[u32; 2]] + = Err(OutputSliceWouldHaveSlop) + ); + test_try_reinterpret_inner!( + Some([0u32; 3].as_slice()) => Option<&[[u8; 5]]> + = Err(OutputSliceWouldHaveSlop) + ); + test_try_reinterpret_inner!( + Pin::new([0u32; 1].as_slice()) => Pin<&[[u32; 2]]> + = Err(OutputSliceWouldHaveSlop) + ); + #[cfg(feature = "non_null_slice_cast")] + { + let mut x = [0u32; 3]; + test_try_reinterpret_inner!( + NonNull::from(x.as_mut_slice()) => NonNull<[[u8; 5]]> + = Err(OutputSliceWouldHaveSlop) + ); + } +} + +#[test] +fn try_reinterpret_inner_resize() { + let x = 0u32; + let x: &[u8; 4] = (&x).reinterpret_inner(); + test_try_reinterpret_inner!(x.as_slice() => &u32 = Ok(&0)); + test_try_reinterpret_inner!(x.as_slice() => &[u16; 2] = Ok(&[0u16; 2])); + test_try_reinterpret_inner!(x.as_slice() => &[u32; 2] = Err(SizeMismatch)); + + let mut x = 0u32; + let x: &mut [u8; 4] = (&mut x).reinterpret_inner(); + test_try_reinterpret_inner!(x.as_mut_slice() => &mut u32 = Ok(&mut 0u32)); + test_try_reinterpret_inner!(x.as_mut_slice() => &mut [u16; 2] = Ok(&mut [0u16; 2])); + test_try_reinterpret_inner!(x.as_mut_slice() => &mut [u32; 2] = Err(SizeMismatch)); +} diff --git a/uitest/Cargo.toml b/uitest/Cargo.toml new file mode 100644 index 0000000..32d6156 --- /dev/null +++ b/uitest/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "uitest" +version = "0.0.0" +edition = "2018" +publish = false +rust-version = "1.63" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies.bytemuck] +path = ".." +features = ["extern_crate_alloc", "non_null_slice_cast", "unified_cast"] + +[dev-dependencies] +trybuild = "1" diff --git a/uitest/src/main.rs b/uitest/src/main.rs new file mode 100644 index 0000000..f328e4d --- /dev/null +++ b/uitest/src/main.rs @@ -0,0 +1 @@ +fn main() {} diff --git a/uitest/tests/ui.rs b/uitest/tests/ui.rs new file mode 100644 index 0000000..d2ba687 --- /dev/null +++ b/uitest/tests/ui.rs @@ -0,0 +1,8 @@ +use trybuild::TestCases; + +#[test] +fn ui() { + let t = TestCases::new(); + t.compile_fail("tests/ui/*.rs"); + t.pass("src/main.rs"); +} diff --git a/uitest/tests/ui/reinterpret_incompatible.rs b/uitest/tests/ui/reinterpret_incompatible.rs new file mode 100644 index 0000000..e765ebf --- /dev/null +++ b/uitest/tests/ui/reinterpret_incompatible.rs @@ -0,0 +1,24 @@ +use bytemuck::{ + Reinterpret, ReinterpretInner, TryReinterpret, TryReinterpretInner, +}; +use core::num::NonZeroU32; + +#[repr(C)] +struct Padded(u16, u8); + +fn main() { + let _: u32 = Padded(0, 0).reinterpret(); + let _: NonZeroU32 = 0u32.reinterpret(); + let _: u32 = Padded(0, 0).try_reinterpret().unwrap(); + let _: Padded = 0u32.try_reinterpret().unwrap(); + + // let _: &u32 = (&Padded(0, 0)).reinterpret_inner(); + // let _: &NonZeroU32 = (&0u32).reinterpret_inner(); + // let _: &u32 = (&Padded(0, 0)).try_reinterpret_inner().unwrap(); + // let _: &Padded = (&0u32).try_reinterpret_inner().unwrap(); + + // let _: &mut u32 = (&mut Padded(0, 0)).reinterpret_inner(); + // let _: &mut NonZeroU32 = (&mut 0u32).reinterpret_inner(); + // let _: &mut u32 = (&mut Padded(0, 0)).try_reinterpret_inner().unwrap(); + // let _: &mut Padded = (&mut 0u32).try_reinterpret_inner().unwrap(); +} diff --git a/uitest/tests/ui/reinterpret_incompatible.stderr b/uitest/tests/ui/reinterpret_incompatible.stderr new file mode 100644 index 0000000..69ca09c --- /dev/null +++ b/uitest/tests/ui/reinterpret_incompatible.stderr @@ -0,0 +1,98 @@ +warning: unused imports: `ReinterpretInner`, `TryReinterpretInner` + --> tests/ui/reinterpret_incompatible.rs:2:16 + | +2 | Reinterpret, ReinterpretInner, TryReinterpret, TryReinterpretInner, + | ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^ + | + = note: `#[warn(unused_imports)]` on by default + +error[E0599]: the method `reinterpret` exists for struct `Padded`, but its trait bounds were not satisfied + --> tests/ui/reinterpret_incompatible.rs:10:29 + | +7 | struct Padded(u16, u8); + | ------------- + | | + | method `reinterpret` not found for this struct + | doesn't satisfy `Padded: NoUninit` + | doesn't satisfy `Padded: Reinterpret<_>` +... +10 | let _: u32 = Padded(0, 0).reinterpret(); + | ^^^^^^^^^^^ method cannot be called on `Padded` due to unsatisfied trait bounds + | + = note: the following trait bounds were not satisfied: + `Padded: NoUninit` + which is required by `Padded: Reinterpret<_>` + `&Padded: NoUninit` + which is required by `&Padded: Reinterpret<_>` + `&mut Padded: NoUninit` + which is required by `&mut Padded: Reinterpret<_>` +note: the trait `NoUninit` must be implemented + --> $WORKSPACE/src/no_uninit.rs + | + | pub unsafe trait NoUninit: Sized + Copy + 'static {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0277]: the trait bound `NonZeroU32: Pod` is not satisfied + --> tests/ui/reinterpret_incompatible.rs:11:28 + | +11 | let _: NonZeroU32 = 0u32.reinterpret(); + | ^^^^^^^^^^^ the trait `Pod` is not implemented for `NonZeroU32` + | + = help: the following other types implement trait `Pod`: + () + ManuallyDrop + Option + PhantomData + PhantomPinned + Wrapping + [T; 0] + [T; 1024] + and $N others + = note: required for `NonZeroU32` to implement `AnyBitPattern` + = note: required for `u32` to implement `Reinterpret` + +error[E0599]: the method `try_reinterpret` exists for struct `Padded`, but its trait bounds were not satisfied + --> tests/ui/reinterpret_incompatible.rs:12:29 + | +7 | struct Padded(u16, u8); + | ------------- + | | + | method `try_reinterpret` not found for this struct + | doesn't satisfy `Padded: NoUninit` + | doesn't satisfy `Padded: TryReinterpret<_>` +... +12 | let _: u32 = Padded(0, 0).try_reinterpret().unwrap(); + | ^^^^^^^^^^^^^^^ method cannot be called on `Padded` due to unsatisfied trait bounds + | + = note: the following trait bounds were not satisfied: + `Padded: NoUninit` + which is required by `Padded: TryReinterpret<_>` + `&Padded: NoUninit` + which is required by `&Padded: TryReinterpret<_>` + `&mut Padded: NoUninit` + which is required by `&mut Padded: TryReinterpret<_>` +note: the trait `NoUninit` must be implemented + --> $WORKSPACE/src/no_uninit.rs + | + | pub unsafe trait NoUninit: Sized + Copy + 'static {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0277]: the trait bound `Padded: Pod` is not satisfied + --> tests/ui/reinterpret_incompatible.rs:13:24 + | +13 | let _: Padded = 0u32.try_reinterpret().unwrap(); + | ^^^^^^^^^^^^^^^ the trait `Pod` is not implemented for `Padded` + | + = help: the following other types implement trait `Pod`: + () + ManuallyDrop + Option + PhantomData + PhantomPinned + Wrapping + [T; 0] + [T; 1024] + and $N others + = note: required for `Padded` to implement `AnyBitPattern` + = note: required for `Padded` to implement `CheckedBitPattern` + = note: required for `u32` to implement `TryReinterpret` diff --git a/uitest/tests/ui/reinterpret_inner_arc.rs b/uitest/tests/ui/reinterpret_inner_arc.rs new file mode 100644 index 0000000..a27f11c --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_arc.rs @@ -0,0 +1,21 @@ +use bytemuck::ReinterpretInner; +use std::sync::Arc; + +fn main() { + let _: Arc = Arc::new([0u8; 2]).reinterpret_inner(); + let _: Arc = Arc::new([0u16; 2]).reinterpret_inner(); + let _: Arc<[u32; 2]> = Arc::new(0u64).reinterpret_inner(); + + let _: Arc<[u8; 2]> = Arc::new(0u8).reinterpret_inner(); + let _: Arc<[u16; 2]> = Arc::new(0u16).reinterpret_inner(); + let _: Arc<[u64; 2]> = Arc::new(0u64).reinterpret_inner(); + + let _: Arc<[u16]> = Arc::new([[0u8; 2]; 2]).reinterpret_inner(); + let _: Arc<[u32]> = Arc::new([[0u16; 2]; 2]).reinterpret_inner(); + let _: Arc<[[u32; 2]]> = Arc::new(0u64).reinterpret_inner(); + + let _: Arc<[()]> = Arc::new([0u8; 1]).reinterpret_inner(); + let _: Arc<[[u8; 2]]> = Arc::new([[0u8; 3]; 3]).reinterpret_inner(); + let _: Arc<[[u16; 2]]> = Arc::new([[0u16; 5]; 1]).reinterpret_inner(); + let _: Arc<[[u32; 2]]> = Arc::new([[0u32; 1]; 1]).reinterpret_inner(); +} diff --git a/uitest/tests/ui/reinterpret_inner_arc.stderr b/uitest/tests/ui/reinterpret_inner_arc.stderr new file mode 100644 index 0000000..af17f22 --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_arc.stderr @@ -0,0 +1,252 @@ +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Arc` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Arc` types with different alignments', $WORKSPACE/src/cast.rs:1328:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0080]: evaluation of `<*const [u8; 2] as bytemuck::cast::CastRaw<*const u16>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::sync::Arc>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_arc.rs:5:21 + | +5 | let _: Arc = Arc::new([0u8; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Arc` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Arc` types with different alignments', $WORKSPACE/src/cast.rs:1328:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0080]: evaluation of `<*const [u16; 2] as bytemuck::cast::CastRaw<*const u32>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::sync::Arc>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_arc.rs:6:21 + | +6 | let _: Arc = Arc::new([0u16; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Arc` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Arc` types with different alignments', $WORKSPACE/src/cast.rs:1328:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::sync::Arc<[u32; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_arc.rs:7:26 + | +7 | let _: Arc<[u32; 2]> = Arc::new(0u64).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u8 as bytemuck::cast::CastRaw<*const [u8; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::sync::Arc<[u8; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_arc.rs:9:25 + | +9 | let _: Arc<[u8; 2]> = Arc::new(0u8).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u16 as bytemuck::cast::CastRaw<*const [u16; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::sync::Arc<[u16; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_arc.rs:10:26 + | +10 | let _: Arc<[u16; 2]> = Arc::new(0u16).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u64 as bytemuck::cast::CastRaw<*const [u64; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::sync::Arc<[u64; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_arc.rs:11:26 + | +11 | let _: Arc<[u64; 2]> = Arc::new(0u64).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Arc` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Arc` types with different alignments', $WORKSPACE/src/cast.rs:1328:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::sync::Arc<[u16]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_arc.rs:13:23 + | +13 | let _: Arc<[u16]> = Arc::new([[0u8; 2]; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Arc` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Arc` types with different alignments', $WORKSPACE/src/cast.rs:1328:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::sync::Arc<[u32]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_arc.rs:14:23 + | +14 | let _: Arc<[u32]> = Arc::new([[0u16; 2]; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Arc` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Arc` types with different alignments', $WORKSPACE/src/cast.rs:1328:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::sync::Arc<[[u32; 2]]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_arc.rs:15:28 + | +15 | let _: Arc<[[u32; 2]]> = Arc::new(0u64).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [u8; 1] as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion to a slice which cannot match the size of the item" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a slice which cannot match the size of the item', $WORKSPACE/src/cast.rs:859:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::sync::Arc<[()]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_arc.rs:17:22 + | +17 | let _: Arc<[()]> = Arc::new([0u8; 1]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [[u8; 3]; 3] as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion to a slice which cannot match the size of the item" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a slice which cannot match the size of the item', $WORKSPACE/src/cast.rs:859:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::sync::Arc<[[u8; 2]]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_arc.rs:18:27 + | +18 | let _: Arc<[[u8; 2]]> = Arc::new([[0u8; 3]; 3]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [[u16; 5]; 1] as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion to a slice which cannot match the size of the item" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a slice which cannot match the size of the item', $WORKSPACE/src/cast.rs:859:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::sync::Arc<[[u16; 2]]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_arc.rs:19:28 + | +19 | let _: Arc<[[u16; 2]]> = Arc::new([[0u16; 5]; 1]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [[u32; 1]; 1] as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion to a slice which cannot match the size of the item" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a slice which cannot match the size of the item', $WORKSPACE/src/cast.rs:859:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::sync::Arc<[[u32; 2]]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_arc.rs:20:28 + | +20 | let _: Arc<[[u32; 2]]> = Arc::new([[0u32; 1]; 1]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/reinterpret_inner_arc_weak.rs b/uitest/tests/ui/reinterpret_inner_arc_weak.rs new file mode 100644 index 0000000..230b69c --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_arc_weak.rs @@ -0,0 +1,12 @@ +use bytemuck::ReinterpretInner; +use std::sync::{Arc, Weak}; + +fn main() { + let _: Weak = Arc::downgrade(&Arc::new([0u8; 2])).reinterpret_inner(); + let _: Weak = Arc::downgrade(&Arc::new([0u16; 2])).reinterpret_inner(); + let _: Weak<[u32; 2]> = Arc::downgrade(&Arc::new(0u64)).reinterpret_inner(); + + let _: Weak<[u8; 2]> = Arc::downgrade(&Arc::new(0u8)).reinterpret_inner(); + let _: Weak<[u16; 2]> = Arc::downgrade(&Arc::new(0u16)).reinterpret_inner(); + let _: Weak<[u64; 2]> = Arc::downgrade(&Arc::new(0u64)).reinterpret_inner(); +} diff --git a/uitest/tests/ui/reinterpret_inner_arc_weak.stderr b/uitest/tests/ui/reinterpret_inner_arc_weak.stderr new file mode 100644 index 0000000..f0e7ddf --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_arc_weak.stderr @@ -0,0 +1,126 @@ +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `sync::Weak` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `sync::Weak` types with different alignments', $WORKSPACE/src/cast.rs:1335:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0080]: evaluation of `<*const [u8; 2] as bytemuck::cast::CastRaw<*const u16>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::sync::Weak>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_arc_weak.rs:5:22 + | +5 | let _: Weak = Arc::downgrade(&Arc::new([0u8; 2])).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `sync::Weak` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `sync::Weak` types with different alignments', $WORKSPACE/src/cast.rs:1335:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0080]: evaluation of `<*const [u16; 2] as bytemuck::cast::CastRaw<*const u32>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::sync::Weak>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_arc_weak.rs:6:22 + | +6 | let _: Weak = Arc::downgrade(&Arc::new([0u16; 2])).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `sync::Weak` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `sync::Weak` types with different alignments', $WORKSPACE/src/cast.rs:1335:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::sync::Weak<[u32; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_arc_weak.rs:7:27 + | +7 | let _: Weak<[u32; 2]> = Arc::downgrade(&Arc::new(0u64)).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u8 as bytemuck::cast::CastRaw<*const [u8; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::sync::Weak<[u8; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_arc_weak.rs:9:26 + | +9 | let _: Weak<[u8; 2]> = Arc::downgrade(&Arc::new(0u8)).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u16 as bytemuck::cast::CastRaw<*const [u16; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::sync::Weak<[u16; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_arc_weak.rs:10:27 + | +10 | let _: Weak<[u16; 2]> = Arc::downgrade(&Arc::new(0u16)).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u64 as bytemuck::cast::CastRaw<*const [u64; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::sync::Weak<[u64; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_arc_weak.rs:11:27 + | +11 | let _: Weak<[u64; 2]> = Arc::downgrade(&Arc::new(0u64)).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/reinterpret_inner_atomic_ptr.rs b/uitest/tests/ui/reinterpret_inner_atomic_ptr.rs new file mode 100644 index 0000000..7ab46a1 --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_atomic_ptr.rs @@ -0,0 +1,12 @@ +use bytemuck::ReinterpretInner; +use core::sync::atomic::AtomicPtr; + +fn main() { + let _: AtomicPtr = AtomicPtr::new(&mut [0u8; 2]).reinterpret_inner(); + let _: AtomicPtr = AtomicPtr::new(&mut [0u16; 2]).reinterpret_inner(); + let _: AtomicPtr = AtomicPtr::new(&mut [0u32; 2]).reinterpret_inner(); + + let _: AtomicPtr<[u8; 2]> = AtomicPtr::new(&mut 0u8).reinterpret_inner(); + let _: AtomicPtr<[u16; 2]> = AtomicPtr::new(&mut 0u16).reinterpret_inner(); + let _: AtomicPtr<[u64; 2]> = AtomicPtr::new(&mut 0u64).reinterpret_inner(); +} diff --git a/uitest/tests/ui/reinterpret_inner_atomic_ptr.stderr b/uitest/tests/ui/reinterpret_inner_atomic_ptr.stderr new file mode 100644 index 0000000..8a8eabc --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_atomic_ptr.stderr @@ -0,0 +1,107 @@ +error[E0080]: evaluation of `<*const [u8; 2] as bytemuck::cast::CastRaw<*const u16>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = <*const Src as CastRaw<*const Dst>>::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::sync::atomic::AtomicPtr>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_atomic_ptr.rs:5:27 + | +5 | let _: AtomicPtr = AtomicPtr::new(&mut [0u8; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [u16; 2] as bytemuck::cast::CastRaw<*const u32>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::sync::atomic::AtomicPtr>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_atomic_ptr.rs:6:27 + | +6 | let _: AtomicPtr = AtomicPtr::new(&mut [0u16; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [u32; 2] as bytemuck::cast::CastRaw<*const u64>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::sync::atomic::AtomicPtr>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_atomic_ptr.rs:7:27 + | +7 | let _: AtomicPtr = AtomicPtr::new(&mut [0u32; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u8 as bytemuck::cast::CastRaw<*const [u8; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::sync::atomic::AtomicPtr<[u8; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_atomic_ptr.rs:9:31 + | +9 | let _: AtomicPtr<[u8; 2]> = AtomicPtr::new(&mut 0u8).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u16 as bytemuck::cast::CastRaw<*const [u16; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::sync::atomic::AtomicPtr<[u16; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_atomic_ptr.rs:10:32 + | +10 | let _: AtomicPtr<[u16; 2]> = AtomicPtr::new(&mut 0u16).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u64 as bytemuck::cast::CastRaw<*const [u64; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::sync::atomic::AtomicPtr<[u64; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_atomic_ptr.rs:11:32 + | +11 | let _: AtomicPtr<[u64; 2]> = AtomicPtr::new(&mut 0u64).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/reinterpret_inner_box.rs b/uitest/tests/ui/reinterpret_inner_box.rs new file mode 100644 index 0000000..3834ba8 --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_box.rs @@ -0,0 +1,20 @@ +use bytemuck::ReinterpretInner; + +fn main() { + let _: Box = Box::new([0u8; 2]).reinterpret_inner(); + let _: Box = Box::new([0u16; 2]).reinterpret_inner(); + let _: Box<[u32; 2]> = Box::new(0u64).reinterpret_inner(); + + let _: Box<[u8; 2]> = Box::new(0u8).reinterpret_inner(); + let _: Box<[u16; 2]> = Box::new(0u16).reinterpret_inner(); + let _: Box<[u64; 2]> = Box::new(0u64).reinterpret_inner(); + + let _: Box<[u16]> = Box::new([[0u8; 2]; 2]).reinterpret_inner(); + let _: Box<[u32]> = Box::new([[0u16; 2]; 2]).reinterpret_inner(); + let _: Box<[[u32; 2]]> = Box::new(0u64).reinterpret_inner(); + + let _: Box<[()]> = Box::new([0u8; 1]).reinterpret_inner(); + let _: Box<[[u8; 2]]> = Box::new([[0u8; 3]; 3]).reinterpret_inner(); + let _: Box<[[u16; 2]]> = Box::new([[0u16; 5]; 1]).reinterpret_inner(); + let _: Box<[[u32; 2]]> = Box::new([[0u32; 1]; 1]).reinterpret_inner(); +} diff --git a/uitest/tests/ui/reinterpret_inner_box.stderr b/uitest/tests/ui/reinterpret_inner_box.stderr new file mode 100644 index 0000000..f2737ff --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_box.stderr @@ -0,0 +1,252 @@ +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Box` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Box` types with different alignments', $WORKSPACE/src/cast.rs:1303:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0080]: evaluation of `<*const [u8; 2] as bytemuck::cast::CastRaw<*const u16>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::boxed::Box>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_box.rs:4:21 + | +4 | let _: Box = Box::new([0u8; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Box` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Box` types with different alignments', $WORKSPACE/src/cast.rs:1303:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0080]: evaluation of `<*const [u16; 2] as bytemuck::cast::CastRaw<*const u32>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::boxed::Box>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_box.rs:5:21 + | +5 | let _: Box = Box::new([0u16; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Box` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Box` types with different alignments', $WORKSPACE/src/cast.rs:1303:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::boxed::Box<[u32; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_box.rs:6:26 + | +6 | let _: Box<[u32; 2]> = Box::new(0u64).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u8 as bytemuck::cast::CastRaw<*const [u8; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::boxed::Box<[u8; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_box.rs:8:25 + | +8 | let _: Box<[u8; 2]> = Box::new(0u8).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u16 as bytemuck::cast::CastRaw<*const [u16; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::boxed::Box<[u16; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_box.rs:9:26 + | +9 | let _: Box<[u16; 2]> = Box::new(0u16).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u64 as bytemuck::cast::CastRaw<*const [u64; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::boxed::Box<[u64; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_box.rs:10:26 + | +10 | let _: Box<[u64; 2]> = Box::new(0u64).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Box` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Box` types with different alignments', $WORKSPACE/src/cast.rs:1303:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::boxed::Box<[u16]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_box.rs:12:23 + | +12 | let _: Box<[u16]> = Box::new([[0u8; 2]; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Box` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Box` types with different alignments', $WORKSPACE/src/cast.rs:1303:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::boxed::Box<[u32]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_box.rs:13:23 + | +13 | let _: Box<[u32]> = Box::new([[0u16; 2]; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Box` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Box` types with different alignments', $WORKSPACE/src/cast.rs:1303:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::boxed::Box<[[u32; 2]]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_box.rs:14:28 + | +14 | let _: Box<[[u32; 2]]> = Box::new(0u64).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [u8; 1] as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion to a slice which cannot match the size of the item" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a slice which cannot match the size of the item', $WORKSPACE/src/cast.rs:859:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::boxed::Box<[()]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_box.rs:16:22 + | +16 | let _: Box<[()]> = Box::new([0u8; 1]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [[u8; 3]; 3] as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion to a slice which cannot match the size of the item" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a slice which cannot match the size of the item', $WORKSPACE/src/cast.rs:859:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::boxed::Box<[[u8; 2]]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_box.rs:17:27 + | +17 | let _: Box<[[u8; 2]]> = Box::new([[0u8; 3]; 3]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [[u16; 5]; 1] as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion to a slice which cannot match the size of the item" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a slice which cannot match the size of the item', $WORKSPACE/src/cast.rs:859:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::boxed::Box<[[u16; 2]]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_box.rs:18:28 + | +18 | let _: Box<[[u16; 2]]> = Box::new([[0u16; 5]; 1]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [[u32; 1]; 1] as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion to a slice which cannot match the size of the item" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a slice which cannot match the size of the item', $WORKSPACE/src/cast.rs:859:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::boxed::Box<[[u32; 2]]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_box.rs:19:28 + | +19 | let _: Box<[[u32; 2]]> = Box::new([[0u32; 1]; 1]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/reinterpret_inner_const_ptr.rs b/uitest/tests/ui/reinterpret_inner_const_ptr.rs new file mode 100644 index 0000000..2a3d028 --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_const_ptr.rs @@ -0,0 +1,11 @@ +use bytemuck::ReinterpretInner; + +fn main() { + let _: *const u16 = (&[0u8; 2] as *const [u8; 2]).reinterpret_inner(); + let _: *const u32 = (&[0u16; 2] as *const [u16; 2]).reinterpret_inner(); + let _: *const u64 = (&[0u32; 2] as *const [u32; 2]).reinterpret_inner(); + + let _: *const [u8; 2] = (&0u8 as *const u8).reinterpret_inner(); + let _: *const [u16; 2] = (&0u16 as *const u16).reinterpret_inner(); + let _: *const [u64; 2] = (&0u64 as *const u64).reinterpret_inner(); +} diff --git a/uitest/tests/ui/reinterpret_inner_const_ptr.stderr b/uitest/tests/ui/reinterpret_inner_const_ptr.stderr new file mode 100644 index 0000000..42da7a7 --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_const_ptr.stderr @@ -0,0 +1,101 @@ +error[E0080]: evaluation of `<*const [u8; 2] as bytemuck::cast::CastRaw<*const u16>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <*const [u8; 2] as bytemuck::ReinterpretInner<'_, *const u16>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_const_ptr.rs:4:23 + | +4 | let _: *const u16 = (&[0u8; 2] as *const [u8; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [u16; 2] as bytemuck::cast::CastRaw<*const u32>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <*const [u16; 2] as bytemuck::ReinterpretInner<'_, *const u32>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_const_ptr.rs:5:23 + | +5 | let _: *const u32 = (&[0u16; 2] as *const [u16; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [u32; 2] as bytemuck::cast::CastRaw<*const u64>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <*const [u32; 2] as bytemuck::ReinterpretInner<'_, *const u64>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_const_ptr.rs:6:23 + | +6 | let _: *const u64 = (&[0u32; 2] as *const [u32; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u8 as bytemuck::cast::CastRaw<*const [u8; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <*const u8 as bytemuck::ReinterpretInner<'_, *const [u8; 2]>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_const_ptr.rs:8:27 + | +8 | let _: *const [u8; 2] = (&0u8 as *const u8).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u16 as bytemuck::cast::CastRaw<*const [u16; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <*const u16 as bytemuck::ReinterpretInner<'_, *const [u16; 2]>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_const_ptr.rs:9:28 + | +9 | let _: *const [u16; 2] = (&0u16 as *const u16).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u64 as bytemuck::cast::CastRaw<*const [u64; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <*const u64 as bytemuck::ReinterpretInner<'_, *const [u64; 2]>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_const_ptr.rs:10:28 + | +10 | let _: *const [u64; 2] = (&0u64 as *const u64).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/reinterpret_inner_cow.rs b/uitest/tests/ui/reinterpret_inner_cow.rs new file mode 100644 index 0000000..7dc62c8 --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_cow.rs @@ -0,0 +1,27 @@ +use bytemuck::ReinterpretInner; +use std::borrow::Cow; + +fn main() { + let _: Cow = Cow::Borrowed(&[0u8; 2]).reinterpret_inner(); + let _: Cow = Cow::Borrowed(&[0u16; 2]).reinterpret_inner(); + let _: Cow = Cow::Borrowed(&[0u32; 2]).reinterpret_inner(); + + let _: Cow<[u8; 2]> = Cow::Borrowed(&0u8).reinterpret_inner(); + let _: Cow<[u16; 2]> = Cow::Borrowed(&0u16).reinterpret_inner(); + let _: Cow<[u64; 2]> = Cow::Borrowed(&0u64).reinterpret_inner(); + + let _: Cow<[u16]> = + Cow::Borrowed([[0u8; 2]; 2].as_slice()).reinterpret_inner(); + let _: Cow<[u32]> = + Cow::Borrowed([[0u16; 2]; 2].as_slice()).reinterpret_inner(); + let _: Cow<[[u32; 2]]> = + Cow::Borrowed([0u64; 2].as_slice()).reinterpret_inner(); + + let _: Cow<[()]> = Cow::Borrowed([0u8; 1].as_slice()).reinterpret_inner(); + let _: Cow<[[u8; 2]]> = + Cow::Borrowed([[0u8; 3]; 3].as_slice()).reinterpret_inner(); + let _: Cow<[[u16; 2]]> = + Cow::Borrowed([[0u16; 5]; 1].as_slice()).reinterpret_inner(); + let _: Cow<[[u32; 2]]> = + Cow::Borrowed([[0u32; 1]; 1].as_slice()).reinterpret_inner(); +} diff --git a/uitest/tests/ui/reinterpret_inner_cow.stderr b/uitest/tests/ui/reinterpret_inner_cow.stderr new file mode 100644 index 0000000..e9c84a7 --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_cow.stderr @@ -0,0 +1,242 @@ +error[E0080]: evaluation of `<*const [u8; 2] as bytemuck::cast::CastRaw<*const u16>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = <*const Src as CastRaw<*const Dst>>::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::borrow::Cow<'_, u16>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_cow.rs:5:21 + | +5 | let _: Cow = Cow::Borrowed(&[0u8; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [u16; 2] as bytemuck::cast::CastRaw<*const u32>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::borrow::Cow<'_, u32>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_cow.rs:6:21 + | +6 | let _: Cow = Cow::Borrowed(&[0u16; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [u32; 2] as bytemuck::cast::CastRaw<*const u64>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::borrow::Cow<'_, u64>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_cow.rs:7:21 + | +7 | let _: Cow = Cow::Borrowed(&[0u32; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u8 as bytemuck::cast::CastRaw<*const [u8; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::borrow::Cow<'_, [u8; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_cow.rs:9:25 + | +9 | let _: Cow<[u8; 2]> = Cow::Borrowed(&0u8).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u16 as bytemuck::cast::CastRaw<*const [u16; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::borrow::Cow<'_, [u16; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_cow.rs:10:26 + | +10 | let _: Cow<[u16; 2]> = Cow::Borrowed(&0u16).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u64 as bytemuck::cast::CastRaw<*const [u64; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::borrow::Cow<'_, [u64; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_cow.rs:11:26 + | +11 | let _: Cow<[u64; 2]> = Cow::Borrowed(&0u64).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ) + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:910:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = as CastRaw>>::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | as CastRaw>>::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::borrow::Cow<'_, [u16]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_cow.rs:14:5 + | +14 | Cow::Borrowed([[0u8; 2]; 2].as_slice()).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ) + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:910:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::borrow::Cow<'_, [u32]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_cow.rs:16:5 + | +16 | Cow::Borrowed([[0u16; 2]; 2].as_slice()).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() == align_of::(), + | | "Attempted conversion between `Cow<[_]>` types with different alignments" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between `Cow<[_]>` types with different alignments', $WORKSPACE/src/cast.rs:1001:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::borrow::Cow<'_, [[u32; 2]]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_cow.rs:18:5 + | +18 | Cow::Borrowed([0u64; 2].as_slice()).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion between slice types which may not succeed" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between slice types which may not succeed', $WORKSPACE/src/cast.rs:905:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::borrow::Cow<'_, [()]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_cow.rs:20:22 + | +20 | let _: Cow<[()]> = Cow::Borrowed([0u8; 1].as_slice()).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion between slice types which may not succeed" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between slice types which may not succeed', $WORKSPACE/src/cast.rs:905:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::borrow::Cow<'_, [[u8; 2]]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_cow.rs:22:5 + | +22 | Cow::Borrowed([[0u8; 3]; 3].as_slice()).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion between slice types which may not succeed" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between slice types which may not succeed', $WORKSPACE/src/cast.rs:905:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::borrow::Cow<'_, [[u16; 2]]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_cow.rs:24:5 + | +24 | Cow::Borrowed([[0u16; 5]; 1].as_slice()).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion between slice types which may not succeed" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between slice types which may not succeed', $WORKSPACE/src/cast.rs:905:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::borrow::Cow<'_, [[u32; 2]]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_cow.rs:26:5 + | +26 | Cow::Borrowed([[0u32; 1]; 1].as_slice()).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/reinterpret_inner_mut.rs b/uitest/tests/ui/reinterpret_inner_mut.rs new file mode 100644 index 0000000..00cea76 --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_mut.rs @@ -0,0 +1,20 @@ +use bytemuck::ReinterpretInner; + +fn main() { + let _: &mut u16 = (&mut [0u8; 2]).reinterpret_inner(); + let _: &mut u32 = (&mut [0u16; 2]).reinterpret_inner(); + let _: &mut u64 = (&mut [0u32; 2]).reinterpret_inner(); + + let _: &mut [u8; 2] = (&mut 0u8).reinterpret_inner(); + let _: &mut [u16; 2] = (&mut 0u16).reinterpret_inner(); + let _: &mut [u64; 2] = (&mut 0u64).reinterpret_inner(); + + let _: &mut [u16] = [[0u8; 2]; 2].as_mut_slice().reinterpret_inner(); + let _: &mut [u32] = [[0u16; 2]; 2].as_mut_slice().reinterpret_inner(); + let _: &mut [u64] = [[0u32; 2]; 2].as_mut_slice().reinterpret_inner(); + + let _: &mut [()] = [0u8; 1].as_mut_slice().reinterpret_inner(); + let _: &mut [[u8; 2]] = [[0u8; 3]; 3].as_mut_slice().reinterpret_inner(); + let _: &mut [[u16; 2]] = [[0u16; 5]; 1].as_mut_slice().reinterpret_inner(); + let _: &mut [[u32; 2]] = [[0u32; 1]; 1].as_mut_slice().reinterpret_inner(); +} diff --git a/uitest/tests/ui/reinterpret_inner_mut.stderr b/uitest/tests/ui/reinterpret_inner_mut.stderr new file mode 100644 index 0000000..9fcaa84 --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_mut.stderr @@ -0,0 +1,236 @@ +error[E0080]: evaluation of `<*const [u8; 2] as bytemuck::cast::CastRaw<*const u16>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = <*const Src as CastRaw<*const Dst>>::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: the above error was encountered while instantiating `fn <&mut [u8; 2] as bytemuck::ReinterpretInner<'_, &mut u16>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_mut.rs:4:21 + | +4 | let _: &mut u16 = (&mut [0u8; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [u16; 2] as bytemuck::cast::CastRaw<*const u32>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&mut [u16; 2] as bytemuck::ReinterpretInner<'_, &mut u32>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_mut.rs:5:21 + | +5 | let _: &mut u32 = (&mut [0u16; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [u32; 2] as bytemuck::cast::CastRaw<*const u64>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&mut [u32; 2] as bytemuck::ReinterpretInner<'_, &mut u64>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_mut.rs:6:21 + | +6 | let _: &mut u64 = (&mut [0u32; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u8 as bytemuck::cast::CastRaw<*const [u8; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&mut u8 as bytemuck::ReinterpretInner<'_, &mut [u8; 2]>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_mut.rs:8:25 + | +8 | let _: &mut [u8; 2] = (&mut 0u8).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u16 as bytemuck::cast::CastRaw<*const [u16; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&mut u16 as bytemuck::ReinterpretInner<'_, &mut [u16; 2]>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_mut.rs:9:26 + | +9 | let _: &mut [u16; 2] = (&mut 0u16).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u64 as bytemuck::cast::CastRaw<*const [u64; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&mut u64 as bytemuck::ReinterpretInner<'_, &mut [u64; 2]>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_mut.rs:10:26 + | +10 | let _: &mut [u64; 2] = (&mut 0u64).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ) + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:910:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = as CastRaw>>::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: the above error was encountered while instantiating `fn <&mut [[u8; 2]] as bytemuck::ReinterpretInner<'_, &mut [u16]>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_mut.rs:12:23 + | +12 | let _: &mut [u16] = [[0u8; 2]; 2].as_mut_slice().reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ) + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:910:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&mut [[u16; 2]] as bytemuck::ReinterpretInner<'_, &mut [u32]>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_mut.rs:13:23 + | +13 | let _: &mut [u32] = [[0u16; 2]; 2].as_mut_slice().reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ) + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:910:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&mut [[u32; 2]] as bytemuck::ReinterpretInner<'_, &mut [u64]>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_mut.rs:14:23 + | +14 | let _: &mut [u64] = [[0u32; 2]; 2].as_mut_slice().reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion between slice types which may not succeed" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between slice types which may not succeed', $WORKSPACE/src/cast.rs:905:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&mut [u8] as bytemuck::ReinterpretInner<'_, &mut [()]>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_mut.rs:16:22 + | +16 | let _: &mut [()] = [0u8; 1].as_mut_slice().reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion between slice types which may not succeed" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between slice types which may not succeed', $WORKSPACE/src/cast.rs:905:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&mut [[u8; 3]] as bytemuck::ReinterpretInner<'_, &mut [[u8; 2]]>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_mut.rs:17:27 + | +17 | let _: &mut [[u8; 2]] = [[0u8; 3]; 3].as_mut_slice().reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion between slice types which may not succeed" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between slice types which may not succeed', $WORKSPACE/src/cast.rs:905:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&mut [[u16; 5]] as bytemuck::ReinterpretInner<'_, &mut [[u16; 2]]>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_mut.rs:18:28 + | +18 | let _: &mut [[u16; 2]] = [[0u16; 5]; 1].as_mut_slice().reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion between slice types which may not succeed" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between slice types which may not succeed', $WORKSPACE/src/cast.rs:905:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&mut [[u32; 1]] as bytemuck::ReinterpretInner<'_, &mut [[u32; 2]]>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_mut.rs:19:28 + | +19 | let _: &mut [[u32; 2]] = [[0u32; 1]; 1].as_mut_slice().reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/reinterpret_inner_mut_ptr.rs b/uitest/tests/ui/reinterpret_inner_mut_ptr.rs new file mode 100644 index 0000000..3a6febe --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_mut_ptr.rs @@ -0,0 +1,11 @@ +use bytemuck::ReinterpretInner; + +fn main() { + let _: *mut u16 = (&mut [0u8; 2] as *mut [u8; 2]).reinterpret_inner(); + let _: *mut u32 = (&mut [0u16; 2] as *mut [u16; 2]).reinterpret_inner(); + let _: *mut u64 = (&mut [0u32; 2] as *mut [u32; 2]).reinterpret_inner(); + + let _: *mut [u8; 2] = (&mut 0u8 as *mut u8).reinterpret_inner(); + let _: *mut [u16; 2] = (&mut 0u16 as *mut u16).reinterpret_inner(); + let _: *mut [u64; 2] = (&mut 0u64 as *mut u64).reinterpret_inner(); +} diff --git a/uitest/tests/ui/reinterpret_inner_mut_ptr.stderr b/uitest/tests/ui/reinterpret_inner_mut_ptr.stderr new file mode 100644 index 0000000..dee0f49 --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_mut_ptr.stderr @@ -0,0 +1,107 @@ +error[E0080]: evaluation of `<*const [u8; 2] as bytemuck::cast::CastRaw<*const u16>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = <*const Src as CastRaw<*const Dst>>::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: the above error was encountered while instantiating `fn <*mut [u8; 2] as bytemuck::ReinterpretInner<'_, *mut u16>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_mut_ptr.rs:4:21 + | +4 | let _: *mut u16 = (&mut [0u8; 2] as *mut [u8; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [u16; 2] as bytemuck::cast::CastRaw<*const u32>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <*mut [u16; 2] as bytemuck::ReinterpretInner<'_, *mut u32>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_mut_ptr.rs:5:21 + | +5 | let _: *mut u32 = (&mut [0u16; 2] as *mut [u16; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [u32; 2] as bytemuck::cast::CastRaw<*const u64>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <*mut [u32; 2] as bytemuck::ReinterpretInner<'_, *mut u64>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_mut_ptr.rs:6:21 + | +6 | let _: *mut u64 = (&mut [0u32; 2] as *mut [u32; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u8 as bytemuck::cast::CastRaw<*const [u8; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <*mut u8 as bytemuck::ReinterpretInner<'_, *mut [u8; 2]>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_mut_ptr.rs:8:25 + | +8 | let _: *mut [u8; 2] = (&mut 0u8 as *mut u8).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u16 as bytemuck::cast::CastRaw<*const [u16; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <*mut u16 as bytemuck::ReinterpretInner<'_, *mut [u16; 2]>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_mut_ptr.rs:9:26 + | +9 | let _: *mut [u16; 2] = (&mut 0u16 as *mut u16).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u64 as bytemuck::cast::CastRaw<*const [u64; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <*mut u64 as bytemuck::ReinterpretInner<'_, *mut [u64; 2]>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_mut_ptr.rs:10:26 + | +10 | let _: *mut [u64; 2] = (&mut 0u64 as *mut u64).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/reinterpret_inner_non_null.rs b/uitest/tests/ui/reinterpret_inner_non_null.rs new file mode 100644 index 0000000..1dc4c01 --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_non_null.rs @@ -0,0 +1,28 @@ +use bytemuck::ReinterpretInner; +use core::ptr::NonNull; + +fn main() { + let _: NonNull = NonNull::from(&mut [0u8; 2]).reinterpret_inner(); + let _: NonNull = NonNull::from(&mut [0u16; 2]).reinterpret_inner(); + let _: NonNull = NonNull::from(&mut [0u32; 2]).reinterpret_inner(); + + let _: NonNull<[u8; 2]> = NonNull::from(&mut 0u8).reinterpret_inner(); + let _: NonNull<[u16; 2]> = NonNull::from(&mut 0u16).reinterpret_inner(); + let _: NonNull<[u64; 2]> = NonNull::from(&mut 0u64).reinterpret_inner(); + + let _: NonNull<[u16]> = + NonNull::from([[0u8; 2]; 2].as_mut_slice()).reinterpret_inner(); + let _: NonNull<[u32]> = + NonNull::from([[0u16; 2]; 2].as_mut_slice()).reinterpret_inner(); + let _: NonNull<[u64]> = + NonNull::from([[0u32; 2]; 2].as_mut_slice()).reinterpret_inner(); + + let _: NonNull<[()]> = + NonNull::from([0u8; 1].as_mut_slice()).reinterpret_inner(); + let _: NonNull<[[u8; 2]]> = + NonNull::from([[0u8; 3]; 3].as_mut_slice()).reinterpret_inner(); + let _: NonNull<[[u16; 2]]> = + NonNull::from([[0u16; 5]; 1].as_mut_slice()).reinterpret_inner(); + let _: NonNull<[[u32; 2]]> = + NonNull::from([[0u32; 1]; 1].as_mut_slice()).reinterpret_inner(); +} diff --git a/uitest/tests/ui/reinterpret_inner_non_null.stderr b/uitest/tests/ui/reinterpret_inner_non_null.stderr new file mode 100644 index 0000000..5b5b9ba --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_non_null.stderr @@ -0,0 +1,236 @@ +error[E0080]: evaluation of `<*const [u8; 2] as bytemuck::cast::CastRaw<*const u16>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = <*const Src as CastRaw<*const Dst>>::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::ptr::NonNull>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_non_null.rs:5:25 + | +5 | let _: NonNull = NonNull::from(&mut [0u8; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [u16; 2] as bytemuck::cast::CastRaw<*const u32>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::ptr::NonNull>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_non_null.rs:6:25 + | +6 | let _: NonNull = NonNull::from(&mut [0u16; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [u32; 2] as bytemuck::cast::CastRaw<*const u64>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::ptr::NonNull>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_non_null.rs:7:25 + | +7 | let _: NonNull = NonNull::from(&mut [0u32; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u8 as bytemuck::cast::CastRaw<*const [u8; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::ptr::NonNull<[u8; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_non_null.rs:9:29 + | +9 | let _: NonNull<[u8; 2]> = NonNull::from(&mut 0u8).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u16 as bytemuck::cast::CastRaw<*const [u16; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::ptr::NonNull<[u16; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_non_null.rs:10:30 + | +10 | let _: NonNull<[u16; 2]> = NonNull::from(&mut 0u16).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u64 as bytemuck::cast::CastRaw<*const [u64; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::ptr::NonNull<[u64; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_non_null.rs:11:30 + | +11 | let _: NonNull<[u64; 2]> = NonNull::from(&mut 0u64).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ) + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:910:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = as CastRaw>>::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::ptr::NonNull<[u16]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_non_null.rs:14:5 + | +14 | NonNull::from([[0u8; 2]; 2].as_mut_slice()).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ) + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:910:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::ptr::NonNull<[u32]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_non_null.rs:16:5 + | +16 | NonNull::from([[0u16; 2]; 2].as_mut_slice()).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ) + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:910:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::ptr::NonNull<[u64]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_non_null.rs:18:5 + | +18 | NonNull::from([[0u32; 2]; 2].as_mut_slice()).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion between slice types which may not succeed" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between slice types which may not succeed', $WORKSPACE/src/cast.rs:905:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::ptr::NonNull<[()]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_non_null.rs:21:5 + | +21 | NonNull::from([0u8; 1].as_mut_slice()).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion between slice types which may not succeed" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between slice types which may not succeed', $WORKSPACE/src/cast.rs:905:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::ptr::NonNull<[[u8; 2]]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_non_null.rs:23:5 + | +23 | NonNull::from([[0u8; 3]; 3].as_mut_slice()).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion between slice types which may not succeed" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between slice types which may not succeed', $WORKSPACE/src/cast.rs:905:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::ptr::NonNull<[[u16; 2]]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_non_null.rs:25:5 + | +25 | NonNull::from([[0u16; 5]; 1].as_mut_slice()).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion between slice types which may not succeed" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between slice types which may not succeed', $WORKSPACE/src/cast.rs:905:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::ptr::NonNull<[[u32; 2]]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_non_null.rs:27:5 + | +27 | NonNull::from([[0u32; 1]; 1].as_mut_slice()).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/reinterpret_inner_option.rs b/uitest/tests/ui/reinterpret_inner_option.rs new file mode 100644 index 0000000..a209f59 --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_option.rs @@ -0,0 +1,9 @@ +use bytemuck::ReinterpretInner; + +fn main() { + let _: Option<&[u8; 2]> = Some(&0u8).reinterpret_inner(); + let _: Option<&mut [u16; 2]> = Some(&mut 0u16).reinterpret_inner(); + let _: Option<&u64> = Some(&[0u32; 2]).reinterpret_inner(); + let _: Option<&mut [()]> = Some([0u8; 1].as_mut_slice().reinterpret_inner()); + let _: Option> = Some(Box::new([0u16; 2]).reinterpret_inner()); +} diff --git a/uitest/tests/ui/reinterpret_inner_option.stderr b/uitest/tests/ui/reinterpret_inner_option.stderr new file mode 100644 index 0000000..f7f92d0 --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_option.stderr @@ -0,0 +1,104 @@ +error[E0080]: evaluation of `<*const u8 as bytemuck::cast::CastRaw<*const [u8; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = >::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::option::Option<&[u8; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_option.rs:4:29 + | +4 | let _: Option<&[u8; 2]> = Some(&0u8).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u16 as bytemuck::cast::CastRaw<*const [u16; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = <*const Src as CastRaw<*const Dst>>::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::option::Option<&mut [u16; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_option.rs:5:34 + | +5 | let _: Option<&mut [u16; 2]> = Some(&mut 0u16).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [u32; 2] as bytemuck::cast::CastRaw<*const u64>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::option::Option<&u64>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_option.rs:6:25 + | +6 | let _: Option<&u64> = Some(&[0u32; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion between slice types which may not succeed" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between slice types which may not succeed', $WORKSPACE/src/cast.rs:905:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = as CastRaw>>::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: the above error was encountered while instantiating `fn <&mut [u8] as bytemuck::ReinterpretInner<'_, &mut [()]>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_option.rs:7:35 + | +7 | let _: Option<&mut [()]> = Some([0u8; 1].as_mut_slice().reinterpret_inner()); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Box` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Box` types with different alignments', $WORKSPACE/src/cast.rs:1303:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::boxed::Box<[u8; 4]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_option.rs:8:38 + | +8 | let _: Option> = Some(Box::new([0u16; 2]).reinterpret_inner()); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/reinterpret_inner_pin.rs b/uitest/tests/ui/reinterpret_inner_pin.rs new file mode 100644 index 0000000..9a0a7e9 --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_pin.rs @@ -0,0 +1,10 @@ +use bytemuck::ReinterpretInner; +use core::pin::Pin; + +fn main() { + let _: Pin<&[u8; 2]> = Pin::new(&0u8).reinterpret_inner(); + let _: Pin<&mut [u16; 2]> = Pin::new(&mut 0u16).reinterpret_inner(); + let _: Pin<&u64> = Pin::new(&[0u32; 2]).reinterpret_inner(); + let _: Pin<&mut [()]> = Pin::new([0u8; 1].as_mut_slice().reinterpret_inner()); + let _: Pin> = Pin::new(Box::new([0u16; 2]).reinterpret_inner()); +} diff --git a/uitest/tests/ui/reinterpret_inner_pin.stderr b/uitest/tests/ui/reinterpret_inner_pin.stderr new file mode 100644 index 0000000..6ca023a --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_pin.stderr @@ -0,0 +1,98 @@ +error[E0080]: evaluation of `<*const u8 as bytemuck::cast::CastRaw<*const [u8; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::pin::Pin<&[u8; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_pin.rs:5:26 + | +5 | let _: Pin<&[u8; 2]> = Pin::new(&0u8).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u16 as bytemuck::cast::CastRaw<*const [u16; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = <*const Src as CastRaw<*const Dst>>::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::pin::Pin<&mut [u16; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_pin.rs:6:31 + | +6 | let _: Pin<&mut [u16; 2]> = Pin::new(&mut 0u16).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [u32; 2] as bytemuck::cast::CastRaw<*const u64>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::pin::Pin<&u64>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_pin.rs:7:22 + | +7 | let _: Pin<&u64> = Pin::new(&[0u32; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion between slice types which may not succeed" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between slice types which may not succeed', $WORKSPACE/src/cast.rs:905:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = as CastRaw>>::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: the above error was encountered while instantiating `fn <&mut [u8] as bytemuck::ReinterpretInner<'_, &mut [()]>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_pin.rs:8:36 + | +8 | let _: Pin<&mut [()]> = Pin::new([0u8; 1].as_mut_slice().reinterpret_inner()); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Box` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Box` types with different alignments', $WORKSPACE/src/cast.rs:1303:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::boxed::Box<[u8; 4]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_pin.rs:9:39 + | +9 | let _: Pin> = Pin::new(Box::new([0u16; 2]).reinterpret_inner()); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/reinterpret_inner_rc.rs b/uitest/tests/ui/reinterpret_inner_rc.rs new file mode 100644 index 0000000..a08fab1 --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_rc.rs @@ -0,0 +1,21 @@ +use bytemuck::ReinterpretInner; +use std::rc::Rc; + +fn main() { + let _: Rc = Rc::new([0u8; 2]).reinterpret_inner(); + let _: Rc = Rc::new([0u16; 2]).reinterpret_inner(); + let _: Rc<[u32; 2]> = Rc::new(0u64).reinterpret_inner(); + + let _: Rc<[u8; 2]> = Rc::new(0u8).reinterpret_inner(); + let _: Rc<[u16; 2]> = Rc::new(0u16).reinterpret_inner(); + let _: Rc<[u64; 2]> = Rc::new(0u64).reinterpret_inner(); + + let _: Rc<[u16]> = Rc::new([[0u8; 2]; 2]).reinterpret_inner(); + let _: Rc<[u32]> = Rc::new([[0u16; 2]; 2]).reinterpret_inner(); + let _: Rc<[[u32; 2]]> = Rc::new(0u64).reinterpret_inner(); + + let _: Rc<[()]> = Rc::new([0u8; 1]).reinterpret_inner(); + let _: Rc<[[u8; 2]]> = Rc::new([[0u8; 3]; 3]).reinterpret_inner(); + let _: Rc<[[u16; 2]]> = Rc::new([[0u16; 5]; 1]).reinterpret_inner(); + let _: Rc<[[u32; 2]]> = Rc::new([[0u32; 1]; 1]).reinterpret_inner(); +} diff --git a/uitest/tests/ui/reinterpret_inner_rc.stderr b/uitest/tests/ui/reinterpret_inner_rc.stderr new file mode 100644 index 0000000..53d0f8b --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_rc.stderr @@ -0,0 +1,252 @@ +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Rc` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Rc` types with different alignments', $WORKSPACE/src/cast.rs:1312:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0080]: evaluation of `<*const [u8; 2] as bytemuck::cast::CastRaw<*const u16>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::rc::Rc>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_rc.rs:5:20 + | +5 | let _: Rc = Rc::new([0u8; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Rc` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Rc` types with different alignments', $WORKSPACE/src/cast.rs:1312:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0080]: evaluation of `<*const [u16; 2] as bytemuck::cast::CastRaw<*const u32>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::rc::Rc>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_rc.rs:6:20 + | +6 | let _: Rc = Rc::new([0u16; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Rc` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Rc` types with different alignments', $WORKSPACE/src/cast.rs:1312:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::rc::Rc<[u32; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_rc.rs:7:25 + | +7 | let _: Rc<[u32; 2]> = Rc::new(0u64).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u8 as bytemuck::cast::CastRaw<*const [u8; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::rc::Rc<[u8; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_rc.rs:9:24 + | +9 | let _: Rc<[u8; 2]> = Rc::new(0u8).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u16 as bytemuck::cast::CastRaw<*const [u16; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::rc::Rc<[u16; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_rc.rs:10:25 + | +10 | let _: Rc<[u16; 2]> = Rc::new(0u16).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u64 as bytemuck::cast::CastRaw<*const [u64; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::rc::Rc<[u64; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_rc.rs:11:25 + | +11 | let _: Rc<[u64; 2]> = Rc::new(0u64).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Rc` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Rc` types with different alignments', $WORKSPACE/src/cast.rs:1312:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::rc::Rc<[u16]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_rc.rs:13:22 + | +13 | let _: Rc<[u16]> = Rc::new([[0u8; 2]; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Rc` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Rc` types with different alignments', $WORKSPACE/src/cast.rs:1312:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::rc::Rc<[u32]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_rc.rs:14:22 + | +14 | let _: Rc<[u32]> = Rc::new([[0u16; 2]; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Rc` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Rc` types with different alignments', $WORKSPACE/src/cast.rs:1312:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::rc::Rc<[[u32; 2]]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_rc.rs:15:27 + | +15 | let _: Rc<[[u32; 2]]> = Rc::new(0u64).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [u8; 1] as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion to a slice which cannot match the size of the item" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a slice which cannot match the size of the item', $WORKSPACE/src/cast.rs:859:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::rc::Rc<[()]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_rc.rs:17:21 + | +17 | let _: Rc<[()]> = Rc::new([0u8; 1]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [[u8; 3]; 3] as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion to a slice which cannot match the size of the item" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a slice which cannot match the size of the item', $WORKSPACE/src/cast.rs:859:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::rc::Rc<[[u8; 2]]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_rc.rs:18:26 + | +18 | let _: Rc<[[u8; 2]]> = Rc::new([[0u8; 3]; 3]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [[u16; 5]; 1] as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion to a slice which cannot match the size of the item" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a slice which cannot match the size of the item', $WORKSPACE/src/cast.rs:859:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::rc::Rc<[[u16; 2]]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_rc.rs:19:27 + | +19 | let _: Rc<[[u16; 2]]> = Rc::new([[0u16; 5]; 1]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [[u32; 1]; 1] as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion to a slice which cannot match the size of the item" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a slice which cannot match the size of the item', $WORKSPACE/src/cast.rs:859:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::rc::Rc<[[u32; 2]]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_rc.rs:20:27 + | +20 | let _: Rc<[[u32; 2]]> = Rc::new([[0u32; 1]; 1]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/reinterpret_inner_rc_weak.rs b/uitest/tests/ui/reinterpret_inner_rc_weak.rs new file mode 100644 index 0000000..6e8208d --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_rc_weak.rs @@ -0,0 +1,12 @@ +use bytemuck::ReinterpretInner; +use std::rc::{Rc, Weak}; + +fn main() { + let _: Weak = Rc::downgrade(&Rc::new([0u8; 2])).reinterpret_inner(); + let _: Weak = Rc::downgrade(&Rc::new([0u16; 2])).reinterpret_inner(); + let _: Weak<[u32; 2]> = Rc::downgrade(&Rc::new(0u64)).reinterpret_inner(); + + let _: Weak<[u8; 2]> = Rc::downgrade(&Rc::new(0u8)).reinterpret_inner(); + let _: Weak<[u16; 2]> = Rc::downgrade(&Rc::new(0u16)).reinterpret_inner(); + let _: Weak<[u64; 2]> = Rc::downgrade(&Rc::new(0u64)).reinterpret_inner(); +} diff --git a/uitest/tests/ui/reinterpret_inner_rc_weak.stderr b/uitest/tests/ui/reinterpret_inner_rc_weak.stderr new file mode 100644 index 0000000..d3afb43 --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_rc_weak.stderr @@ -0,0 +1,126 @@ +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `rc::Weak` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `rc::Weak` types with different alignments', $WORKSPACE/src/cast.rs:1319:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0080]: evaluation of `<*const [u8; 2] as bytemuck::cast::CastRaw<*const u16>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::rc::Weak>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_rc_weak.rs:5:22 + | +5 | let _: Weak = Rc::downgrade(&Rc::new([0u8; 2])).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `rc::Weak` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `rc::Weak` types with different alignments', $WORKSPACE/src/cast.rs:1319:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0080]: evaluation of `<*const [u16; 2] as bytemuck::cast::CastRaw<*const u32>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::rc::Weak>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_rc_weak.rs:6:22 + | +6 | let _: Weak = Rc::downgrade(&Rc::new([0u16; 2])).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `rc::Weak` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `rc::Weak` types with different alignments', $WORKSPACE/src/cast.rs:1319:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::rc::Weak<[u32; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_rc_weak.rs:7:27 + | +7 | let _: Weak<[u32; 2]> = Rc::downgrade(&Rc::new(0u64)).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u8 as bytemuck::cast::CastRaw<*const [u8; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::rc::Weak<[u8; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_rc_weak.rs:9:26 + | +9 | let _: Weak<[u8; 2]> = Rc::downgrade(&Rc::new(0u8)).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u16 as bytemuck::cast::CastRaw<*const [u16; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::rc::Weak<[u16; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_rc_weak.rs:10:27 + | +10 | let _: Weak<[u16; 2]> = Rc::downgrade(&Rc::new(0u16)).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u64 as bytemuck::cast::CastRaw<*const [u64; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::rc::Weak<[u64; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_rc_weak.rs:11:27 + | +11 | let _: Weak<[u64; 2]> = Rc::downgrade(&Rc::new(0u64)).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/reinterpret_inner_ref.rs b/uitest/tests/ui/reinterpret_inner_ref.rs new file mode 100644 index 0000000..f100b5b --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_ref.rs @@ -0,0 +1,20 @@ +use bytemuck::ReinterpretInner; + +fn main() { + let _: &u16 = (&[0u8; 2]).reinterpret_inner(); + let _: &u32 = (&[0u16; 2]).reinterpret_inner(); + let _: &u64 = (&[0u32; 2]).reinterpret_inner(); + + let _: &[u8; 2] = (&0u8).reinterpret_inner(); + let _: &[u16; 2] = (&0u16).reinterpret_inner(); + let _: &[u64; 2] = (&0u64).reinterpret_inner(); + + let _: &[u16] = [[0u8; 2]; 2].as_slice().reinterpret_inner(); + let _: &[u32] = [[0u16; 2]; 2].as_slice().reinterpret_inner(); + let _: &[u64] = [[0u32; 2]; 2].as_slice().reinterpret_inner(); + + let _: &[()] = [0u8; 1].as_slice().reinterpret_inner(); + let _: &[[u8; 2]] = [[0u8; 3]; 3].as_slice().reinterpret_inner(); + let _: &[[u16; 2]] = [[0u16; 5]; 1].as_slice().reinterpret_inner(); + let _: &[[u32; 2]] = [[0u32; 1]; 1].as_slice().reinterpret_inner(); +} diff --git a/uitest/tests/ui/reinterpret_inner_ref.stderr b/uitest/tests/ui/reinterpret_inner_ref.stderr new file mode 100644 index 0000000..fd48fff --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_ref.stderr @@ -0,0 +1,224 @@ +error[E0080]: evaluation of `<*const [u8; 2] as bytemuck::cast::CastRaw<*const u16>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&[u8; 2] as bytemuck::ReinterpretInner<'_, &u16>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_ref.rs:4:17 + | +4 | let _: &u16 = (&[0u8; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [u16; 2] as bytemuck::cast::CastRaw<*const u32>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&[u16; 2] as bytemuck::ReinterpretInner<'_, &u32>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_ref.rs:5:17 + | +5 | let _: &u32 = (&[0u16; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [u32; 2] as bytemuck::cast::CastRaw<*const u64>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:824:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&[u32; 2] as bytemuck::ReinterpretInner<'_, &u64>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_ref.rs:6:17 + | +6 | let _: &u64 = (&[0u32; 2]).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u8 as bytemuck::cast::CastRaw<*const [u8; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&u8 as bytemuck::ReinterpretInner<'_, &[u8; 2]>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_ref.rs:8:21 + | +8 | let _: &[u8; 2] = (&0u8).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u16 as bytemuck::cast::CastRaw<*const [u16; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&u16 as bytemuck::ReinterpretInner<'_, &[u16; 2]>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_ref.rs:9:22 + | +9 | let _: &[u16; 2] = (&0u16).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u64 as bytemuck::cast::CastRaw<*const [u64; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:820:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&u64 as bytemuck::ReinterpretInner<'_, &[u64; 2]>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_ref.rs:10:22 + | +10 | let _: &[u64; 2] = (&0u64).reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ) + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:910:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&[[u8; 2]] as bytemuck::ReinterpretInner<'_, &[u16]>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_ref.rs:12:19 + | +12 | let _: &[u16] = [[0u8; 2]; 2].as_slice().reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ) + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:910:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&[[u16; 2]] as bytemuck::ReinterpretInner<'_, &[u32]>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_ref.rs:13:19 + | +13 | let _: &[u32] = [[0u16; 2]; 2].as_slice().reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ) + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:910:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&[[u32; 2]] as bytemuck::ReinterpretInner<'_, &[u64]>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_ref.rs:14:19 + | +14 | let _: &[u64] = [[0u32; 2]; 2].as_slice().reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion between slice types which may not succeed" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between slice types which may not succeed', $WORKSPACE/src/cast.rs:905:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&[u8] as bytemuck::ReinterpretInner<'_, &[()]>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_ref.rs:16:18 + | +16 | let _: &[()] = [0u8; 1].as_slice().reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion between slice types which may not succeed" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between slice types which may not succeed', $WORKSPACE/src/cast.rs:905:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&[[u8; 3]] as bytemuck::ReinterpretInner<'_, &[[u8; 2]]>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_ref.rs:17:23 + | +17 | let _: &[[u8; 2]] = [[0u8; 3]; 3].as_slice().reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion between slice types which may not succeed" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between slice types which may not succeed', $WORKSPACE/src/cast.rs:905:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&[[u16; 5]] as bytemuck::ReinterpretInner<'_, &[[u16; 2]]>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_ref.rs:18:24 + | +18 | let _: &[[u16; 2]] = [[0u16; 5]; 1].as_slice().reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion between slice types which may not succeed" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between slice types which may not succeed', $WORKSPACE/src/cast.rs:905:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&[[u32; 1]] as bytemuck::ReinterpretInner<'_, &[[u32; 2]]>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_ref.rs:19:24 + | +19 | let _: &[[u32; 2]] = [[0u32; 1]; 1].as_slice().reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/reinterpret_inner_vec.rs b/uitest/tests/ui/reinterpret_inner_vec.rs new file mode 100644 index 0000000..423fff5 --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_vec.rs @@ -0,0 +1,12 @@ +use bytemuck::ReinterpretInner; + +fn main() { + let _: Vec = vec![[0u8; 2]; 2].reinterpret_inner(); + let _: Vec = vec![[0u16; 2]; 2].reinterpret_inner(); + let _: Vec<[u32; 2]> = vec![0u64].reinterpret_inner(); + + let _: Vec<()> = vec![[0u8; 1]].reinterpret_inner(); + let _: Vec<[u8; 2]> = vec![0u8].reinterpret_inner(); + let _: Vec<[u16; 2]> = vec![0u16].reinterpret_inner(); + let _: Vec<[u64; 2]> = vec![0u64].reinterpret_inner(); +} diff --git a/uitest/tests/ui/reinterpret_inner_vec.stderr b/uitest/tests/ui/reinterpret_inner_vec.stderr new file mode 100644 index 0000000..973ca9f --- /dev/null +++ b/uitest/tests/ui/reinterpret_inner_vec.stderr @@ -0,0 +1,153 @@ +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Vec` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Vec` types with different alignments', $WORKSPACE/src/cast.rs:1342:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ) + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:910:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = as CastRaw>>::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::vec::Vec>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_vec.rs:4:21 + | +4 | let _: Vec = vec![[0u8; 2]; 2].reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Vec` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Vec` types with different alignments', $WORKSPACE/src/cast.rs:1342:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | align_of::() >= align_of::(), + | | "Attempted conversion to a type with a stricter alignment" + | | ) + | |_____^ the evaluated program panicked at 'Attempted conversion to a type with a stricter alignment', $WORKSPACE/src/cast.rs:910:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::vec::Vec>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_vec.rs:5:21 + | +5 | let _: Vec = vec![[0u16; 2]; 2].reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Vec` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Vec` types with different alignments', $WORKSPACE/src/cast.rs:1342:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::vec::Vec<[u32; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_vec.rs:6:26 + | +6 | let _: Vec<[u32; 2]> = vec![0u64].reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion between slice types which may not succeed" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between slice types which may not succeed', $WORKSPACE/src/cast.rs:905:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::vec::Vec<()>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_vec.rs:8:20 + | +8 | let _: Vec<()> = vec![[0u8; 1]].reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion between slice types which may not succeed" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between slice types which may not succeed', $WORKSPACE/src/cast.rs:905:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::vec::Vec<[u8; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_vec.rs:9:25 + | +9 | let _: Vec<[u8; 2]> = vec![0u8].reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion between slice types which may not succeed" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between slice types which may not succeed', $WORKSPACE/src/cast.rs:905:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::vec::Vec<[u16; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_vec.rs:10:26 + | +10 | let _: Vec<[u16; 2]> = vec![0u16].reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::CastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion between slice types which may not succeed" + | | ); + | |_____^ the evaluated program panicked at 'Attempted conversion between slice types which may not succeed', $WORKSPACE/src/cast.rs:905:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::ReinterpretInner<'_, std::vec::Vec<[u64; 2]>>>::reinterpret_inner` + --> tests/ui/reinterpret_inner_vec.rs:11:26 + | +11 | let _: Vec<[u64; 2]> = vec![0u64].reinterpret_inner(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/reinterpret_wrong_size.rs b/uitest/tests/ui/reinterpret_wrong_size.rs new file mode 100644 index 0000000..bbece7b --- /dev/null +++ b/uitest/tests/ui/reinterpret_wrong_size.rs @@ -0,0 +1,7 @@ +use bytemuck::Reinterpret; + +fn main() { + let _: u16 = 0u8.reinterpret(); + let _: u32 = 0u16.reinterpret(); + let _: u32 = 0u64.reinterpret(); +} diff --git a/uitest/tests/ui/reinterpret_wrong_size.stderr b/uitest/tests/ui/reinterpret_wrong_size.stderr new file mode 100644 index 0000000..80123c8 --- /dev/null +++ b/uitest/tests/ui/reinterpret_wrong_size.stderr @@ -0,0 +1,53 @@ +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "attempted conversion between types of different sizes" + | | ); + | |___^ the evaluated program panicked at 'attempted conversion between types of different sizes', $WORKSPACE/src/cast.rs:67:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn >::reinterpret` + --> tests/ui/reinterpret_wrong_size.rs:4:16 + | +4 | let _: u16 = 0u8.reinterpret(); + | ^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "attempted conversion between types of different sizes" + | | ); + | |___^ the evaluated program panicked at 'attempted conversion between types of different sizes', $WORKSPACE/src/cast.rs:67:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn >::reinterpret` + --> tests/ui/reinterpret_wrong_size.rs:5:16 + | +5 | let _: u32 = 0u16.reinterpret(); + | ^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "attempted conversion between types of different sizes" + | | ); + | |___^ the evaluated program panicked at 'attempted conversion between types of different sizes', $WORKSPACE/src/cast.rs:67:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn >::reinterpret` + --> tests/ui/reinterpret_wrong_size.rs:6:16 + | +6 | let _: u32 = 0u64.reinterpret(); + | ^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/try_reinterpret_inner_arc.rs b/uitest/tests/ui/try_reinterpret_inner_arc.rs new file mode 100644 index 0000000..e06e0a5 --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_arc.rs @@ -0,0 +1,18 @@ +use bytemuck::TryReinterpretInner; +use std::sync::Arc; + +fn main() { + let _: Arc = Arc::new([0u8; 2]).try_reinterpret_inner().unwrap(); + let _: Arc = Arc::new([0u16; 2]).try_reinterpret_inner().unwrap(); + let _: Arc<[u32; 2]> = Arc::new(0u64).try_reinterpret_inner().unwrap(); + + let _: Arc<[u8; 2]> = Arc::new(0u8).try_reinterpret_inner().unwrap(); + let _: Arc<[u16; 2]> = Arc::new(0u16).try_reinterpret_inner().unwrap(); + let _: Arc<[u64; 2]> = Arc::new(0u64).try_reinterpret_inner().unwrap(); + + let _: Arc<[u16]> = Arc::new([[0u8; 2]; 2]).try_reinterpret_inner().unwrap(); + let _: Arc<[u32]> = Arc::new([[0u16; 2]; 2]).try_reinterpret_inner().unwrap(); + let _: Arc<[[u32; 2]]> = Arc::new(0u64).try_reinterpret_inner().unwrap(); + + let _: Arc<[()]> = Arc::new([0u8; 1]).try_reinterpret_inner().unwrap(); +} diff --git a/uitest/tests/ui/try_reinterpret_inner_arc.stderr b/uitest/tests/ui/try_reinterpret_inner_arc.stderr new file mode 100644 index 0000000..052b2cc --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_arc.stderr @@ -0,0 +1,180 @@ +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Arc` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Arc` types with different alignments', $WORKSPACE/src/cast.rs:1328:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::sync::Arc>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_arc.rs:5:21 + | +5 | let _: Arc = Arc::new([0u8; 2]).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Arc` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Arc` types with different alignments', $WORKSPACE/src/cast.rs:1328:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::sync::Arc>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_arc.rs:6:21 + | +6 | let _: Arc = Arc::new([0u16; 2]).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Arc` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Arc` types with different alignments', $WORKSPACE/src/cast.rs:1328:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::sync::Arc<[u32; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_arc.rs:7:26 + | +7 | let _: Arc<[u32; 2]> = Arc::new(0u64).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u8 as bytemuck::cast::TryCastRaw<*const [u8; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::sync::Arc<[u8; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_arc.rs:9:25 + | +9 | let _: Arc<[u8; 2]> = Arc::new(0u8).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u16 as bytemuck::cast::TryCastRaw<*const [u16; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::sync::Arc<[u16; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_arc.rs:10:26 + | +10 | let _: Arc<[u16; 2]> = Arc::new(0u16).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u64 as bytemuck::cast::TryCastRaw<*const [u64; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::sync::Arc<[u64; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_arc.rs:11:26 + | +11 | let _: Arc<[u64; 2]> = Arc::new(0u64).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Arc` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Arc` types with different alignments', $WORKSPACE/src/cast.rs:1328:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::sync::Arc<[u16]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_arc.rs:13:23 + | +13 | let _: Arc<[u16]> = Arc::new([[0u8; 2]; 2]).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Arc` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Arc` types with different alignments', $WORKSPACE/src/cast.rs:1328:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::sync::Arc<[u32]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_arc.rs:14:23 + | +14 | let _: Arc<[u32]> = Arc::new([[0u16; 2]; 2]).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Arc` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Arc` types with different alignments', $WORKSPACE/src/cast.rs:1328:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::sync::Arc<[[u32; 2]]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_arc.rs:15:28 + | +15 | let _: Arc<[[u32; 2]]> = Arc::new(0u64).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [u8; 1] as bytemuck::cast::TryCastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion to a slice which cannot match the size of the item" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion to a slice which cannot match the size of the item', $WORKSPACE/src/cast.rs:1173:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::sync::Arc<[()]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_arc.rs:17:22 + | +17 | let _: Arc<[()]> = Arc::new([0u8; 1]).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/try_reinterpret_inner_arc_weak.rs b/uitest/tests/ui/try_reinterpret_inner_arc_weak.rs new file mode 100644 index 0000000..1f464df --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_arc_weak.rs @@ -0,0 +1,18 @@ +use bytemuck::TryReinterpretInner; +use std::sync::{Arc, Weak}; + +fn main() { + let _: Weak = + Arc::downgrade(&Arc::new([0u8; 2])).try_reinterpret_inner().unwrap(); + let _: Weak = + Arc::downgrade(&Arc::new([0u16; 2])).try_reinterpret_inner().unwrap(); + let _: Weak<[u32; 2]> = + Arc::downgrade(&Arc::new(0u64)).try_reinterpret_inner().unwrap(); + + let _: Weak<[u8; 2]> = + Arc::downgrade(&Arc::new(0u8)).try_reinterpret_inner().unwrap(); + let _: Weak<[u16; 2]> = + Arc::downgrade(&Arc::new(0u16)).try_reinterpret_inner().unwrap(); + let _: Weak<[u64; 2]> = + Arc::downgrade(&Arc::new(0u64)).try_reinterpret_inner().unwrap(); +} diff --git a/uitest/tests/ui/try_reinterpret_inner_arc_weak.stderr b/uitest/tests/ui/try_reinterpret_inner_arc_weak.stderr new file mode 100644 index 0000000..f622208 --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_arc_weak.stderr @@ -0,0 +1,107 @@ +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `sync::Weak` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `sync::Weak` types with different alignments', $WORKSPACE/src/cast.rs:1335:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::sync::Weak>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_arc_weak.rs:6:5 + | +6 | Arc::downgrade(&Arc::new([0u8; 2])).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `sync::Weak` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `sync::Weak` types with different alignments', $WORKSPACE/src/cast.rs:1335:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::sync::Weak>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_arc_weak.rs:8:5 + | +8 | Arc::downgrade(&Arc::new([0u16; 2])).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `sync::Weak` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `sync::Weak` types with different alignments', $WORKSPACE/src/cast.rs:1335:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::sync::Weak<[u32; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_arc_weak.rs:10:5 + | +10 | Arc::downgrade(&Arc::new(0u64)).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u8 as bytemuck::cast::TryCastRaw<*const [u8; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::sync::Weak<[u8; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_arc_weak.rs:13:5 + | +13 | Arc::downgrade(&Arc::new(0u8)).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u16 as bytemuck::cast::TryCastRaw<*const [u16; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::sync::Weak<[u16; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_arc_weak.rs:15:5 + | +15 | Arc::downgrade(&Arc::new(0u16)).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u64 as bytemuck::cast::TryCastRaw<*const [u64; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::sync::Weak<[u64; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_arc_weak.rs:17:5 + | +17 | Arc::downgrade(&Arc::new(0u64)).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/try_reinterpret_inner_atomic_ptr.rs b/uitest/tests/ui/try_reinterpret_inner_atomic_ptr.rs new file mode 100644 index 0000000..1b8d2f2 --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_atomic_ptr.rs @@ -0,0 +1,11 @@ +use bytemuck::TryReinterpretInner; +use core::sync::atomic::AtomicPtr; + +fn main() { + let _: AtomicPtr<[u8; 2]> = + AtomicPtr::new(&mut 0u8).try_reinterpret_inner().unwrap(); + let _: AtomicPtr<[u16; 2]> = + AtomicPtr::new(&mut 0u16).try_reinterpret_inner().unwrap(); + let _: AtomicPtr<[u64; 2]> = + AtomicPtr::new(&mut 0u64).try_reinterpret_inner().unwrap(); +} diff --git a/uitest/tests/ui/try_reinterpret_inner_atomic_ptr.stderr b/uitest/tests/ui/try_reinterpret_inner_atomic_ptr.stderr new file mode 100644 index 0000000..6d76ef0 --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_atomic_ptr.stderr @@ -0,0 +1,59 @@ +error[E0080]: evaluation of `<*const u8 as bytemuck::cast::TryCastRaw<*const [u8; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = <*const Src as TryCastRaw<*const Dst>>::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::sync::atomic::AtomicPtr<[u8; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_atomic_ptr.rs:6:5 + | +6 | AtomicPtr::new(&mut 0u8).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u16 as bytemuck::cast::TryCastRaw<*const [u16; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::sync::atomic::AtomicPtr<[u16; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_atomic_ptr.rs:8:5 + | +8 | AtomicPtr::new(&mut 0u16).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u64 as bytemuck::cast::TryCastRaw<*const [u64; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::sync::atomic::AtomicPtr<[u64; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_atomic_ptr.rs:10:5 + | +10 | AtomicPtr::new(&mut 0u64).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/try_reinterpret_inner_box.rs b/uitest/tests/ui/try_reinterpret_inner_box.rs new file mode 100644 index 0000000..2ddf2e7 --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_box.rs @@ -0,0 +1,17 @@ +use bytemuck::TryReinterpretInner; + +fn main() { + let _: Box = Box::new([0u8; 2]).try_reinterpret_inner().unwrap(); + let _: Box = Box::new([0u16; 2]).try_reinterpret_inner().unwrap(); + let _: Box<[u32; 2]> = Box::new(0u64).try_reinterpret_inner().unwrap(); + + let _: Box<[u8; 2]> = Box::new(0u8).try_reinterpret_inner().unwrap(); + let _: Box<[u16; 2]> = Box::new(0u16).try_reinterpret_inner().unwrap(); + let _: Box<[u64; 2]> = Box::new(0u64).try_reinterpret_inner().unwrap(); + + let _: Box<[u16]> = Box::new([[0u8; 2]; 2]).try_reinterpret_inner().unwrap(); + let _: Box<[u32]> = Box::new([[0u16; 2]; 2]).try_reinterpret_inner().unwrap(); + let _: Box<[[u32; 2]]> = Box::new(0u64).try_reinterpret_inner().unwrap(); + + let _: Box<[()]> = Box::new([0u8; 1]).try_reinterpret_inner().unwrap(); +} diff --git a/uitest/tests/ui/try_reinterpret_inner_box.stderr b/uitest/tests/ui/try_reinterpret_inner_box.stderr new file mode 100644 index 0000000..ab8e3b0 --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_box.stderr @@ -0,0 +1,180 @@ +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Box` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Box` types with different alignments', $WORKSPACE/src/cast.rs:1303:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::boxed::Box>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_box.rs:4:21 + | +4 | let _: Box = Box::new([0u8; 2]).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Box` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Box` types with different alignments', $WORKSPACE/src/cast.rs:1303:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::boxed::Box>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_box.rs:5:21 + | +5 | let _: Box = Box::new([0u16; 2]).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Box` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Box` types with different alignments', $WORKSPACE/src/cast.rs:1303:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::boxed::Box<[u32; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_box.rs:6:26 + | +6 | let _: Box<[u32; 2]> = Box::new(0u64).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u8 as bytemuck::cast::TryCastRaw<*const [u8; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::boxed::Box<[u8; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_box.rs:8:25 + | +8 | let _: Box<[u8; 2]> = Box::new(0u8).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u16 as bytemuck::cast::TryCastRaw<*const [u16; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::boxed::Box<[u16; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_box.rs:9:26 + | +9 | let _: Box<[u16; 2]> = Box::new(0u16).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u64 as bytemuck::cast::TryCastRaw<*const [u64; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::boxed::Box<[u64; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_box.rs:10:26 + | +10 | let _: Box<[u64; 2]> = Box::new(0u64).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Box` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Box` types with different alignments', $WORKSPACE/src/cast.rs:1303:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::boxed::Box<[u16]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_box.rs:12:23 + | +12 | let _: Box<[u16]> = Box::new([[0u8; 2]; 2]).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Box` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Box` types with different alignments', $WORKSPACE/src/cast.rs:1303:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::boxed::Box<[u32]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_box.rs:13:23 + | +13 | let _: Box<[u32]> = Box::new([[0u16; 2]; 2]).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Box` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Box` types with different alignments', $WORKSPACE/src/cast.rs:1303:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::boxed::Box<[[u32; 2]]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_box.rs:14:28 + | +14 | let _: Box<[[u32; 2]]> = Box::new(0u64).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [u8; 1] as bytemuck::cast::TryCastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion to a slice which cannot match the size of the item" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion to a slice which cannot match the size of the item', $WORKSPACE/src/cast.rs:1173:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::boxed::Box<[()]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_box.rs:16:22 + | +16 | let _: Box<[()]> = Box::new([0u8; 1]).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/try_reinterpret_inner_const_ptr.rs b/uitest/tests/ui/try_reinterpret_inner_const_ptr.rs new file mode 100644 index 0000000..94dd937 --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_const_ptr.rs @@ -0,0 +1,9 @@ +use bytemuck::TryReinterpretInner; + +fn main() { + let _: *const [u8; 2] = (&0u8 as *const u8).try_reinterpret_inner().unwrap(); + let _: *const [u16; 2] = + (&0u16 as *const u16).try_reinterpret_inner().unwrap(); + let _: *const [u64; 2] = + (&0u64 as *const u64).try_reinterpret_inner().unwrap(); +} diff --git a/uitest/tests/ui/try_reinterpret_inner_const_ptr.stderr b/uitest/tests/ui/try_reinterpret_inner_const_ptr.stderr new file mode 100644 index 0000000..3c82a9f --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_const_ptr.stderr @@ -0,0 +1,53 @@ +error[E0080]: evaluation of `<*const u8 as bytemuck::cast::TryCastRaw<*const [u8; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <*const u8 as bytemuck::TryReinterpretInner<'_, *const [u8; 2]>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_const_ptr.rs:4:27 + | +4 | let _: *const [u8; 2] = (&0u8 as *const u8).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u16 as bytemuck::cast::TryCastRaw<*const [u16; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <*const u16 as bytemuck::TryReinterpretInner<'_, *const [u16; 2]>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_const_ptr.rs:6:5 + | +6 | (&0u16 as *const u16).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u64 as bytemuck::cast::TryCastRaw<*const [u64; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <*const u64 as bytemuck::TryReinterpretInner<'_, *const [u64; 2]>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_const_ptr.rs:8:5 + | +8 | (&0u64 as *const u64).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/try_reinterpret_inner_cow.rs b/uitest/tests/ui/try_reinterpret_inner_cow.rs new file mode 100644 index 0000000..0bbb97a --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_cow.rs @@ -0,0 +1,18 @@ +use bytemuck::TryReinterpretInner; +use std::borrow::Cow; + +fn main() { + let _: Cow<[u8; 2]> = Cow::Borrowed(&0u8).try_reinterpret_inner().unwrap(); + let _: Cow<[u16; 2]> = Cow::Borrowed(&0u16).try_reinterpret_inner().unwrap(); + let _: Cow<[u64; 2]> = Cow::Borrowed(&0u64).try_reinterpret_inner().unwrap(); + + let _: Cow<[u16]> = + Cow::Borrowed([[0u8; 2]; 2].as_slice()).try_reinterpret_inner().unwrap(); + let _: Cow<[u32]> = + Cow::Borrowed([[0u16; 2]; 2].as_slice()).try_reinterpret_inner().unwrap(); + let _: Cow<[[u32; 2]]> = + Cow::Borrowed([0u64; 2].as_slice()).try_reinterpret_inner().unwrap(); + + let _: Cow<[()]> = + Cow::Borrowed([0u8; 1].as_slice()).try_reinterpret_inner().unwrap(); +} diff --git a/uitest/tests/ui/try_reinterpret_inner_cow.stderr b/uitest/tests/ui/try_reinterpret_inner_cow.stderr new file mode 100644 index 0000000..817a5d9 --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_cow.stderr @@ -0,0 +1,89 @@ +error[E0080]: evaluation of `<*const u8 as bytemuck::cast::TryCastRaw<*const [u8; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = <*const Src as TryCastRaw<*const Dst>>::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::borrow::Cow<'_, [u8; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_cow.rs:5:25 + | +5 | let _: Cow<[u8; 2]> = Cow::Borrowed(&0u8).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u16 as bytemuck::cast::TryCastRaw<*const [u16; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::borrow::Cow<'_, [u16; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_cow.rs:6:26 + | +6 | let _: Cow<[u16; 2]> = Cow::Borrowed(&0u16).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u64 as bytemuck::cast::TryCastRaw<*const [u64; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::borrow::Cow<'_, [u64; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_cow.rs:7:26 + | +7 | let _: Cow<[u64; 2]> = Cow::Borrowed(&0u64).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::TryCastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | (size_of::() == 0) == (size_of::() == 0), + | | "Attempted conversion between a zero-sized type and a non-zero-sized type" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between a zero-sized type and a non-zero-sized type', $WORKSPACE/src/cast.rs:1082:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = as TryCastRaw>>::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = as TryCastRaw>>::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::borrow::Cow<'_, [()]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_cow.rs:17:5 + | +17 | Cow::Borrowed([0u8; 1].as_slice()).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/try_reinterpret_inner_mut.rs b/uitest/tests/ui/try_reinterpret_inner_mut.rs new file mode 100644 index 0000000..13c61ef --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_mut.rs @@ -0,0 +1,9 @@ +use bytemuck::TryReinterpretInner; + +fn main() { + let _: &mut [u8; 2] = (&mut 0u8).try_reinterpret_inner().unwrap(); + let _: &mut [u16; 2] = (&mut 0u16).try_reinterpret_inner().unwrap(); + let _: &mut [u64; 2] = (&mut 0u64).try_reinterpret_inner().unwrap(); + + let _: &mut [()] = [0u8; 1].as_mut_slice().try_reinterpret_inner().unwrap(); +} diff --git a/uitest/tests/ui/try_reinterpret_inner_mut.stderr b/uitest/tests/ui/try_reinterpret_inner_mut.stderr new file mode 100644 index 0000000..eb49a52 --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_mut.stderr @@ -0,0 +1,83 @@ +error[E0080]: evaluation of `<*const u8 as bytemuck::cast::TryCastRaw<*const [u8; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = <*const Src as TryCastRaw<*const Dst>>::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: the above error was encountered while instantiating `fn <&mut u8 as bytemuck::TryReinterpretInner<'_, &mut [u8; 2]>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_mut.rs:4:25 + | +4 | let _: &mut [u8; 2] = (&mut 0u8).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u16 as bytemuck::cast::TryCastRaw<*const [u16; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&mut u16 as bytemuck::TryReinterpretInner<'_, &mut [u16; 2]>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_mut.rs:5:26 + | +5 | let _: &mut [u16; 2] = (&mut 0u16).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u64 as bytemuck::cast::TryCastRaw<*const [u64; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&mut u64 as bytemuck::TryReinterpretInner<'_, &mut [u64; 2]>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_mut.rs:6:26 + | +6 | let _: &mut [u64; 2] = (&mut 0u64).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::TryCastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | (size_of::() == 0) == (size_of::() == 0), + | | "Attempted conversion between a zero-sized type and a non-zero-sized type" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between a zero-sized type and a non-zero-sized type', $WORKSPACE/src/cast.rs:1082:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = as TryCastRaw>>::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: the above error was encountered while instantiating `fn <&mut [u8] as bytemuck::TryReinterpretInner<'_, &mut [()]>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_mut.rs:8:22 + | +8 | let _: &mut [()] = [0u8; 1].as_mut_slice().try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/try_reinterpret_inner_mut_ptr.rs b/uitest/tests/ui/try_reinterpret_inner_mut_ptr.rs new file mode 100644 index 0000000..ef0cf33 --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_mut_ptr.rs @@ -0,0 +1,9 @@ +use bytemuck::TryReinterpretInner; + +fn main() { + let _: *mut [u8; 2] = (&mut 0u8 as *mut u8).try_reinterpret_inner().unwrap(); + let _: *mut [u16; 2] = + (&mut 0u16 as *mut u16).try_reinterpret_inner().unwrap(); + let _: *mut [u64; 2] = + (&mut 0u64 as *mut u64).try_reinterpret_inner().unwrap(); +} diff --git a/uitest/tests/ui/try_reinterpret_inner_mut_ptr.stderr b/uitest/tests/ui/try_reinterpret_inner_mut_ptr.stderr new file mode 100644 index 0000000..c7b9d5b --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_mut_ptr.stderr @@ -0,0 +1,59 @@ +error[E0080]: evaluation of `<*const u8 as bytemuck::cast::TryCastRaw<*const [u8; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = <*const Src as TryCastRaw<*const Dst>>::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: the above error was encountered while instantiating `fn <*mut u8 as bytemuck::TryReinterpretInner<'_, *mut [u8; 2]>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_mut_ptr.rs:4:25 + | +4 | let _: *mut [u8; 2] = (&mut 0u8 as *mut u8).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u16 as bytemuck::cast::TryCastRaw<*const [u16; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <*mut u16 as bytemuck::TryReinterpretInner<'_, *mut [u16; 2]>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_mut_ptr.rs:6:5 + | +6 | (&mut 0u16 as *mut u16).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u64 as bytemuck::cast::TryCastRaw<*const [u64; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <*mut u64 as bytemuck::TryReinterpretInner<'_, *mut [u64; 2]>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_mut_ptr.rs:8:5 + | +8 | (&mut 0u64 as *mut u64).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/try_reinterpret_inner_non_null.rs b/uitest/tests/ui/try_reinterpret_inner_non_null.rs new file mode 100644 index 0000000..1c3cd28 --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_non_null.rs @@ -0,0 +1,14 @@ +use bytemuck::TryReinterpretInner; +use core::ptr::NonNull; + +fn main() { + let _: NonNull<[u8; 2]> = + NonNull::from(&mut 0u8).try_reinterpret_inner().unwrap(); + let _: NonNull<[u16; 2]> = + NonNull::from(&mut 0u16).try_reinterpret_inner().unwrap(); + let _: NonNull<[u64; 2]> = + NonNull::from(&mut 0u64).try_reinterpret_inner().unwrap(); + + let _: NonNull<[()]> = + NonNull::from([0u8; 1].as_mut_slice()).try_reinterpret_inner().unwrap(); +} diff --git a/uitest/tests/ui/try_reinterpret_inner_non_null.stderr b/uitest/tests/ui/try_reinterpret_inner_non_null.stderr new file mode 100644 index 0000000..84eb51e --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_non_null.stderr @@ -0,0 +1,83 @@ +error[E0080]: evaluation of `<*const u8 as bytemuck::cast::TryCastRaw<*const [u8; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = <*const Src as TryCastRaw<*const Dst>>::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::ptr::NonNull<[u8; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_non_null.rs:6:5 + | +6 | NonNull::from(&mut 0u8).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u16 as bytemuck::cast::TryCastRaw<*const [u16; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::ptr::NonNull<[u16; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_non_null.rs:8:5 + | +8 | NonNull::from(&mut 0u16).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u64 as bytemuck::cast::TryCastRaw<*const [u64; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::ptr::NonNull<[u64; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_non_null.rs:10:5 + | +10 | NonNull::from(&mut 0u64).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::TryCastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | (size_of::() == 0) == (size_of::() == 0), + | | "Attempted conversion between a zero-sized type and a non-zero-sized type" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between a zero-sized type and a non-zero-sized type', $WORKSPACE/src/cast.rs:1082:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = as TryCastRaw>>::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::ptr::NonNull<[()]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_non_null.rs:13:5 + | +13 | NonNull::from([0u8; 1].as_mut_slice()).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/try_reinterpret_inner_option.rs b/uitest/tests/ui/try_reinterpret_inner_option.rs new file mode 100644 index 0000000..ed06fa5 --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_option.rs @@ -0,0 +1,11 @@ +use bytemuck::TryReinterpretInner; + +fn main() { + let _: Option<&[u8; 2]> = Some(&0u8).try_reinterpret_inner().unwrap(); + let _: Option<&mut [u16; 2]> = + Some(&mut 0u16).try_reinterpret_inner().unwrap(); + let _: Option<&mut [()]> = + Some([0u8; 1].as_mut_slice().try_reinterpret_inner().unwrap()); + let _: Option> = + Some(Box::new([0u16; 2]).try_reinterpret_inner().unwrap()); +} diff --git a/uitest/tests/ui/try_reinterpret_inner_option.stderr b/uitest/tests/ui/try_reinterpret_inner_option.stderr new file mode 100644 index 0000000..5cb1bfa --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_option.stderr @@ -0,0 +1,89 @@ +error[E0080]: evaluation of `<*const u8 as bytemuck::cast::TryCastRaw<*const [u8; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = >::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::option::Option<&[u8; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_option.rs:4:29 + | +4 | let _: Option<&[u8; 2]> = Some(&0u8).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u16 as bytemuck::cast::TryCastRaw<*const [u16; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = <*const Src as TryCastRaw<*const Dst>>::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::option::Option<&mut [u16; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_option.rs:6:5 + | +6 | Some(&mut 0u16).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::TryCastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | (size_of::() == 0) == (size_of::() == 0), + | | "Attempted conversion between a zero-sized type and a non-zero-sized type" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between a zero-sized type and a non-zero-sized type', $WORKSPACE/src/cast.rs:1082:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = as TryCastRaw>>::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: the above error was encountered while instantiating `fn <&mut [u8] as bytemuck::TryReinterpretInner<'_, &mut [()]>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_option.rs:8:10 + | +8 | Some([0u8; 1].as_mut_slice().try_reinterpret_inner().unwrap()); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Box` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Box` types with different alignments', $WORKSPACE/src/cast.rs:1303:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::boxed::Box<[u8; 4]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_option.rs:10:10 + | +10 | Some(Box::new([0u16; 2]).try_reinterpret_inner().unwrap()); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/try_reinterpret_inner_pin.rs b/uitest/tests/ui/try_reinterpret_inner_pin.rs new file mode 100644 index 0000000..10804c4 --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_pin.rs @@ -0,0 +1,12 @@ +use bytemuck::TryReinterpretInner; +use core::pin::Pin; + +fn main() { + let _: Pin<&[u8; 2]> = Pin::new(&0u8).try_reinterpret_inner().unwrap(); + let _: Pin<&mut [u16; 2]> = + Pin::new(&mut 0u16).try_reinterpret_inner().unwrap(); + let _: Pin<&mut [()]> = + Pin::new([0u8; 1].as_mut_slice().try_reinterpret_inner().unwrap()); + let _: Pin> = + Pin::new(Box::new([0u16; 2]).try_reinterpret_inner().unwrap()); +} diff --git a/uitest/tests/ui/try_reinterpret_inner_pin.stderr b/uitest/tests/ui/try_reinterpret_inner_pin.stderr new file mode 100644 index 0000000..faa48d5 --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_pin.stderr @@ -0,0 +1,83 @@ +error[E0080]: evaluation of `<*const u8 as bytemuck::cast::TryCastRaw<*const [u8; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::pin::Pin<&[u8; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_pin.rs:5:26 + | +5 | let _: Pin<&[u8; 2]> = Pin::new(&0u8).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u16 as bytemuck::cast::TryCastRaw<*const [u16; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = <*const Src as TryCastRaw<*const Dst>>::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::pin::Pin<&mut [u16; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_pin.rs:7:5 + | +7 | Pin::new(&mut 0u16).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::TryCastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | (size_of::() == 0) == (size_of::() == 0), + | | "Attempted conversion between a zero-sized type and a non-zero-sized type" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between a zero-sized type and a non-zero-sized type', $WORKSPACE/src/cast.rs:1082:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = as TryCastRaw>>::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: the above error was encountered while instantiating `fn <&mut [u8] as bytemuck::TryReinterpretInner<'_, &mut [()]>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_pin.rs:9:14 + | +9 | Pin::new([0u8; 1].as_mut_slice().try_reinterpret_inner().unwrap()); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Box` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Box` types with different alignments', $WORKSPACE/src/cast.rs:1303:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::boxed::Box<[u8; 4]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_pin.rs:11:14 + | +11 | Pin::new(Box::new([0u16; 2]).try_reinterpret_inner().unwrap()); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/try_reinterpret_inner_rc.rs b/uitest/tests/ui/try_reinterpret_inner_rc.rs new file mode 100644 index 0000000..4e190ac --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_rc.rs @@ -0,0 +1,18 @@ +use bytemuck::TryReinterpretInner; +use std::rc::Rc; + +fn main() { + let _: Rc = Rc::new([0u8; 2]).try_reinterpret_inner().unwrap(); + let _: Rc = Rc::new([0u16; 2]).try_reinterpret_inner().unwrap(); + let _: Rc<[u32; 2]> = Rc::new(0u64).try_reinterpret_inner().unwrap(); + + let _: Rc<[u8; 2]> = Rc::new(0u8).try_reinterpret_inner().unwrap(); + let _: Rc<[u16; 2]> = Rc::new(0u16).try_reinterpret_inner().unwrap(); + let _: Rc<[u64; 2]> = Rc::new(0u64).try_reinterpret_inner().unwrap(); + + let _: Rc<[u16]> = Rc::new([[0u8; 2]; 2]).try_reinterpret_inner().unwrap(); + let _: Rc<[u32]> = Rc::new([[0u16; 2]; 2]).try_reinterpret_inner().unwrap(); + let _: Rc<[[u32; 2]]> = Rc::new(0u64).try_reinterpret_inner().unwrap(); + + let _: Rc<[()]> = Rc::new([0u8; 1]).try_reinterpret_inner().unwrap(); +} diff --git a/uitest/tests/ui/try_reinterpret_inner_rc.stderr b/uitest/tests/ui/try_reinterpret_inner_rc.stderr new file mode 100644 index 0000000..b1e01e4 --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_rc.stderr @@ -0,0 +1,180 @@ +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Rc` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Rc` types with different alignments', $WORKSPACE/src/cast.rs:1312:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::rc::Rc>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_rc.rs:5:20 + | +5 | let _: Rc = Rc::new([0u8; 2]).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Rc` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Rc` types with different alignments', $WORKSPACE/src/cast.rs:1312:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::rc::Rc>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_rc.rs:6:20 + | +6 | let _: Rc = Rc::new([0u16; 2]).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Rc` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Rc` types with different alignments', $WORKSPACE/src/cast.rs:1312:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::rc::Rc<[u32; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_rc.rs:7:25 + | +7 | let _: Rc<[u32; 2]> = Rc::new(0u64).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u8 as bytemuck::cast::TryCastRaw<*const [u8; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::rc::Rc<[u8; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_rc.rs:9:24 + | +9 | let _: Rc<[u8; 2]> = Rc::new(0u8).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u16 as bytemuck::cast::TryCastRaw<*const [u16; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::rc::Rc<[u16; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_rc.rs:10:25 + | +10 | let _: Rc<[u16; 2]> = Rc::new(0u16).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u64 as bytemuck::cast::TryCastRaw<*const [u64; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::rc::Rc<[u64; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_rc.rs:11:25 + | +11 | let _: Rc<[u64; 2]> = Rc::new(0u64).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Rc` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Rc` types with different alignments', $WORKSPACE/src/cast.rs:1312:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::rc::Rc<[u16]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_rc.rs:13:22 + | +13 | let _: Rc<[u16]> = Rc::new([[0u8; 2]; 2]).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Rc` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Rc` types with different alignments', $WORKSPACE/src/cast.rs:1312:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::rc::Rc<[u32]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_rc.rs:14:22 + | +14 | let _: Rc<[u32]> = Rc::new([[0u16; 2]; 2]).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Rc` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Rc` types with different alignments', $WORKSPACE/src/cast.rs:1312:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::rc::Rc<[[u32; 2]]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_rc.rs:15:27 + | +15 | let _: Rc<[[u32; 2]]> = Rc::new(0u64).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const [u8; 1] as bytemuck::cast::TryCastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::() + | | || (size_of::() != 0 && size_of::() % size_of::() == 0), + | | "Attempted conversion to a slice which cannot match the size of the item" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion to a slice which cannot match the size of the item', $WORKSPACE/src/cast.rs:1173:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::rc::Rc<[()]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_rc.rs:17:21 + | +17 | let _: Rc<[()]> = Rc::new([0u8; 1]).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/try_reinterpret_inner_rc_weak.rs b/uitest/tests/ui/try_reinterpret_inner_rc_weak.rs new file mode 100644 index 0000000..92f91ef --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_rc_weak.rs @@ -0,0 +1,18 @@ +use bytemuck::TryReinterpretInner; +use std::rc::{Rc, Weak}; + +fn main() { + let _: Weak = + Rc::downgrade(&Rc::new([0u8; 2])).try_reinterpret_inner().unwrap(); + let _: Weak = + Rc::downgrade(&Rc::new([0u16; 2])).try_reinterpret_inner().unwrap(); + let _: Weak<[u32; 2]> = + Rc::downgrade(&Rc::new(0u64)).try_reinterpret_inner().unwrap(); + + let _: Weak<[u8; 2]> = + Rc::downgrade(&Rc::new(0u8)).try_reinterpret_inner().unwrap(); + let _: Weak<[u16; 2]> = + Rc::downgrade(&Rc::new(0u16)).try_reinterpret_inner().unwrap(); + let _: Weak<[u64; 2]> = + Rc::downgrade(&Rc::new(0u64)).try_reinterpret_inner().unwrap(); +} diff --git a/uitest/tests/ui/try_reinterpret_inner_rc_weak.stderr b/uitest/tests/ui/try_reinterpret_inner_rc_weak.stderr new file mode 100644 index 0000000..3b3fde9 --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_rc_weak.stderr @@ -0,0 +1,107 @@ +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `rc::Weak` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `rc::Weak` types with different alignments', $WORKSPACE/src/cast.rs:1319:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::rc::Weak>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_rc_weak.rs:6:5 + | +6 | Rc::downgrade(&Rc::new([0u8; 2])).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `rc::Weak` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `rc::Weak` types with different alignments', $WORKSPACE/src/cast.rs:1319:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::rc::Weak>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_rc_weak.rs:8:5 + | +8 | Rc::downgrade(&Rc::new([0u16; 2])).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `rc::Weak` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `rc::Weak` types with different alignments', $WORKSPACE/src/cast.rs:1319:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::rc::Weak<[u32; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_rc_weak.rs:10:5 + | +10 | Rc::downgrade(&Rc::new(0u64)).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u8 as bytemuck::cast::TryCastRaw<*const [u8; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::rc::Weak<[u8; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_rc_weak.rs:13:5 + | +13 | Rc::downgrade(&Rc::new(0u8)).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u16 as bytemuck::cast::TryCastRaw<*const [u16; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::rc::Weak<[u16; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_rc_weak.rs:15:5 + | +15 | Rc::downgrade(&Rc::new(0u16)).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u64 as bytemuck::cast::TryCastRaw<*const [u64; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::rc::Weak<[u64; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_rc_weak.rs:17:5 + | +17 | Rc::downgrade(&Rc::new(0u64)).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/try_reinterpret_inner_ref.rs b/uitest/tests/ui/try_reinterpret_inner_ref.rs new file mode 100644 index 0000000..5eacf70 --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_ref.rs @@ -0,0 +1,8 @@ +use bytemuck::TryReinterpretInner; + +fn main() { + let _: &[u8; 2] = (&0u8).try_reinterpret_inner().unwrap(); + let _: &[u16; 2] = (&0u16).try_reinterpret_inner().unwrap(); + let _: &[u64; 2] = (&0u64).try_reinterpret_inner().unwrap(); + let _: &[()] = [0u8; 1].as_slice().try_reinterpret_inner().unwrap(); +} diff --git a/uitest/tests/ui/try_reinterpret_inner_ref.stderr b/uitest/tests/ui/try_reinterpret_inner_ref.stderr new file mode 100644 index 0000000..3832fcf --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_ref.stderr @@ -0,0 +1,71 @@ +error[E0080]: evaluation of `<*const u8 as bytemuck::cast::TryCastRaw<*const [u8; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&u8 as bytemuck::TryReinterpretInner<'_, &[u8; 2]>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_ref.rs:4:21 + | +4 | let _: &[u8; 2] = (&0u8).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u16 as bytemuck::cast::TryCastRaw<*const [u16; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&u16 as bytemuck::TryReinterpretInner<'_, &[u16; 2]>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_ref.rs:5:22 + | +5 | let _: &[u16; 2] = (&0u16).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `<*const u64 as bytemuck::cast::TryCastRaw<*const [u64; 2]>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | size_of::() == size_of::(), + | | "Attempted conversion between types of different sizes." + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between types of different sizes.', $WORKSPACE/src/cast.rs:1049:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&u64 as bytemuck::TryReinterpretInner<'_, &[u64; 2]>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_ref.rs:6:22 + | +6 | let _: &[u64; 2] = (&0u64).try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::TryCastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | (size_of::() == 0) == (size_of::() == 0), + | | "Attempted conversion between a zero-sized type and a non-zero-sized type" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between a zero-sized type and a non-zero-sized type', $WORKSPACE/src/cast.rs:1082:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn <&[u8] as bytemuck::TryReinterpretInner<'_, &[()]>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_ref.rs:7:18 + | +7 | let _: &[()] = [0u8; 1].as_slice().try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/try_reinterpret_inner_vec.rs b/uitest/tests/ui/try_reinterpret_inner_vec.rs new file mode 100644 index 0000000..4e84f70 --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_vec.rs @@ -0,0 +1,9 @@ +use bytemuck::TryReinterpretInner; + +fn main() { + let _: Vec = vec![[0u8; 2]; 2].try_reinterpret_inner().unwrap(); + let _: Vec = vec![[0u16; 2]; 2].try_reinterpret_inner().unwrap(); + let _: Vec<[u32; 2]> = vec![0u64; 2].try_reinterpret_inner().unwrap(); + + let _: Vec<()> = vec![[0u8; 1]].try_reinterpret_inner().unwrap(); +} diff --git a/uitest/tests/ui/try_reinterpret_inner_vec.stderr b/uitest/tests/ui/try_reinterpret_inner_vec.stderr new file mode 100644 index 0000000..646cb31 --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_inner_vec.stderr @@ -0,0 +1,77 @@ +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Vec` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Vec` types with different alignments', $WORKSPACE/src/cast.rs:1342:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::vec::Vec>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_vec.rs:4:21 + | +4 | let _: Vec = vec![[0u8; 2]; 2].try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Vec` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Vec` types with different alignments', $WORKSPACE/src/cast.rs:1342:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::vec::Vec>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_vec.rs:5:21 + | +5 | let _: Vec = vec![[0u16; 2]; 2].try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | Src::ALIGN == Dst::ALIGN, + | | "Attempted conversion between `Vec` types with different alignments" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between `Vec` types with different alignments', $WORKSPACE/src/cast.rs:1342:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::vec::Vec<[u32; 2]>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_vec.rs:6:26 + | +6 | let _: Vec<[u32; 2]> = vec![0u64; 2].try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of ` as bytemuck::cast::TryCastRaw>>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = assert!( + | ______________________^ + | | (size_of::() == 0) == (size_of::() == 0), + | | "Attempted conversion between a zero-sized type and a non-zero-sized type" + | | ); + | |___^ the evaluated program panicked at 'Attempted conversion between a zero-sized type and a non-zero-sized type', $WORKSPACE/src/cast.rs:1082:22 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: erroneous constant used + --> $WORKSPACE/src/cast.rs + | + | const ASSERT: () = as TryCastRaw>>::ASSERT; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: the above error was encountered while instantiating `fn as bytemuck::TryReinterpretInner<'_, std::vec::Vec<()>>>::try_reinterpret_inner` + --> tests/ui/try_reinterpret_inner_vec.rs:8:20 + | +8 | let _: Vec<()> = vec![[0u8; 1]].try_reinterpret_inner().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uitest/tests/ui/try_reinterpret_wrong_size.rs b/uitest/tests/ui/try_reinterpret_wrong_size.rs new file mode 100644 index 0000000..d0a846c --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_wrong_size.rs @@ -0,0 +1,7 @@ +use bytemuck::TryReinterpret; + +fn main() { + let _: u16 = 0u8.try_reinterpret().unwrap(); + let _: u32 = 0u16.try_reinterpret().unwrap(); + let _: u32 = 0u64.try_reinterpret().unwrap(); +} diff --git a/uitest/tests/ui/try_reinterpret_wrong_size.stderr b/uitest/tests/ui/try_reinterpret_wrong_size.stderr new file mode 100644 index 0000000..089c3db --- /dev/null +++ b/uitest/tests/ui/try_reinterpret_wrong_size.stderr @@ -0,0 +1,50 @@ +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "attempted conversion between types of different sizes" + | | ); + | |_____^ the evaluated program panicked at 'attempted conversion between types of different sizes', $WORKSPACE/src/cast.rs:102:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn >::try_reinterpret` + --> tests/ui/try_reinterpret_wrong_size.rs:4:16 + | +4 | let _: u16 = 0u8.try_reinterpret().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "attempted conversion between types of different sizes" + | | ); + | |_____^ the evaluated program panicked at 'attempted conversion between types of different sizes', $WORKSPACE/src/cast.rs:102:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn >::try_reinterpret` + --> tests/ui/try_reinterpret_wrong_size.rs:5:16 + | +5 | let _: u32 = 0u16.try_reinterpret().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: evaluation of `>::ASSERT` failed + --> $WORKSPACE/src/cast.rs + | + | / assert!( + | | size_of::() == size_of::(), + | | "attempted conversion between types of different sizes" + | | ); + | |_____^ the evaluated program panicked at 'attempted conversion between types of different sizes', $WORKSPACE/src/cast.rs:102:5 + | + = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: the above error was encountered while instantiating `fn >::try_reinterpret` + --> tests/ui/try_reinterpret_wrong_size.rs:6:16 + | +6 | let _: u32 = 0u64.try_reinterpret().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^