Skip to content

rustc: avoid compiler generated unsafe blocks leaking. #12419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions src/librustc/middle/effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,28 @@ impl Visitor<()> for EffectCheckVisitor {

fn visit_block(&mut self, block: &ast::Block, _:()) {
let old_unsafe_context = self.unsafe_context;
let is_unsafe = match block.rules {
ast::UnsafeBlock(..) => true, ast::DefaultBlock => false
};
if is_unsafe && self.unsafe_context == SafeContext {
self.unsafe_context = UnsafeBlock(block.id)
match block.rules {
ast::DefaultBlock => {}
ast::UnsafeBlock(source) => {
// By default only the outermost `unsafe` block is
// "used" and so nested unsafe blocks are pointless
// (the inner ones are unnecessary and we actually
// warn about them). As such, there are two cases when
// we need to create a new context, when we're
// - outside `unsafe` and found a `unsafe` block
// (normal case)
// - inside `unsafe` but found an `unsafe` block
// created internally to the compiler
//
// The second case is necessary to ensure that the
// compiler `unsafe` blocks don't accidentally "use"
// external blocks (e.g. `unsafe { println("") }`,
// expands to `unsafe { ... unsafe { ... } }` where
// the inner one is compiler generated).
if self.unsafe_context == SafeContext || source == ast::CompilerGenerated {
self.unsafe_context = UnsafeBlock(block.id)
}
}
}

visit::walk_block(self, block, ());
Expand Down
16 changes: 6 additions & 10 deletions src/librustc/middle/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -924,11 +924,9 @@ pub fn invoke<'a>(
}

if need_invoke(bcx) {
unsafe {
debug!("invoking {} at {}", llfn, bcx.llbb);
for &llarg in llargs.iter() {
debug!("arg: {}", llarg);
}
debug!("invoking {} at {}", llfn, bcx.llbb);
for &llarg in llargs.iter() {
debug!("arg: {}", llarg);
}
let normal_bcx = bcx.fcx.new_temp_block("normal-return");
let landing_pad = bcx.fcx.get_landing_pad();
Expand All @@ -946,11 +944,9 @@ pub fn invoke<'a>(
attributes);
return (llresult, normal_bcx);
} else {
unsafe {
debug!("calling {} at {}", llfn, bcx.llbb);
for &llarg in llargs.iter() {
debug!("arg: {}", llarg);
}
debug!("calling {} at {}", llfn, bcx.llbb);
for &llarg in llargs.iter() {
debug!("arg: {}", llarg);
}

match call_info {
Expand Down
4 changes: 1 addition & 3 deletions src/librustc/middle/typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -992,9 +992,7 @@ impl RegionScope for infer::InferCtxt {

impl FnCtxt {
pub fn tag(&self) -> ~str {
unsafe {
format!("{}", self as *FnCtxt)
}
format!("{}", self as *FnCtxt)
}

pub fn local_ty(&self, span: Span, nid: ast::NodeId) -> ty::t {
Expand Down
16 changes: 7 additions & 9 deletions src/libstd/comm/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,14 +486,12 @@ impl<T: Send> Packet<T> {
#[unsafe_destructor]
impl<T: Send> Drop for Packet<T> {
fn drop(&mut self) {
unsafe {
// Note that this load is not only an assert for correctness about
// disconnection, but also a proper fence before the read of
// `to_wake`, so this assert cannot be removed with also removing
// the `to_wake` assert.
assert_eq!(self.cnt.load(atomics::SeqCst), DISCONNECTED);
assert_eq!(self.to_wake.load(atomics::SeqCst), 0);
assert_eq!(self.channels.load(atomics::SeqCst), 0);
}
// Note that this load is not only an assert for correctness about
// disconnection, but also a proper fence before the read of
// `to_wake`, so this assert cannot be removed with also removing
// the `to_wake` assert.
assert_eq!(self.cnt.load(atomics::SeqCst), DISCONNECTED);
assert_eq!(self.to_wake.load(atomics::SeqCst), 0);
assert_eq!(self.channels.load(atomics::SeqCst), 0);
}
}
14 changes: 6 additions & 8 deletions src/libstd/comm/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,13 +471,11 @@ impl<T: Send> Packet<T> {
#[unsafe_destructor]
impl<T: Send> Drop for Packet<T> {
fn drop(&mut self) {
unsafe {
// Note that this load is not only an assert for correctness about
// disconnection, but also a proper fence before the read of
// `to_wake`, so this assert cannot be removed with also removing
// the `to_wake` assert.
assert_eq!(self.cnt.load(atomics::SeqCst), DISCONNECTED);
assert_eq!(self.to_wake.load(atomics::SeqCst), 0);
}
// Note that this load is not only an assert for correctness about
// disconnection, but also a proper fence before the read of
// `to_wake`, so this assert cannot be removed with also removing
// the `to_wake` assert.
assert_eq!(self.cnt.load(atomics::SeqCst), DISCONNECTED);
assert_eq!(self.to_wake.load(atomics::SeqCst), 0);
}
}
17 changes: 17 additions & 0 deletions src/test/compile-fail/unsafe-around-compiler-generated-unsafe.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2014 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.

// issue #12418

#[deny(unused_unsafe)];

fn main() {
unsafe { println!("foo"); } //~ ERROR unnecessary `unsafe`
}