Skip to content

Commit

Permalink
[tests] add transmute! UI tests (#456)
Browse files Browse the repository at this point in the history
These tests are split into distinct files, so that changes to
`transmute!` can be more easily carefully and incrementally
reviewed.
  • Loading branch information
jswrenn committed Oct 4, 2023
1 parent 1ed57ef commit 0a84008
Show file tree
Hide file tree
Showing 36 changed files with 293 additions and 46 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ jobs:
--package ${{ matrix.crate }} \
--target ${{ matrix.target }} \
${{ matrix.features }} \
--verbose
--verbose \
-- \
${{ (matrix.features == '--no-default-features' || matrix.features == '') && '--skip ui' || '' }} \
# Only run tests when targetting x86 (32- or 64-bit) - we're executing on
# x86_64, so we can't run tests for any non-x86 target.
#
Expand Down
1 change: 1 addition & 0 deletions tests/ui-msrv/transmute-dst-not-frombytes.rs
12 changes: 12 additions & 0 deletions tests/ui-msrv/transmute-dst-not-frombytes.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0277]: the trait bound `NotZerocopy: FromBytes` is not satisfied
--> tests/ui-msrv/transmute-dst-not-frombytes.rs:14:41
|
14 | const DST_NOT_FROM_BYTES: NotZerocopy = transmute!(AU16(0));
| ^^^^^^^^^^^^^^^^^^^ the trait `FromBytes` is not implemented for `NotZerocopy`
|
note: required by a bound in `DST_NOT_FROM_BYTES::transmute`
--> tests/ui-msrv/transmute-dst-not-frombytes.rs:14:41
|
14 | const DST_NOT_FROM_BYTES: NotZerocopy = transmute!(AU16(0));
| ^^^^^^^^^^^^^^^^^^^ 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)
1 change: 0 additions & 1 deletion tests/ui-msrv/transmute-illegal.rs

This file was deleted.

18 changes: 0 additions & 18 deletions tests/ui-msrv/transmute-illegal.stderr

This file was deleted.

1 change: 1 addition & 0 deletions tests/ui-msrv/transmute-ptr-to-usize.rs
18 changes: 18 additions & 0 deletions tests/ui-msrv/transmute-ptr-to-usize.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error[E0277]: the trait bound `*const usize: AsBytes` is not satisfied
--> tests/ui-msrv/transmute-ptr-to-usize.rs:16:30
|
16 | const POINTER_VALUE: usize = transmute!(&0usize as *const usize);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `AsBytes` is not implemented for `*const usize`
|
= help: the following implementations were found:
<usize as AsBytes>
<f32 as AsBytes>
<f64 as AsBytes>
<i128 as AsBytes>
and $N others
note: required by a bound in `POINTER_VALUE::transmute`
--> tests/ui-msrv/transmute-ptr-to-usize.rs:16:30
|
16 | 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)
1 change: 1 addition & 0 deletions tests/ui-msrv/transmute-size-decrease.rs
9 changes: 9 additions & 0 deletions tests/ui-msrv/transmute-size-decrease.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
--> tests/ui-msrv/transmute-size-decrease.rs:15:27
|
15 | const DECREASE_SIZE: u8 = transmute!(AU16(0));
| ^^^^^^^^^^^^^^^^^^^
|
= note: source type: `AU16` (16 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)
1 change: 1 addition & 0 deletions tests/ui-msrv/transmute-size-increase.rs
9 changes: 9 additions & 0 deletions tests/ui-msrv/transmute-size-increase.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
--> tests/ui-msrv/transmute-size-increase.rs:15:29
|
15 | const INCREASE_SIZE: AU16 = transmute!(0u8);
| ^^^^^^^^^^^^^^^
|
= note: source type: `u8` (8 bits)
= note: target type: `AU16` (16 bits)
= note: this error originates in the macro `transmute` (in Nightly builds, run with -Z macro-backtrace for more info)
1 change: 1 addition & 0 deletions tests/ui-msrv/transmute-src-not-asbytes.rs
12 changes: 12 additions & 0 deletions tests/ui-msrv/transmute-src-not-asbytes.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0277]: the trait bound `NotZerocopy<AU16>: AsBytes` is not satisfied
--> tests/ui-msrv/transmute-src-not-asbytes.rs:14:32
|
14 | const SRC_NOT_AS_BYTES: AU16 = transmute!(NotZerocopy(AU16(0)));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `AsBytes` is not implemented for `NotZerocopy<AU16>`
|
note: required by a bound in `SRC_NOT_AS_BYTES::transmute`
--> tests/ui-msrv/transmute-src-not-asbytes.rs:14:32
|
14 | const SRC_NOT_AS_BYTES: AU16 = transmute!(NotZerocopy(AU16(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)
14 changes: 14 additions & 0 deletions tests/ui-nightly/transmute-dst-not-frombytes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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.

include!("../../zerocopy-derive/tests/util.rs");

extern crate zerocopy;

use zerocopy::transmute;

fn main() {}

// `transmute` requires that the destination type implements `FromBytes`
const DST_NOT_FROM_BYTES: NotZerocopy = transmute!(AU16(0));
22 changes: 22 additions & 0 deletions tests/ui-nightly/transmute-dst-not-frombytes.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error[E0277]: the trait bound `NotZerocopy: FromBytes` is not satisfied
--> tests/ui-nightly/transmute-dst-not-frombytes.rs:14:41
|
14 | const DST_NOT_FROM_BYTES: NotZerocopy = transmute!(AU16(0));
| ^^^^^^^^^^^^^^^^^^^ the trait `FromBytes` is not implemented for `NotZerocopy`
|
= help: the following other types implement trait `FromBytes`:
()
AU16
F32<O>
F64<O>
I128<O>
I16<O>
I32<O>
I64<O>
and $N others
note: required by a bound in `DST_NOT_FROM_BYTES::transmute`
--> tests/ui-nightly/transmute-dst-not-frombytes.rs:14:41
|
14 | const DST_NOT_FROM_BYTES: NotZerocopy = transmute!(AU16(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)
10 changes: 0 additions & 10 deletions tests/ui-nightly/transmute-illegal.rs

This file was deleted.

16 changes: 16 additions & 0 deletions tests/ui-nightly/transmute-ptr-to-usize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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;

use zerocopy::transmute;

fn main() {}

// It is unclear whether we can or should support this transmutation, especially
// in a const context. This test ensures that even if such a transmutation
// becomes valid due to the requisite implementations of `FromBytes` being
// added, that we re-examine whether it should specifically be valid in a const
// context.
const POINTER_VALUE: usize = transmute!(&0usize as *const usize);
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
error[E0277]: the trait bound `*const usize: AsBytes` is not satisfied
--> tests/ui-stable/transmute-illegal.rs:10:30
--> tests/ui-nightly/transmute-ptr-to-usize.rs:16:30
|
10 | const POINTER_VALUE: usize = zerocopy::transmute!(&0usize as *const usize);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16 | 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-nightly/transmute-ptr-to-usize.rs:16: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)
16 | 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)
15 changes: 15 additions & 0 deletions tests/ui-nightly/transmute-size-decrease.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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.

include!("../../zerocopy-derive/tests/util.rs");

extern crate zerocopy;

use zerocopy::transmute;

fn main() {}

// Although this is not a soundness requirement, we currently require that the
// size of the destination type is not smaller than the size of the source type.
const DECREASE_SIZE: u8 = transmute!(AU16(0));
9 changes: 9 additions & 0 deletions tests/ui-nightly/transmute-size-decrease.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
--> tests/ui-nightly/transmute-size-decrease.rs:15:27
|
15 | const DECREASE_SIZE: u8 = transmute!(AU16(0));
| ^^^^^^^^^^^^^^^^^^^
|
= note: source type: `AU16` (16 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)
15 changes: 15 additions & 0 deletions tests/ui-nightly/transmute-size-increase.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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.

include!("../../zerocopy-derive/tests/util.rs");

extern crate zerocopy;

use zerocopy::transmute;

fn main() {}

// Although this is not a soundness requirement, we currently require that the
// size of the destination type is not larger than the size of the source type.
const INCREASE_SIZE: AU16 = transmute!(0u8);
9 changes: 9 additions & 0 deletions tests/ui-nightly/transmute-size-increase.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
--> tests/ui-nightly/transmute-size-increase.rs:15:29
|
15 | const INCREASE_SIZE: AU16 = transmute!(0u8);
| ^^^^^^^^^^^^^^^
|
= note: source type: `u8` (8 bits)
= note: target type: `AU16` (16 bits)
= note: this error originates in the macro `transmute` (in Nightly builds, run with -Z macro-backtrace for more info)
14 changes: 14 additions & 0 deletions tests/ui-nightly/transmute-src-not-asbytes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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.

include!("../../zerocopy-derive/tests/util.rs");

extern crate zerocopy;

use zerocopy::transmute;

fn main() {}

// `transmute` requires that the source type implements `AsBytes`
const SRC_NOT_AS_BYTES: AU16 = transmute!(NotZerocopy(AU16(0)));
25 changes: 25 additions & 0 deletions tests/ui-nightly/transmute-src-not-asbytes.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
error[E0277]: the trait bound `NotZerocopy<AU16>: AsBytes` is not satisfied
--> tests/ui-nightly/transmute-src-not-asbytes.rs:14:32
|
14 | const SRC_NOT_AS_BYTES: AU16 = transmute!(NotZerocopy(AU16(0)));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| the trait `AsBytes` is not implemented for `NotZerocopy<AU16>`
| required by a bound introduced by this call
|
= help: the following other types implement trait `AsBytes`:
()
AU16
F32<O>
F64<O>
I128<O>
I16<O>
I32<O>
I64<O>
and $N others
note: required by a bound in `SRC_NOT_AS_BYTES::transmute`
--> tests/ui-nightly/transmute-src-not-asbytes.rs:14:32
|
14 | const SRC_NOT_AS_BYTES: AU16 = transmute!(NotZerocopy(AU16(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)
1 change: 1 addition & 0 deletions tests/ui-stable/transmute-dst-not-frombytes.rs
22 changes: 22 additions & 0 deletions tests/ui-stable/transmute-dst-not-frombytes.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error[E0277]: the trait bound `NotZerocopy: FromBytes` is not satisfied
--> tests/ui-stable/transmute-dst-not-frombytes.rs:14:41
|
14 | const DST_NOT_FROM_BYTES: NotZerocopy = transmute!(AU16(0));
| ^^^^^^^^^^^^^^^^^^^ the trait `FromBytes` is not implemented for `NotZerocopy`
|
= help: the following other types implement trait `FromBytes`:
()
AU16
F32<O>
F64<O>
I128<O>
I16<O>
I32<O>
I64<O>
and $N others
note: required by a bound in `DST_NOT_FROM_BYTES::transmute`
--> tests/ui-stable/transmute-dst-not-frombytes.rs:14:41
|
14 | const DST_NOT_FROM_BYTES: NotZerocopy = transmute!(AU16(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)
1 change: 0 additions & 1 deletion tests/ui-stable/transmute-illegal.rs

This file was deleted.

1 change: 1 addition & 0 deletions tests/ui-stable/transmute-ptr-to-usize.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
error[E0277]: the trait bound `*const usize: AsBytes` is not satisfied
--> tests/ui-nightly/transmute-illegal.rs:10:30
--> tests/ui-stable/transmute-ptr-to-usize.rs:16:30
|
10 | const POINTER_VALUE: usize = zerocopy::transmute!(&0usize as *const usize);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16 | 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-stable/transmute-ptr-to-usize.rs:16: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)
16 | 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)
1 change: 1 addition & 0 deletions tests/ui-stable/transmute-size-decrease.rs
9 changes: 9 additions & 0 deletions tests/ui-stable/transmute-size-decrease.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
--> tests/ui-stable/transmute-size-decrease.rs:15:27
|
15 | const DECREASE_SIZE: u8 = transmute!(AU16(0));
| ^^^^^^^^^^^^^^^^^^^
|
= note: source type: `AU16` (16 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)
1 change: 1 addition & 0 deletions tests/ui-stable/transmute-size-increase.rs
9 changes: 9 additions & 0 deletions tests/ui-stable/transmute-size-increase.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
--> tests/ui-stable/transmute-size-increase.rs:15:29
|
15 | const INCREASE_SIZE: AU16 = transmute!(0u8);
| ^^^^^^^^^^^^^^^
|
= note: source type: `u8` (8 bits)
= note: target type: `AU16` (16 bits)
= note: this error originates in the macro `transmute` (in Nightly builds, run with -Z macro-backtrace for more info)
1 change: 1 addition & 0 deletions tests/ui-stable/transmute-src-not-asbytes.rs
Loading

0 comments on commit 0a84008

Please sign in to comment.