]>
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, | |
c30ab7b3 SL |
27 | OhNo, //~ ERROR enum discriminant overflowed [E0370] |
28 | //~| NOTE overflowed on value after 127i8 | |
29 | //~| NOTE explicitly set `OhNo = -128i8` if that is desired outcome | |
c34b1796 AL |
30 | } |
31 | } | |
32 | ||
33 | fn f_u8() { | |
34 | #[repr(u8)] | |
35 | enum A { | |
36 | Ok = u8::MAX - 1, | |
37 | Ok2, | |
c30ab7b3 SL |
38 | OhNo, //~ ERROR enum discriminant overflowed [E0370] |
39 | //~| NOTE overflowed on value after 255u8 | |
40 | //~| NOTE explicitly set `OhNo = 0u8` if that is desired outcome | |
c34b1796 AL |
41 | } |
42 | } | |
43 | ||
44 | fn f_i16() { | |
45 | #[repr(i16)] | |
46 | enum A { | |
47 | Ok = i16::MAX - 1, | |
48 | Ok2, | |
c30ab7b3 SL |
49 | OhNo, //~ ERROR enum discriminant overflowed [E0370] |
50 | //~| NOTE overflowed on value after 32767i16 | |
51 | //~| NOTE explicitly set `OhNo = -32768i16` if that is desired outcome | |
c34b1796 AL |
52 | } |
53 | } | |
54 | ||
55 | fn f_u16() { | |
56 | #[repr(u16)] | |
57 | enum A { | |
58 | Ok = u16::MAX - 1, | |
59 | Ok2, | |
c30ab7b3 SL |
60 | OhNo, //~ ERROR enum discriminant overflowed [E0370] |
61 | //~| NOTE overflowed on value after 65535u16 | |
62 | //~| NOTE explicitly set `OhNo = 0u16` if that is desired outcome | |
c34b1796 AL |
63 | } |
64 | } | |
65 | ||
66 | fn f_i32() { | |
67 | #[repr(i32)] | |
68 | enum A { | |
69 | Ok = i32::MAX - 1, | |
70 | Ok2, | |
c30ab7b3 SL |
71 | OhNo, //~ ERROR enum discriminant overflowed [E0370] |
72 | //~| NOTE overflowed on value after 2147483647i32 | |
73 | //~| NOTE explicitly set `OhNo = -2147483648i32` if that is desired outcome | |
c34b1796 AL |
74 | } |
75 | } | |
76 | ||
77 | fn f_u32() { | |
78 | #[repr(u32)] | |
79 | enum A { | |
80 | Ok = u32::MAX - 1, | |
81 | Ok2, | |
c30ab7b3 SL |
82 | OhNo, //~ ERROR enum discriminant overflowed [E0370] |
83 | //~| NOTE overflowed on value after 4294967295u32 | |
84 | //~| NOTE explicitly set `OhNo = 0u32` if that is desired outcome | |
c34b1796 AL |
85 | } |
86 | } | |
87 | ||
88 | fn f_i64() { | |
89 | #[repr(i64)] | |
90 | enum A { | |
91 | Ok = i64::MAX - 1, | |
92 | Ok2, | |
c30ab7b3 SL |
93 | OhNo, //~ ERROR enum discriminant overflowed [E0370] |
94 | //~| NOTE overflowed on value after 9223372036854775807i64 | |
95 | //~| NOTE explicitly set `OhNo = -9223372036854775808i64` if that is desired outcome | |
c34b1796 AL |
96 | } |
97 | } | |
98 | ||
99 | fn f_u64() { | |
100 | #[repr(u64)] | |
101 | enum A { | |
102 | Ok = u64::MAX - 1, | |
103 | Ok2, | |
c30ab7b3 SL |
104 | OhNo, //~ ERROR enum discriminant overflowed [E0370] |
105 | //~| NOTE overflowed on value after 18446744073709551615u64 | |
106 | //~| NOTE explicitly set `OhNo = 0u64` if that is desired outcome | |
c34b1796 AL |
107 | } |
108 | } | |
109 | ||
110 | fn main() { } |