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