summaryrefslogtreecommitdiffstats
path: root/src/test/ui/transmutability/enums/repr/primitive_reprs_should_have_correct_length.rs
blob: 940f070e7e3605b840a9285d6f261df864982cbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//! An enum with a primitive repr should have exactly the size of that primitive.

#![crate_type = "lib"]
#![feature(transmutability)]
#![allow(dead_code)]

mod assert {
    use std::mem::{Assume, BikeshedIntrinsicFrom};

    pub fn is_transmutable<Src, Dst, Context>()
    where
        Dst: BikeshedIntrinsicFrom<Src, Context, {
            Assume {
                alignment: true,
                lifetimes: true,
                safety: true,
                validity: true,
            }
        }>
    {}
}

#[repr(C)]
struct Zst;

#[derive(Clone, Copy)]
#[repr(i8)] enum V0i8 { V }
#[repr(u8)] enum V0u8 { V }
#[repr(i16)] enum V0i16 { V }
#[repr(u16)] enum V0u16 { V }
#[repr(i32)] enum V0i32 { V }
#[repr(u32)] enum V0u32 { V }
#[repr(i64)] enum V0i64 { V }
#[repr(u64)] enum V0u64 { V }
#[repr(isize)] enum V0isize { V }
#[repr(usize)] enum V0usize { V }

fn n8() {
    struct Context;

    type Smaller = Zst;
    type Analog = u8;
    type Larger = u16;

    fn i_should_have_correct_length() {
        type Current = V0i8;

        assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR cannot be safely transmuted
        assert::is_transmutable::<Current, Analog, Context>();
        assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR cannot be safely transmuted
    }

    fn u_should_have_correct_length() {
        type Current = V0u8;

        assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR cannot be safely transmuted
        assert::is_transmutable::<Current, Analog, Context>();
        assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR cannot be safely transmuted
    }
}

fn n16() {
    struct Context;

    type Smaller = u8;
    type Analog = u16;
    type Larger = u32;

    fn i_should_have_correct_length() {
        type Current = V0i16;

        assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR cannot be safely transmuted
        assert::is_transmutable::<Current, Analog, Context>();
        assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR cannot be safely transmuted
    }

    fn u_should_have_correct_length() {
        type Current = V0u16;

        assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR cannot be safely transmuted
        assert::is_transmutable::<Current, Analog, Context>();
        assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR cannot be safely transmuted
    }
}

fn n32() {
    struct Context;

    type Smaller = u16;
    type Analog = u32;
    type Larger = u64;

    fn i_should_have_correct_length() {
        type Current = V0i32;

        assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR cannot be safely transmuted
        assert::is_transmutable::<Current, Analog, Context>();
        assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR cannot be safely transmuted
    }

    fn u_should_have_correct_length() {
        type Current = V0u32;

        assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR cannot be safely transmuted
        assert::is_transmutable::<Current, Analog, Context>();
        assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR cannot be safely transmuted
    }
}

fn n64() {
    struct Context;

    type Smaller = u32;
    type Analog = u64;
    type Larger = u128;

    fn i_should_have_correct_length() {
        type Current = V0i64;

        assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR cannot be safely transmuted
        assert::is_transmutable::<Current, Analog, Context>();
        assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR cannot be safely transmuted
    }

    fn u_should_have_correct_length() {
        type Current = V0u64;

        assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR cannot be safely transmuted
        assert::is_transmutable::<Current, Analog, Context>();
        assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR cannot be safely transmuted
    }
}

fn nsize() {
    struct Context;

    type Smaller = u8;
    type Analog = usize;
    type Larger = [usize; 2];

    fn i_should_have_correct_length() {
        type Current = V0isize;

        assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR cannot be safely transmuted
        assert::is_transmutable::<Current, Analog, Context>();
        assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR cannot be safely transmuted
    }

    fn u_should_have_correct_length() {
        type Current = V0usize;

        assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR cannot be safely transmuted
        assert::is_transmutable::<Current, Analog, Context>();
        assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR cannot be safely transmuted
    }
}