Skip to content

Commit 6948538

Browse files
committed
[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
1 parent ed986dc commit 6948538

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0277]: the trait bound `FromBytes1: FromZeroes` is not satisfied
2+
--> tests/ui-nightly/trivial_bounds.rs:55:10
3+
|
4+
55 | #[derive(FromBytes)]
5+
| ^^^^^^^^^ the trait `FromZeroes` is not implemented for `FromBytes1`
6+
|
7+
= help: the following other types implement trait `FromZeroes`:
8+
bool
9+
char
10+
isize
11+
i8
12+
i16
13+
i32
14+
i64
15+
i128
16+
and $N others
17+
note: required by a bound in `FromBytes`
18+
--> $WORKSPACE/src/lib.rs
19+
|
20+
| pub unsafe trait FromBytes: FromZeroes {
21+
| ^^^^^^^^^^ required by this bound in `FromBytes`
22+
= note: this error originates in the derive macro `FromBytes` (in Nightly builds, run with -Z macro-backtrace for more info)

0 commit comments

Comments
 (0)