]> git.proxmox.com Git - rustc.git/blob - src/test/compile-fail/pushpop-unsafe-rejects.rs
Imported Upstream version 1.3.0+dfsg1
[rustc.git] / src / test / compile-fail / pushpop-unsafe-rejects.rs
1 // Copyright 2012 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 // Basic sanity check for `push_unsafe!(EXPR)` and
12 // `pop_unsafe!(EXPR)`: we can call unsafe code when there are a
13 // positive number of pushes in the stack, or if we are within a
14 // normal `unsafe` block, but otherwise cannot.
15
16 #![feature(pushpop_unsafe)]
17
18 static mut X: i32 = 0;
19
20 unsafe fn f() { X += 1; return; }
21 fn g() { unsafe { X += 1_000; } return; }
22
23 fn main() {
24 push_unsafe!( {
25 f(); pop_unsafe!({
26 f() //~ ERROR: call to unsafe function
27 })
28 } );
29
30 push_unsafe!({
31 f();
32 pop_unsafe!({
33 g();
34 f(); //~ ERROR: call to unsafe function
35 })
36 } );
37
38 push_unsafe!({
39 g(); pop_unsafe!({
40 unsafe {
41 f();
42 }
43 f(); //~ ERROR: call to unsafe function
44 })
45 });
46
47
48 // Note: For implementation simplicity the compiler just
49 // ICE's if you underflow the push_unsafe stack.
50 //
51 // Thus all of the following cases cause an ICE.
52 //
53 // (The "ERROR" notes are from an earlier version
54 // that used saturated arithmetic rather than checked
55 // arithmetic.)
56
57 // pop_unsafe!{ g() };
58 //
59 // push_unsafe!({
60 // pop_unsafe!(pop_unsafe!{ g() })
61 // });
62 //
63 // push_unsafe!({
64 // g();
65 // pop_unsafe!(pop_unsafe!({
66 // f() // ERROR: call to unsafe function
67 // }))
68 // });
69 //
70 // pop_unsafe!({
71 // f(); // ERROR: call to unsafe function
72 // })
73
74 }