From 6948538de026801459bcfa3c01944e83d8e260a0 Mon Sep 17 00:00:00 2001 From: Joshua Liebow-Feeser Date: Mon, 16 Oct 2023 23:48:36 +0000 Subject: [PATCH] [derive] Test with trivial_bounds feature The nightly `trivial_bounds` causes some derive-emitted code to compile which is designed to fail compilation. This commit adds a test that ensures that the emitted code is still sound even with `trivial_bounds` enabled. Affects #61 Closes #500 --- .../tests/ui-nightly/trivial_bounds.rs | 84 +++++++++++++++++++ .../tests/ui-nightly/trivial_bounds.stderr | 22 +++++ 2 files changed, 106 insertions(+) create mode 100644 zerocopy-derive/tests/ui-nightly/trivial_bounds.rs create mode 100644 zerocopy-derive/tests/ui-nightly/trivial_bounds.stderr diff --git a/zerocopy-derive/tests/ui-nightly/trivial_bounds.rs b/zerocopy-derive/tests/ui-nightly/trivial_bounds.rs new file mode 100644 index 0000000000..b34d0cc02e --- /dev/null +++ b/zerocopy-derive/tests/ui-nightly/trivial_bounds.rs @@ -0,0 +1,84 @@ +// Copyright 2023 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. + +#![feature(trivial_bounds)] + +extern crate zerocopy; + +#[path = "../util.rs"] +mod util; + +use static_assertions::assert_impl_all; +use zerocopy::{AsBytes, FromBytes, FromZeroes, Unaligned}; + +use self::util::{NotZerocopy, AU16}; + +fn main() {} + +// These tests are compiled with the `trivial_bounds` feature enabled. We emit +// code which is designed to fail compilation for types we wish to reject. E.g.: +// +// #[derive(FromZeroes)] +// struct Foo(NotZerocopy); +// +// It would be unsound to emit an impl of `FromZeroes for Foo` in this case. Our +// custom derives are designed to cause compilation to fail for this type. +// `trivial_bounds` instead allows this code to succeed by simply not emitting +// `impl FromZeroes for Foo`. This isn't a soundness issue, but is likely +// confusing to users. +// +// As of this writing, the code in this file only fails to compile thanks to the +// use of `assert_impl_all!`, which depends upon the incorrect trait bounds. +// +// TODO(https://github.com/rust-lang/rust/issues/48214): If support is ever +// added for `#[deny(trivially_false_bounds)]` as discussed in the +// `trivial_bounds` tracking issue, we should emit that annotation in our +// derives. That will result in the `#[derive(...)]` annotations in this file +// failing to compile, not just the `assert_impl_all!` calls. + +// +// FromZeroes errors +// + +#[derive(FromZeroes)] +struct FromZeroes1 { + value: NotZerocopy, +} + +assert_impl_all!(FromZeroes1: FromZeroes); + +// +// FromBytes errors +// + +#[derive(FromBytes)] +struct FromBytes1 { + value: NotZerocopy, +} + +assert_impl_all!(FromBytes1: FromBytes); + +// +// AsBytes errors +// + +#[derive(AsBytes)] +#[repr(C)] +struct AsBytes1 { + value: NotZerocopy, +} + +assert_impl_all!(AsBytes1: AsBytes); + +// +// Unaligned errors +// + +#[derive(Unaligned)] +#[repr(C)] +struct Unaligned1 { + aligned: AU16, +} + +assert_impl_all!(Unaligned1: Unaligned); diff --git a/zerocopy-derive/tests/ui-nightly/trivial_bounds.stderr b/zerocopy-derive/tests/ui-nightly/trivial_bounds.stderr new file mode 100644 index 0000000000..a35a1690b1 --- /dev/null +++ b/zerocopy-derive/tests/ui-nightly/trivial_bounds.stderr @@ -0,0 +1,22 @@ +error[E0277]: the trait bound `FromBytes1: FromZeroes` is not satisfied + --> tests/ui-nightly/trivial_bounds.rs:55:10 + | +55 | #[derive(FromBytes)] + | ^^^^^^^^^ the trait `FromZeroes` is not implemented for `FromBytes1` + | + = help: the following other types implement trait `FromZeroes`: + bool + char + isize + i8 + i16 + i32 + i64 + i128 + and $N others +note: required by a bound in `FromBytes` + --> $WORKSPACE/src/lib.rs + | + | pub unsafe trait FromBytes: FromZeroes { + | ^^^^^^^^^^ required by this bound in `FromBytes` + = note: this error originates in the derive macro `FromBytes` (in Nightly builds, run with -Z macro-backtrace for more info)