]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/coherence/unsafety.rs
Auto merge of #21065 - ColonelJ:master, r=brson
[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, polarity, _, _, _, _) => {
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, polarity) {
50                             (ast::Unsafety::Unsafe,
51                              ast::Unsafety::Unsafe, ast::ImplPolarity::Negative) => {
52                                 self.tcx.sess.span_err(
53                                     item.span,
54                                     format!("negative implementations are not unsafe").as_slice());
55                             }
56
57                             (ast::Unsafety::Normal, ast::Unsafety::Unsafe, _) => {
58                                 self.tcx.sess.span_err(
59                                     item.span,
60                                     format!("implementing the trait `{}` is not unsafe",
61                                             trait_ref.user_string(self.tcx)).as_slice());
62                             }
63
64                             (ast::Unsafety::Unsafe,
65                              ast::Unsafety::Normal, ast::ImplPolarity::Positive) => {
66                                 self.tcx.sess.span_err(
67                                     item.span,
68                                     format!("the trait `{}` requires an `unsafe impl` declaration",
69                                             trait_ref.user_string(self.tcx)).as_slice());
70                             }
71
72                             (ast::Unsafety::Unsafe,
73                              ast::Unsafety::Normal, ast::ImplPolarity::Negative) |
74                             (ast::Unsafety::Unsafe,
75                              ast::Unsafety::Unsafe, ast::ImplPolarity::Positive) |
76                             (ast::Unsafety::Normal, ast::Unsafety::Normal, _) => {
77                                 /* OK */
78                             }
79                         }
80                     }
81                 }
82             }
83             _ => { }
84         }
85
86         visit::walk_item(self, item);
87     }
88 }