]> git.proxmox.com Git - rustc.git/blame - 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
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
54a0048b 11#![feature(rustc_attrs)]
c34b1796 12#![allow(unused_imports)]
c34b1796
AL
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.
c34b1796
AL
18
19use std::fmt;
20use std::{i8, i16, i32, i64, isize};
21use std::{u8, u16, u32, u64, usize};
22
23const VALS_I8: (i8, i8, i8, i8) =
24 (-i8::MIN,
25 //~^ ERROR attempted to negate with overflow
26 i8::MIN - 1,
54a0048b 27 //~^ ERROR attempted to subtract with overflow
c34b1796
AL
28 i8::MAX + 1,
29 //~^ ERROR attempted to add with overflow
30 i8::MIN * 2,
54a0048b 31 //~^ ERROR attempted to multiply with overflow
c34b1796
AL
32 );
33
34const VALS_I16: (i16, i16, i16, i16) =
35 (-i16::MIN,
36 //~^ ERROR attempted to negate with overflow
37 i16::MIN - 1,
54a0048b 38 //~^ ERROR attempted to subtract with overflow
c34b1796
AL
39 i16::MAX + 1,
40 //~^ ERROR attempted to add with overflow
41 i16::MIN * 2,
54a0048b 42 //~^ ERROR attempted to multiply with overflow
c34b1796
AL
43 );
44
45const VALS_I32: (i32, i32, i32, i32) =
46 (-i32::MIN,
47 //~^ ERROR attempted to negate with overflow
48 i32::MIN - 1,
54a0048b 49 //~^ ERROR attempted to subtract with overflow
c34b1796
AL
50 i32::MAX + 1,
51 //~^ ERROR attempted to add with overflow
52 i32::MIN * 2,
54a0048b 53 //~^ ERROR attempted to multiply with overflow
c34b1796
AL
54 );
55
56const VALS_I64: (i64, i64, i64, i64) =
57 (-i64::MIN,
58 //~^ ERROR attempted to negate with overflow
59 i64::MIN - 1,
54a0048b 60 //~^ ERROR attempted to subtract with overflow
c34b1796
AL
61 i64::MAX + 1,
62 //~^ ERROR attempted to add with overflow
63 i64::MAX * 2,
54a0048b 64 //~^ ERROR attempted to multiply with overflow
c34b1796
AL
65 );
66
67const VALS_U8: (u8, u8, u8, u8) =
9cc50fc6 68 (-(u8::MIN as i8) as u8,
c34b1796 69 u8::MIN - 1,
54a0048b 70 //~^ ERROR attempted to subtract with overflow
c34b1796
AL
71 u8::MAX + 1,
72 //~^ ERROR attempted to add with overflow
73 u8::MAX * 2,
54a0048b 74 //~^ ERROR attempted to multiply with overflow
c34b1796
AL
75 );
76
77const VALS_U16: (u16, u16, u16, u16) =
9cc50fc6 78 (-(u16::MIN as i16) as u16,
c34b1796 79 u16::MIN - 1,
54a0048b 80 //~^ ERROR attempted to subtract with overflow
c34b1796
AL
81 u16::MAX + 1,
82 //~^ ERROR attempted to add with overflow
83 u16::MAX * 2,
54a0048b 84 //~^ ERROR attempted to multiply with overflow
c34b1796
AL
85 );
86
87const VALS_U32: (u32, u32, u32, u32) =
9cc50fc6 88 (-(u32::MIN as i32) as u32,
c34b1796 89 u32::MIN - 1,
54a0048b 90 //~^ ERROR attempted to subtract with overflow
c34b1796
AL
91 u32::MAX + 1,
92 //~^ ERROR attempted to add with overflow
93 u32::MAX * 2,
54a0048b 94 //~^ ERROR attempted to multiply with overflow
c34b1796
AL
95 );
96
97const VALS_U64: (u64, u64, u64, u64) =
9cc50fc6 98 (-(u64::MIN as i64) as u64,
c34b1796 99 u64::MIN - 1,
54a0048b 100 //~^ ERROR attempted to subtract with overflow
c34b1796
AL
101 u64::MAX + 1,
102 //~^ ERROR attempted to add with overflow
103 u64::MAX * 2,
54a0048b 104 //~^ ERROR attempted to multiply with overflow
c34b1796
AL
105 );
106
54a0048b 107#[rustc_no_mir] // FIXME #29769 MIR overflow checking is TBD.
c34b1796
AL
108fn 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
120fn foo<T:fmt::Debug>(x: T) {
121 println!("{:?}", x);
122}