]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/const-eval/ub-enum.rs
bcb71af54afdb4f03225b147f169f8303795c685
[rust.git] / src / test / ui / consts / const-eval / ub-enum.rs
1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #[repr(usize)]
12 #[derive(Copy, Clone)]
13 enum Enum {
14     A = 0,
15 }
16 union TransmuteEnum {
17     a: &'static u8,
18     b: Enum,
19 }
20
21 // A pointer is guaranteed non-null
22 const BAD_ENUM: Enum = unsafe { TransmuteEnum { a: &1 }.b };
23 //~^ ERROR this constant likely exhibits undefined behavior
24
25 // Invalid enum discriminant
26 #[repr(usize)]
27 #[derive(Copy, Clone)]
28 enum Enum2 {
29     A = 2,
30 }
31 union TransmuteEnum2 {
32     a: usize,
33     b: Enum2,
34 }
35 const BAD_ENUM2 : Enum2 = unsafe { TransmuteEnum2 { a: 0 }.b };
36 //~^ ERROR this constant likely exhibits undefined behavior
37
38 // Invalid enum field content (mostly to test printing of apths for enum tuple
39 // variants and tuples).
40 union TransmuteChar {
41     a: u32,
42     b: char,
43 }
44 // Need to create something which does not clash with enum layout optimizations.
45 const BAD_ENUM_CHAR : Option<(char, char)> = Some(('x', unsafe { TransmuteChar { a: !0 }.b }));
46 //~^ ERROR this constant likely exhibits undefined behavior
47
48 fn main() {
49 }