]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/const-eval-overflow.rs
Add several lints into `unused` lint group
[rust.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 #![allow(unused_imports)]
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.
17
18 #![warn(const_err)]
19
20 use std::fmt;
21 use std::{i8, i16, i32, i64, isize};
22 use std::{u8, u16, u32, u64, usize};
23
24 const VALS_I8: (i8, i8, i8, i8) =
25     (-i8::MIN,
26      //~^ ERROR constant evaluation error
27      //~| attempt to negate with overflow
28      i8::MIN - 1,
29      //~^ ERROR constant evaluation error
30      //~| attempt to subtract with overflow
31      i8::MAX + 1,
32      //~^ ERROR constant evaluation error
33      //~| attempt to add with overflow
34      i8::MIN * 2,
35      //~^ ERROR constant evaluation error
36      //~| attempt to multiply with overflow
37      );
38
39 const VALS_I16: (i16, i16, i16, i16) =
40     (-i16::MIN,
41      //~^ ERROR constant evaluation error
42      //~| attempt to negate with overflow
43      i16::MIN - 1,
44      //~^ ERROR constant evaluation error
45      //~| attempt to subtract with overflow
46      i16::MAX + 1,
47      //~^ ERROR constant evaluation error
48      //~| attempt to add with overflow
49      i16::MIN * 2,
50      //~^ ERROR constant evaluation error
51      //~| attempt to multiply with overflow
52      );
53
54 const VALS_I32: (i32, i32, i32, i32) =
55     (-i32::MIN,
56      //~^ ERROR constant evaluation error
57      //~| attempt to negate with overflow
58      i32::MIN - 1,
59      //~^ ERROR constant evaluation error
60      //~| attempt to subtract with overflow
61      i32::MAX + 1,
62      //~^ ERROR constant evaluation error
63      //~| attempt to add with overflow
64      i32::MIN * 2,
65      //~^ ERROR constant evaluation error
66      //~| attempt to multiply with overflow
67      );
68
69 const VALS_I64: (i64, i64, i64, i64) =
70     (-i64::MIN,
71      //~^ ERROR constant evaluation error
72      //~| attempt to negate with overflow
73      i64::MIN - 1,
74      //~^ ERROR constant evaluation error
75      //~| attempt to subtract with overflow
76      i64::MAX + 1,
77      //~^ ERROR constant evaluation error
78      //~| attempt to add with overflow
79      i64::MAX * 2,
80      //~^ ERROR constant evaluation error
81      //~| attempt to multiply with overflow
82      );
83
84 const VALS_U8: (u8, u8, u8, u8) =
85     ( //~ WARN constant evaluation error: attempt to subtract with overflow
86      -(u8::MIN as i8) as u8,
87      u8::MIN - 1,
88      //~^ ERROR constant evaluation error
89      //~| attempt to subtract with overflow
90      u8::MAX + 1,
91      //~^ ERROR constant evaluation error
92      //~| attempt to add with overflow
93      u8::MAX * 2,
94      //~^ ERROR constant evaluation error
95      //~| attempt to multiply with overflow
96      );
97
98 const VALS_U16: (u16, u16, u16, u16) =
99     ( //~ WARN constant evaluation error: attempt to subtract with overflow
100      -(u16::MIN as i16) as u16,
101      u16::MIN - 1,
102      //~^ ERROR constant evaluation error
103      //~| attempt to subtract with overflow
104      u16::MAX + 1,
105      //~^ ERROR constant evaluation error
106      //~| attempt to add with overflow
107      u16::MAX * 2,
108      //~^ ERROR constant evaluation error
109      //~| attempt to multiply with overflow
110      );
111
112 const VALS_U32: (u32, u32, u32, u32) =
113     ( //~ WARN constant evaluation error: attempt to subtract with overflow
114      -(u32::MIN as i32) as u32,
115      u32::MIN - 1,
116      //~^ ERROR constant evaluation error
117      //~| attempt to subtract with overflow
118      u32::MAX + 1,
119      //~^ ERROR constant evaluation error
120      //~| attempt to add with overflow
121      u32::MAX * 2,
122      //~^ ERROR constant evaluation error
123      //~| attempt to multiply with overflow
124      );
125
126 const VALS_U64: (u64, u64, u64, u64) =
127     ( //~ WARN constant evaluation error: attempt to subtract with overflow
128      -(u64::MIN as i64) as u64,
129      u64::MIN - 1,
130      //~^ ERROR constant evaluation error
131      //~| attempt to subtract with overflow
132      u64::MAX + 1,
133      //~^ ERROR constant evaluation error
134      //~| attempt to add with overflow
135      u64::MAX * 2,
136      //~^ ERROR constant evaluation error
137      //~| attempt to multiply with overflow
138      );
139
140 fn main() {
141     foo(VALS_I8);
142     foo(VALS_I16);
143     foo(VALS_I32);
144     foo(VALS_I64);
145
146     foo(VALS_U8);
147     foo(VALS_U16);
148     foo(VALS_U32);
149     foo(VALS_U64);
150 }
151
152 fn foo<T:fmt::Debug>(x: T) {
153     println!("{:?}", x);
154 }