]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/coherence/unsafety.rs
Remove usage of global variable "inlined_types"
[rust.git] / src / librustc_typeck / coherence / unsafety.rs
1 //! Unsafety checker: every impl either implements a trait defined in this
2 //! crate or pertains to a type defined in this crate.
3
4 use rustc::ty::TyCtxt;
5 use rustc_errors::struct_span_err;
6 use rustc_hir as hir;
7 use rustc_hir::itemlikevisit::ItemLikeVisitor;
8 use rustc_hir::Unsafety;
9
10 use rustc_error_codes::*;
11
12 pub fn check(tcx: TyCtxt<'_>) {
13     let mut unsafety = UnsafetyChecker { tcx };
14     tcx.hir().krate().visit_all_item_likes(&mut unsafety);
15 }
16
17 struct UnsafetyChecker<'tcx> {
18     tcx: TyCtxt<'tcx>,
19 }
20
21 impl UnsafetyChecker<'tcx> {
22     fn check_unsafety_coherence(
23         &mut self,
24         item: &'v hir::Item<'v>,
25         impl_generics: Option<&hir::Generics<'_>>,
26         unsafety: hir::Unsafety,
27         polarity: hir::ImplPolarity,
28     ) {
29         let local_did = self.tcx.hir().local_def_id(item.hir_id);
30         if let Some(trait_ref) = self.tcx.impl_trait_ref(local_did) {
31             let trait_def = self.tcx.trait_def(trait_ref.def_id);
32             let unsafe_attr = impl_generics.and_then(|generics| {
33                 generics.params.iter().find(|p| p.pure_wrt_drop).map(|_| "may_dangle")
34             });
35             match (trait_def.unsafety, unsafe_attr, unsafety, polarity) {
36                 (Unsafety::Normal, None, Unsafety::Unsafe, hir::ImplPolarity::Positive) => {
37                     struct_span_err!(
38                         self.tcx.sess,
39                         item.span,
40                         E0199,
41                         "implementing the trait `{}` is not unsafe",
42                         trait_ref.print_only_trait_path()
43                     )
44                     .emit();
45                 }
46
47                 (Unsafety::Unsafe, _, Unsafety::Normal, hir::ImplPolarity::Positive) => {
48                     struct_span_err!(
49                         self.tcx.sess,
50                         item.span,
51                         E0200,
52                         "the trait `{}` requires an `unsafe impl` declaration",
53                         trait_ref.print_only_trait_path()
54                     )
55                     .emit();
56                 }
57
58                 (
59                     Unsafety::Normal,
60                     Some(attr_name),
61                     Unsafety::Normal,
62                     hir::ImplPolarity::Positive,
63                 ) => {
64                     struct_span_err!(
65                         self.tcx.sess,
66                         item.span,
67                         E0569,
68                         "requires an `unsafe impl` declaration due to `#[{}]` attribute",
69                         attr_name
70                     )
71                     .emit();
72                 }
73
74                 (_, _, Unsafety::Unsafe, hir::ImplPolarity::Negative) => {
75                     // Reported in AST validation
76                     self.tcx.sess.delay_span_bug(item.span, "unsafe negative impl");
77                 }
78                 (_, _, Unsafety::Normal, hir::ImplPolarity::Negative)
79                 | (Unsafety::Unsafe, _, Unsafety::Unsafe, hir::ImplPolarity::Positive)
80                 | (Unsafety::Normal, Some(_), Unsafety::Unsafe, hir::ImplPolarity::Positive)
81                 | (Unsafety::Normal, None, Unsafety::Normal, _) => {
82                     // OK
83                 }
84             }
85         }
86     }
87 }
88
89 impl ItemLikeVisitor<'v> for UnsafetyChecker<'tcx> {
90     fn visit_item(&mut self, item: &'v hir::Item<'v>) {
91         if let hir::ItemKind::Impl { unsafety, polarity, ref generics, .. } = item.kind {
92             self.check_unsafety_coherence(item, Some(generics), unsafety, polarity);
93         }
94     }
95
96     fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem<'_>) {}
97
98     fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem<'_>) {}
99 }