about summary refs log tree commit diff
path: root/src/test/ui/discrim-overflow.rs
blob: a3039b8d9573adafb1093d06efc0fb058e4f308c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// 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 [E0370]
              //~| NOTE overflowed on value after 127i8
              //~| NOTE explicitly set `OhNo = -128i8` 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 [E0370]
              //~| NOTE overflowed on value after 255u8
              //~| NOTE explicitly set `OhNo = 0u8` 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 [E0370]
              //~| NOTE overflowed on value after 32767i16
              //~| NOTE explicitly set `OhNo = -32768i16` if that is desired outcome
    }

    let x = A::Ok;
}

fn f_u16() {
    #[repr(u16)]
    enum A {
        Ok = u16::MAX - 1,
        Ok2,
        OhNo, //~ ERROR enum discriminant overflowed [E0370]
              //~| overflowed on value after 65535u16
              //~| NOTE explicitly set `OhNo = 0u16` if that is desired outcome
    }

    let x = A::Ok;
}

fn f_i32() {
    #[repr(i32)]
    enum A {
        Ok = i32::MAX - 1,
        Ok2,
        OhNo, //~ ERROR enum discriminant overflowed [E0370]
              //~| overflowed on value after 2147483647i32
              //~| NOTE explicitly set `OhNo = -2147483648i32` if that is desired outcome
    }

    let x = A::Ok;
}

fn f_u32() {
    #[repr(u32)]
    enum A {
        Ok = u32::MAX - 1,
        Ok2,
        OhNo, //~ ERROR enum discriminant overflowed [E0370]
              //~| overflowed on value after 4294967295u32
              //~| NOTE explicitly set `OhNo = 0u32` if that is desired outcome
    }

    let x = A::Ok;
}

fn f_i64() {
    #[repr(i64)]
    enum A {
        Ok = i64::MAX - 1,
        Ok2,
        OhNo, //~ ERROR enum discriminant overflowed [E0370]
              //~| overflowed on value after 9223372036854775807i64
              //~| NOTE explicitly set `OhNo = -9223372036854775808i64` if that is desired outcome
    }

    let x = A::Ok;
}

fn f_u64() {
    #[repr(u64)]
    enum A {
        Ok = u64::MAX - 1,
        Ok2,
        OhNo, //~ ERROR enum discriminant overflowed [E0370]
              //~| overflowed on value after 18446744073709551615u64
              //~| NOTE explicitly set `OhNo = 0u64` if that is desired outcome
    }

    let x = A::Ok;
}

fn main() { }