| 1 |
// Copyright 2022 The Fuchsia Authors |
| 2 |
// |
| 3 |
// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 |
| 4 |
// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT |
| 5 |
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. |
| 6 |
// This file may not be copied, modified, or distributed except according to |
| 7 |
// those terms. |
| 8 |
|
| 9 |
include!("../include.rs"); |
| 10 |
|
| 11 |
use util::NotZerocopy; |
| 12 |
use zerocopy::{FromBytes, FromZeros, Immutable, IntoBytes, KnownLayout, TryFromBytes, Unaligned}; |
| 13 |
|
| 14 |
fn main() { |
| 15 |
// We expect the proper diagnostic to be emitted on Rust 1.78.0 and later. |
| 16 |
takes_from_bytes::<NotZerocopy>(); |
| 17 |
//~[msrv, stable, nightly]^ ERROR: the trait bound `NotZerocopy: FromBytes` is not satisfied |
| 18 |
takes_from_zeros::<NotZerocopy>(); |
| 19 |
//~[msrv, stable, nightly]^ ERROR: the trait bound `NotZerocopy: FromZeros` is not satisfied |
| 20 |
takes_immutable::<NotZerocopy>(); |
| 21 |
//~[msrv, stable, nightly]^ ERROR: the trait bound `NotZerocopy: Immutable` is not satisfied |
| 22 |
takes_into_bytes::<NotZerocopy>(); |
| 23 |
//~[msrv, stable, nightly]^ ERROR: the trait bound `NotZerocopy: IntoBytes` is not satisfied |
| 24 |
takes_known_layout::<NotZerocopy>(); |
| 25 |
//~[msrv, stable, nightly]^ ERROR: the trait bound `NotZerocopy: KnownLayout` is not satisfied |
| 26 |
takes_try_from_bytes::<NotZerocopy>(); |
| 27 |
//~[msrv, stable, nightly]^ ERROR: the trait bound `NotZerocopy: TryFromBytes` is not satisfied |
| 28 |
takes_unaligned::<NotZerocopy>(); |
| 29 |
//~[msrv, stable, nightly]^ ERROR: the trait bound `NotZerocopy: zerocopy::Unaligned` is not satisfied |
| 30 |
|
| 31 |
// This is adapted from #1296, which includes the following text: |
| 32 |
// |
| 33 |
// The compiler errors when a type is missing Immutable are somewhat |
| 34 |
// misleading, although I'm not sure there's much zerocopy can do about |
| 35 |
// this. An example where the compiler recommends adding a reference |
| 36 |
// rather than implementing Immutable (some were even more confusing than |
| 37 |
// this): |
| 38 |
// |
| 39 |
// error[E0277]: the trait bound `virtio::wl::CtrlVfdNewDmabuf: zerocopy::Immutable` is not satisfied |
| 40 |
// --> devices/src/virtio/wl.rs:317:20 |
| 41 |
// | |
| 42 |
// 317 | .write_obj(ctrl_vfd_new_dmabuf) |
| 43 |
// | --------- ^^^^^^^^^^^^^^^^^^^ the trait `zerocopy::Immutable` is not implemented for `virtio::wl::CtrlVfdNewDmabuf` |
| 44 |
// | | |
| 45 |
// | required by a bound introduced by this call |
| 46 |
// | |
| 47 |
// note: required by a bound in `virtio::descriptor_utils::Writer::write_obj` |
| 48 |
// --> devices/src/virtio/descriptor_utils.rs:536:25 |
| 49 |
// | |
| 50 |
// 536 | pub fn write_obj<T: Immutable + IntoBytes>(&mut self, val: T) -> io::Result<()> { |
| 51 |
// | ^^^^^^^^^ required by this bound in `Writer::write_obj` |
| 52 |
// help: consider borrowing here |
| 53 |
// | |
| 54 |
// 317 | .write_obj(&ctrl_vfd_new_dmabuf) |
| 55 |
// | + |
| 56 |
// 317 | .write_obj(&mut ctrl_vfd_new_dmabuf) |
| 57 |
// | ++++ |
| 58 |
// |
| 59 |
// Taking the compiler's suggestion results in a different error with a |
| 60 |
// recommendation to remove the reference (back to the original code). |
| 61 |
// |
| 62 |
// As of this writing, the described problem is still happening thanks to |
| 63 |
// https://github.com/rust-lang/rust/issues/130563. We include this test so |
| 64 |
// that we can capture the current behavior, but we will update it once that |
| 65 |
// Rust issue is fixed. |
| 66 |
Foo.write_obj(NotZerocopy(())); |
| 67 |
//~[msrv, stable, nightly]^ ERROR: the trait bound `NotZerocopy: Immutable` is not satisfied |
| 68 |
//~[msrv, stable, nightly]^^ ERROR: the trait bound `NotZerocopy: IntoBytes` is not satisfied |
| 69 |
|
| 70 |
struct Foo; |
| 71 |
|
| 72 |
impl Foo { |
| 73 |
fn write_obj<T: Immutable + IntoBytes>(&mut self, _val: T) {} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
fn takes_from_bytes<T: FromBytes>() {} |
| 78 |
fn takes_from_zeros<T: FromZeros>() {} |
| 79 |
fn takes_immutable<T: Immutable>() {} |
| 80 |
fn takes_into_bytes<T: IntoBytes>() {} |
| 81 |
fn takes_known_layout<T: KnownLayout>() {} |
| 82 |
fn takes_try_from_bytes<T: TryFromBytes>() {} |
| 83 |
fn takes_unaligned<T: Unaligned>() {} |