]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/coherence/unsafety.rs
Auto merge of #37860 - giannicic:defaultimpl, r=nagisa
[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 rustc::ty::TyCtxt;
15 use rustc::hir::itemlikevisit::ItemLikeVisitor;
16 use rustc::hir::{self, Unsafety};
17
18 pub fn check<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
19     let mut unsafety = UnsafetyChecker { tcx: tcx };
20     tcx.hir.krate().visit_all_item_likes(&mut unsafety);
21 }
22
23 struct UnsafetyChecker<'cx, 'tcx: 'cx> {
24     tcx: TyCtxt<'cx, 'tcx, 'tcx>,
25 }
26
27 impl<'cx, 'tcx, 'v> UnsafetyChecker<'cx, 'tcx> {
28     fn check_unsafety_coherence(&mut self,
29                                 item: &'v hir::Item,
30                                 impl_generics: Option<&hir::Generics>,
31                                 unsafety: hir::Unsafety,
32                                 polarity: hir::ImplPolarity) {
33         match self.tcx.impl_trait_ref(self.tcx.hir.local_def_id(item.id)) {
34             None => {}
35
36             Some(trait_ref) => {
37                 let trait_def = self.tcx.trait_def(trait_ref.def_id);
38                 let unsafe_attr = impl_generics.and_then(|g| g.carries_unsafe_attr());
39                 match (trait_def.unsafety, unsafe_attr, unsafety, polarity) {
40                     (_, _, Unsafety::Unsafe, hir::ImplPolarity::Negative) => {
41                         span_err!(self.tcx.sess,
42                                   item.span,
43                                   E0198,
44                                   "negative implementations are not unsafe");
45                     }
46
47                     (Unsafety::Normal, None, Unsafety::Unsafe, _) => {
48                         span_err!(self.tcx.sess,
49                                   item.span,
50                                   E0199,
51                                   "implementing the trait `{}` is not unsafe",
52                                   trait_ref);
53                     }
54
55                     (Unsafety::Unsafe, _, Unsafety::Normal, hir::ImplPolarity::Positive) => {
56                         span_err!(self.tcx.sess,
57                                   item.span,
58                                   E0200,
59                                   "the trait `{}` requires an `unsafe impl` declaration",
60                                   trait_ref);
61                     }
62
63                     (Unsafety::Normal, Some(g), Unsafety::Normal, hir::ImplPolarity::Positive) =>
64                     {
65                         span_err!(self.tcx.sess,
66                                   item.span,
67                                   E0569,
68                                   "requires an `unsafe impl` declaration due to `#[{}]` attribute",
69                                   g.attr_name());
70                     }
71
72                     (_, _, Unsafety::Normal, hir::ImplPolarity::Negative) |
73                     (Unsafety::Unsafe, _, Unsafety::Unsafe, hir::ImplPolarity::Positive) |
74                     (Unsafety::Normal, Some(_), Unsafety::Unsafe, hir::ImplPolarity::Positive) |
75                     (Unsafety::Normal, None, Unsafety::Normal, _) => {
76                         // OK
77                     }
78                 }
79             }
80         }
81     }
82 }
83
84 impl<'cx, 'tcx, 'v> ItemLikeVisitor<'v> for UnsafetyChecker<'cx, 'tcx> {
85     fn visit_item(&mut self, item: &'v hir::Item) {
86         match item.node {
87             hir::ItemDefaultImpl(unsafety, _) => {
88                 self.check_unsafety_coherence(item, None, unsafety, hir::ImplPolarity::Positive);
89             }
90             hir::ItemImpl(unsafety, polarity, _, ref generics, ..) => {
91                 self.check_unsafety_coherence(item, Some(generics), unsafety, polarity);
92             }
93             _ => {}
94         }
95     }
96
97     fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem) {
98     }
99
100     fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem) {
101     }
102 }