From de9bb9738d8fcd95222fc0c9383953881e31d4d5 Mon Sep 17 00:00:00 2001 From: Jed Davis Date: Sun, 29 Sep 2013 14:00:34 -0700 Subject: [PATCH] Add tests for enum discriminant sizing. --- .../compile-fail/enum-discrim-too-small.rs | 58 +++++++++++++ src/test/compile-fail/lint-ctypes-enum.rs | 25 ++++++ src/test/run-pass/enum-discrim-autosizing.rs | 62 ++++++++++++++ .../run-pass/enum-discrim-manual-sizing.rs | 84 +++++++++++++++++++ src/test/run-pass/enum-discrim-width-stuff.rs | 49 +++++++++-- 5 files changed, 271 insertions(+), 7 deletions(-) create mode 100644 src/test/compile-fail/enum-discrim-too-small.rs create mode 100644 src/test/compile-fail/lint-ctypes-enum.rs create mode 100644 src/test/run-pass/enum-discrim-autosizing.rs create mode 100644 src/test/run-pass/enum-discrim-manual-sizing.rs diff --git a/src/test/compile-fail/enum-discrim-too-small.rs b/src/test/compile-fail/enum-discrim-too-small.rs new file mode 100644 index 00000000000..2de50ad1d1d --- /dev/null +++ b/src/test/compile-fail/enum-discrim-too-small.rs @@ -0,0 +1,58 @@ +// Copyright 2013 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[repr(u8)] //~ NOTE discriminant type specified here +enum Eu8 { + Au8 = 23, + Bu8 = 223, + Cu8 = -23, //~ ERROR discriminant value outside specified type +} + +#[repr(i8)] //~ NOTE discriminant type specified here +enum Ei8 { + Ai8 = 23, + Bi8 = -23, + Ci8 = 223, //~ ERROR discriminant value outside specified type +} + +#[repr(u16)] //~ NOTE discriminant type specified here +enum Eu16 { + Au16 = 23, + Bu16 = 55555, + Cu16 = -22333, //~ ERROR discriminant value outside specified type +} + +#[repr(i16)] //~ NOTE discriminant type specified here +enum Ei16 { + Ai16 = 23, + Bi16 = -22333, + Ci16 = 55555, //~ ERROR discriminant value outside specified type +} + +#[repr(u32)] //~ NOTE discriminant type specified here +enum Eu32 { + Au32 = 23, + Bu32 = 3_000_000_000, + Cu32 = -2_000_000_000, //~ ERROR discriminant value outside specified type +} + +#[repr(i32)] //~ NOTE discriminant type specified here +enum Ei32 { + Ai32 = 23, + Bi32 = -2_000_000_000, + Ci32 = 3_000_000_000, //~ ERROR discriminant value outside specified type +} + +// u64 currently allows negative numbers, and i64 allows numbers greater than `1<<63`. This is a +// little counterintuitive, but since the discriminant can store all the bits, and extracting it +// with a cast requires specifying the signedness, there is no loss of information in those cases. +// This also applies to int and uint on 64-bit targets. + +pub fn main() { } diff --git a/src/test/compile-fail/lint-ctypes-enum.rs b/src/test/compile-fail/lint-ctypes-enum.rs new file mode 100644 index 00000000000..857e3bb4b8d --- /dev/null +++ b/src/test/compile-fail/lint-ctypes-enum.rs @@ -0,0 +1,25 @@ +// Copyright 2013 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[deny(ctypes)]; + +enum Z { } +enum U { A } +enum B { C, D } +enum T { E, F, G } + +extern { + fn zf(x: Z); + fn uf(x: U); + fn bf(x: B); //~ ERROR found enum type without foreign-function-safe + fn tf(x: T); //~ ERROR found enum type without foreign-function-safe +} + +pub fn main() { } diff --git a/src/test/run-pass/enum-discrim-autosizing.rs b/src/test/run-pass/enum-discrim-autosizing.rs new file mode 100644 index 00000000000..ef34115739d --- /dev/null +++ b/src/test/run-pass/enum-discrim-autosizing.rs @@ -0,0 +1,62 @@ +// Copyright 2013 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::mem::size_of; + +enum Ei8 { + Ai8 = -1, + Bi8 = 0 +} + +enum Eu8 { + Au8 = 0, + Bu8 = 0x80 +} + +enum Ei16 { + Ai16 = -1, + Bi16 = 0x80 +} + +enum Eu16 { + Au16 = 0, + Bu16 = 0x8000 +} + +enum Ei32 { + Ai32 = -1, + Bi32 = 0x8000 +} + +enum Eu32 { + Au32 = 0, + Bu32 = 0x8000_0000 +} + +enum Ei64 { + Ai64 = -1, + Bi64 = 0x8000_0000 +} + +enum Eu64 { + Au64 = 0, + Bu64 = 0x8000_0000_0000_0000 +} + +pub fn main() { + assert_eq!(size_of::(), 1); + assert_eq!(size_of::(), 1); + assert_eq!(size_of::(), 2); + assert_eq!(size_of::(), 2); + assert_eq!(size_of::(), 4); + assert_eq!(size_of::(), 4); + assert_eq!(size_of::(), 8); + assert_eq!(size_of::(), 8); +} diff --git a/src/test/run-pass/enum-discrim-manual-sizing.rs b/src/test/run-pass/enum-discrim-manual-sizing.rs new file mode 100644 index 00000000000..16eaac082ab --- /dev/null +++ b/src/test/run-pass/enum-discrim-manual-sizing.rs @@ -0,0 +1,84 @@ +// Copyright 2013 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::mem::size_of; + +#[repr(i8)] +enum Ei8 { + Ai8 = 0, + Bi8 = 1 +} + +#[repr(u8)] +enum Eu8 { + Au8 = 0, + Bu8 = 1 +} + +#[repr(i16)] +enum Ei16 { + Ai16 = 0, + Bi16 = 1 +} + +#[repr(u16)] +enum Eu16 { + Au16 = 0, + Bu16 = 1 +} + +#[repr(i32)] +enum Ei32 { + Ai32 = 0, + Bi32 = 1 +} + +#[repr(u32)] +enum Eu32 { + Au32 = 0, + Bu32 = 1 +} + +#[repr(i64)] +enum Ei64 { + Ai64 = 0, + Bi64 = 1 +} + +#[repr(u64)] +enum Eu64 { + Au64 = 0, + Bu64 = 1 +} + +#[repr(int)] +enum Eint { + Aint = 0, + Bint = 1 +} + +#[repr(uint)] +enum Euint { + Auint = 0, + Buint = 1 +} + +pub fn main() { + assert_eq!(size_of::(), 1); + assert_eq!(size_of::(), 1); + assert_eq!(size_of::(), 2); + assert_eq!(size_of::(), 2); + assert_eq!(size_of::(), 4); + assert_eq!(size_of::(), 4); + assert_eq!(size_of::(), 8); + assert_eq!(size_of::(), 8); + assert_eq!(size_of::(), size_of::()); + assert_eq!(size_of::(), size_of::()); +} diff --git a/src/test/run-pass/enum-discrim-width-stuff.rs b/src/test/run-pass/enum-discrim-width-stuff.rs index bc4cb87f06b..61efb2c8a42 100644 --- a/src/test/run-pass/enum-discrim-width-stuff.rs +++ b/src/test/run-pass/enum-discrim-width-stuff.rs @@ -8,13 +8,48 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::mem; +#[feature(macro_rules)]; + +macro_rules! check { + ($m:ident, $t:ty, $v:expr) => {{ + mod $m { + use std::mem::size_of; + enum E { + V = $v, + A = 0 + } + static C: E = V; + pub fn check() { + assert_eq!(size_of::(), size_of::<$t>()); + assert_eq!(V as $t, $v); + assert_eq!(C as $t, $v); + assert_eq!(format!("{:?}", V), ~"V"); + assert_eq!(format!("{:?}", C), ~"V"); + } + } + $m::check(); + }} +} pub fn main() { - enum E { V = 0x1717171717171717 } - static C: E = V; - assert_eq!(V as u64, 0x1717171717171717u64); - assert_eq!(C as u64, 0x1717171717171717u64); - assert_eq!(format!("{:?}", V), ~"V"); - assert_eq!(format!("{:?}", C), ~"V"); + check!(a, u8, 0x17); + check!(b, u8, 0xe8); + check!(c, u16, 0x1727); + check!(d, u16, 0xe8d8); + check!(e, u32, 0x17273747); + check!(f, u32, 0xe8d8c8b8); + check!(g, u64, 0x1727374757677787u64); + check!(h, u64, 0xe8d8c8b8a8988878u64); + + check!(z, i8, 0x17); + check!(y, i8, -0x17); + check!(x, i16, 0x1727); + check!(w, i16, -0x1727); + check!(v, i32, 0x17273747); + check!(u, i32, -0x17273747); + check!(t, i64, 0x1727374757677787); + check!(s, i64, -0x1727374757677787); + + enum Simple { A, B } + assert_eq!(std::mem::size_of::(), 1); } -- 2.44.0