Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Inline Size Assertion Annotations #1405

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4484,7 +4484,7 @@ macro_rules! transmute_ref {
// value returned from this branch.
let u;

$crate::assert_size_eq!(t, u);
$crate::assert_type_size_eq!(t, u);
$crate::assert_align_gt_eq!(t, u);

&u
Expand All @@ -4495,7 +4495,7 @@ macro_rules! transmute_ref {
// `AssertSrcIsImmutable`, `AssertDstIsFromBytes`, and
// `AssertDstIsImmutable` above.
// - We know that `size_of::<Src>() == size_of::<Dst>()` thanks to
// the use of `assert_size_eq!` above.
// the use of `assert_type_size_eq!` above.
// - We know that `align_of::<Src>() >= align_of::<Dst>()` thanks to
// the use of `assert_align_gt_eq!` above.
let u = unsafe { $crate::macro_util::transmute_ref(e) };
Expand Down Expand Up @@ -4640,14 +4640,14 @@ macro_rules! transmute_mut {
// the value returned from this branch.
let u;

$crate::assert_size_eq!(t, u);
$crate::assert_type_size_eq!(t, u);
$crate::assert_align_gt_eq!(t, u);

&mut u
} else {
// SAFETY: For source type `Src` and destination type `Dst`:
// - We know that `size_of::<Src>() == size_of::<Dst>()` thanks to
// the use of `assert_size_eq!` above.
// the use of `assert_type_size_eq!` above.
// - We know that `align_of::<Src>() >= align_of::<Dst>()` thanks to
// the use of `assert_align_gt_eq!` above.
let u = unsafe { $crate::macro_util::transmute_mut(e) };
Expand Down
15 changes: 14 additions & 1 deletion src/macro_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ macro_rules! assert_align_gt_eq {
/// `transmute_ref!` and `transmute_mut!`.
#[doc(hidden)] // `#[macro_export]` bypasses this module's `#[doc(hidden)]`.
#[macro_export]
macro_rules! assert_size_eq {
macro_rules! assert_type_size_eq {
($t:ident, $u: ident) => {{
// The comments here should be read in the context of this macro's
// invocations in `transmute_ref!` and `transmute_mut!`.
Expand All @@ -359,6 +359,19 @@ macro_rules! assert_size_eq {
}};
}

/// Panic if the expression evaluates to `false`.
#[doc(hidden)]
#[macro_export]
macro_rules! static_const_assert {
($x:expr $(,)?) => {
#[allow(unknown_lints, eq_op)]
const _: [(); 0 - !{
const ASSERT: bool = $x;
ASSERT
} as usize] = [];
};
}

/// Transmutes a reference of one type to a reference of another type.
///
/// # Safety
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-msrv/transmute-mut-dst-generic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ error[E0512]: cannot transmute between types of different sizes, or dependently-
|
= note: source type: `u8` (8 bits)
= note: target type: `T` (this type does not have a fixed size)
= note: this error originates in the macro `$crate::assert_size_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::assert_type_size_eq` (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-mut-dst-generic.rs:17:5
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-msrv/transmute-mut-dst-unsized.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ note: required by a bound in `transmute`
|
| pub fn transmute<T, U>(e: T) -> U;
| ^ required by this bound in `transmute`
= note: this error originates in the macro `$crate::assert_size_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::assert_type_size_eq` (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-mut-dst-unsized.rs:17:32
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-msrv/transmute-mut-size-decrease.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ error[E0512]: cannot transmute between types of different sizes, or dependently-
|
= note: source type: `[u8; 2]` (16 bits)
= note: target type: `u8` (8 bits)
= note: this error originates in the macro `$crate::assert_size_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::assert_type_size_eq` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0658]: mutable references are not allowed in constants
--> tests/ui-msrv/transmute-mut-size-decrease.rs:17:47
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-msrv/transmute-mut-size-increase.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ error[E0512]: cannot transmute between types of different sizes, or dependently-
|
= note: source type: `u8` (8 bits)
= note: target type: `[u8; 2]` (16 bits)
= note: this error originates in the macro `$crate::assert_size_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::assert_type_size_eq` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0658]: mutable references are not allowed in constants
--> tests/ui-msrv/transmute-mut-size-increase.rs:17:52
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-msrv/transmute-mut-src-dst-generic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ error[E0512]: cannot transmute between types of different sizes, or dependently-
|
= note: source type: `T` (this type does not have a fixed size)
= note: target type: `U` (this type does not have a fixed size)
= note: this error originates in the macro `$crate::assert_size_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::assert_type_size_eq` (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-mut-src-dst-generic.rs:20:5
Expand Down
4 changes: 2 additions & 2 deletions tests/ui-msrv/transmute-mut-src-dst-unsized.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ note: required by a bound in `transmute`
|
| pub fn transmute<T, U>(e: T) -> U;
| ^ required by this bound in `transmute`
= note: this error originates in the macro `$crate::assert_size_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::assert_type_size_eq` (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-mut-src-dst-unsized.rs:17:36
Expand Down Expand Up @@ -191,7 +191,7 @@ note: required by a bound in `transmute`
|
| pub fn transmute<T, U>(e: T) -> U;
| ^ required by this bound in `transmute`
= note: this error originates in the macro `$crate::assert_size_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::assert_type_size_eq` (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-mut-src-dst-unsized.rs:17:36
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-msrv/transmute-mut-src-generic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ error[E0512]: cannot transmute between types of different sizes, or dependently-
|
= note: source type: `T` (this type does not have a fixed size)
= note: target type: `u8` (8 bits)
= note: this error originates in the macro `$crate::assert_size_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::assert_type_size_eq` (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-mut-src-generic.rs:17:5
Expand Down
4 changes: 2 additions & 2 deletions tests/ui-msrv/transmute-mut-src-unsized.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ note: required by a bound in `transmute`
|
| pub fn transmute<T, U>(e: T) -> U;
| ^ required by this bound in `transmute`
= note: this error originates in the macro `$crate::assert_size_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::assert_type_size_eq` (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-mut-src-unsized.rs:16:35
Expand Down Expand Up @@ -176,4 +176,4 @@ error[E0277]: the size for values of type `[u8]` cannot be known at compilation
|
= help: the trait `Sized` is not implemented for `[u8]`
= note: all function arguments must have a statically known size
= note: this error originates in the macro `$crate::assert_size_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::assert_type_size_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
2 changes: 1 addition & 1 deletion tests/ui-msrv/transmute-ref-dst-generic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ error[E0512]: cannot transmute between types of different sizes, or dependently-
|
= note: source type: `u8` (8 bits)
= note: target type: `T` (this type does not have a fixed size)
= note: this error originates in the macro `$crate::assert_size_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::assert_type_size_eq` (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-ref-dst-generic.rs:17:5
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-msrv/transmute-ref-dst-unsized.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ note: required by a bound in `transmute`
|
| pub fn transmute<T, U>(e: T) -> U;
| ^ required by this bound in `transmute`
= note: this error originates in the macro `$crate::assert_size_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::assert_type_size_eq` (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-ref-dst-unsized.rs:17:28
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-msrv/transmute-ref-size-decrease.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ error[E0512]: cannot transmute between types of different sizes, or dependently-
|
= note: source type: `[u8; 2]` (16 bits)
= note: target type: `u8` (8 bits)
= note: this error originates in the macro `$crate::assert_size_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::assert_type_size_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
2 changes: 1 addition & 1 deletion tests/ui-msrv/transmute-ref-size-increase.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ error[E0512]: cannot transmute between types of different sizes, or dependently-
|
= note: source type: `u8` (8 bits)
= note: target type: `[u8; 2]` (16 bits)
= note: this error originates in the macro `$crate::assert_size_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::assert_type_size_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
2 changes: 1 addition & 1 deletion tests/ui-msrv/transmute-ref-src-dst-generic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ error[E0512]: cannot transmute between types of different sizes, or dependently-
|
= note: source type: `T` (this type does not have a fixed size)
= note: target type: `U` (this type does not have a fixed size)
= note: this error originates in the macro `$crate::assert_size_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::assert_type_size_eq` (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-ref-src-dst-generic.rs:18:5
Expand Down
4 changes: 2 additions & 2 deletions tests/ui-msrv/transmute-ref-src-dst-unsized.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ note: required by a bound in `transmute`
|
| pub fn transmute<T, U>(e: T) -> U;
| ^ required by this bound in `transmute`
= note: this error originates in the macro `$crate::assert_size_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::assert_type_size_eq` (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-ref-src-dst-unsized.rs:17:32
Expand Down Expand Up @@ -191,7 +191,7 @@ note: required by a bound in `transmute`
|
| pub fn transmute<T, U>(e: T) -> U;
| ^ required by this bound in `transmute`
= note: this error originates in the macro `$crate::assert_size_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::assert_type_size_eq` (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-ref-src-dst-unsized.rs:17:32
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-msrv/transmute-ref-src-generic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ error[E0512]: cannot transmute between types of different sizes, or dependently-
|
= note: source type: `T` (this type does not have a fixed size)
= note: target type: `u8` (8 bits)
= note: this error originates in the macro `$crate::assert_size_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::assert_type_size_eq` (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-ref-src-generic.rs:17:5
Expand Down
4 changes: 2 additions & 2 deletions tests/ui-msrv/transmute-ref-src-unsized.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ note: required by a bound in `transmute`
|
| pub fn transmute<T, U>(e: T) -> U;
| ^ required by this bound in `transmute`
= note: this error originates in the macro `$crate::assert_size_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::assert_type_size_eq` (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-ref-src-unsized.rs:16:31
Expand Down Expand Up @@ -176,4 +176,4 @@ error[E0277]: the size for values of type `[u8]` cannot be known at compilation
|
= help: the trait `Sized` is not implemented for `[u8]`
= note: all function arguments must have a statically known size
= note: this error originates in the macro `$crate::assert_size_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::assert_type_size_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
11 changes: 11 additions & 0 deletions tests/ui-nightly/inline_assert_size_eq_failed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extern crate zerocopy;
extern crate zerocopy_derive;
use zerocopy::*;
use zerocopy_derive::assert_size_eq;

#[assert_size_eq(1)]
struct TestSizeNeqStruct {
i: i32,
}

fn main() {}
7 changes: 7 additions & 0 deletions tests/ui-nightly/inline_assert_size_eq_failed.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error[E0080]: evaluation of constant value failed
--> tests/ui-nightly/inline_assert_size_eq_failed.rs:6:1
|
6 | #[assert_size_eq(1)]
| ^^^^^^^^^^^^^^^^^^^^ attempt to compute `0_usize - 1_usize`, which would overflow
|
= note: this error originates in the macro `static_const_assert` which comes from the expansion of the attribute macro `assert_size_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
2 changes: 1 addition & 1 deletion tests/ui-nightly/transmute-mut-dst-generic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ error[E0512]: cannot transmute between types of different sizes, or dependently-
|
= note: source type: `u8` (8 bits)
= note: target type: `T` (this type does not have a fixed size)
= note: this error originates in the macro `$crate::assert_size_eq` which comes from the expansion of the macro `transmute_mut` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::assert_type_size_eq` which comes from the expansion of the macro `transmute_mut` (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-mut-dst-generic.rs:17:5
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-nightly/transmute-mut-dst-unsized.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ note: required by an implicit `Sized` bound in `transmute`
|
| pub fn transmute<Src, Dst>(src: Src) -> Dst;
| ^^^ required by the implicit `Sized` requirement on this type parameter in `transmute`
= note: this error originates in the macro `$crate::assert_size_eq` which comes from the expansion of the macro `transmute_mut` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::assert_type_size_eq` which comes from the expansion of the macro `transmute_mut` (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-mut-dst-unsized.rs:17:32
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-nightly/transmute-mut-size-decrease.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ error[E0512]: cannot transmute between types of different sizes, or dependently-
|
= note: source type: `[u8; 2]` (16 bits)
= note: target type: `u8` (8 bits)
= note: this error originates in the macro `$crate::assert_size_eq` which comes from the expansion of the macro `transmute_mut` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::assert_type_size_eq` which comes from the expansion of the macro `transmute_mut` (in Nightly builds, run with -Z macro-backtrace for more info)
2 changes: 1 addition & 1 deletion tests/ui-nightly/transmute-mut-size-increase.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ error[E0512]: cannot transmute between types of different sizes, or dependently-
|
= note: source type: `u8` (8 bits)
= note: target type: `[u8; 2]` (16 bits)
= note: this error originates in the macro `$crate::assert_size_eq` which comes from the expansion of the macro `transmute_mut` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::assert_type_size_eq` which comes from the expansion of the macro `transmute_mut` (in Nightly builds, run with -Z macro-backtrace for more info)
2 changes: 1 addition & 1 deletion tests/ui-nightly/transmute-mut-src-dst-generic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ error[E0512]: cannot transmute between types of different sizes, or dependently-
|
= note: source type: `T` (this type does not have a fixed size)
= note: target type: `U` (this type does not have a fixed size)
= note: this error originates in the macro `$crate::assert_size_eq` which comes from the expansion of the macro `transmute_mut` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::assert_type_size_eq` which comes from the expansion of the macro `transmute_mut` (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-mut-src-dst-generic.rs:20:5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ warning: never type fallback affects this call to an `unsafe` function
= note: for more information, see issue #123748 <https://github.com/rust-lang/rust/issues/123748>
= help: specify the type explicitly
= note: `#[warn(never_type_fallback_flowing_into_unsafe)]` on by default
= note: this warning originates in the macro `$crate::assert_size_eq` which comes from the expansion of the macro `transmute_mut` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this warning originates in the macro `$crate::assert_type_size_eq` which comes from the expansion of the macro `transmute_mut` (in Nightly builds, run with -Z macro-backtrace for more info)
2 changes: 1 addition & 1 deletion tests/ui-nightly/transmute-mut-src-dst-unsized.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ note: required by an implicit `Sized` bound in `transmute`
|
| pub fn transmute<Src, Dst>(src: Src) -> Dst;
| ^^^ required by the implicit `Sized` requirement on this type parameter in `transmute`
= note: this error originates in the macro `$crate::assert_size_eq` which comes from the expansion of the macro `transmute_mut` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::assert_type_size_eq` which comes from the expansion of the macro `transmute_mut` (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-mut-src-dst-unsized.rs:17:36
Expand Down
Loading