Hack for "unsafety hygiene" -- push_unsafe! and pop_unsafe!.

Even after expansion, the generated expressions still track depth of
such pushes (i.e. how often you have "pushed" without a corresponding
"pop"), and we add a rule that in a context with a positive
`push_unsafe!` depth, it is effectively an `unsafe` block context.

(This way, we can inject code that uses `unsafe` features, but still
contains within it a sub-expression that should inherit the outer
safety checking setting, outside of the injected code.)

This is a total hack; it not only needs a feature-gate, but probably
should be feature-gated forever (if possible).

ignore-pretty in test/run-pass/pushpop-unsafe-okay.rs
This commit is contained in:
Felix S. Klock II 2015-06-05 08:31:27 +02:00
parent 25281b121f
commit 1829fa5199
12 changed files with 301 additions and 16 deletions

View file

@ -0,0 +1,14 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
fn main() {
let c = push_unsafe!('c'); //~ ERROR push/pop_unsafe macros are experimental
let c = pop_unsafe!('c'); //~ ERROR push/pop_unsafe macros are experimental
}

View file

@ -0,0 +1,71 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Basic sanity check for `push_unsafe!(EXPR)` and
// `pop_unsafe!(EXPR)`: we can call unsafe code when there are a
// positive number of pushes in the stack, or if we are within a
// normal `unsafe` block, but otherwise cannot.
static mut X: i32 = 0;
unsafe fn f() { X += 1; return; }
fn g() { unsafe { X += 1_000; } return; }
fn main() {
push_unsafe!( {
f(); pop_unsafe!({
f() //~ ERROR: call to unsafe function
})
} );
push_unsafe!({
f();
pop_unsafe!({
g();
f(); //~ ERROR: call to unsafe function
})
} );
push_unsafe!({
g(); pop_unsafe!({
unsafe {
f();
}
f(); //~ ERROR: call to unsafe function
})
});
// Note: For implementation simplicity I have chosen to just have
// the stack do "saturated pop", but perhaps we would prefer to
// have cases like these two here be errors:
pop_unsafe!{ g() };
push_unsafe!({
pop_unsafe!(pop_unsafe!{ g() })
});
// Okay, back to examples that do error, even in the presence of
// "saturated pop"
push_unsafe!({
g();
pop_unsafe!(pop_unsafe!({
f() //~ ERROR: call to unsafe function
}))
});
pop_unsafe!({
f(); //~ ERROR: call to unsafe function
})
}

View file

@ -0,0 +1,56 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Basic sanity check for `push_unsafe!(EXPR)` and
// `pop_unsafe!(EXPR)`: we can call unsafe code when there are a
// positive number of pushes in the stack, or if we are within a
// normal `unsafe` block, but otherwise cannot.
// ignore-pretty because the `push_unsafe!` and `pop_unsafe!` macros
// are not integrated with the pretty-printer.
#![feature(pushpop_unsafe)]
static mut X: i32 = 0;
unsafe fn f() { X += 1; return; }
fn g() { unsafe { X += 1_000; } return; }
fn check_reset_x(x: i32) -> bool {
#![allow(unused_parens)] // dont you judge my style choices!
unsafe {
let ret = (x == X);
X = 0;
ret
}
}
fn main() {
// double-check test infrastructure
assert!(check_reset_x(0));
unsafe { f(); }
assert!(check_reset_x(1));
assert!(check_reset_x(0));
{ g(); }
assert!(check_reset_x(1000));
assert!(check_reset_x(0));
unsafe { f(); g(); g(); }
assert!(check_reset_x(2001));
push_unsafe!( { f(); pop_unsafe!( g() ) } );
assert!(check_reset_x(1_001));
push_unsafe!( { g(); pop_unsafe!( unsafe { f(); f(); } ) } );
assert!(check_reset_x(1_002));
unsafe { push_unsafe!( { f(); pop_unsafe!( { f(); f(); } ) } ); }
assert!(check_reset_x(3));
push_unsafe!( { f(); push_unsafe!( { pop_unsafe!( { f(); f(); f(); } ) } ); } );
assert!(check_reset_x(4));
}