]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/coherence/unsafety.rs
Merge branch 'master' into cfg_tmp_dir
[rust.git] / src / librustc_typeck / coherence / unsafety.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Unsafety checker: every impl either implements a trait defined in this
12 //! crate or pertains to a type defined in this crate.
13
14 use middle::ty;
15 use syntax::ast::{Item, ItemImpl};
16 use syntax::ast;
17 use syntax::ast_util;
18 use syntax::visit;
19 use util::ppaux::UserString;
20
21 pub fn check(tcx: &ty::ctxt) {
22     let mut orphan = UnsafetyChecker { tcx: tcx };
23     visit::walk_crate(&mut orphan, tcx.map.krate());
24 }
25
26 struct UnsafetyChecker<'cx, 'tcx:'cx> {
27     tcx: &'cx ty::ctxt<'tcx>
28 }
29
30 impl<'cx, 'tcx,'v> visit::Visitor<'v> for UnsafetyChecker<'cx, 'tcx> {
31     fn visit_item(&mut self, item: &'v ast::Item) {
32         match item.node {
33             ast::ItemImpl(unsafety, _, _, _, _) => {
34                 match ty::impl_trait_ref(self.tcx, ast_util::local_def(item.id)) {
35                     None => {
36                         // Inherent impl.
37                         match unsafety {
38                             ast::Unsafety::Normal => { /* OK */ }
39                             ast::Unsafety::Unsafe => {
40                                 self.tcx.sess.span_err(
41                                     item.span,
42                                     "inherent impls cannot be declared as unsafe");
43                             }
44                         }
45                     }
46
47                     Some(trait_ref) => {
48                         let trait_def = ty::lookup_trait_def(self.tcx, trait_ref.def_id);
49                         match (trait_def.unsafety, unsafety) {
50                             (ast::Unsafety::Normal, ast::Unsafety::Unsafe) => {
51                                 self.tcx.sess.span_err(
52                                     item.span,
53                                     format!("implementing the trait `{}` is not unsafe",
54                                             trait_ref.user_string(self.tcx)).as_slice());
55                             }
56
57                             (ast::Unsafety::Unsafe, ast::Unsafety::Normal) => {
58                                 self.tcx.sess.span_err(
59                                     item.span,
60                                     format!("the trait `{}` requires an `unsafe impl` declaration",
61                                             trait_ref.user_string(self.tcx)).as_slice());
62                             }
63
64                             (ast::Unsafety::Unsafe, ast::Unsafety::Unsafe) |
65                             (ast::Unsafety::Normal, ast::Unsafety::Normal) => {
66                                 /* OK */
67                             }
68                         }
69                     }
70                 }
71             }
72             _ => { }
73         }
74
75         visit::walk_item(self, item);
76     }
77 }