|
3 | 3 |
|
4 | 4 | use rustc_errors::struct_span_err;
|
5 | 5 | use rustc_hir as hir;
|
6 |
| -use rustc_hir::itemlikevisit::ItemLikeVisitor; |
| 6 | +use rustc_hir::def::DefKind; |
7 | 7 | use rustc_hir::Unsafety;
|
8 | 8 | use rustc_middle::ty::TyCtxt;
|
9 | 9 |
|
10 | 10 | pub fn check(tcx: TyCtxt<'_>) {
|
11 |
| - let mut unsafety = UnsafetyChecker { tcx }; |
12 |
| - tcx.hir().visit_all_item_likes(&mut unsafety); |
| 11 | + for id in tcx.hir().items() { |
| 12 | + if matches!(tcx.hir().def_kind(id.def_id), DefKind::Impl) { |
| 13 | + let item = tcx.hir().item(id); |
| 14 | + if let hir::ItemKind::Impl(ref impl_) = item.kind { |
| 15 | + check_unsafety_coherence( |
| 16 | + tcx, |
| 17 | + item, |
| 18 | + Some(&impl_.generics), |
| 19 | + impl_.unsafety, |
| 20 | + impl_.polarity, |
| 21 | + ); |
| 22 | + } |
| 23 | + } |
| 24 | + } |
13 | 25 | }
|
14 | 26 |
|
15 |
| -struct UnsafetyChecker<'tcx> { |
| 27 | +fn check_unsafety_coherence<'tcx>( |
16 | 28 | tcx: TyCtxt<'tcx>,
|
17 |
| -} |
18 |
| - |
19 |
| -impl<'tcx> UnsafetyChecker<'tcx> { |
20 |
| - fn check_unsafety_coherence( |
21 |
| - &mut self, |
22 |
| - item: &hir::Item<'_>, |
23 |
| - impl_generics: Option<&hir::Generics<'_>>, |
24 |
| - unsafety: hir::Unsafety, |
25 |
| - polarity: hir::ImplPolarity, |
26 |
| - ) { |
27 |
| - if let Some(trait_ref) = self.tcx.impl_trait_ref(item.def_id) { |
28 |
| - let trait_def = self.tcx.trait_def(trait_ref.def_id); |
29 |
| - let unsafe_attr = impl_generics.and_then(|generics| { |
30 |
| - generics.params.iter().find(|p| p.pure_wrt_drop).map(|_| "may_dangle") |
31 |
| - }); |
32 |
| - match (trait_def.unsafety, unsafe_attr, unsafety, polarity) { |
33 |
| - (Unsafety::Normal, None, Unsafety::Unsafe, hir::ImplPolarity::Positive) => { |
34 |
| - struct_span_err!( |
35 |
| - self.tcx.sess, |
36 |
| - item.span, |
37 |
| - E0199, |
38 |
| - "implementing the trait `{}` is not unsafe", |
39 |
| - trait_ref.print_only_trait_path() |
40 |
| - ) |
41 |
| - .emit(); |
42 |
| - } |
| 29 | + item: &hir::Item<'_>, |
| 30 | + impl_generics: Option<&hir::Generics<'_>>, |
| 31 | + unsafety: hir::Unsafety, |
| 32 | + polarity: hir::ImplPolarity, |
| 33 | +) { |
| 34 | + if let Some(trait_ref) = tcx.impl_trait_ref(item.def_id) { |
| 35 | + let trait_def = tcx.trait_def(trait_ref.def_id); |
| 36 | + let unsafe_attr = impl_generics.and_then(|generics| { |
| 37 | + generics.params.iter().find(|p| p.pure_wrt_drop).map(|_| "may_dangle") |
| 38 | + }); |
| 39 | + match (trait_def.unsafety, unsafe_attr, unsafety, polarity) { |
| 40 | + (Unsafety::Normal, None, Unsafety::Unsafe, hir::ImplPolarity::Positive) => { |
| 41 | + struct_span_err!( |
| 42 | + tcx.sess, |
| 43 | + item.span, |
| 44 | + E0199, |
| 45 | + "implementing the trait `{}` is not unsafe", |
| 46 | + trait_ref.print_only_trait_path() |
| 47 | + ) |
| 48 | + .emit(); |
| 49 | + } |
43 | 50 |
|
44 |
| - (Unsafety::Unsafe, _, Unsafety::Normal, hir::ImplPolarity::Positive) => { |
45 |
| - struct_span_err!( |
46 |
| - self.tcx.sess, |
47 |
| - item.span, |
48 |
| - E0200, |
49 |
| - "the trait `{}` requires an `unsafe impl` declaration", |
50 |
| - trait_ref.print_only_trait_path() |
51 |
| - ) |
52 |
| - .emit(); |
53 |
| - } |
| 51 | + (Unsafety::Unsafe, _, Unsafety::Normal, hir::ImplPolarity::Positive) => { |
| 52 | + struct_span_err!( |
| 53 | + tcx.sess, |
| 54 | + item.span, |
| 55 | + E0200, |
| 56 | + "the trait `{}` requires an `unsafe impl` declaration", |
| 57 | + trait_ref.print_only_trait_path() |
| 58 | + ) |
| 59 | + .emit(); |
| 60 | + } |
54 | 61 |
|
55 |
| - ( |
56 |
| - Unsafety::Normal, |
57 |
| - Some(attr_name), |
58 |
| - Unsafety::Normal, |
59 |
| - hir::ImplPolarity::Positive, |
60 |
| - ) => { |
61 |
| - struct_span_err!( |
62 |
| - self.tcx.sess, |
63 |
| - item.span, |
64 |
| - E0569, |
65 |
| - "requires an `unsafe impl` declaration due to `#[{}]` attribute", |
66 |
| - attr_name |
67 |
| - ) |
68 |
| - .emit(); |
69 |
| - } |
| 62 | + (Unsafety::Normal, Some(attr_name), Unsafety::Normal, hir::ImplPolarity::Positive) => { |
| 63 | + struct_span_err!( |
| 64 | + tcx.sess, |
| 65 | + item.span, |
| 66 | + E0569, |
| 67 | + "requires an `unsafe impl` declaration due to `#[{}]` attribute", |
| 68 | + attr_name |
| 69 | + ) |
| 70 | + .emit(); |
| 71 | + } |
70 | 72 |
|
71 |
| - (_, _, Unsafety::Unsafe, hir::ImplPolarity::Negative(_)) => { |
72 |
| - // Reported in AST validation |
73 |
| - self.tcx.sess.delay_span_bug(item.span, "unsafe negative impl"); |
74 |
| - } |
75 |
| - (_, _, Unsafety::Normal, hir::ImplPolarity::Negative(_)) |
76 |
| - | (Unsafety::Unsafe, _, Unsafety::Unsafe, hir::ImplPolarity::Positive) |
77 |
| - | (Unsafety::Normal, Some(_), Unsafety::Unsafe, hir::ImplPolarity::Positive) |
78 |
| - | (Unsafety::Normal, None, Unsafety::Normal, _) => { |
79 |
| - // OK |
80 |
| - } |
| 73 | + (_, _, Unsafety::Unsafe, hir::ImplPolarity::Negative(_)) => { |
| 74 | + // Reported in AST validation |
| 75 | + tcx.sess.delay_span_bug(item.span, "unsafe negative impl"); |
| 76 | + } |
| 77 | + (_, _, Unsafety::Normal, hir::ImplPolarity::Negative(_)) |
| 78 | + | (Unsafety::Unsafe, _, Unsafety::Unsafe, hir::ImplPolarity::Positive) |
| 79 | + | (Unsafety::Normal, Some(_), Unsafety::Unsafe, hir::ImplPolarity::Positive) |
| 80 | + | (Unsafety::Normal, None, Unsafety::Normal, _) => { |
| 81 | + // OK |
81 | 82 | }
|
82 | 83 | }
|
83 | 84 | }
|
84 | 85 | }
|
85 | 86 |
|
86 |
| -impl<'tcx> ItemLikeVisitor<'_> for UnsafetyChecker<'tcx> { |
87 |
| - fn visit_item(&mut self, item: &hir::Item<'_>) { |
88 |
| - if let hir::ItemKind::Impl(ref impl_) = item.kind { |
89 |
| - self.check_unsafety_coherence( |
90 |
| - item, |
91 |
| - Some(&impl_.generics), |
92 |
| - impl_.unsafety, |
93 |
| - impl_.polarity, |
94 |
| - ); |
95 |
| - } |
96 |
| - } |
97 |
| - |
98 |
| - fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem<'_>) {} |
99 |
| - |
100 |
| - fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem<'_>) {} |
101 |
| - |
102 |
| - fn visit_foreign_item(&mut self, _foreign_item: &hir::ForeignItem<'_>) {} |
103 |
| -} |
| 87 | +// struct UnsafetyChecker<'tcx> { |
| 88 | +// tcx: TyCtxt<'tcx>, |
| 89 | +// } |
| 90 | +// |
| 91 | +// impl<'tcx> UnsafetyChecker<'tcx> { |
| 92 | +// fn check_unsafety_coherence( |
| 93 | +// &mut self, |
| 94 | +// item: &hir::Item<'_>, |
| 95 | +// impl_generics: Option<&hir::Generics<'_>>, |
| 96 | +// unsafety: hir::Unsafety, |
| 97 | +// polarity: hir::ImplPolarity, |
| 98 | +// ) { |
| 99 | +// if let Some(trait_ref) = self.tcx.impl_trait_ref(item.def_id) { |
| 100 | +// let trait_def = self.tcx.trait_def(trait_ref.def_id); |
| 101 | +// let unsafe_attr = impl_generics.and_then(|generics| { |
| 102 | +// generics.params.iter().find(|p| p.pure_wrt_drop).map(|_| "may_dangle") |
| 103 | +// }); |
| 104 | +// match (trait_def.unsafety, unsafe_attr, unsafety, polarity) { |
| 105 | +// (Unsafety::Normal, None, Unsafety::Unsafe, hir::ImplPolarity::Positive) => { |
| 106 | +// struct_span_err!( |
| 107 | +// self.tcx.sess, |
| 108 | +// item.span, |
| 109 | +// E0199, |
| 110 | +// "implementing the trait `{}` is not unsafe", |
| 111 | +// trait_ref.print_only_trait_path() |
| 112 | +// ) |
| 113 | +// .emit(); |
| 114 | +// } |
| 115 | +// |
| 116 | +// (Unsafety::Unsafe, _, Unsafety::Normal, hir::ImplPolarity::Positive) => { |
| 117 | +// struct_span_err!( |
| 118 | +// self.tcx.sess, |
| 119 | +// item.span, |
| 120 | +// E0200, |
| 121 | +// "the trait `{}` requires an `unsafe impl` declaration", |
| 122 | +// trait_ref.print_only_trait_path() |
| 123 | +// ) |
| 124 | +// .emit(); |
| 125 | +// } |
| 126 | +// |
| 127 | +// ( |
| 128 | +// Unsafety::Normal, |
| 129 | +// Some(attr_name), |
| 130 | +// Unsafety::Normal, |
| 131 | +// hir::ImplPolarity::Positive, |
| 132 | +// ) => { |
| 133 | +// struct_span_err!( |
| 134 | +// self.tcx.sess, |
| 135 | +// item.span, |
| 136 | +// E0569, |
| 137 | +// "requires an `unsafe impl` declaration due to `#[{}]` attribute", |
| 138 | +// attr_name |
| 139 | +// ) |
| 140 | +// .emit(); |
| 141 | +// } |
| 142 | +// |
| 143 | +// (_, _, Unsafety::Unsafe, hir::ImplPolarity::Negative(_)) => { |
| 144 | +// // Reported in AST validation |
| 145 | +// self.tcx.sess.delay_span_bug(item.span, "unsafe negative impl"); |
| 146 | +// } |
| 147 | +// (_, _, Unsafety::Normal, hir::ImplPolarity::Negative(_)) |
| 148 | +// | (Unsafety::Unsafe, _, Unsafety::Unsafe, hir::ImplPolarity::Positive) |
| 149 | +// | (Unsafety::Normal, Some(_), Unsafety::Unsafe, hir::ImplPolarity::Positive) |
| 150 | +// | (Unsafety::Normal, None, Unsafety::Normal, _) => { |
| 151 | +// // OK |
| 152 | +// } |
| 153 | +// } |
| 154 | +// } |
| 155 | +// } |
| 156 | +// } |
0 commit comments