@@ -1129,43 +1129,19 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivacyVisitor<'a, 'tcx> {
1129
1129
1130
1130
struct SanePrivacyVisitor < ' a , ' tcx : ' a > {
1131
1131
tcx : & ' a ty:: ctxt < ' tcx > ,
1132
- in_block : bool ,
1133
1132
}
1134
1133
1135
1134
impl < ' a , ' tcx , ' v > Visitor < ' v > for SanePrivacyVisitor < ' a , ' tcx > {
1136
- /// We want to visit items in the context of their containing
1137
- /// module and so forth, so supply a crate for doing a deep walk.
1138
- fn visit_nested_item ( & mut self , item : hir:: ItemId ) {
1139
- self . visit_item ( self . tcx . map . expect_item ( item. id ) )
1140
- }
1141
-
1142
1135
fn visit_item ( & mut self , item : & hir:: Item ) {
1143
1136
self . check_sane_privacy ( item) ;
1144
- if self . in_block {
1145
- self . check_all_inherited ( item) ;
1146
- }
1147
-
1148
- let orig_in_block = self . in_block ;
1149
-
1150
- // Modules turn privacy back on, otherwise we inherit
1151
- self . in_block = if let hir:: ItemMod ( ..) = item. node { false } else { orig_in_block } ;
1152
-
1153
1137
intravisit:: walk_item ( self , item) ;
1154
- self . in_block = orig_in_block;
1155
- }
1156
-
1157
- fn visit_block ( & mut self , b : & ' v hir:: Block ) {
1158
- let orig_in_block = replace ( & mut self . in_block , true ) ;
1159
- intravisit:: walk_block ( self , b) ;
1160
- self . in_block = orig_in_block;
1161
1138
}
1162
1139
}
1163
1140
1164
1141
impl < ' a , ' tcx > SanePrivacyVisitor < ' a , ' tcx > {
1165
- /// Validates all of the visibility qualifiers placed on the item given. This
1166
- /// ensures that there are no extraneous qualifiers that don't actually do
1167
- /// anything. In theory these qualifiers wouldn't parse, but that may happen
1168
- /// later on down the road...
1142
+ /// Validate that items that shouldn't have visibility qualifiers don't have them.
1143
+ /// Such qualifiers can be set by syntax extensions even if the parser doesn't allow them,
1144
+ /// so we check things like variant fields too.
1169
1145
fn check_sane_privacy ( & self , item : & hir:: Item ) {
1170
1146
let check_inherited = |sp, vis, note : & str | {
1171
1147
if vis != hir:: Inherited {
@@ -1179,13 +1155,12 @@ impl<'a, 'tcx> SanePrivacyVisitor<'a, 'tcx> {
1179
1155
} ;
1180
1156
1181
1157
match item. node {
1182
- // implementations of traits don't need visibility qualifiers because
1183
- // that's controlled by having the trait in scope.
1184
1158
hir:: ItemImpl ( _, _, _, Some ( ..) , _, ref impl_items) => {
1185
1159
check_inherited ( item. span , item. vis ,
1186
1160
"visibility qualifiers have no effect on trait impls" ) ;
1187
1161
for impl_item in impl_items {
1188
- check_inherited ( impl_item. span , impl_item. vis , "" ) ;
1162
+ check_inherited ( impl_item. span , impl_item. vis ,
1163
+ "visibility qualifiers have no effect on trait impl items" ) ;
1189
1164
}
1190
1165
}
1191
1166
hir:: ItemImpl ( _, _, _, None , _, _) => {
@@ -1200,41 +1175,15 @@ impl<'a, 'tcx> SanePrivacyVisitor<'a, 'tcx> {
1200
1175
check_inherited ( item. span , item. vis ,
1201
1176
"place qualifiers on individual functions instead" ) ;
1202
1177
}
1203
- hir:: ItemStruct ( ..) | hir:: ItemEnum ( ..) | hir:: ItemTrait ( ..) |
1204
- hir:: ItemConst ( ..) | hir:: ItemStatic ( ..) | hir:: ItemFn ( ..) |
1205
- hir:: ItemMod ( ..) | hir:: ItemExternCrate ( ..) |
1206
- hir:: ItemUse ( ..) | hir:: ItemTy ( ..) => { }
1207
- }
1208
- }
1209
-
1210
- /// When inside of something like a function or a method, visibility has no
1211
- /// control over anything so this forbids any mention of any visibility
1212
- fn check_all_inherited ( & self , item : & hir:: Item ) {
1213
- let check_inherited = |sp, vis| {
1214
- if vis != hir:: Inherited {
1215
- span_err ! ( self . tcx. sess, sp, E0447 ,
1216
- "visibility has no effect inside functions or block expressions" ) ;
1217
- }
1218
- } ;
1219
-
1220
- check_inherited ( item. span , item. vis ) ;
1221
- match item. node {
1222
- hir:: ItemImpl ( _, _, _, _, _, ref impl_items) => {
1223
- for impl_item in impl_items {
1224
- check_inherited ( impl_item. span , impl_item. vis ) ;
1225
- }
1226
- }
1227
- hir:: ItemForeignMod ( ref fm) => {
1228
- for fi in & fm. items {
1229
- check_inherited ( fi. span , fi. vis ) ;
1230
- }
1231
- }
1232
- hir:: ItemStruct ( ref vdata, _) => {
1233
- for f in vdata. fields ( ) {
1234
- check_inherited ( f. span , f. node . kind . visibility ( ) ) ;
1178
+ hir:: ItemEnum ( ref def, _) => {
1179
+ for variant in & def. variants {
1180
+ for field in variant. node . data . fields ( ) {
1181
+ check_inherited ( field. span , field. node . kind . visibility ( ) ,
1182
+ "visibility qualifiers have no effect on variant fields" ) ;
1183
+ }
1235
1184
}
1236
1185
}
1237
- hir:: ItemDefaultImpl ( .. ) | hir :: ItemEnum ( ..) | hir:: ItemTrait ( ..) |
1186
+ hir:: ItemStruct ( ..) | hir:: ItemTrait ( ..) |
1238
1187
hir:: ItemConst ( ..) | hir:: ItemStatic ( ..) | hir:: ItemFn ( ..) |
1239
1188
hir:: ItemMod ( ..) | hir:: ItemExternCrate ( ..) |
1240
1189
hir:: ItemUse ( ..) | hir:: ItemTy ( ..) => { }
@@ -1821,13 +1770,9 @@ pub fn check_crate(tcx: &ty::ctxt,
1821
1770
1822
1771
let krate = tcx. map . krate ( ) ;
1823
1772
1824
- // Sanity check to make sure that all privacy usage and controls are
1825
- // reasonable.
1826
- let mut visitor = SanePrivacyVisitor {
1827
- tcx : tcx,
1828
- in_block : false ,
1829
- } ;
1830
- intravisit:: walk_crate ( & mut visitor, krate) ;
1773
+ // Sanity check to make sure that all privacy usage is reasonable.
1774
+ let mut visitor = SanePrivacyVisitor { tcx : tcx } ;
1775
+ krate. visit_all_items ( & mut visitor) ;
1831
1776
1832
1777
// Figure out who everyone's parent is
1833
1778
let mut visitor = ParentVisitor {
0 commit comments