| 1 |
// Copyright 2019 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 |
// Make sure that macro hygiene will ensure that when we reference "zerocopy", |
| 10 |
// that will work properly even if they've renamed the crate and have not |
| 11 |
// imported its traits. |
| 12 |
|
| 13 |
// See comment in `include.rs` for why we disable the prelude. |
| 14 |
#![no_implicit_prelude] |
| 15 |
#![allow(warnings)] |
| 16 |
|
| 17 |
include!("include.rs"); |
| 18 |
|
| 19 |
extern crate zerocopy_renamed as _zerocopy; |
| 20 |
|
| 21 |
extern crate self as derive_path_test; |
| 22 |
|
| 23 |
pub mod nested { |
| 24 |
pub extern crate zerocopy_renamed as reexported_zerocopy; |
| 25 |
} |
| 26 |
|
| 27 |
macro_rules! test { |
| 28 |
($($path:tt)::*, $crate_str:tt) => { |
| 29 |
#[derive( |
| 30 |
$($path)::*::KnownLayout, |
| 31 |
$($path)::*::FromBytes, |
| 32 |
$($path)::*::Unaligned, |
| 33 |
)] |
| 34 |
#[zerocopy(crate = $crate_str)] |
| 35 |
#[repr(C)] |
| 36 |
struct TypeParams<'a, T, I: imp::Iterator> { |
| 37 |
a: T, |
| 38 |
c: I::Item, |
| 39 |
d: u8, |
| 40 |
e: imp::PhantomData<&'a [::core::primitive::u8]>, |
| 41 |
f: imp::PhantomData<&'static ::core::primitive::str>, |
| 42 |
g: imp::PhantomData<imp::String>, |
| 43 |
} |
| 44 |
|
| 45 |
util_assert_impl_all!( |
| 46 |
TypeParams<'static, (), imp::IntoIter<()>>: |
| 47 |
$($path)::*::KnownLayout, |
| 48 |
$($path)::*::FromZeros, |
| 49 |
$($path)::*::FromBytes, |
| 50 |
$($path)::*::Unaligned |
| 51 |
); |
| 52 |
}; |
| 53 |
} |
| 54 |
|
| 55 |
test!(_zerocopy, "zerocopy_renamed"); |
| 56 |
|
| 57 |
mod nested_external { |
| 58 |
use super::*; |
| 59 |
|
| 60 |
test!( |
| 61 |
derive_path_test::nested::reexported_zerocopy, |
| 62 |
"derive_path_test::nested::reexported_zerocopy" |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
mod crate_relative { |
| 67 |
use super::*; |
| 68 |
|
| 69 |
test!(crate::nested::reexported_zerocopy, "crate::nested::reexported_zerocopy"); |
| 70 |
} |
| 71 |
|
| 72 |
mod super_relative { |
| 73 |
use super::*; |
| 74 |
|
| 75 |
test!(super::nested::reexported_zerocopy, "super::nested::reexported_zerocopy"); |
| 76 |
} |
| 77 |
|
| 78 |
mod self_super_relative { |
| 79 |
use super::*; |
| 80 |
|
| 81 |
test!(self::super::nested::reexported_zerocopy, "self::super::nested::reexported_zerocopy"); |
| 82 |
} |
| 83 |
|
| 84 |
mod super_super_relative { |
| 85 |
mod inner { |
| 86 |
use super::super::*; |
| 87 |
|
| 88 |
test!( |
| 89 |
super::super::nested::reexported_zerocopy, |
| 90 |
"super::super::nested::reexported_zerocopy" |
| 91 |
); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
// Regression test for #2177. |
| 96 |
// |
| 97 |
// This test ensures that `#[derive(KnownLayout)]` does not trigger the |
| 98 |
// `private_bounds` lint when used on a public struct in a macro. |
| 99 |
mod issue_2177 { |
| 100 |
#![deny(private_bounds)] |
| 101 |
// We need to access `_zerocopy` from the parent module. |
| 102 |
use super::_zerocopy; |
| 103 |
|
| 104 |
macro_rules! define { |
| 105 |
($name:ident, $repr:ty) => { |
| 106 |
#[derive(_zerocopy::KnownLayout)] |
| 107 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 108 |
#[repr(C)] |
| 109 |
pub struct $name($repr); |
| 110 |
}; |
| 111 |
} |
| 112 |
|
| 113 |
define!(Foo, u8); |
| 114 |
} |