2018-08-18 13:46:52 +02:00
|
|
|
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2018-11-01 14:14:51 +01:00
|
|
|
#![allow(const_err)] // make sure we cannot allow away the errors tested here
|
|
|
|
|
2018-08-18 13:46:52 +02:00
|
|
|
#[repr(usize)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
enum Enum {
|
|
|
|
A = 0,
|
|
|
|
}
|
|
|
|
union TransmuteEnum {
|
|
|
|
a: &'static u8,
|
2018-11-03 12:44:10 +01:00
|
|
|
out: Enum,
|
2018-08-18 13:46:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// A pointer is guaranteed non-null
|
2018-11-03 12:44:10 +01:00
|
|
|
const BAD_ENUM: Enum = unsafe { TransmuteEnum { a: &1 }.out };
|
2018-08-26 15:19:34 +02:00
|
|
|
//~^ ERROR is undefined behavior
|
2018-08-18 13:46:52 +02:00
|
|
|
|
2018-11-03 12:44:10 +01:00
|
|
|
// (Potentially) invalid enum discriminant
|
2018-08-18 13:46:52 +02:00
|
|
|
#[repr(usize)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
enum Enum2 {
|
|
|
|
A = 2,
|
|
|
|
}
|
2018-11-03 12:44:10 +01:00
|
|
|
#[repr(transparent)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct Wrap<T>(T);
|
2018-08-18 13:46:52 +02:00
|
|
|
union TransmuteEnum2 {
|
2018-11-03 12:44:10 +01:00
|
|
|
in1: usize,
|
|
|
|
in2: &'static u8,
|
|
|
|
in3: (),
|
|
|
|
out1: Enum2,
|
|
|
|
out2: Wrap<Enum2>, // something wrapping the enum so that we test layout first, not enum
|
2018-08-18 13:46:52 +02:00
|
|
|
}
|
2018-11-03 12:44:10 +01:00
|
|
|
const BAD_ENUM2: Enum2 = unsafe { TransmuteEnum2 { in1: 0 }.out1 };
|
|
|
|
//~^ ERROR is undefined behavior
|
|
|
|
const BAD_ENUM3: Enum2 = unsafe { TransmuteEnum2 { in2: &0 }.out1 };
|
|
|
|
//~^ ERROR is undefined behavior
|
|
|
|
const BAD_ENUM4: Wrap<Enum2> = unsafe { TransmuteEnum2 { in2: &0 }.out2 };
|
2018-08-26 15:19:34 +02:00
|
|
|
//~^ ERROR is undefined behavior
|
2018-08-18 13:46:52 +02:00
|
|
|
|
2018-11-01 14:41:36 +01:00
|
|
|
// Undef enum discriminant. In an arry to avoid `Scalar` layout.
|
2018-11-03 12:44:10 +01:00
|
|
|
const BAD_ENUM_UNDEF: [Enum2; 2] = [unsafe { TransmuteEnum2 { in3: () }.out1 }; 2];
|
2018-11-01 14:41:36 +01:00
|
|
|
//~^ ERROR is undefined behavior
|
|
|
|
|
2018-11-03 12:44:10 +01:00
|
|
|
// Invalid enum field content (mostly to test printing of paths for enum tuple
|
2018-08-18 13:46:52 +02:00
|
|
|
// variants and tuples).
|
|
|
|
union TransmuteChar {
|
|
|
|
a: u32,
|
|
|
|
b: char,
|
|
|
|
}
|
|
|
|
// Need to create something which does not clash with enum layout optimizations.
|
2018-11-03 12:44:10 +01:00
|
|
|
const BAD_ENUM_CHAR: Option<(char, char)> = Some(('x', unsafe { TransmuteChar { a: !0 }.b }));
|
2018-08-26 15:19:34 +02:00
|
|
|
//~^ ERROR is undefined behavior
|
2018-08-18 13:46:52 +02:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
}
|