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;
|
|
|
|
use middle::typeck::method_map;
|
|
|
|
use util::ppaux;
|
|
|
|
|
|
|
|
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;
|
2013-10-07 16:25:30 -05:00
|
|
|
use syntax::visit::Visitor;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
fn type_is_unsafe_function(ty: ty::t) -> bool {
|
|
|
|
match ty::get(ty).sty {
|
2013-10-07 16:25:30 -05:00
|
|
|
ty::ty_bare_fn(ref f) => f.purity == ast::unsafe_fn,
|
|
|
|
ty::ty_closure(ref f) => f.purity == ast::unsafe_fn,
|
2013-05-23 21:12:16 -05:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-12 21:15:54 -05:00
|
|
|
struct EffectCheckVisitor {
|
|
|
|
tcx: ty::ctxt,
|
2013-10-07 16:25:30 -05:00
|
|
|
|
|
|
|
/// The method map.
|
|
|
|
method_map: method_map,
|
|
|
|
/// Whether we're in an unsafe context.
|
|
|
|
unsafe_context: UnsafeContext,
|
2013-08-12 21:15:54 -05:00
|
|
|
}
|
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-10-07 16:25:30 -05:00
|
|
|
match self.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-09-28 00:38:08 -05:00
|
|
|
format!("{} requires unsafe function or block",
|
2013-05-23 21:12:16 -05:00
|
|
|
description))
|
|
|
|
}
|
|
|
|
UnsafeBlock(block_id) => {
|
|
|
|
// OK, but record this.
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("effect: recording unsafe block as used: {:?}", block_id);
|
2013-12-20 19:46:04 -06:00
|
|
|
let mut used_unsafe = self.tcx.used_unsafe.borrow_mut();
|
|
|
|
let _ = used_unsafe.get().insert(block_id);
|
2013-05-23 21:12:16 -05:00
|
|
|
}
|
|
|
|
UnsafeFn => {}
|
|
|
|
}
|
2013-08-12 21:15:54 -05:00
|
|
|
}
|
2013-10-07 16:25:30 -05:00
|
|
|
|
|
|
|
fn check_str_index(&mut self, e: @ast::Expr) {
|
|
|
|
let base_type = match e.node {
|
|
|
|
ast::ExprIndex(_, base, _) => ty::node_id_to_type(self.tcx, base.id),
|
|
|
|
_ => return
|
|
|
|
};
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("effect: checking index with base type {}",
|
2013-10-07 16:25:30 -05:00
|
|
|
ppaux::ty_to_str(self.tcx, base_type));
|
|
|
|
match ty::get(base_type).sty {
|
2013-11-28 14:22:53 -06:00
|
|
|
ty::ty_estr(..) => {
|
2013-10-07 16:25:30 -05:00
|
|
|
self.tcx.sess.span_err(e.span,
|
|
|
|
"modification of string types is not allowed");
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2013-08-12 21:15:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Visitor<()> for EffectCheckVisitor {
|
2013-10-07 16:25:30 -05:00
|
|
|
fn visit_fn(&mut self, fn_kind: &visit::fn_kind, fn_decl: &ast::fn_decl,
|
2013-11-30 16:00:39 -06:00
|
|
|
block: ast::P<ast::Block>, span: Span, node_id: ast::NodeId, _:()) {
|
2013-10-07 16:25:30 -05:00
|
|
|
|
|
|
|
let (is_item_fn, is_unsafe_fn) = match *fn_kind {
|
|
|
|
visit::fk_item_fn(_, _, purity, _) =>
|
|
|
|
(true, purity == ast::unsafe_fn),
|
|
|
|
visit::fk_method(_, _, method) =>
|
|
|
|
(true, method.purity == ast::unsafe_fn),
|
|
|
|
_ => (false, false),
|
|
|
|
};
|
|
|
|
|
|
|
|
let old_unsafe_context = self.unsafe_context;
|
|
|
|
if is_unsafe_fn {
|
|
|
|
self.unsafe_context = UnsafeFn
|
|
|
|
} else if is_item_fn {
|
|
|
|
self.unsafe_context = SafeContext
|
|
|
|
}
|
2013-05-23 21:12:16 -05:00
|
|
|
|
2013-10-07 16:25:30 -05:00
|
|
|
visit::walk_fn(self, fn_kind, fn_decl, block, span, node_id, ());
|
2013-08-12 21:15:54 -05:00
|
|
|
|
2013-10-07 16:25:30 -05:00
|
|
|
self.unsafe_context = old_unsafe_context
|
2013-08-12 21:15:54 -05:00
|
|
|
}
|
2013-05-23 21:12:16 -05:00
|
|
|
|
2013-11-30 16:00:39 -06:00
|
|
|
fn visit_block(&mut self, block: ast::P<ast::Block>, _:()) {
|
2013-10-07 16:25:30 -05:00
|
|
|
let old_unsafe_context = self.unsafe_context;
|
|
|
|
let is_unsafe = match block.rules {
|
2013-11-28 14:22:53 -06:00
|
|
|
ast::UnsafeBlock(..) => true, ast::DefaultBlock => false
|
2013-10-07 16:25:30 -05:00
|
|
|
};
|
|
|
|
if is_unsafe && self.unsafe_context == SafeContext {
|
|
|
|
self.unsafe_context = UnsafeBlock(block.id)
|
|
|
|
}
|
2013-05-23 21:12:16 -05:00
|
|
|
|
2013-10-07 16:25:30 -05:00
|
|
|
visit::walk_block(self, block, ());
|
2013-05-23 21:12:16 -05:00
|
|
|
|
2013-10-07 16:25:30 -05:00
|
|
|
self.unsafe_context = old_unsafe_context
|
2013-08-12 21:15:54 -05:00
|
|
|
}
|
|
|
|
|
2013-10-07 16:25:30 -05:00
|
|
|
fn visit_expr(&mut self, expr: @ast::Expr, _:()) {
|
|
|
|
match expr.node {
|
|
|
|
ast::ExprMethodCall(callee_id, _, _, _, _, _) => {
|
|
|
|
let base_type = ty::node_id_to_type(self.tcx, callee_id);
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("effect: method call case, base type is {}",
|
2013-10-07 16:25:30 -05:00
|
|
|
ppaux::ty_to_str(self.tcx, base_type));
|
|
|
|
if type_is_unsafe_function(base_type) {
|
|
|
|
self.require_unsafe(expr.span,
|
|
|
|
"invocation of unsafe method")
|
2013-05-23 21:12:16 -05:00
|
|
|
}
|
2013-10-07 16:25:30 -05:00
|
|
|
}
|
|
|
|
ast::ExprCall(base, _, _) => {
|
|
|
|
let base_type = ty::node_id_to_type(self.tcx, base.id);
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("effect: call case, base type is {}",
|
2013-10-07 16:25:30 -05:00
|
|
|
ppaux::ty_to_str(self.tcx, base_type));
|
|
|
|
if type_is_unsafe_function(base_type) {
|
|
|
|
self.require_unsafe(expr.span, "call to unsafe function")
|
2013-05-23 21:12:16 -05:00
|
|
|
}
|
2013-10-07 16:25:30 -05:00
|
|
|
}
|
|
|
|
ast::ExprUnary(_, ast::UnDeref, base) => {
|
|
|
|
let base_type = ty::node_id_to_type(self.tcx, base.id);
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("effect: unary case, base type is {}",
|
2013-10-07 16:25:30 -05:00
|
|
|
ppaux::ty_to_str(self.tcx, base_type));
|
|
|
|
match ty::get(base_type).sty {
|
|
|
|
ty::ty_ptr(_) => {
|
|
|
|
self.require_unsafe(expr.span,
|
|
|
|
"dereference of unsafe pointer")
|
2013-05-23 21:12:16 -05:00
|
|
|
}
|
2013-10-07 16:25:30 -05:00
|
|
|
_ => {}
|
2013-05-23 21:12:16 -05:00
|
|
|
}
|
2013-10-07 16:25:30 -05:00
|
|
|
}
|
|
|
|
ast::ExprAssign(base, _) | ast::ExprAssignOp(_, _, base, _) => {
|
|
|
|
self.check_str_index(base);
|
|
|
|
}
|
|
|
|
ast::ExprAddrOf(ast::MutMutable, base) => {
|
|
|
|
self.check_str_index(base);
|
|
|
|
}
|
2013-11-28 14:22:53 -06:00
|
|
|
ast::ExprInlineAsm(..) => {
|
2013-10-07 16:25:30 -05:00
|
|
|
self.require_unsafe(expr.span, "use of inline assembly")
|
|
|
|
}
|
2013-11-28 14:22:53 -06:00
|
|
|
ast::ExprPath(..) => {
|
2013-10-07 16:25:30 -05:00
|
|
|
match ty::resolve_expr(self.tcx, expr) {
|
|
|
|
ast::DefStatic(_, true) => {
|
|
|
|
self.require_unsafe(expr.span, "use of mutable static")
|
2013-06-21 20:46:34 -05:00
|
|
|
}
|
2013-10-07 16:25:30 -05:00
|
|
|
_ => {}
|
2013-06-21 20:46:34 -05:00
|
|
|
}
|
2013-05-23 21:12:16 -05:00
|
|
|
}
|
2013-10-07 16:25:30 -05:00
|
|
|
_ => {}
|
|
|
|
}
|
2013-05-23 21:12:16 -05:00
|
|
|
|
2013-10-07 16:25:30 -05:00
|
|
|
visit::walk_expr(self, expr, ());
|
2013-08-12 21:15:54 -05:00
|
|
|
}
|
|
|
|
}
|
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 mut visitor = EffectCheckVisitor {
|
|
|
|
tcx: tcx,
|
2013-10-07 16:25:30 -05:00
|
|
|
method_map: method_map,
|
|
|
|
unsafe_context: SafeContext,
|
2013-08-12 21:15:54 -05:00
|
|
|
};
|
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
|
|
|
}
|