about summary refs log tree commit diff
path: root/tests/ui/transmutability/char.rs
blob: 55a615373295539924884fcff349aa43b7fc653c (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
#![feature(never_type)]
#![feature(transmutability)]

use std::mem::{Assume, TransmuteFrom};

pub fn is_transmutable<Src, Dst>()
where
    Dst: TransmuteFrom<Src, { Assume::SAFETY }>,
{
}

fn main() {
    is_transmutable::<char, u32>();

    // `char`s can be in the following ranges:
    // - [0, 0xD7FF]
    // - [0xE000, 10FFFF]
    //
    // `Char` has variants whose tags are in the top and bottom of each range.
    // It also has generic variants which are out of bounds of these ranges, but
    // are generic on types which may be set to `!` to "disable" them in the
    // transmutability analysis.
    #[repr(u32)]
    enum Char<B, C, D> {
        A = 0,
        B = 0xD7FF,
        OverB(B) = 0xD800,
        UnderC(C) = 0xDFFF,
        C = 0xE000,
        D = 0x10FFFF,
        OverD(D) = 0x110000,
    }

    is_transmutable::<Char<!, !, !>, char>();
    is_transmutable::<Char<(), !, !>, char>();
    //~^ ERROR cannot be safely transmuted into `char`
    is_transmutable::<Char<!, (), !>, char>();
    //~^ ERROR cannot be safely transmuted into `char`
    is_transmutable::<Char<!, !, ()>, char>();
    //~^ ERROR cannot be safely transmuted into `char`
}