From a629a67976213c62c104c3441fbb98eddc33b773 Mon Sep 17 00:00:00 2001 From: Joshua Liebow-Feeser Date: Thu, 25 May 2023 22:01:30 +0000 Subject: [PATCH] Add `transmute_ref!` macro This macro is like the existing `transmute!`, but it transmutes immutable references rather than values. Issue #159 --- Cargo.toml | 4 +- src/lib.rs | 127 ++++++ tests/ui-msrv/transmute-illegal-lifetime.rs | 1 + .../ui-msrv/transmute-illegal-lifetime.stderr | 9 + tests/ui-msrv/transmute-illegal.stderr | 422 ++++++++++++++++- .../ui-nightly/transmute-illegal-lifetime.rs | 13 + .../transmute-illegal-lifetime.stderr | 12 + tests/ui-nightly/transmute-illegal.rs | 37 +- tests/ui-nightly/transmute-illegal.stderr | 428 +++++++++++++++++- tests/ui-stable/transmute-illegal-lifetime.rs | 1 + .../transmute-illegal-lifetime.stderr | 12 + tests/ui-stable/transmute-illegal.stderr | 416 ++++++++++++++++- zerocopy-derive/Cargo.toml | 4 +- 13 files changed, 1459 insertions(+), 27 deletions(-) create mode 120000 tests/ui-msrv/transmute-illegal-lifetime.rs create mode 100644 tests/ui-msrv/transmute-illegal-lifetime.stderr create mode 100644 tests/ui-nightly/transmute-illegal-lifetime.rs create mode 100644 tests/ui-nightly/transmute-illegal-lifetime.stderr create mode 120000 tests/ui-stable/transmute-illegal-lifetime.rs create mode 100644 tests/ui-stable/transmute-illegal-lifetime.stderr diff --git a/Cargo.toml b/Cargo.toml index ae067a74708..59e5c76952e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -51,5 +51,5 @@ optional = true rand = "0.6" rustversion = "1.0" static_assertions = "1.1" -# Required for "and $N others" normalization -trybuild = ">=1.0.70" +# Version >=1.0.70 is required for "and $N others" normalization. +trybuild = { version = ">=1.0.70", features = ["diff"] } diff --git a/src/lib.rs b/src/lib.rs index 6e438a31cc5..8d8e7d962ba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1463,6 +1463,96 @@ macro_rules! transmute { }} } +/// A type whose size is equal to `mem::align_of::()`. +/// +/// This type is used internally in `transmute_ref!`, and so needs to be +/// exported. It is not intended for direct use by users of zerocopy, and so is +/// `#[doc(hidden)]`. +#[doc(hidden)] +#[allow(missing_debug_implementations)] +#[repr(C)] +pub struct SizeIsAlign { + // This field ensures that: + // - The size is always at least 1 (the minimum possible alignment). + // - If the alignment is greater than 1, Rust has to round up to the next + // multiple of it in order to make sure that `SizeIsAlign`'s size is a + // multiple of that alignment. Without this field, its size could be 0, + // which is a valid multiple of any alignment. + _u: u8, + _a: [T; 0], +} + +impl SizeIsAlign { + #[doc(hidden)] + pub fn new(_t: T) -> SizeIsAlign { + SizeIsAlign { _u: 0, _a: [] } + } + + #[doc(hidden)] + pub fn into_t(self) -> T { + unreachable!() + } +} + +/// Safely transmutes an immutable reference of one type to an immutable +/// reference of another type of the same size and alignment. +/// +/// The expression `$e` must have a concrete type, `&T`, where `T: Sized + +/// AsBytes`. The `transmute_ref!` expression must also have a concrete type, +/// `&U` (`U` is inferred from the calling context), where `U: Sized + +/// FromBytes`. +/// +/// The lifetime of the input type, `&T`, must be the same as or outlive the +/// lifetime of the output type, `&U`. +#[macro_export] +macro_rules! transmute_ref { + ($e:expr) => {{ + // NOTE: This must be a macro (rather than a function with trait bounds) + // because there's no way, in a generic context, to enforce that two + // types have the same size or alignment. `core::mem::transmute` uses + // compiler magic to enforce size equality so long as the types are + // concrete. We use `SizeIsAlign` to create a type whose size is equal + // to the alignment of another type so that we can use `transmute` to + // check alignment as well. + + let e = $e; + #[allow(unused, clippy::diverging_sub_expression)] + if false { + // This branch, though never taken, ensures that the type of `e` is + // `&T` where `T: 't + Sized + AsBytes`, that the type of this macro + // expression is `&U` where `U: 'u + Sized + FromBytes`, and that + // `'t` outlives `'u`. + const fn transmute<'u, 't: 'u, T: 't + Sized + $crate::AsBytes, U: 'u + Sized + $crate::FromBytes>(_t: &'t T) -> &'u U { + unreachable!() + } + transmute(e) + } else if false { + // This branch, though never taken, ensures that the alignment of + // `T` is equal to the alignment of `U`. + let target = unreachable!(); + e = ⌖ + + // SAFETY: This code is never executed. + let ret: $crate::SizeIsAlign<_> = unsafe { $crate::__real_transmute($crate::SizeIsAlign::new(target)) }; + &ret.into_t() + } else { + // SAFETY: `core::mem::transmute` ensures that the type of `e` and + // the type of this macro invocation expression have the same size. + // We know this transmute is safe thanks to the `AsBytes` and + // `FromBytes` bounds enforced by the `false` branch. + // + // We use `$crate::__real_transmute` because we know it will always + // be available for crates which are using the 2015 edition of Rust. + // By contrast, if we were to use `std::mem::transmute`, this macro + // would not work for such crates in `no_std` contexts, and if we + // were to use `core::mem::transmute`, this macro would not work in + // `std` contexts in which `core` was not manually imported. This is + // not a problem for 2018 edition crates. + unsafe { $crate::__real_transmute(e) } + } + }} +} + /// A length- and alignment-checked reference to a byte slice which can safely /// be reinterpreted as another type. /// @@ -3195,6 +3285,43 @@ mod tests { assert_eq!(X, ARRAY_OF_ARRAYS); } + #[test] + fn test_size_is_align() { + macro_rules! test { + ($ty:ty) => { + assert_eq!(mem::size_of::>(), mem::align_of::<$ty>()); + }; + } + + test!(()); + test!(u8); + test!(AU64); + test!([AU64; 2]); + } + + #[test] + fn test_transmute_ref() { + // Test that memory is transmuted as expected. + let array_of_u8s = [0u8, 1, 2, 3, 4, 5, 6, 7]; + let array_of_arrays = [[0, 1], [2, 3], [4, 5], [6, 7]]; + let x: &[[u8; 2]; 4] = transmute_ref!(&array_of_u8s); + assert_eq!(*x, array_of_arrays); + let x: &[u8; 8] = transmute_ref!(&array_of_arrays); + assert_eq!(*x, array_of_u8s); + + // Test that `transmute_ref!` is legal in a const context. + const ARRAY_OF_U8S: [u8; 8] = [0u8, 1, 2, 3, 4, 5, 6, 7]; + const ARRAY_OF_ARRAYS: [[u8; 2]; 4] = [[0, 1], [2, 3], [4, 5], [6, 7]]; + #[allow(clippy::redundant_static_lifetimes)] + const X: &'static [[u8; 2]; 4] = transmute_ref!(&ARRAY_OF_U8S); + assert_eq!(*X, ARRAY_OF_ARRAYS); + + // Test that it's legal to transmute a reference while shrinking the + // lifetime (note that `X` has the lifetime `'static`). + let x: &[u8; 8] = transmute_ref!(X); + assert_eq!(*x, ARRAY_OF_U8S); + } + #[test] fn test_address() { // Test that the `Deref` and `DerefMut` implementations return a diff --git a/tests/ui-msrv/transmute-illegal-lifetime.rs b/tests/ui-msrv/transmute-illegal-lifetime.rs new file mode 120000 index 00000000000..35ca7817e83 --- /dev/null +++ b/tests/ui-msrv/transmute-illegal-lifetime.rs @@ -0,0 +1 @@ +../ui-nightly/transmute-illegal-lifetime.rs \ No newline at end of file diff --git a/tests/ui-msrv/transmute-illegal-lifetime.stderr b/tests/ui-msrv/transmute-illegal-lifetime.stderr new file mode 100644 index 00000000000..1485ab48cd8 --- /dev/null +++ b/tests/ui-msrv/transmute-illegal-lifetime.stderr @@ -0,0 +1,9 @@ +error[E0597]: `x` does not live long enough + --> tests/ui-msrv/transmute-illegal-lifetime.rs:12:52 + | +12 | let _: &'static u64 = zerocopy::transmute_ref!(&x); + | ------------ ^^ borrowed value does not live long enough + | | + | type annotation requires that `x` is borrowed for `'static` +13 | } + | - `x` dropped here while still borrowed diff --git a/tests/ui-msrv/transmute-illegal.stderr b/tests/ui-msrv/transmute-illegal.stderr index 731134d4790..8df8f01ee14 100644 --- a/tests/ui-msrv/transmute-illegal.stderr +++ b/tests/ui-msrv/transmute-illegal.stderr @@ -1,8 +1,8 @@ error[E0277]: the trait bound `*const usize: AsBytes` is not satisfied - --> tests/ui-msrv/transmute-illegal.rs:10:30 + --> tests/ui-msrv/transmute-illegal.rs:12:30 | -10 | const POINTER_VALUE: usize = zerocopy::transmute!(&0usize as *const usize); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +12 | const POINTER_VALUE: usize = transmute!(&0usize as *const usize); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | | | the trait `AsBytes` is not implemented for `*const usize` | required by a bound introduced by this call @@ -18,8 +18,416 @@ error[E0277]: the trait bound `*const usize: AsBytes` is not satisfied isize and $N others note: required by a bound in `POINTER_VALUE::transmute` - --> tests/ui-msrv/transmute-illegal.rs:10:30 + --> tests/ui-msrv/transmute-illegal.rs:12:30 | -10 | const POINTER_VALUE: usize = zerocopy::transmute!(&0usize as *const usize); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `POINTER_VALUE::transmute` - = note: this error originates in the macro `zerocopy::transmute` (in Nightly builds, run with -Z macro-backtrace for more info) +12 | const POINTER_VALUE: usize = transmute!(&0usize as *const usize); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `POINTER_VALUE::transmute` + = note: this error originates in the macro `transmute` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the trait bound `NotZerocopy: AsBytes` is not satisfied + --> tests/ui-msrv/transmute-illegal.rs:17:33 + | +17 | const SRC_NOT_AS_BYTES: usize = transmute!(NotZerocopy(0)); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | the trait `AsBytes` is not implemented for `NotZerocopy` + | required by a bound introduced by this call + | + = help: the following other types implement trait `AsBytes`: + () + F32 + F64 + I128 + I16 + I32 + I64 + ManuallyDrop + and $N others +note: required by a bound in `SRC_NOT_AS_BYTES::transmute` + --> tests/ui-msrv/transmute-illegal.rs:17:33 + | +17 | const SRC_NOT_AS_BYTES: usize = transmute!(NotZerocopy(0)); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `SRC_NOT_AS_BYTES::transmute` + = note: this error originates in the macro `transmute` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the trait bound `NotZerocopy: FromBytes` is not satisfied + --> tests/ui-msrv/transmute-illegal.rs:19:41 + | +19 | const DST_NOT_FROM_BYTES: NotZerocopy = transmute!(0usize); + | ^^^^^^^^^^^^^^^^^^ the trait `FromBytes` is not implemented for `NotZerocopy` + | + = help: the following other types implement trait `FromBytes`: + () + F32 + F64 + I128 + I16 + I32 + I64 + ManuallyDrop + and $N others +note: required by a bound in `DST_NOT_FROM_BYTES::transmute` + --> tests/ui-msrv/transmute-illegal.rs:19:41 + | +19 | const DST_NOT_FROM_BYTES: NotZerocopy = transmute!(0usize); + | ^^^^^^^^^^^^^^^^^^ required by this bound in `DST_NOT_FROM_BYTES::transmute` + = note: this error originates in the macro `transmute` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0512]: cannot transmute between types of different sizes, or dependently-sized types + --> tests/ui-msrv/transmute-illegal.rs:21:30 + | +21 | const INCREASE_SIZE: usize = transmute!(0u8); + | ^^^^^^^^^^^^^^^ + | + = note: source type: `u8` (8 bits) + = note: target type: `usize` (64 bits) + = note: this error originates in the macro `transmute` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0512]: cannot transmute between types of different sizes, or dependently-sized types + --> tests/ui-msrv/transmute-illegal.rs:23:27 + | +23 | const DECREASE_SIZE: u8 = transmute!(0usize); + | ^^^^^^^^^^^^^^^^^^ + | + = note: source type: `usize` (64 bits) + = note: target type: `u8` (8 bits) + = note: this error originates in the macro `transmute` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the trait bound `NotZerocopy: AsBytes` is not satisfied + --> tests/ui-msrv/transmute-illegal.rs:25:38 + | +25 | const REF_SRC_NOT_AS_BYTES: &usize = transmute_ref!(&NotZerocopy(0)); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | the trait `AsBytes` is not implemented for `NotZerocopy` + | required by a bound introduced by this call + | + = help: the following other types implement trait `AsBytes`: + () + F32 + F64 + I128 + I16 + I32 + I64 + ManuallyDrop + and $N others +note: required by a bound in `REF_SRC_NOT_AS_BYTES::transmute` + --> tests/ui-msrv/transmute-illegal.rs:25:38 + | +25 | const REF_SRC_NOT_AS_BYTES: &usize = transmute_ref!(&NotZerocopy(0)); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `REF_SRC_NOT_AS_BYTES::transmute` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the trait bound `NotZerocopy: FromBytes` is not satisfied + --> tests/ui-msrv/transmute-illegal.rs:27:46 + | +27 | const REF_DST_NOT_FROM_BYTES: &NotZerocopy = transmute_ref!(&0usize); + | ^^^^^^^^^^^^^^^^^^^^^^^ the trait `FromBytes` is not implemented for `NotZerocopy` + | + = help: the following other types implement trait `FromBytes`: + () + F32 + F64 + I128 + I16 + I32 + I64 + ManuallyDrop + and $N others +note: required by a bound in `REF_DST_NOT_FROM_BYTES::transmute` + --> tests/ui-msrv/transmute-illegal.rs:27:46 + | +27 | const REF_DST_NOT_FROM_BYTES: &NotZerocopy = transmute_ref!(&0usize); + | ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `REF_DST_NOT_FROM_BYTES::transmute` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0512]: cannot transmute between types of different sizes, or dependently-sized types + --> tests/ui-msrv/transmute-illegal.rs:29:35 + | +29 | const REF_INCREASE_SIZE: &usize = transmute_ref!(&0u8); + | ^^^^^^^^^^^^^^^^^^^^ + | + = note: source type: `SizeIsAlign` (8 bits) + = note: target type: `SizeIsAlign` (64 bits) + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0512]: cannot transmute between types of different sizes, or dependently-sized types + --> tests/ui-msrv/transmute-illegal.rs:31:32 + | +31 | const REF_DECREASE_SIZE: &u8 = transmute_ref!(&0usize); + | ^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: source type: `SizeIsAlign` (64 bits) + = note: target type: `SizeIsAlign` (8 bits) + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0512]: cannot transmute between types of different sizes, or dependently-sized types + --> tests/ui-msrv/transmute-illegal.rs:33:38 + | +33 | const REF_INCREASE_ALIGNMENT: &u64 = transmute_ref!(&[0u8; 8]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: source type: `SizeIsAlign<[u8; 8]>` (8 bits) + = note: target type: `SizeIsAlign` (64 bits) + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0512]: cannot transmute between types of different sizes, or dependently-sized types + --> tests/ui-msrv/transmute-illegal.rs:37:42 + | +37 | const REF_DECREASE_ALIGNMENT: &[u8; 8] = transmute_ref!(&0u64); + | ^^^^^^^^^^^^^^^^^^^^^ + | + = note: source type: `SizeIsAlign` (64 bits) + = note: target type: `SizeIsAlign<[u8; 8]>` (8 bits) + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-msrv/transmute-illegal.rs:39:35 + | +39 | const REF_SRC_UNSIZED: &[u8; 1] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | doesn't have a size known at compile-time + | required by a bound introduced by this call + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `REF_SRC_UNSIZED::transmute` + --> tests/ui-msrv/transmute-illegal.rs:39:35 + | +39 | const REF_SRC_UNSIZED: &[u8; 1] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `REF_SRC_UNSIZED::transmute` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-msrv/transmute-illegal.rs:39:35 + | +39 | const REF_SRC_UNSIZED: &[u8; 1] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` + = note: all local variables must have a statically known size + = help: unsized locals are gated as an unstable feature + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-msrv/transmute-illegal.rs:39:35 + | +39 | const REF_SRC_UNSIZED: &[u8; 1] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | doesn't have a size known at compile-time + | required by a bound introduced by this call + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign::::new` + --> src/lib.rs + | + | impl SizeIsAlign { + | ^ required by this bound in `SizeIsAlign::::new` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-msrv/transmute-illegal.rs:39:35 + | +39 | const REF_SRC_UNSIZED: &[u8; 1] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign` + --> src/lib.rs + | + | pub struct SizeIsAlign { + | ^ required by this bound in `SizeIsAlign` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-msrv/transmute-illegal.rs:39:35 + | +39 | const REF_SRC_UNSIZED: &[u8; 1] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign` + --> src/lib.rs + | + | pub struct SizeIsAlign { + | ^ required by this bound in `SizeIsAlign` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-msrv/transmute-illegal.rs:41:32 + | +41 | const REF_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8; 1]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `REF_DST_UNSIZED::transmute` + --> tests/ui-msrv/transmute-illegal.rs:41:32 + | +41 | const REF_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8; 1]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `REF_DST_UNSIZED::transmute` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-msrv/transmute-illegal.rs:41:32 + | +41 | const REF_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8; 1]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | doesn't have a size known at compile-time + | required by a bound introduced by this call + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign::::into_t` + --> src/lib.rs + | + | impl SizeIsAlign { + | ^ required by this bound in `SizeIsAlign::::into_t` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-msrv/transmute-illegal.rs:41:32 + | +41 | const REF_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8; 1]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign` + --> src/lib.rs + | + | pub struct SizeIsAlign { + | ^ required by this bound in `SizeIsAlign` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-msrv/transmute-illegal.rs:41:32 + | +41 | const REF_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8; 1]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign` + --> src/lib.rs + | + | pub struct SizeIsAlign { + | ^ required by this bound in `SizeIsAlign` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-msrv/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | doesn't have a size known at compile-time + | required by a bound introduced by this call + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `REF_SRC_DST_UNSIZED::transmute` + --> tests/ui-msrv/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `REF_SRC_DST_UNSIZED::transmute` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-msrv/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `REF_SRC_DST_UNSIZED::transmute` + --> tests/ui-msrv/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `REF_SRC_DST_UNSIZED::transmute` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-msrv/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` + = note: all local variables must have a statically known size + = help: unsized locals are gated as an unstable feature + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-msrv/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | doesn't have a size known at compile-time + | required by a bound introduced by this call + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign::::new` + --> src/lib.rs + | + | impl SizeIsAlign { + | ^ required by this bound in `SizeIsAlign::::new` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-msrv/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign` + --> src/lib.rs + | + | pub struct SizeIsAlign { + | ^ required by this bound in `SizeIsAlign` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-msrv/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign` + --> src/lib.rs + | + | pub struct SizeIsAlign { + | ^ required by this bound in `SizeIsAlign` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-msrv/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | doesn't have a size known at compile-time + | required by a bound introduced by this call + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign::::into_t` + --> src/lib.rs + | + | impl SizeIsAlign { + | ^ required by this bound in `SizeIsAlign::::into_t` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-msrv/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign` + --> src/lib.rs + | + | pub struct SizeIsAlign { + | ^ required by this bound in `SizeIsAlign` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/tests/ui-nightly/transmute-illegal-lifetime.rs b/tests/ui-nightly/transmute-illegal-lifetime.rs new file mode 100644 index 00000000000..c7eedd636aa --- /dev/null +++ b/tests/ui-nightly/transmute-illegal-lifetime.rs @@ -0,0 +1,13 @@ +// Copyright 2022 The Fuchsia Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +extern crate zerocopy; + +fn main() {} + +fn increase_lifetime() { + let x = 0u64; + // It is illegal to increase the lifetime scope. + let _: &'static u64 = zerocopy::transmute_ref!(&x); +} diff --git a/tests/ui-nightly/transmute-illegal-lifetime.stderr b/tests/ui-nightly/transmute-illegal-lifetime.stderr new file mode 100644 index 00000000000..8dd9835c1d2 --- /dev/null +++ b/tests/ui-nightly/transmute-illegal-lifetime.stderr @@ -0,0 +1,12 @@ +error[E0597]: `x` does not live long enough + --> tests/ui-nightly/transmute-illegal-lifetime.rs:12:52 + | +10 | let x = 0u64; + | - binding `x` declared here +11 | // It is illegal to increase the lifetime scope. +12 | let _: &'static u64 = zerocopy::transmute_ref!(&x); + | ------------ ^^ borrowed value does not live long enough + | | + | type annotation requires that `x` is borrowed for `'static` +13 | } + | - `x` dropped here while still borrowed diff --git a/tests/ui-nightly/transmute-illegal.rs b/tests/ui-nightly/transmute-illegal.rs index 74b84391cd1..155546a0c6f 100644 --- a/tests/ui-nightly/transmute-illegal.rs +++ b/tests/ui-nightly/transmute-illegal.rs @@ -4,7 +4,40 @@ extern crate zerocopy; +use zerocopy::{transmute, transmute_ref}; + fn main() {} -// It is unsound to inspect the usize value of a pointer during const eval. -const POINTER_VALUE: usize = zerocopy::transmute!(&0usize as *const usize); +// It is unsupported to inspect the usize value of a pointer during const eval. +const POINTER_VALUE: usize = transmute!(&0usize as *const usize); + +#[repr(transparent)] +struct NotZerocopy(usize); + +const SRC_NOT_AS_BYTES: usize = transmute!(NotZerocopy(0)); + +const DST_NOT_FROM_BYTES: NotZerocopy = transmute!(0usize); + +const INCREASE_SIZE: usize = transmute!(0u8); + +const DECREASE_SIZE: u8 = transmute!(0usize); + +const REF_SRC_NOT_AS_BYTES: &usize = transmute_ref!(&NotZerocopy(0)); + +const REF_DST_NOT_FROM_BYTES: &NotZerocopy = transmute_ref!(&0usize); + +const REF_INCREASE_SIZE: &usize = transmute_ref!(&0u8); + +const REF_DECREASE_SIZE: &u8 = transmute_ref!(&0usize); + +const REF_INCREASE_ALIGNMENT: &u64 = transmute_ref!(&[0u8; 8]); + +// It is legal but currently unsupported to transmute into a type with lesser +// alignment. +const REF_DECREASE_ALIGNMENT: &[u8; 8] = transmute_ref!(&0u64); + +const REF_SRC_UNSIZED: &[u8; 1] = transmute_ref!(&[0u8][..]); + +const REF_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8; 1]); + +const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); diff --git a/tests/ui-nightly/transmute-illegal.stderr b/tests/ui-nightly/transmute-illegal.stderr index a57544b26f2..7a93069a9fe 100644 --- a/tests/ui-nightly/transmute-illegal.stderr +++ b/tests/ui-nightly/transmute-illegal.stderr @@ -1,16 +1,430 @@ error[E0277]: the trait bound `*const usize: AsBytes` is not satisfied - --> tests/ui-nightly/transmute-illegal.rs:10:30 + --> tests/ui-nightly/transmute-illegal.rs:12:30 | -10 | const POINTER_VALUE: usize = zerocopy::transmute!(&0usize as *const usize); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +12 | const POINTER_VALUE: usize = transmute!(&0usize as *const usize); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | | | the trait `AsBytes` is not implemented for `*const usize` | required by a bound introduced by this call | = help: the trait `AsBytes` is implemented for `usize` note: required by a bound in `POINTER_VALUE::transmute` - --> tests/ui-nightly/transmute-illegal.rs:10:30 + --> tests/ui-nightly/transmute-illegal.rs:12:30 | -10 | const POINTER_VALUE: usize = zerocopy::transmute!(&0usize as *const usize); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `transmute` - = note: this error originates in the macro `zerocopy::transmute` (in Nightly builds, run with -Z macro-backtrace for more info) +12 | const POINTER_VALUE: usize = transmute!(&0usize as *const usize); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `transmute` + = note: this error originates in the macro `transmute` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the trait bound `NotZerocopy: AsBytes` is not satisfied + --> tests/ui-nightly/transmute-illegal.rs:17:33 + | +17 | const SRC_NOT_AS_BYTES: usize = transmute!(NotZerocopy(0)); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | the trait `AsBytes` is not implemented for `NotZerocopy` + | required by a bound introduced by this call + | + = help: the following other types implement trait `AsBytes`: + () + F32 + F64 + I128 + I16 + I32 + I64 + ManuallyDrop + and $N others +note: required by a bound in `SRC_NOT_AS_BYTES::transmute` + --> tests/ui-nightly/transmute-illegal.rs:17:33 + | +17 | const SRC_NOT_AS_BYTES: usize = transmute!(NotZerocopy(0)); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `transmute` + = note: this error originates in the macro `transmute` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the trait bound `NotZerocopy: FromBytes` is not satisfied + --> tests/ui-nightly/transmute-illegal.rs:19:41 + | +19 | const DST_NOT_FROM_BYTES: NotZerocopy = transmute!(0usize); + | ^^^^^^^^^^^^^^^^^^ the trait `FromBytes` is not implemented for `NotZerocopy` + | + = help: the following other types implement trait `FromBytes`: + () + F32 + F64 + I128 + I16 + I32 + I64 + ManuallyDrop + and $N others +note: required by a bound in `DST_NOT_FROM_BYTES::transmute` + --> tests/ui-nightly/transmute-illegal.rs:19:41 + | +19 | const DST_NOT_FROM_BYTES: NotZerocopy = transmute!(0usize); + | ^^^^^^^^^^^^^^^^^^ required by this bound in `transmute` + = note: this error originates in the macro `transmute` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0512]: cannot transmute between types of different sizes, or dependently-sized types + --> tests/ui-nightly/transmute-illegal.rs:21:30 + | +21 | const INCREASE_SIZE: usize = transmute!(0u8); + | ^^^^^^^^^^^^^^^ + | + = note: source type: `u8` (8 bits) + = note: target type: `usize` (64 bits) + = note: this error originates in the macro `transmute` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0512]: cannot transmute between types of different sizes, or dependently-sized types + --> tests/ui-nightly/transmute-illegal.rs:23:27 + | +23 | const DECREASE_SIZE: u8 = transmute!(0usize); + | ^^^^^^^^^^^^^^^^^^ + | + = note: source type: `usize` (64 bits) + = note: target type: `u8` (8 bits) + = note: this error originates in the macro `transmute` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the trait bound `NotZerocopy: AsBytes` is not satisfied + --> tests/ui-nightly/transmute-illegal.rs:25:38 + | +25 | const REF_SRC_NOT_AS_BYTES: &usize = transmute_ref!(&NotZerocopy(0)); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | the trait `AsBytes` is not implemented for `NotZerocopy` + | required by a bound introduced by this call + | + = help: the following other types implement trait `AsBytes`: + () + F32 + F64 + I128 + I16 + I32 + I64 + ManuallyDrop + and $N others +note: required by a bound in `REF_SRC_NOT_AS_BYTES::transmute` + --> tests/ui-nightly/transmute-illegal.rs:25:38 + | +25 | const REF_SRC_NOT_AS_BYTES: &usize = transmute_ref!(&NotZerocopy(0)); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `transmute` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the trait bound `NotZerocopy: FromBytes` is not satisfied + --> tests/ui-nightly/transmute-illegal.rs:27:46 + | +27 | const REF_DST_NOT_FROM_BYTES: &NotZerocopy = transmute_ref!(&0usize); + | ^^^^^^^^^^^^^^^^^^^^^^^ the trait `FromBytes` is not implemented for `NotZerocopy` + | + = help: the following other types implement trait `FromBytes`: + () + F32 + F64 + I128 + I16 + I32 + I64 + ManuallyDrop + and $N others +note: required by a bound in `REF_DST_NOT_FROM_BYTES::transmute` + --> tests/ui-nightly/transmute-illegal.rs:27:46 + | +27 | const REF_DST_NOT_FROM_BYTES: &NotZerocopy = transmute_ref!(&0usize); + | ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `transmute` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0512]: cannot transmute between types of different sizes, or dependently-sized types + --> tests/ui-nightly/transmute-illegal.rs:29:35 + | +29 | const REF_INCREASE_SIZE: &usize = transmute_ref!(&0u8); + | ^^^^^^^^^^^^^^^^^^^^ + | + = note: source type: `SizeIsAlign` (8 bits) + = note: target type: `SizeIsAlign` (64 bits) + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0512]: cannot transmute between types of different sizes, or dependently-sized types + --> tests/ui-nightly/transmute-illegal.rs:31:32 + | +31 | const REF_DECREASE_SIZE: &u8 = transmute_ref!(&0usize); + | ^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: source type: `SizeIsAlign` (64 bits) + = note: target type: `SizeIsAlign` (8 bits) + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0512]: cannot transmute between types of different sizes, or dependently-sized types + --> tests/ui-nightly/transmute-illegal.rs:33:38 + | +33 | const REF_INCREASE_ALIGNMENT: &u64 = transmute_ref!(&[0u8; 8]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: source type: `SizeIsAlign<[u8; 8]>` (8 bits) + = note: target type: `SizeIsAlign` (64 bits) + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0512]: cannot transmute between types of different sizes, or dependently-sized types + --> tests/ui-nightly/transmute-illegal.rs:37:42 + | +37 | const REF_DECREASE_ALIGNMENT: &[u8; 8] = transmute_ref!(&0u64); + | ^^^^^^^^^^^^^^^^^^^^^ + | + = note: source type: `SizeIsAlign` (64 bits) + = note: target type: `SizeIsAlign<[u8; 8]>` (8 bits) + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-nightly/transmute-illegal.rs:39:35 + | +39 | const REF_SRC_UNSIZED: &[u8; 1] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | doesn't have a size known at compile-time + | required by a bound introduced by this call + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `REF_SRC_UNSIZED::transmute` + --> tests/ui-nightly/transmute-illegal.rs:39:35 + | +39 | const REF_SRC_UNSIZED: &[u8; 1] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `transmute` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-nightly/transmute-illegal.rs:39:35 + | +39 | const REF_SRC_UNSIZED: &[u8; 1] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` + = note: all local variables must have a statically known size + = help: unsized locals are gated as an unstable feature + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-nightly/transmute-illegal.rs:39:35 + | +39 | const REF_SRC_UNSIZED: &[u8; 1] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | doesn't have a size known at compile-time + | required by a bound introduced by this call + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign::::new` + --> src/lib.rs + | + | impl SizeIsAlign { + | ^ required by this bound in `SizeIsAlign::::new` + | #[doc(hidden)] + | pub fn new(_t: T) -> SizeIsAlign { + | --- required by a bound in this associated function + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-nightly/transmute-illegal.rs:39:35 + | +39 | const REF_SRC_UNSIZED: &[u8; 1] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign` + --> src/lib.rs + | + | pub struct SizeIsAlign { + | ^ required by this bound in `SizeIsAlign` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-nightly/transmute-illegal.rs:39:35 + | +39 | const REF_SRC_UNSIZED: &[u8; 1] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign` + --> src/lib.rs + | + | pub struct SizeIsAlign { + | ^ required by this bound in `SizeIsAlign` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-nightly/transmute-illegal.rs:41:32 + | +41 | const REF_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8; 1]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `REF_DST_UNSIZED::transmute` + --> tests/ui-nightly/transmute-illegal.rs:41:32 + | +41 | const REF_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8; 1]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `transmute` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-nightly/transmute-illegal.rs:41:32 + | +41 | const REF_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8; 1]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign::::into_t` + --> src/lib.rs + | + | impl SizeIsAlign { + | ^ required by this bound in `SizeIsAlign::::into_t` +... + | pub fn into_t(self) -> T { + | ------ required by a bound in this associated function + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-nightly/transmute-illegal.rs:41:32 + | +41 | const REF_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8; 1]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign` + --> src/lib.rs + | + | pub struct SizeIsAlign { + | ^ required by this bound in `SizeIsAlign` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-nightly/transmute-illegal.rs:41:32 + | +41 | const REF_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8; 1]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign` + --> src/lib.rs + | + | pub struct SizeIsAlign { + | ^ required by this bound in `SizeIsAlign` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-nightly/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | doesn't have a size known at compile-time + | required by a bound introduced by this call + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `REF_SRC_DST_UNSIZED::transmute` + --> tests/ui-nightly/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `transmute` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-nightly/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `REF_SRC_DST_UNSIZED::transmute` + --> tests/ui-nightly/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `transmute` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-nightly/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` + = note: all local variables must have a statically known size + = help: unsized locals are gated as an unstable feature + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-nightly/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | doesn't have a size known at compile-time + | required by a bound introduced by this call + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign::::new` + --> src/lib.rs + | + | impl SizeIsAlign { + | ^ required by this bound in `SizeIsAlign::::new` + | #[doc(hidden)] + | pub fn new(_t: T) -> SizeIsAlign { + | --- required by a bound in this associated function + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-nightly/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign` + --> src/lib.rs + | + | pub struct SizeIsAlign { + | ^ required by this bound in `SizeIsAlign` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-nightly/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign` + --> src/lib.rs + | + | pub struct SizeIsAlign { + | ^ required by this bound in `SizeIsAlign` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-nightly/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign::::into_t` + --> src/lib.rs + | + | impl SizeIsAlign { + | ^ required by this bound in `SizeIsAlign::::into_t` +... + | pub fn into_t(self) -> T { + | ------ required by a bound in this associated function + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-nightly/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign` + --> src/lib.rs + | + | pub struct SizeIsAlign { + | ^ required by this bound in `SizeIsAlign` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/tests/ui-stable/transmute-illegal-lifetime.rs b/tests/ui-stable/transmute-illegal-lifetime.rs new file mode 120000 index 00000000000..35ca7817e83 --- /dev/null +++ b/tests/ui-stable/transmute-illegal-lifetime.rs @@ -0,0 +1 @@ +../ui-nightly/transmute-illegal-lifetime.rs \ No newline at end of file diff --git a/tests/ui-stable/transmute-illegal-lifetime.stderr b/tests/ui-stable/transmute-illegal-lifetime.stderr new file mode 100644 index 00000000000..913c110cb54 --- /dev/null +++ b/tests/ui-stable/transmute-illegal-lifetime.stderr @@ -0,0 +1,12 @@ +error[E0597]: `x` does not live long enough + --> tests/ui-stable/transmute-illegal-lifetime.rs:12:52 + | +10 | let x = 0u64; + | - binding `x` declared here +11 | // It is illegal to increase the lifetime scope. +12 | let _: &'static u64 = zerocopy::transmute_ref!(&x); + | ------------ ^^ borrowed value does not live long enough + | | + | type annotation requires that `x` is borrowed for `'static` +13 | } + | - `x` dropped here while still borrowed diff --git a/tests/ui-stable/transmute-illegal.stderr b/tests/ui-stable/transmute-illegal.stderr index e9ac2402daa..e1a8eafbd33 100644 --- a/tests/ui-stable/transmute-illegal.stderr +++ b/tests/ui-stable/transmute-illegal.stderr @@ -1,16 +1,418 @@ error[E0277]: the trait bound `*const usize: AsBytes` is not satisfied - --> tests/ui-stable/transmute-illegal.rs:10:30 + --> tests/ui-stable/transmute-illegal.rs:12:30 | -10 | const POINTER_VALUE: usize = zerocopy::transmute!(&0usize as *const usize); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +12 | const POINTER_VALUE: usize = transmute!(&0usize as *const usize); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | | | the trait `AsBytes` is not implemented for `*const usize` | required by a bound introduced by this call | = help: the trait `AsBytes` is implemented for `usize` note: required by a bound in `POINTER_VALUE::transmute` - --> tests/ui-stable/transmute-illegal.rs:10:30 + --> tests/ui-stable/transmute-illegal.rs:12:30 | -10 | const POINTER_VALUE: usize = zerocopy::transmute!(&0usize as *const usize); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `transmute` - = note: this error originates in the macro `zerocopy::transmute` (in Nightly builds, run with -Z macro-backtrace for more info) +12 | const POINTER_VALUE: usize = transmute!(&0usize as *const usize); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `transmute` + = note: this error originates in the macro `transmute` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the trait bound `NotZerocopy: AsBytes` is not satisfied + --> tests/ui-stable/transmute-illegal.rs:17:33 + | +17 | const SRC_NOT_AS_BYTES: usize = transmute!(NotZerocopy(0)); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | the trait `AsBytes` is not implemented for `NotZerocopy` + | required by a bound introduced by this call + | + = help: the following other types implement trait `AsBytes`: + () + F32 + F64 + I128 + I16 + I32 + I64 + ManuallyDrop + and $N others +note: required by a bound in `SRC_NOT_AS_BYTES::transmute` + --> tests/ui-stable/transmute-illegal.rs:17:33 + | +17 | const SRC_NOT_AS_BYTES: usize = transmute!(NotZerocopy(0)); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `transmute` + = note: this error originates in the macro `transmute` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the trait bound `NotZerocopy: FromBytes` is not satisfied + --> tests/ui-stable/transmute-illegal.rs:19:41 + | +19 | const DST_NOT_FROM_BYTES: NotZerocopy = transmute!(0usize); + | ^^^^^^^^^^^^^^^^^^ the trait `FromBytes` is not implemented for `NotZerocopy` + | + = help: the following other types implement trait `FromBytes`: + () + F32 + F64 + I128 + I16 + I32 + I64 + ManuallyDrop + and $N others +note: required by a bound in `DST_NOT_FROM_BYTES::transmute` + --> tests/ui-stable/transmute-illegal.rs:19:41 + | +19 | const DST_NOT_FROM_BYTES: NotZerocopy = transmute!(0usize); + | ^^^^^^^^^^^^^^^^^^ required by this bound in `transmute` + = note: this error originates in the macro `transmute` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0512]: cannot transmute between types of different sizes, or dependently-sized types + --> tests/ui-stable/transmute-illegal.rs:21:30 + | +21 | const INCREASE_SIZE: usize = transmute!(0u8); + | ^^^^^^^^^^^^^^^ + | + = note: source type: `u8` (8 bits) + = note: target type: `usize` (64 bits) + = note: this error originates in the macro `transmute` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0512]: cannot transmute between types of different sizes, or dependently-sized types + --> tests/ui-stable/transmute-illegal.rs:23:27 + | +23 | const DECREASE_SIZE: u8 = transmute!(0usize); + | ^^^^^^^^^^^^^^^^^^ + | + = note: source type: `usize` (64 bits) + = note: target type: `u8` (8 bits) + = note: this error originates in the macro `transmute` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the trait bound `NotZerocopy: AsBytes` is not satisfied + --> tests/ui-stable/transmute-illegal.rs:25:38 + | +25 | const REF_SRC_NOT_AS_BYTES: &usize = transmute_ref!(&NotZerocopy(0)); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | the trait `AsBytes` is not implemented for `NotZerocopy` + | required by a bound introduced by this call + | + = help: the following other types implement trait `AsBytes`: + () + F32 + F64 + I128 + I16 + I32 + I64 + ManuallyDrop + and $N others +note: required by a bound in `REF_SRC_NOT_AS_BYTES::transmute` + --> tests/ui-stable/transmute-illegal.rs:25:38 + | +25 | const REF_SRC_NOT_AS_BYTES: &usize = transmute_ref!(&NotZerocopy(0)); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `transmute` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the trait bound `NotZerocopy: FromBytes` is not satisfied + --> tests/ui-stable/transmute-illegal.rs:27:46 + | +27 | const REF_DST_NOT_FROM_BYTES: &NotZerocopy = transmute_ref!(&0usize); + | ^^^^^^^^^^^^^^^^^^^^^^^ the trait `FromBytes` is not implemented for `NotZerocopy` + | + = help: the following other types implement trait `FromBytes`: + () + F32 + F64 + I128 + I16 + I32 + I64 + ManuallyDrop + and $N others +note: required by a bound in `REF_DST_NOT_FROM_BYTES::transmute` + --> tests/ui-stable/transmute-illegal.rs:27:46 + | +27 | const REF_DST_NOT_FROM_BYTES: &NotZerocopy = transmute_ref!(&0usize); + | ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `transmute` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0512]: cannot transmute between types of different sizes, or dependently-sized types + --> tests/ui-stable/transmute-illegal.rs:29:35 + | +29 | const REF_INCREASE_SIZE: &usize = transmute_ref!(&0u8); + | ^^^^^^^^^^^^^^^^^^^^ + | + = note: source type: `SizeIsAlign` (8 bits) + = note: target type: `SizeIsAlign` (64 bits) + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0512]: cannot transmute between types of different sizes, or dependently-sized types + --> tests/ui-stable/transmute-illegal.rs:31:32 + | +31 | const REF_DECREASE_SIZE: &u8 = transmute_ref!(&0usize); + | ^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: source type: `SizeIsAlign` (64 bits) + = note: target type: `SizeIsAlign` (8 bits) + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0512]: cannot transmute between types of different sizes, or dependently-sized types + --> tests/ui-stable/transmute-illegal.rs:33:38 + | +33 | const REF_INCREASE_ALIGNMENT: &u64 = transmute_ref!(&[0u8; 8]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: source type: `SizeIsAlign<[u8; 8]>` (8 bits) + = note: target type: `SizeIsAlign` (64 bits) + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0512]: cannot transmute between types of different sizes, or dependently-sized types + --> tests/ui-stable/transmute-illegal.rs:37:42 + | +37 | const REF_DECREASE_ALIGNMENT: &[u8; 8] = transmute_ref!(&0u64); + | ^^^^^^^^^^^^^^^^^^^^^ + | + = note: source type: `SizeIsAlign` (64 bits) + = note: target type: `SizeIsAlign<[u8; 8]>` (8 bits) + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-stable/transmute-illegal.rs:39:35 + | +39 | const REF_SRC_UNSIZED: &[u8; 1] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | doesn't have a size known at compile-time + | required by a bound introduced by this call + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `REF_SRC_UNSIZED::transmute` + --> tests/ui-stable/transmute-illegal.rs:39:35 + | +39 | const REF_SRC_UNSIZED: &[u8; 1] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `transmute` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-stable/transmute-illegal.rs:39:35 + | +39 | const REF_SRC_UNSIZED: &[u8; 1] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` + = note: all local variables must have a statically known size + = help: unsized locals are gated as an unstable feature + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-stable/transmute-illegal.rs:39:35 + | +39 | const REF_SRC_UNSIZED: &[u8; 1] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | doesn't have a size known at compile-time + | required by a bound introduced by this call + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign::::new` + --> src/lib.rs + | + | impl SizeIsAlign { + | ^ required by this bound in `SizeIsAlign::::new` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-stable/transmute-illegal.rs:39:35 + | +39 | const REF_SRC_UNSIZED: &[u8; 1] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign` + --> src/lib.rs + | + | pub struct SizeIsAlign { + | ^ required by this bound in `SizeIsAlign` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-stable/transmute-illegal.rs:39:35 + | +39 | const REF_SRC_UNSIZED: &[u8; 1] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign` + --> src/lib.rs + | + | pub struct SizeIsAlign { + | ^ required by this bound in `SizeIsAlign` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-stable/transmute-illegal.rs:41:32 + | +41 | const REF_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8; 1]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `REF_DST_UNSIZED::transmute` + --> tests/ui-stable/transmute-illegal.rs:41:32 + | +41 | const REF_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8; 1]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `transmute` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-stable/transmute-illegal.rs:41:32 + | +41 | const REF_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8; 1]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign::::into_t` + --> src/lib.rs + | + | impl SizeIsAlign { + | ^ required by this bound in `SizeIsAlign::::into_t` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-stable/transmute-illegal.rs:41:32 + | +41 | const REF_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8; 1]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign` + --> src/lib.rs + | + | pub struct SizeIsAlign { + | ^ required by this bound in `SizeIsAlign` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-stable/transmute-illegal.rs:41:32 + | +41 | const REF_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8; 1]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign` + --> src/lib.rs + | + | pub struct SizeIsAlign { + | ^ required by this bound in `SizeIsAlign` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-stable/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | doesn't have a size known at compile-time + | required by a bound introduced by this call + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `REF_SRC_DST_UNSIZED::transmute` + --> tests/ui-stable/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `transmute` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-stable/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `REF_SRC_DST_UNSIZED::transmute` + --> tests/ui-stable/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `transmute` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-stable/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` + = note: all local variables must have a statically known size + = help: unsized locals are gated as an unstable feature + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-stable/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | doesn't have a size known at compile-time + | required by a bound introduced by this call + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign::::new` + --> src/lib.rs + | + | impl SizeIsAlign { + | ^ required by this bound in `SizeIsAlign::::new` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-stable/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign` + --> src/lib.rs + | + | pub struct SizeIsAlign { + | ^ required by this bound in `SizeIsAlign` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-stable/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign` + --> src/lib.rs + | + | pub struct SizeIsAlign { + | ^ required by this bound in `SizeIsAlign` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-stable/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign::::into_t` + --> src/lib.rs + | + | impl SizeIsAlign { + | ^ required by this bound in `SizeIsAlign::::into_t` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> tests/ui-stable/transmute-illegal.rs:43:36 + | +43 | const REF_SRC_DST_UNSIZED: &[u8] = transmute_ref!(&[0u8][..]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `SizeIsAlign` + --> src/lib.rs + | + | pub struct SizeIsAlign { + | ^ required by this bound in `SizeIsAlign` + = note: this error originates in the macro `transmute_ref` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/zerocopy-derive/Cargo.toml b/zerocopy-derive/Cargo.toml index ca1a43cde25..89b4a8d73d1 100644 --- a/zerocopy-derive/Cargo.toml +++ b/zerocopy-derive/Cargo.toml @@ -25,6 +25,6 @@ syn = { version = "2", features = ["visit"] } [dev-dependencies] rustversion = "1.0" static_assertions = "1.1" -# Required for "and $N others" normalization -trybuild = ">=1.0.70" +# Version >=1.0.70 is required for "and $N others" normalization. +trybuild = { version = ">=1.0.70", features = ["diff"] } zerocopy = { path = "../" }