2013-05-23 21:12:16 -05:00
|
|
|
// Copyright 2012-2013 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.
|
|
|
|
|
|
|
|
//! Enforces the Rust effect system. Currently there is just one effect,
|
|
|
|
/// `unsafe`.
|
|
|
|
|
|
|
|
use middle::ty::{ty_bare_fn, ty_closure, ty_ptr};
|
|
|
|
use middle::ty;
|
|
|
|
use middle::typeck::method_map;
|
|
|
|
use util::ppaux;
|
|
|
|
|
2013-09-01 20:45:37 -05:00
|
|
|
use syntax::ast::{UnDeref, ExprCall, ExprInlineAsm, ExprMethodCall};
|
|
|
|
use syntax::ast::{ExprUnary, unsafe_fn, ExprPath};
|
2013-05-23 21:12:16 -05:00
|
|
|
use syntax::ast;
|
2013-08-31 11:13:04 -05:00
|
|
|
use syntax::codemap::Span;
|
2013-08-12 21:15:54 -05:00
|
|
|
use syntax::visit::{fk_item_fn, fk_method};
|
|
|
|
use syntax::visit;
|
|
|
|
use syntax::visit::{Visitor,fn_kind};
|
2013-09-01 20:45:37 -05:00
|
|
|
use syntax::ast::{fn_decl,Block,NodeId,Expr};
|
2013-05-23 21:12:16 -05:00
|
|
|
|
|
|
|
#[deriving(Eq)]
|
|
|
|
enum UnsafeContext {
|
|
|
|
SafeContext,
|
|
|
|
UnsafeFn,
|
2013-07-27 03:25:59 -05:00
|
|
|
UnsafeBlock(ast::NodeId),
|
2013-05-23 21:12:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Context {
|
|
|
|
/// The method map.
|
|
|
|
method_map: method_map,
|
|
|
|
/// Whether we're in an unsafe context.
|
|
|
|
unsafe_context: UnsafeContext,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn type_is_unsafe_function(ty: ty::t) -> bool {
|
|
|
|
match ty::get(ty).sty {
|
|
|
|
ty_bare_fn(ref f) => f.purity == unsafe_fn,
|
|
|
|
ty_closure(ref f) => f.purity == unsafe_fn,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-12 21:15:54 -05:00
|
|
|
struct EffectCheckVisitor {
|
|
|
|
tcx: ty::ctxt,
|
|
|
|
context: @mut Context,
|
|
|
|
}
|
2013-05-23 21:12:16 -05:00
|
|
|
|
2013-08-12 21:15:54 -05:00
|
|
|
impl EffectCheckVisitor {
|
2013-08-31 11:13:04 -05:00
|
|
|
fn require_unsafe(&mut self, span: Span, description: &str) {
|
2013-08-12 21:15:54 -05:00
|
|
|
match self.context.unsafe_context {
|
2013-05-23 21:12:16 -05:00
|
|
|
SafeContext => {
|
|
|
|
// Report an error.
|
2013-08-12 21:15:54 -05:00
|
|
|
self.tcx.sess.span_err(span,
|
2013-05-23 21:12:16 -05:00
|
|
|
fmt!("%s requires unsafe function or block",
|
|
|
|
description))
|
|
|
|
}
|
|
|
|
UnsafeBlock(block_id) => {
|
|
|
|
// OK, but record this.
|
|
|
|
debug!("effect: recording unsafe block as used: %?", block_id);
|
2013-08-12 21:15:54 -05:00
|
|
|
let _ = self.tcx.used_unsafe.insert(block_id);
|
2013-05-23 21:12:16 -05:00
|
|
|
}
|
|
|
|
UnsafeFn => {}
|
|
|
|
}
|
2013-08-12 21:15:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Visitor<()> for EffectCheckVisitor {
|
|
|
|
fn visit_fn(&mut self, fn_kind:&fn_kind, fn_decl:&fn_decl,
|
2013-08-31 11:13:04 -05:00
|
|
|
block:&Block, span:Span, node_id:NodeId, _:()) {
|
2013-05-23 21:12:16 -05:00
|
|
|
|
2013-05-24 21:35:29 -05:00
|
|
|
let (is_item_fn, is_unsafe_fn) = match *fn_kind {
|
|
|
|
fk_item_fn(_, _, purity, _) => (true, purity == unsafe_fn),
|
|
|
|
fk_method(_, _, method) => (true, method.purity == unsafe_fn),
|
|
|
|
_ => (false, false),
|
2013-05-23 21:12:16 -05:00
|
|
|
};
|
|
|
|
|
2013-08-12 21:15:54 -05:00
|
|
|
let old_unsafe_context = self.context.unsafe_context;
|
2013-05-23 21:12:16 -05:00
|
|
|
if is_unsafe_fn {
|
2013-08-12 21:15:54 -05:00
|
|
|
self.context.unsafe_context = UnsafeFn
|
2013-05-24 21:35:29 -05:00
|
|
|
} else if is_item_fn {
|
2013-08-12 21:15:54 -05:00
|
|
|
self.context.unsafe_context = SafeContext
|
2013-05-23 21:12:16 -05:00
|
|
|
}
|
|
|
|
|
2013-08-12 21:15:54 -05:00
|
|
|
visit::walk_fn(self,
|
|
|
|
fn_kind,
|
2013-05-23 21:12:16 -05:00
|
|
|
fn_decl,
|
|
|
|
block,
|
|
|
|
span,
|
|
|
|
node_id,
|
2013-08-12 21:15:54 -05:00
|
|
|
());
|
|
|
|
|
|
|
|
self.context.unsafe_context = old_unsafe_context
|
|
|
|
}
|
2013-05-23 21:12:16 -05:00
|
|
|
|
2013-08-12 21:15:54 -05:00
|
|
|
fn visit_block(&mut self, block:&Block, _:()) {
|
2013-05-23 21:12:16 -05:00
|
|
|
|
2013-08-12 21:15:54 -05:00
|
|
|
let old_unsafe_context = self.context.unsafe_context;
|
2013-08-29 01:47:26 -05:00
|
|
|
let is_unsafe = match block.rules {
|
|
|
|
ast::UnsafeBlock(*) => true, ast::DefaultBlock => false
|
|
|
|
};
|
|
|
|
if is_unsafe && self.context.unsafe_context == SafeContext {
|
2013-08-12 21:15:54 -05:00
|
|
|
self.context.unsafe_context = UnsafeBlock(block.id)
|
2013-05-23 21:12:16 -05:00
|
|
|
}
|
|
|
|
|
2013-08-12 21:15:54 -05:00
|
|
|
visit::walk_block(self, block, ());
|
2013-05-23 21:12:16 -05:00
|
|
|
|
2013-08-12 21:15:54 -05:00
|
|
|
self.context.unsafe_context = old_unsafe_context
|
|
|
|
}
|
|
|
|
|
2013-09-01 20:45:37 -05:00
|
|
|
fn visit_expr(&mut self, expr:@Expr, _:()) {
|
2013-05-23 21:12:16 -05:00
|
|
|
|
|
|
|
match expr.node {
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprMethodCall(callee_id, _, _, _, _, _) => {
|
2013-08-12 21:15:54 -05:00
|
|
|
let base_type = ty::node_id_to_type(self.tcx, callee_id);
|
2013-05-23 21:12:16 -05:00
|
|
|
debug!("effect: method call case, base type is %s",
|
2013-08-12 21:15:54 -05:00
|
|
|
ppaux::ty_to_str(self.tcx, base_type));
|
2013-05-23 21:12:16 -05:00
|
|
|
if type_is_unsafe_function(base_type) {
|
2013-08-12 21:15:54 -05:00
|
|
|
self.require_unsafe(expr.span,
|
2013-05-23 21:12:16 -05:00
|
|
|
"invocation of unsafe method")
|
|
|
|
}
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprCall(base, _, _) => {
|
2013-08-12 21:15:54 -05:00
|
|
|
let base_type = ty::node_id_to_type(self.tcx, base.id);
|
2013-05-23 21:12:16 -05:00
|
|
|
debug!("effect: call case, base type is %s",
|
2013-08-12 21:15:54 -05:00
|
|
|
ppaux::ty_to_str(self.tcx, base_type));
|
2013-05-23 21:12:16 -05:00
|
|
|
if type_is_unsafe_function(base_type) {
|
2013-08-12 21:15:54 -05:00
|
|
|
self.require_unsafe(expr.span, "call to unsafe function")
|
2013-05-23 21:12:16 -05:00
|
|
|
}
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprUnary(_, UnDeref, base) => {
|
2013-08-12 21:15:54 -05:00
|
|
|
let base_type = ty::node_id_to_type(self.tcx, base.id);
|
2013-05-23 21:12:16 -05:00
|
|
|
debug!("effect: unary case, base type is %s",
|
2013-08-12 21:15:54 -05:00
|
|
|
ppaux::ty_to_str(self.tcx, base_type));
|
2013-05-23 21:12:16 -05:00
|
|
|
match ty::get(base_type).sty {
|
|
|
|
ty_ptr(_) => {
|
2013-08-12 21:15:54 -05:00
|
|
|
self.require_unsafe(expr.span,
|
2013-05-23 21:12:16 -05:00
|
|
|
"dereference of unsafe pointer")
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprInlineAsm(*) => {
|
2013-08-12 21:15:54 -05:00
|
|
|
self.require_unsafe(expr.span, "use of inline assembly")
|
2013-05-23 21:12:16 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprPath(*) => {
|
2013-08-12 21:15:54 -05:00
|
|
|
match ty::resolve_expr(self.tcx, expr) {
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::DefStatic(_, true) => {
|
2013-08-12 21:15:54 -05:00
|
|
|
self.require_unsafe(expr.span, "use of mutable static")
|
2013-06-21 20:46:34 -05:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2013-05-23 21:12:16 -05:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2013-08-12 21:15:54 -05:00
|
|
|
visit::walk_expr(self, expr, ());
|
|
|
|
}
|
|
|
|
}
|
2013-05-23 21:12:16 -05:00
|
|
|
|
2013-08-12 21:15:54 -05:00
|
|
|
pub fn check_crate(tcx: ty::ctxt,
|
|
|
|
method_map: method_map,
|
|
|
|
crate: &ast::Crate) {
|
|
|
|
let context = @mut Context {
|
|
|
|
method_map: method_map,
|
|
|
|
unsafe_context: SafeContext,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut visitor = EffectCheckVisitor {
|
|
|
|
tcx: tcx,
|
|
|
|
context: context,
|
|
|
|
};
|
2013-05-23 21:12:16 -05:00
|
|
|
|
2013-08-12 21:15:54 -05:00
|
|
|
visit::walk_crate(&mut visitor, crate, ());
|
2013-05-23 21:12:16 -05:00
|
|
|
}
|