]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/pushpop-unsafe-okay.rs
Imported Upstream version 1.3.0+dfsg1
[rustc.git] / src / test / run-pass / pushpop-unsafe-okay.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 // 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 // ignore-pretty because the `push_unsafe!` and `pop_unsafe!` macros
17 // are not integrated with the pretty-printer.
18
19 #![feature(pushpop_unsafe)]
20
21 static mut X: i32 = 0;
22
23 unsafe fn f() { X += 1; return; }
24 fn g() { unsafe { X += 1_000; } return; }
25
26 fn check_reset_x(x: i32) -> bool {
27 #![allow(unused_parens)] // dont you judge my style choices!
28 unsafe {
29 let ret = (x == X);
30 X = 0;
31 ret
32 }
33 }
34
35 fn main() {
36 // double-check test infrastructure
37 assert!(check_reset_x(0));
38 unsafe { f(); }
39 assert!(check_reset_x(1));
40 assert!(check_reset_x(0));
41 { g(); }
42 assert!(check_reset_x(1000));
43 assert!(check_reset_x(0));
44 unsafe { f(); g(); g(); }
45 assert!(check_reset_x(2001));
46
47 push_unsafe!( { f(); pop_unsafe!( g() ) } );
48 assert!(check_reset_x(1_001));
49 push_unsafe!( { g(); pop_unsafe!( unsafe { f(); f(); } ) } );
50 assert!(check_reset_x(1_002));
51
52 unsafe { push_unsafe!( { f(); pop_unsafe!( { f(); f(); } ) } ); }
53 assert!(check_reset_x(3));
54 push_unsafe!( { f(); push_unsafe!( { pop_unsafe!( { f(); f(); f(); } ) } ); } );
55 assert!(check_reset_x(4));
56 }