]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/coherence/unsafety.rs
Auto merge of #27338 - alexcrichton:remove-morestack, 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
20 pub fn check(tcx: &ty::ctxt) {
21     let mut orphan = UnsafetyChecker { tcx: tcx };
22     visit::walk_crate(&mut orphan, tcx.map.krate());
23 }
24
25 struct UnsafetyChecker<'cx, 'tcx:'cx> {
26     tcx: &'cx ty::ctxt<'tcx>
27 }
28
29 impl<'cx, 'tcx, 'v> UnsafetyChecker<'cx, 'tcx> {
30     fn check_unsafety_coherence(&mut self, item: &'v ast::Item,
31                                 unsafety: ast::Unsafety,
32                                 polarity: ast::ImplPolarity) {
33         match self.tcx.impl_trait_ref(ast_util::local_def(item.id)) {
34             None => {
35                 // Inherent impl.
36                 match unsafety {
37                     ast::Unsafety::Normal => { /* OK */ }
38                     ast::Unsafety::Unsafe => {
39                         span_err!(self.tcx.sess, item.span, E0197,
40                                   "inherent impls cannot be declared as unsafe");
41                     }
42                 }
43             }
44
45             Some(trait_ref) => {
46                 let trait_def = self.tcx.lookup_trait_def(trait_ref.def_id);
47                 match (trait_def.unsafety, unsafety, polarity) {
48                     (ast::Unsafety::Unsafe,
49                      ast::Unsafety::Unsafe, ast::ImplPolarity::Negative) => {
50                         span_err!(self.tcx.sess, item.span, E0198,
51                                   "negative implementations are not unsafe");
52                     }
53
54                     (ast::Unsafety::Normal, ast::Unsafety::Unsafe, _) => {
55                         span_err!(self.tcx.sess, item.span, E0199,
56                                   "implementing the trait `{}` is not unsafe",
57                                   trait_ref);
58                     }
59
60                     (ast::Unsafety::Unsafe,
61                      ast::Unsafety::Normal, ast::ImplPolarity::Positive) => {
62                         span_err!(self.tcx.sess, item.span, E0200,
63                                   "the trait `{}` requires an `unsafe impl` declaration",
64                                   trait_ref);
65                     }
66
67                     (ast::Unsafety::Unsafe,
68                      ast::Unsafety::Normal, ast::ImplPolarity::Negative) |
69                     (ast::Unsafety::Unsafe,
70                      ast::Unsafety::Unsafe, ast::ImplPolarity::Positive) |
71                     (ast::Unsafety::Normal, ast::Unsafety::Normal, _) => {
72                         /* OK */
73                     }
74                 }
75             }
76         }
77     }
78 }
79
80 impl<'cx, 'tcx,'v> visit::Visitor<'v> for UnsafetyChecker<'cx, 'tcx> {
81     fn visit_item(&mut self, item: &'v ast::Item) {
82         match item.node {
83             ast::ItemDefaultImpl(unsafety, _) => {
84                 self.check_unsafety_coherence(item, unsafety, ast::ImplPolarity::Positive);
85             }
86             ast::ItemImpl(unsafety, polarity, _, _, _, _) => {
87                 self.check_unsafety_coherence(item, unsafety, polarity);
88             }
89             _ => { }
90         }
91
92         visit::walk_item(self, item);
93     }
94 }