]> git.lizzy.rs Git - rust.git/blob - src/test/incremental/hashes/panic_exprs.rs
ffb66c29219d3454896a11d50a62c193fd449eff
[rust.git] / src / test / incremental / hashes / panic_exprs.rs
1 // Copyright 2016 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 // This test case tests the incremental compilation hash (ICH) implementation
12 // for exprs that can panic at runtime (e.g. because of bounds checking). For
13 // these expressions an error message containing their source location is
14 // generated, so their hash must always depend on their location in the source
15 // code, not just when debuginfo is enabled.
16
17 // The general pattern followed here is: Change one thing between rev1 and rev2
18 // and make sure that the hash has changed, then change nothing between rev2 and
19 // rev3 and make sure that the hash has not changed.
20
21 // compile-pass
22 // revisions: cfail1 cfail2 cfail3
23 // compile-flags: -Z query-dep-graph -C debug-assertions
24
25 #![allow(warnings)]
26 #![feature(rustc_attrs)]
27 #![crate_type="rlib"]
28
29
30 // Indexing expression ---------------------------------------------------------
31 #[rustc_clean(cfg="cfail2", except="HirBody,MirValidated,MirOptimized")]
32 #[rustc_clean(cfg="cfail3")]
33 pub fn indexing(slice: &[u8]) -> u8 {
34     #[cfg(cfail1)]
35     {
36         slice[100]
37     }
38     #[cfg(not(cfail1))]
39     {
40         slice[100]
41     }
42 }
43
44
45 // Arithmetic overflow plus ----------------------------------------------------
46 #[rustc_clean(cfg="cfail2", except="HirBody,MirValidated,MirOptimized")]
47 #[rustc_clean(cfg="cfail3")]
48 pub fn arithmetic_overflow_plus(val: i32) -> i32 {
49     #[cfg(cfail1)]
50     {
51         val + 1
52     }
53     #[cfg(not(cfail1))]
54     {
55         val + 1
56     }
57 }
58
59
60 // Arithmetic overflow minus ----------------------------------------------------
61 #[rustc_clean(cfg="cfail2", except="HirBody,MirValidated,MirOptimized")]
62 #[rustc_clean(cfg="cfail3")]
63 pub fn arithmetic_overflow_minus(val: i32) -> i32 {
64     #[cfg(cfail1)]
65     {
66         val - 1
67     }
68     #[cfg(not(cfail1))]
69     {
70         val - 1
71     }
72 }
73
74
75 // Arithmetic overflow mult ----------------------------------------------------
76 #[rustc_clean(cfg="cfail2", except="HirBody,MirValidated,MirOptimized")]
77 #[rustc_clean(cfg="cfail3")]
78 pub fn arithmetic_overflow_mult(val: i32) -> i32 {
79     #[cfg(cfail1)]
80     {
81         val * 2
82     }
83     #[cfg(not(cfail1))]
84     {
85         val * 2
86     }
87 }
88
89
90 // Arithmetic overflow negation ------------------------------------------------
91 #[rustc_clean(cfg="cfail2", except="HirBody,MirValidated,MirOptimized")]
92 #[rustc_clean(cfg="cfail3")]
93 pub fn arithmetic_overflow_negation(val: i32) -> i32 {
94     #[cfg(cfail1)]
95     {
96         -val
97     }
98     #[cfg(not(cfail1))]
99     {
100         -val
101     }
102 }
103
104
105 // Division by zero ------------------------------------------------------------
106 #[rustc_clean(cfg="cfail2", except="HirBody,MirValidated,MirOptimized")]
107 #[rustc_clean(cfg="cfail3")]
108 pub fn division_by_zero(val: i32) -> i32 {
109     #[cfg(cfail1)]
110     {
111         2 / val
112     }
113     #[cfg(not(cfail1))]
114     {
115         2 / val
116     }
117 }
118
119 // Division by zero ------------------------------------------------------------
120 #[rustc_clean(cfg="cfail2", except="HirBody,MirValidated,MirOptimized")]
121 #[rustc_clean(cfg="cfail3")]
122 pub fn mod_by_zero(val: i32) -> i32 {
123     #[cfg(cfail1)]
124     {
125         2 % val
126     }
127     #[cfg(not(cfail1))]
128     {
129         2 % val
130     }
131 }
132
133
134 // shift left ------------------------------------------------------------------
135 #[rustc_clean(cfg="cfail2", except="HirBody,MirValidated,MirOptimized")]
136 #[rustc_clean(cfg="cfail3")]
137 pub fn shift_left(val: i32, shift: usize) -> i32 {
138     #[cfg(cfail1)]
139     {
140         val << shift
141     }
142     #[cfg(not(cfail1))]
143     {
144         val << shift
145     }
146 }
147
148
149 // shift right ------------------------------------------------------------------
150 #[rustc_clean(cfg="cfail2", except="HirBody,MirValidated,MirOptimized")]
151 #[rustc_clean(cfg="cfail3")]
152 pub fn shift_right(val: i32, shift: usize) -> i32 {
153     #[cfg(cfail1)]
154     {
155         val >> shift
156     }
157     #[cfg(not(cfail1))]
158     {
159         val >> shift
160     }
161 }