|
| 1 | +// Copyright 2023 The Fuchsia Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#![feature(trivial_bounds)] |
| 6 | + |
| 7 | +extern crate zerocopy; |
| 8 | + |
| 9 | +#[path = "../util.rs"] |
| 10 | +mod util; |
| 11 | + |
| 12 | +use static_assertions::assert_impl_all; |
| 13 | +use zerocopy::{AsBytes, FromBytes, FromZeroes, Unaligned}; |
| 14 | + |
| 15 | +use self::util::{NotZerocopy, AU16}; |
| 16 | + |
| 17 | +fn main() {} |
| 18 | + |
| 19 | +// These tests are compiled with the `trivial_bounds` feature enabled. We emit |
| 20 | +// code which is designed to fail compilation for types we wish to reject. E.g.: |
| 21 | +// |
| 22 | +// #[derive(FromZeroes)] |
| 23 | +// struct Foo(NotZerocopy); |
| 24 | +// |
| 25 | +// It would be unsound to emit an impl of `FromZeroes for Foo` in this case. Our |
| 26 | +// custom derives are designed to cause compilation to fail for this type. |
| 27 | +// `trivial_bounds` instead allows this code to succeed by simply not emitting |
| 28 | +// `impl FromZeroes for Foo`. This isn't a soundness issue, but is likely |
| 29 | +// confusing to users. |
| 30 | +// |
| 31 | +// As of this writing, the code in this file only fails to compile thanks to the |
| 32 | +// use of `assert_impl_all!`, which depends upon the incorrect trait bounds. |
| 33 | +// |
| 34 | +// TODO(https://github.com/rust-lang/rust/issues/48214): If support is ever |
| 35 | +// added for `#[deny(trivially_false_bounds)]` as discussed in the |
| 36 | +// `trivial_bounds` tracking issue, we should emit that annotation in our |
| 37 | +// derives. That will result in the `#[derive(...)]` annotations in this file |
| 38 | +// failing to compile, not just the `assert_impl_all!` calls. |
| 39 | + |
| 40 | +// |
| 41 | +// FromZeroes errors |
| 42 | +// |
| 43 | + |
| 44 | +#[derive(FromZeroes)] |
| 45 | +struct FromZeroes1 { |
| 46 | + value: NotZerocopy, |
| 47 | +} |
| 48 | + |
| 49 | +assert_impl_all!(FromZeroes1: FromZeroes); |
| 50 | + |
| 51 | +// |
| 52 | +// FromBytes errors |
| 53 | +// |
| 54 | + |
| 55 | +#[derive(FromBytes)] |
| 56 | +struct FromBytes1 { |
| 57 | + value: NotZerocopy, |
| 58 | +} |
| 59 | + |
| 60 | +assert_impl_all!(FromBytes1: FromBytes); |
| 61 | + |
| 62 | +// |
| 63 | +// AsBytes errors |
| 64 | +// |
| 65 | + |
| 66 | +#[derive(AsBytes)] |
| 67 | +#[repr(C)] |
| 68 | +struct AsBytes1 { |
| 69 | + value: NotZerocopy, |
| 70 | +} |
| 71 | + |
| 72 | +assert_impl_all!(AsBytes1: AsBytes); |
| 73 | + |
| 74 | +// |
| 75 | +// Unaligned errors |
| 76 | +// |
| 77 | + |
| 78 | +#[derive(Unaligned)] |
| 79 | +#[repr(C)] |
| 80 | +struct Unaligned1 { |
| 81 | + aligned: AU16, |
| 82 | +} |
| 83 | + |
| 84 | +assert_impl_all!(Unaligned1: Unaligned); |
0 commit comments