@@ -27,55 +27,66 @@ struct UnsafetyChecker<'cx, 'tcx:'cx> {
27
27
tcx : & ' cx ty:: ctxt < ' tcx >
28
28
}
29
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
- span_err ! ( self . tcx. sess, item. span, E0197 ,
41
- "inherent impls cannot be declared as unsafe" ) ;
42
- }
43
- }
30
+ impl < ' cx , ' tcx , ' v > UnsafetyChecker < ' cx , ' tcx > {
31
+ fn check_unsafety_coherence ( & mut self , item : & ' v ast:: Item ,
32
+ unsafety : ast:: Unsafety ,
33
+ polarity : ast:: ImplPolarity ) {
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
+ span_err ! ( self . tcx. sess, item. span, E0197 ,
41
+ "inherent impls cannot be declared as unsafe" ) ;
44
42
}
43
+ }
44
+ }
45
45
46
- Some ( trait_ref) => {
47
- let trait_def = ty:: lookup_trait_def ( self . tcx , trait_ref. def_id ) ;
48
- match ( trait_def. unsafety , unsafety, polarity) {
49
- ( ast:: Unsafety :: Unsafe ,
50
- ast:: Unsafety :: Unsafe , ast:: ImplPolarity :: Negative ) => {
51
- span_err ! ( self . tcx. sess, item. span, E0198 ,
52
- "negative implementations are not unsafe" ) ;
53
- }
46
+ Some ( trait_ref) => {
47
+ let trait_def = ty:: lookup_trait_def ( self . tcx , trait_ref. def_id ) ;
48
+ match ( trait_def. unsafety , unsafety, polarity) {
49
+ ( ast:: Unsafety :: Unsafe ,
50
+ ast:: Unsafety :: Unsafe , ast:: ImplPolarity :: Negative ) => {
51
+ span_err ! ( self . tcx. sess, item. span, E0198 ,
52
+ "negative implementations are not unsafe" ) ;
53
+ }
54
54
55
- ( ast:: Unsafety :: Normal , ast:: Unsafety :: Unsafe , _) => {
56
- span_err ! ( self . tcx. sess, item. span, E0199 ,
57
- "implementing the trait `{}` is not unsafe" ,
58
- trait_ref. user_string( self . tcx) ) ;
59
- }
55
+ ( ast:: Unsafety :: Normal , ast:: Unsafety :: Unsafe , _) => {
56
+ span_err ! ( self . tcx. sess, item. span, E0199 ,
57
+ "implementing the trait `{}` is not unsafe" ,
58
+ trait_ref. user_string( self . tcx) ) ;
59
+ }
60
60
61
- ( ast:: Unsafety :: Unsafe ,
62
- ast:: Unsafety :: Normal , ast:: ImplPolarity :: Positive ) => {
63
- span_err ! ( self . tcx. sess, item. span, E0200 ,
64
- "the trait `{}` requires an `unsafe impl` declaration" ,
65
- trait_ref. user_string( self . tcx) ) ;
66
- }
61
+ ( ast:: Unsafety :: Unsafe ,
62
+ ast:: Unsafety :: Normal , ast:: ImplPolarity :: Positive ) => {
63
+ span_err ! ( self . tcx. sess, item. span, E0200 ,
64
+ "the trait `{}` requires an `unsafe impl` declaration" ,
65
+ trait_ref. user_string( self . tcx) ) ;
66
+ }
67
67
68
- ( ast:: Unsafety :: Unsafe ,
69
- ast:: Unsafety :: Normal , ast:: ImplPolarity :: Negative ) |
70
- ( ast:: Unsafety :: Unsafe ,
71
- ast:: Unsafety :: Unsafe , ast:: ImplPolarity :: Positive ) |
72
- ( ast:: Unsafety :: Normal , ast:: Unsafety :: Normal , _) => {
73
- /* OK */
74
- }
75
- }
68
+ ( ast:: Unsafety :: Unsafe ,
69
+ ast:: Unsafety :: Normal , ast:: ImplPolarity :: Negative ) |
70
+ ( ast:: Unsafety :: Unsafe ,
71
+ ast:: Unsafety :: Unsafe , ast:: ImplPolarity :: Positive ) |
72
+ ( ast:: Unsafety :: Normal , ast:: Unsafety :: Normal , _) => {
73
+ /* OK */
76
74
}
77
75
}
78
76
}
77
+ }
78
+ }
79
+ }
80
+
81
+ impl < ' cx , ' tcx , ' v > visit:: Visitor < ' v > for UnsafetyChecker < ' cx , ' tcx > {
82
+ fn visit_item ( & mut self , item : & ' v ast:: Item ) {
83
+ match item. node {
84
+ ast:: ItemDefaultImpl ( unsafety, _) => {
85
+ self . check_unsafety_coherence ( item, unsafety, ast:: ImplPolarity :: Positive ) ;
86
+ }
87
+ ast:: ItemImpl ( unsafety, polarity, _, _, _, _) => {
88
+ self . check_unsafety_coherence ( item, unsafety, polarity) ;
89
+ }
79
90
_ => { }
80
91
}
81
92
0 commit comments