Added tests for discriminant overflows.

This commit is contained in:
Felix S. Klock II 2015-03-28 11:14:06 +01:00
parent 4e04d57efa
commit 36b6d0ecdc
4 changed files with 476 additions and 0 deletions

View File

@ -0,0 +1,118 @@
// Copyright 2015 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.
// When explicit discriminant value has
// a type that does not match the representation
// type, rustc should fail gracefully.
// See also run-pass/discrim-explicit-23030.rs where the input types
// are correct.
#![allow(dead_code, unused_variables, unused_imports)]
use std::{i8,u8,i16,u16,i32,u32,i64, u64};
fn f_i8() {
#[repr(i8)]
enum A {
Ok = i8::MAX - 1,
Ok2,
OhNo = 0_u8,
//~^ ERROR mismatched types
}
let x = A::Ok;
}
fn f_u8() {
#[repr(u8)]
enum A {
Ok = u8::MAX - 1,
Ok2,
OhNo = 0_i8,
//~^ ERROR mismatched types
}
let x = A::Ok;
}
fn f_i16() {
#[repr(i16)]
enum A {
Ok = i16::MAX - 1,
Ok2,
OhNo = 0_u16,
//~^ ERROR mismatched types
}
let x = A::Ok;
}
fn f_u16() {
#[repr(u16)]
enum A {
Ok = u16::MAX - 1,
Ok2,
OhNo = 0_i16,
//~^ ERROR mismatched types
}
let x = A::Ok;
}
fn f_i32() {
#[repr(i32)]
enum A {
Ok = i32::MAX - 1,
Ok2,
OhNo = 0_u32,
//~^ ERROR mismatched types
}
let x = A::Ok;
}
fn f_u32() {
#[repr(u32)]
enum A {
Ok = u32::MAX - 1,
Ok2,
OhNo = 0_i32,
//~^ ERROR mismatched types
}
let x = A::Ok;
}
fn f_i64() {
#[repr(i64)]
enum A {
Ok = i64::MAX - 1,
Ok2,
OhNo = 0_u64,
//~^ ERROR mismatched types
}
let x = A::Ok;
}
fn f_u64() {
#[repr(u64)]
enum A {
Ok = u64::MAX - 1,
Ok2,
OhNo = 0_i64,
//~^ ERROR mismatched types
}
let x = A::Ok;
}
fn main() { }

View File

@ -0,0 +1,94 @@
// Copyright 2015 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.
// ignore-tidy-linelength
// Issue 23030: Detect overflowing discriminant
//
// Check that we detect the overflow even if enum is not used.
// See also run-pass/discrim-explicit-23030.rs where the suggested
// workaround is tested.
use std::{i8,u8,i16,u16,i32,u32,i64, u64};
fn f_i8() {
#[repr(i8)]
enum A {
Ok = i8::MAX - 1,
Ok2,
OhNo, //~ ERROR enum discriminant overflowed on value after 127: i8; set explicitly via OhNo = -128 if that is desired outcome
}
}
fn f_u8() {
#[repr(u8)]
enum A {
Ok = u8::MAX - 1,
Ok2,
OhNo, //~ ERROR enum discriminant overflowed on value after 255: u8; set explicitly via OhNo = 0 if that is desired outcome
}
}
fn f_i16() {
#[repr(i16)]
enum A {
Ok = i16::MAX - 1,
Ok2,
OhNo, //~ ERROR enum discriminant overflowed
}
}
fn f_u16() {
#[repr(u16)]
enum A {
Ok = u16::MAX - 1,
Ok2,
OhNo, //~ ERROR enum discriminant overflowed
}
}
fn f_i32() {
#[repr(i32)]
enum A {
Ok = i32::MAX - 1,
Ok2,
OhNo, //~ ERROR enum discriminant overflowed
}
}
fn f_u32() {
#[repr(u32)]
enum A {
Ok = u32::MAX - 1,
Ok2,
OhNo, //~ ERROR enum discriminant overflowed
}
}
fn f_i64() {
#[repr(i64)]
enum A {
Ok = i64::MAX - 1,
Ok2,
OhNo, //~ ERROR enum discriminant overflowed
}
}
fn f_u64() {
#[repr(u64)]
enum A {
Ok = u64::MAX - 1,
Ok2,
OhNo, //~ ERROR enum discriminant overflowed
}
}
fn main() { }

View File

@ -0,0 +1,108 @@
// Copyright 2015 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.
// ignore-tidy-linelength
// Issue 23030: Detect overflowing discriminant
// See also run-pass/discrim-explicit-23030.rs where the suggested
// workaround is tested.
use std::{i8,u8,i16,u16,i32,u32,i64, u64};
fn f_i8() {
#[repr(i8)]
enum A {
Ok = i8::MAX - 1,
Ok2,
OhNo, //~ ERROR enum discriminant overflowed on value after 127: i8; set explicitly via OhNo = -128 if that is desired outcome
}
let x = A::Ok;
}
fn f_u8() {
#[repr(u8)]
enum A {
Ok = u8::MAX - 1,
Ok2,
OhNo, //~ ERROR enum discriminant overflowed on value after 255: u8; set explicitly via OhNo = 0 if that is desired outcome
}
let x = A::Ok;
}
fn f_i16() {
#[repr(i16)]
enum A {
Ok = i16::MAX - 1,
Ok2,
OhNo, //~ ERROR enum discriminant overflowed
}
let x = A::Ok;
}
fn f_u16() {
#[repr(u16)]
enum A {
Ok = u16::MAX - 1,
Ok2,
OhNo, //~ ERROR enum discriminant overflowed
}
let x = A::Ok;
}
fn f_i32() {
#[repr(i32)]
enum A {
Ok = i32::MAX - 1,
Ok2,
OhNo, //~ ERROR enum discriminant overflowed
}
let x = A::Ok;
}
fn f_u32() {
#[repr(u32)]
enum A {
Ok = u32::MAX - 1,
Ok2,
OhNo, //~ ERROR enum discriminant overflowed
}
let x = A::Ok;
}
fn f_i64() {
#[repr(i64)]
enum A {
Ok = i64::MAX - 1,
Ok2,
OhNo, //~ ERROR enum discriminant overflowed
}
let x = A::Ok;
}
fn f_u64() {
#[repr(u64)]
enum A {
Ok = u64::MAX - 1,
Ok2,
OhNo, //~ ERROR enum discriminant overflowed
}
let x = A::Ok;
}
fn main() { }

View File

@ -0,0 +1,156 @@
// Copyright 2015 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.
// Issue 23030: Workaround overflowing discriminant
// with explicit assignments.
// See also compile-fail/overflow-discrim.rs, which shows what
// happens if you leave the OhNo explicit cases out here.
use std::{i8,u8,i16,u16,i32,u32,i64,u64,isize,usize};
fn f_i8() {
#[repr(i8)]
enum A {
Ok = i8::MAX - 1,
Ok2,
OhNo = i8::MIN,
NotTheEnd = -1,
Zero,
}
let _x = (A::Ok, A::Ok2, A::OhNo);
let z = (A::NotTheEnd, A::Zero).1 as i8;
assert_eq!(z, 0);
}
fn f_u8() {
#[repr(u8)]
enum A {
Ok = u8::MAX - 1,
Ok2,
OhNo = u8::MIN,
}
let _x = (A::Ok, A::Ok2, A::OhNo);
}
fn f_i16() {
#[repr(i16)]
enum A {
Ok = i16::MAX - 1,
Ok2,
OhNo = i16::MIN,
NotTheEnd = -1,
Zero,
}
let _x = (A::Ok, A::Ok2, A::OhNo);
let z = (A::NotTheEnd, A::Zero).1 as i16;
assert_eq!(z, 0);
}
fn f_u16() {
#[repr(u16)]
enum A {
Ok = u16::MAX - 1,
Ok2,
OhNo = u16::MIN,
}
let _x = (A::Ok, A::Ok2, A::OhNo);
}
fn f_i32() {
#[repr(i32)]
enum A {
Ok = i32::MAX - 1,
Ok2,
OhNo = i32::MIN,
NotTheEnd = -1,
Zero,
}
let _x = (A::Ok, A::Ok2, A::OhNo);
let z = (A::NotTheEnd, A::Zero).1 as i32;
assert_eq!(z, 0);
}
fn f_u32() {
#[repr(u32)]
enum A {
Ok = u32::MAX - 1,
Ok2,
OhNo = u32::MIN,
}
let _x = (A::Ok, A::Ok2, A::OhNo);
}
fn f_i64() {
#[repr(i64)]
enum A {
Ok = i64::MAX - 1,
Ok2,
OhNo = i64::MIN,
NotTheEnd = -1,
Zero,
}
let _x = (A::Ok, A::Ok2, A::OhNo);
let z = (A::NotTheEnd, A::Zero).1 as i64;
assert_eq!(z, 0);
}
fn f_u64() {
#[repr(u64)]
enum A {
Ok = u64::MAX - 1,
Ok2,
OhNo = u64::MIN,
}
let _x = (A::Ok, A::Ok2, A::OhNo);
}
fn f_isize() {
#[repr(isize)]
enum A {
Ok = isize::MAX - 1,
Ok2,
OhNo = isize::MIN,
NotTheEnd = -1,
Zero,
}
let _x = (A::Ok, A::Ok2, A::OhNo);
let z = (A::NotTheEnd, A::Zero).1 as isize;
assert_eq!(z, 0);
}
fn f_usize() {
#[repr(usize)]
enum A {
Ok = usize::MAX - 1,
Ok2,
OhNo = usize::MIN,
}
let _x = (A::Ok, A::Ok2, A::OhNo);
}
fn main() {
f_i8(); f_u8();
f_i16(); f_u16();
f_i32(); f_u32();
f_i64(); f_u64();
f_isize(); f_usize();
}