Skip to content

Commit e741a40

Browse files
committed
Added tests for discriminant overflows.
1 parent 8601802 commit e741a40

File tree

4 files changed

+476
-0
lines changed

4 files changed

+476
-0
lines changed
+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright 2015 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+
// When explicit discriminant value has
12+
// a type that does not match the representation
13+
// type, rustc should fail gracefully.
14+
15+
// See also run-pass/discrim-explicit-23030.rs where the input types
16+
// are correct.
17+
18+
#![allow(dead_code, unused_variables, unused_imports)]
19+
20+
use std::{i8,u8,i16,u16,i32,u32,i64, u64};
21+
22+
fn f_i8() {
23+
#[repr(i8)]
24+
enum A {
25+
Ok = i8::MAX - 1,
26+
Ok2,
27+
OhNo = 0_u8,
28+
//~^ ERROR mismatched types
29+
}
30+
31+
let x = A::Ok;
32+
}
33+
34+
fn f_u8() {
35+
#[repr(u8)]
36+
enum A {
37+
Ok = u8::MAX - 1,
38+
Ok2,
39+
OhNo = 0_i8,
40+
//~^ ERROR mismatched types
41+
}
42+
43+
let x = A::Ok;
44+
}
45+
46+
fn f_i16() {
47+
#[repr(i16)]
48+
enum A {
49+
Ok = i16::MAX - 1,
50+
Ok2,
51+
OhNo = 0_u16,
52+
//~^ ERROR mismatched types
53+
}
54+
55+
let x = A::Ok;
56+
}
57+
58+
fn f_u16() {
59+
#[repr(u16)]
60+
enum A {
61+
Ok = u16::MAX - 1,
62+
Ok2,
63+
OhNo = 0_i16,
64+
//~^ ERROR mismatched types
65+
}
66+
67+
let x = A::Ok;
68+
}
69+
70+
fn f_i32() {
71+
#[repr(i32)]
72+
enum A {
73+
Ok = i32::MAX - 1,
74+
Ok2,
75+
OhNo = 0_u32,
76+
//~^ ERROR mismatched types
77+
}
78+
79+
let x = A::Ok;
80+
}
81+
82+
fn f_u32() {
83+
#[repr(u32)]
84+
enum A {
85+
Ok = u32::MAX - 1,
86+
Ok2,
87+
OhNo = 0_i32,
88+
//~^ ERROR mismatched types
89+
}
90+
91+
let x = A::Ok;
92+
}
93+
94+
fn f_i64() {
95+
#[repr(i64)]
96+
enum A {
97+
Ok = i64::MAX - 1,
98+
Ok2,
99+
OhNo = 0_u64,
100+
//~^ ERROR mismatched types
101+
}
102+
103+
let x = A::Ok;
104+
}
105+
106+
fn f_u64() {
107+
#[repr(u64)]
108+
enum A {
109+
Ok = u64::MAX - 1,
110+
Ok2,
111+
OhNo = 0_i64,
112+
//~^ ERROR mismatched types
113+
}
114+
115+
let x = A::Ok;
116+
}
117+
118+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright 2015 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+
// ignore-tidy-linelength
12+
13+
// Issue 23030: Detect overflowing discriminant
14+
//
15+
// Check that we detect the overflow even if enum is not used.
16+
17+
// See also run-pass/discrim-explicit-23030.rs where the suggested
18+
// workaround is tested.
19+
20+
use std::{i8,u8,i16,u16,i32,u32,i64, u64};
21+
22+
fn f_i8() {
23+
#[repr(i8)]
24+
enum A {
25+
Ok = i8::MAX - 1,
26+
Ok2,
27+
OhNo, //~ ERROR enum discriminant overflowed on value after 127: i8; set explicitly via OhNo = -128 if that is desired outcome
28+
}
29+
}
30+
31+
fn f_u8() {
32+
#[repr(u8)]
33+
enum A {
34+
Ok = u8::MAX - 1,
35+
Ok2,
36+
OhNo, //~ ERROR enum discriminant overflowed on value after 255: u8; set explicitly via OhNo = 0 if that is desired outcome
37+
}
38+
}
39+
40+
fn f_i16() {
41+
#[repr(i16)]
42+
enum A {
43+
Ok = i16::MAX - 1,
44+
Ok2,
45+
OhNo, //~ ERROR enum discriminant overflowed
46+
}
47+
}
48+
49+
fn f_u16() {
50+
#[repr(u16)]
51+
enum A {
52+
Ok = u16::MAX - 1,
53+
Ok2,
54+
OhNo, //~ ERROR enum discriminant overflowed
55+
}
56+
}
57+
58+
fn f_i32() {
59+
#[repr(i32)]
60+
enum A {
61+
Ok = i32::MAX - 1,
62+
Ok2,
63+
OhNo, //~ ERROR enum discriminant overflowed
64+
}
65+
}
66+
67+
fn f_u32() {
68+
#[repr(u32)]
69+
enum A {
70+
Ok = u32::MAX - 1,
71+
Ok2,
72+
OhNo, //~ ERROR enum discriminant overflowed
73+
}
74+
}
75+
76+
fn f_i64() {
77+
#[repr(i64)]
78+
enum A {
79+
Ok = i64::MAX - 1,
80+
Ok2,
81+
OhNo, //~ ERROR enum discriminant overflowed
82+
}
83+
}
84+
85+
fn f_u64() {
86+
#[repr(u64)]
87+
enum A {
88+
Ok = u64::MAX - 1,
89+
Ok2,
90+
OhNo, //~ ERROR enum discriminant overflowed
91+
}
92+
}
93+
94+
fn main() { }
+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright 2015 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+
// ignore-tidy-linelength
12+
13+
// Issue 23030: Detect overflowing discriminant
14+
15+
// See also run-pass/discrim-explicit-23030.rs where the suggested
16+
// workaround is tested.
17+
18+
use std::{i8,u8,i16,u16,i32,u32,i64, u64};
19+
20+
fn f_i8() {
21+
#[repr(i8)]
22+
enum A {
23+
Ok = i8::MAX - 1,
24+
Ok2,
25+
OhNo, //~ ERROR enum discriminant overflowed on value after 127: i8; set explicitly via OhNo = -128 if that is desired outcome
26+
}
27+
28+
let x = A::Ok;
29+
}
30+
31+
fn f_u8() {
32+
#[repr(u8)]
33+
enum A {
34+
Ok = u8::MAX - 1,
35+
Ok2,
36+
OhNo, //~ ERROR enum discriminant overflowed on value after 255: u8; set explicitly via OhNo = 0 if that is desired outcome
37+
}
38+
39+
let x = A::Ok;
40+
}
41+
42+
fn f_i16() {
43+
#[repr(i16)]
44+
enum A {
45+
Ok = i16::MAX - 1,
46+
Ok2,
47+
OhNo, //~ ERROR enum discriminant overflowed
48+
}
49+
50+
let x = A::Ok;
51+
}
52+
53+
fn f_u16() {
54+
#[repr(u16)]
55+
enum A {
56+
Ok = u16::MAX - 1,
57+
Ok2,
58+
OhNo, //~ ERROR enum discriminant overflowed
59+
}
60+
61+
let x = A::Ok;
62+
}
63+
64+
fn f_i32() {
65+
#[repr(i32)]
66+
enum A {
67+
Ok = i32::MAX - 1,
68+
Ok2,
69+
OhNo, //~ ERROR enum discriminant overflowed
70+
}
71+
72+
let x = A::Ok;
73+
}
74+
75+
fn f_u32() {
76+
#[repr(u32)]
77+
enum A {
78+
Ok = u32::MAX - 1,
79+
Ok2,
80+
OhNo, //~ ERROR enum discriminant overflowed
81+
}
82+
83+
let x = A::Ok;
84+
}
85+
86+
fn f_i64() {
87+
#[repr(i64)]
88+
enum A {
89+
Ok = i64::MAX - 1,
90+
Ok2,
91+
OhNo, //~ ERROR enum discriminant overflowed
92+
}
93+
94+
let x = A::Ok;
95+
}
96+
97+
fn f_u64() {
98+
#[repr(u64)]
99+
enum A {
100+
Ok = u64::MAX - 1,
101+
Ok2,
102+
OhNo, //~ ERROR enum discriminant overflowed
103+
}
104+
105+
let x = A::Ok;
106+
}
107+
108+
fn main() { }

0 commit comments

Comments
 (0)