]> git.proxmox.com Git - rustc.git/blame - src/test/ui/transmutability/visibility/assume/should_accept_if_dst_has_tricky_unreachable_field.rs
New upstream version 1.65.0+dfsg1
[rustc.git] / src / test / ui / transmutability / visibility / assume / should_accept_if_dst_has_tricky_unreachable_field.rs
CommitLineData
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
15mod 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
25mod 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
33mod 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
43fn test() {
44 struct Context;
45 assert::is_transmutable::<src::Src, dst::Dst, Context>();
46}