]> git.proxmox.com Git - rustc.git/blob - src/test/compile-fail/const-eval-overflow.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / test / compile-fail / const-eval-overflow.rs
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 #![feature(rustc_attrs)]
12 #![allow(unused_imports)]
13
14 // Note: the relevant lint pass here runs before some of the constant
15 // evaluation below (e.g. that performed by trans and llvm), so if you
16 // change this warn to a deny, then the compiler will exit before
17 // those errors are detected.
18
19 use std::fmt;
20 use std::{i8, i16, i32, i64, isize};
21 use std::{u8, u16, u32, u64, usize};
22
23 const VALS_I8: (i8, i8, i8, i8) =
24 (-i8::MIN,
25 //~^ ERROR attempted to negate with overflow
26 i8::MIN - 1,
27 //~^ ERROR attempted to subtract with overflow
28 i8::MAX + 1,
29 //~^ ERROR attempted to add with overflow
30 i8::MIN * 2,
31 //~^ ERROR attempted to multiply with overflow
32 );
33
34 const VALS_I16: (i16, i16, i16, i16) =
35 (-i16::MIN,
36 //~^ ERROR attempted to negate with overflow
37 i16::MIN - 1,
38 //~^ ERROR attempted to subtract with overflow
39 i16::MAX + 1,
40 //~^ ERROR attempted to add with overflow
41 i16::MIN * 2,
42 //~^ ERROR attempted to multiply with overflow
43 );
44
45 const VALS_I32: (i32, i32, i32, i32) =
46 (-i32::MIN,
47 //~^ ERROR attempted to negate with overflow
48 i32::MIN - 1,
49 //~^ ERROR attempted to subtract with overflow
50 i32::MAX + 1,
51 //~^ ERROR attempted to add with overflow
52 i32::MIN * 2,
53 //~^ ERROR attempted to multiply with overflow
54 );
55
56 const VALS_I64: (i64, i64, i64, i64) =
57 (-i64::MIN,
58 //~^ ERROR attempted to negate with overflow
59 i64::MIN - 1,
60 //~^ ERROR attempted to subtract with overflow
61 i64::MAX + 1,
62 //~^ ERROR attempted to add with overflow
63 i64::MAX * 2,
64 //~^ ERROR attempted to multiply with overflow
65 );
66
67 const VALS_U8: (u8, u8, u8, u8) =
68 (-(u8::MIN as i8) as u8,
69 u8::MIN - 1,
70 //~^ ERROR attempted to subtract with overflow
71 u8::MAX + 1,
72 //~^ ERROR attempted to add with overflow
73 u8::MAX * 2,
74 //~^ ERROR attempted to multiply with overflow
75 );
76
77 const VALS_U16: (u16, u16, u16, u16) =
78 (-(u16::MIN as i16) as u16,
79 u16::MIN - 1,
80 //~^ ERROR attempted to subtract with overflow
81 u16::MAX + 1,
82 //~^ ERROR attempted to add with overflow
83 u16::MAX * 2,
84 //~^ ERROR attempted to multiply with overflow
85 );
86
87 const VALS_U32: (u32, u32, u32, u32) =
88 (-(u32::MIN as i32) as u32,
89 u32::MIN - 1,
90 //~^ ERROR attempted to subtract with overflow
91 u32::MAX + 1,
92 //~^ ERROR attempted to add with overflow
93 u32::MAX * 2,
94 //~^ ERROR attempted to multiply with overflow
95 );
96
97 const VALS_U64: (u64, u64, u64, u64) =
98 (-(u64::MIN as i64) as u64,
99 u64::MIN - 1,
100 //~^ ERROR attempted to subtract with overflow
101 u64::MAX + 1,
102 //~^ ERROR attempted to add with overflow
103 u64::MAX * 2,
104 //~^ ERROR attempted to multiply with overflow
105 );
106
107 #[rustc_no_mir] // FIXME #29769 MIR overflow checking is TBD.
108 fn main() {
109 foo(VALS_I8);
110 foo(VALS_I16);
111 foo(VALS_I32);
112 foo(VALS_I64);
113
114 foo(VALS_U8);
115 foo(VALS_U16);
116 foo(VALS_U32);
117 foo(VALS_U64);
118 }
119
120 fn foo<T:fmt::Debug>(x: T) {
121 println!("{:?}", x);
122 }