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