]>
Commit | Line | Data |
---|---|---|
c34b1796 AL |
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, | |
54a0048b | 25 | OhNo, //~ ERROR enum discriminant overflowed on value after 127i8; set explicitly via OhNo = -128i8 if that is desired outcome |
c34b1796 AL |
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, | |
54a0048b | 36 | OhNo, //~ ERROR enum discriminant overflowed on value after 255u8; set explicitly via OhNo = 0u8 if that is desired outcome |
c34b1796 AL |
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() { } |