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,
|
2015-02-19 23:35:33 -06:00
|
|
|
//! `unsafe`.
|
2015-06-05 01:31:27 -05:00
|
|
|
use self::RootUnsafeContext::*;
|
2013-05-23 21:12:16 -05:00
|
|
|
|
2016-01-29 14:04:07 -06:00
|
|
|
use dep_graph::DepNode;
|
2016-03-22 10:30:57 -05:00
|
|
|
use ty::{self, Ty, TyCtxt};
|
|
|
|
use ty::MethodCall;
|
2016-08-26 11:23:42 -05:00
|
|
|
use lint;
|
2013-05-23 21:12:16 -05:00
|
|
|
|
|
|
|
use syntax::ast;
|
2016-06-21 17:08:13 -05:00
|
|
|
use syntax_pos::Span;
|
2016-08-18 12:12:28 -05:00
|
|
|
use hir::{self, PatKind};
|
|
|
|
use hir::def::Def;
|
2016-11-28 13:00:26 -06:00
|
|
|
use hir::intravisit::{self, FnKind, Visitor, NestedVisitorMap};
|
2013-05-23 21:12:16 -05:00
|
|
|
|
2015-06-05 01:31:27 -05:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct UnsafeContext {
|
|
|
|
push_unsafe_count: usize,
|
|
|
|
root: RootUnsafeContext,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UnsafeContext {
|
|
|
|
fn new(root: RootUnsafeContext) -> UnsafeContext {
|
|
|
|
UnsafeContext { root: root, push_unsafe_count: 0 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-30 08:38:44 -05:00
|
|
|
#[derive(Copy, Clone, PartialEq)]
|
2015-06-05 01:31:27 -05:00
|
|
|
enum RootUnsafeContext {
|
2013-05-23 21:12:16 -05:00
|
|
|
SafeContext,
|
|
|
|
UnsafeFn,
|
2013-07-27 03:25:59 -05:00
|
|
|
UnsafeBlock(ast::NodeId),
|
2013-05-23 21:12:16 -05:00
|
|
|
}
|
|
|
|
|
2014-09-13 13:09:25 -05:00
|
|
|
fn type_is_unsafe_function(ty: Ty) -> bool {
|
2014-10-31 03:51:16 -05:00
|
|
|
match ty.sty {
|
2016-08-26 11:23:42 -05:00
|
|
|
ty::TyFnDef(.., ref f) |
|
2015-06-13 15:15:03 -05:00
|
|
|
ty::TyFnPtr(ref f) => f.unsafety == hir::Unsafety::Unsafe,
|
2013-05-23 21:12:16 -05:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
struct EffectCheckVisitor<'a, 'tcx: 'a> {
|
2016-05-02 21:23:22 -05:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2013-10-07 16:25:30 -05:00
|
|
|
|
|
|
|
/// 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
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
impl<'a, 'tcx> EffectCheckVisitor<'a, 'tcx> {
|
2016-08-26 11:23:42 -05:00
|
|
|
fn require_unsafe_ext(&mut self, node_id: ast::NodeId, span: Span,
|
|
|
|
description: &str, is_lint: bool) {
|
2015-06-05 01:31:27 -05:00
|
|
|
if self.unsafe_context.push_unsafe_count > 0 { return; }
|
|
|
|
match self.unsafe_context.root {
|
2013-05-23 21:12:16 -05:00
|
|
|
SafeContext => {
|
2016-08-26 11:23:42 -05:00
|
|
|
if is_lint {
|
|
|
|
self.tcx.sess.add_lint(lint::builtin::SAFE_EXTERN_STATICS,
|
|
|
|
node_id,
|
|
|
|
span,
|
|
|
|
format!("{} requires unsafe function or \
|
|
|
|
block (error E0133)", description));
|
|
|
|
} else {
|
|
|
|
// Report an error.
|
|
|
|
struct_span_err!(
|
|
|
|
self.tcx.sess, span, E0133,
|
|
|
|
"{} requires unsafe function or block", description)
|
|
|
|
.span_label(span, &description)
|
|
|
|
.emit();
|
|
|
|
}
|
2013-05-23 21:12:16 -05:00
|
|
|
}
|
|
|
|
UnsafeBlock(block_id) => {
|
|
|
|
// OK, but record this.
|
2014-10-15 01:25:34 -05:00
|
|
|
debug!("effect: recording unsafe block as used: {}", block_id);
|
2014-03-20 21:49:20 -05:00
|
|
|
self.tcx.used_unsafe.borrow_mut().insert(block_id);
|
2013-05-23 21:12:16 -05:00
|
|
|
}
|
|
|
|
UnsafeFn => {}
|
|
|
|
}
|
2013-08-12 21:15:54 -05:00
|
|
|
}
|
2016-08-26 11:23:42 -05:00
|
|
|
|
|
|
|
fn require_unsafe(&mut self, span: Span, description: &str) {
|
|
|
|
self.require_unsafe_ext(ast::DUMMY_NODE_ID, span, description, false)
|
|
|
|
}
|
2013-08-12 21:15:54 -05:00
|
|
|
}
|
|
|
|
|
2016-10-28 15:58:32 -05:00
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for EffectCheckVisitor<'a, 'tcx> {
|
2016-11-28 13:00:26 -06:00
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
|
|
|
|
NestedVisitorMap::OnlyBodies(&self.tcx.map)
|
2016-10-28 15:58:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_fn(&mut self, fn_kind: FnKind<'tcx>, fn_decl: &'tcx hir::FnDecl,
|
|
|
|
body_id: hir::ExprId, span: Span, id: ast::NodeId) {
|
2013-10-07 16:25:30 -05:00
|
|
|
|
2014-09-09 17:54:36 -05:00
|
|
|
let (is_item_fn, is_unsafe_fn) = match fn_kind {
|
2016-08-26 11:23:42 -05:00
|
|
|
FnKind::ItemFn(_, _, unsafety, ..) =>
|
2015-07-31 02:04:06 -05:00
|
|
|
(true, unsafety == hir::Unsafety::Unsafe),
|
2016-08-26 11:23:42 -05:00
|
|
|
FnKind::Method(_, sig, ..) =>
|
2015-07-31 02:04:06 -05:00
|
|
|
(true, sig.unsafety == hir::Unsafety::Unsafe),
|
2013-10-07 16:25:30 -05:00
|
|
|
_ => (false, false),
|
|
|
|
};
|
|
|
|
|
|
|
|
let old_unsafe_context = self.unsafe_context;
|
|
|
|
if is_unsafe_fn {
|
2015-06-05 01:31:27 -05:00
|
|
|
self.unsafe_context = UnsafeContext::new(UnsafeFn)
|
2013-10-07 16:25:30 -05:00
|
|
|
} else if is_item_fn {
|
2015-06-05 01:31:27 -05:00
|
|
|
self.unsafe_context = UnsafeContext::new(SafeContext)
|
2013-10-07 16:25:30 -05:00
|
|
|
}
|
2013-05-23 21:12:16 -05:00
|
|
|
|
2016-10-28 15:58:32 -05:00
|
|
|
intravisit::walk_fn(self, fn_kind, fn_decl, body_id, span, 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
|
|
|
|
2016-10-28 15:58:32 -05:00
|
|
|
fn visit_block(&mut self, block: &'tcx hir::Block) {
|
2013-10-07 16:25:30 -05:00
|
|
|
let old_unsafe_context = self.unsafe_context;
|
2014-02-20 06:22:45 -06:00
|
|
|
match block.rules {
|
2015-07-31 02:04:06 -05:00
|
|
|
hir::UnsafeBlock(source) => {
|
2014-02-20 06:22:45 -06:00
|
|
|
// 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)
|
2014-08-23 05:41:32 -05:00
|
|
|
// - inside `unsafe`, found an `unsafe` block
|
2014-02-20 06:22:45 -06:00
|
|
|
// 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).
|
2015-07-31 02:04:06 -05:00
|
|
|
if self.unsafe_context.root == SafeContext || source == hir::CompilerGenerated {
|
2015-06-05 01:31:27 -05:00
|
|
|
self.unsafe_context.root = UnsafeBlock(block.id)
|
2014-02-20 06:22:45 -06:00
|
|
|
}
|
|
|
|
}
|
2015-07-31 02:04:06 -05:00
|
|
|
hir::PushUnsafeBlock(..) => {
|
2015-06-05 01:31:27 -05:00
|
|
|
self.unsafe_context.push_unsafe_count =
|
2015-07-23 09:20:59 -05:00
|
|
|
self.unsafe_context.push_unsafe_count.checked_add(1).unwrap();
|
2015-06-05 01:31:27 -05:00
|
|
|
}
|
2015-07-31 02:04:06 -05:00
|
|
|
hir::PopUnsafeBlock(..) => {
|
2015-06-05 01:31:27 -05:00
|
|
|
self.unsafe_context.push_unsafe_count =
|
2015-07-23 09:20:59 -05:00
|
|
|
self.unsafe_context.push_unsafe_count.checked_sub(1).unwrap();
|
2015-06-05 01:31:27 -05:00
|
|
|
}
|
2016-10-29 19:42:12 -05:00
|
|
|
hir::DefaultBlock => {}
|
2013-10-07 16:25:30 -05:00
|
|
|
}
|
2013-05-23 21:12:16 -05:00
|
|
|
|
2015-11-17 16:51:44 -06:00
|
|
|
intravisit::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
|
|
|
}
|
|
|
|
|
2016-10-28 15:58:32 -05:00
|
|
|
fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
|
2013-10-07 16:25:30 -05:00
|
|
|
match expr.node {
|
2016-08-26 11:23:42 -05:00
|
|
|
hir::ExprMethodCall(..) => {
|
2014-03-06 11:24:11 -06:00
|
|
|
let method_call = MethodCall::expr(expr.id);
|
2016-10-26 20:52:10 -05:00
|
|
|
let base_type = self.tcx.tables().method_map[&method_call].ty;
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("effect: method call case, base type is {:?}",
|
|
|
|
base_type);
|
2013-10-07 16:25:30 -05:00
|
|
|
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
|
|
|
}
|
2015-07-31 02:04:06 -05:00
|
|
|
hir::ExprCall(ref base, _) => {
|
2016-10-19 22:33:20 -05:00
|
|
|
let base_type = self.tcx.tables().expr_ty_adjusted(base);
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("effect: call case, base type is {:?}",
|
|
|
|
base_type);
|
2013-10-07 16:25:30 -05:00
|
|
|
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
|
|
|
}
|
2015-07-31 02:04:06 -05:00
|
|
|
hir::ExprUnary(hir::UnDeref, ref base) => {
|
2016-10-19 22:33:20 -05:00
|
|
|
let base_type = self.tcx.tables().expr_ty_adjusted(base);
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("effect: unary case, base type is {:?}",
|
|
|
|
base_type);
|
2015-06-11 18:21:46 -05:00
|
|
|
if let ty::TyRawPtr(_) = base_type.sty {
|
2015-06-09 15:49:24 -05:00
|
|
|
self.require_unsafe(expr.span, "dereference of raw pointer")
|
2013-05-23 21:12:16 -05:00
|
|
|
}
|
2013-10-07 16:25:30 -05:00
|
|
|
}
|
2015-07-31 02:04:06 -05:00
|
|
|
hir::ExprInlineAsm(..) => {
|
2014-11-29 15:41:21 -06:00
|
|
|
self.require_unsafe(expr.span, "use of inline assembly");
|
2013-10-07 16:25:30 -05:00
|
|
|
}
|
2016-11-25 05:21:19 -06:00
|
|
|
hir::ExprPath(hir::QPath::Resolved(_, ref path)) => {
|
|
|
|
if let Def::Static(def_id, mutbl) = path.def {
|
2016-08-26 11:23:42 -05:00
|
|
|
if mutbl {
|
|
|
|
self.require_unsafe(expr.span, "use of mutable static");
|
|
|
|
} else if match self.tcx.map.get_if_local(def_id) {
|
|
|
|
Some(hir::map::NodeForeignItem(..)) => true,
|
|
|
|
Some(..) => false,
|
|
|
|
None => self.tcx.sess.cstore.is_foreign_item(def_id),
|
|
|
|
} {
|
|
|
|
self.require_unsafe_ext(expr.id, expr.span, "use of extern static", true);
|
|
|
|
}
|
2013-06-21 20:46:34 -05:00
|
|
|
}
|
2013-05-23 21:12:16 -05:00
|
|
|
}
|
2016-08-18 12:12:28 -05:00
|
|
|
hir::ExprField(ref base_expr, field) => {
|
2016-10-19 22:33:20 -05:00
|
|
|
if let ty::TyAdt(adt, ..) = self.tcx.tables().expr_ty_adjusted(base_expr).sty {
|
2016-09-05 17:26:02 -05:00
|
|
|
if adt.is_union() {
|
|
|
|
self.require_unsafe(field.span, "access to union field");
|
|
|
|
}
|
2016-08-18 12:12:28 -05:00
|
|
|
}
|
|
|
|
}
|
2013-10-07 16:25:30 -05:00
|
|
|
_ => {}
|
|
|
|
}
|
2013-05-23 21:12:16 -05:00
|
|
|
|
2015-11-17 16:51:44 -06:00
|
|
|
intravisit::walk_expr(self, expr);
|
2013-08-12 21:15:54 -05:00
|
|
|
}
|
2016-08-18 12:12:28 -05:00
|
|
|
|
2016-10-28 15:58:32 -05:00
|
|
|
fn visit_pat(&mut self, pat: &'tcx hir::Pat) {
|
2016-08-18 12:12:28 -05:00
|
|
|
if let PatKind::Struct(_, ref fields, _) = pat.node {
|
2016-10-26 20:52:10 -05:00
|
|
|
if let ty::TyAdt(adt, ..) = self.tcx.tables().pat_ty(pat).sty {
|
2016-09-05 17:26:02 -05:00
|
|
|
if adt.is_union() {
|
|
|
|
for field in fields {
|
|
|
|
self.require_unsafe(field.span, "matching on union field");
|
|
|
|
}
|
2016-08-18 12:12:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
intravisit::walk_pat(self, pat);
|
|
|
|
}
|
2013-08-12 21:15:54 -05:00
|
|
|
}
|
2013-05-23 21:12:16 -05:00
|
|
|
|
2016-05-02 21:23:22 -05:00
|
|
|
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
|
2016-01-29 14:04:07 -06:00
|
|
|
let _task = tcx.dep_graph.in_task(DepNode::EffectCheck);
|
|
|
|
|
2013-08-12 21:15:54 -05:00
|
|
|
let mut visitor = EffectCheckVisitor {
|
|
|
|
tcx: tcx,
|
2015-06-05 01:31:27 -05:00
|
|
|
unsafe_context: UnsafeContext::new(SafeContext),
|
2013-08-12 21:15:54 -05:00
|
|
|
};
|
2013-05-23 21:12:16 -05:00
|
|
|
|
2016-11-02 17:22:59 -05:00
|
|
|
tcx.map.krate().visit_all_item_likes(&mut visitor.as_deep_visitor());
|
2013-05-23 21:12:16 -05:00
|
|
|
}
|