]>
Commit | Line | Data |
---|---|---|
064997fb FG |
1 | // check-pass |
2 | //! Unless visibility is assumed, a transmutation should be rejected if the | |
3 | //! destination type contains an unreachable field (e.g., a public field with a | |
4 | //! private type). (This rule is distinct from type privacy, which still may | |
5 | //! forbid naming such types.) | |
6 | //! | |
7 | //! This test exercises a tricky-to-implement instance of this principle: the | |
8 | //! "pub-in-priv trick". In the below example, the type `dst::private::Zst` is | |
9 | //! unreachable from `Context`. | |
10 | ||
11 | #![crate_type = "lib"] | |
12 | #![feature(transmutability)] | |
13 | #![allow(dead_code)] | |
14 | ||
15 | mod assert { | |
f2b60f7d | 16 | use std::mem::{Assume, BikeshedIntrinsicFrom}; |
064997fb FG |
17 | |
18 | pub fn is_transmutable<Src, Dst, Context>() | |
19 | where | |
f2b60f7d FG |
20 | Dst: BikeshedIntrinsicFrom<Src, Context, { Assume::SAFETY }> |
21 | // safety IS assumed --------------------^^^^^^^^^^^^^^^^^^ | |
064997fb FG |
22 | {} |
23 | } | |
24 | ||
25 | mod src { | |
26 | #[repr(C)] pub(in super) struct Zst; | |
27 | ||
28 | #[repr(C)] pub(in super) struct Src { | |
29 | pub(in super) field: Zst, | |
30 | } | |
31 | } | |
32 | ||
33 | mod dst { | |
34 | mod private { | |
35 | #[repr(C)] pub struct Zst; // <- unreachable type | |
36 | } | |
37 | ||
38 | #[repr(C)] pub(in super) struct Dst { | |
39 | pub(in super) field: private::Zst, | |
40 | } | |
41 | } | |
42 | ||
43 | fn test() { | |
44 | struct Context; | |
45 | assert::is_transmutable::<src::Src, dst::Dst, Context>(); | |
46 | } |