@@ -1031,32 +1031,29 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivacyVisitor<'a, 'tcx> {
1031
1031
1032
1032
struct SanePrivacyVisitor < ' a , ' tcx : ' a > {
1033
1033
tcx : & ' a ty:: ctxt < ' tcx > ,
1034
- in_fn : bool ,
1034
+ in_block : bool ,
1035
1035
}
1036
1036
1037
1037
impl < ' a , ' tcx , ' v > Visitor < ' v > for SanePrivacyVisitor < ' a , ' tcx > {
1038
1038
fn visit_item ( & mut self , item : & hir:: Item ) {
1039
- if self . in_fn {
1039
+ self . check_sane_privacy ( item) ;
1040
+ if self . in_block {
1040
1041
self . check_all_inherited ( item) ;
1041
- } else {
1042
- self . check_sane_privacy ( item) ;
1043
1042
}
1044
1043
1045
- let in_fn = self . in_fn ;
1046
- let orig_in_fn = replace ( & mut self . in_fn , match item . node {
1047
- hir :: ItemMod ( .. ) => false , // modules turn privacy back on
1048
- _ => in_fn , // otherwise we inherit
1049
- } ) ;
1044
+ let orig_in_block = self . in_block ;
1045
+
1046
+ // Modules turn privacy back on, otherwise we inherit
1047
+ self . in_block = if let hir :: ItemMod ( .. ) = item . node { false } else { orig_in_block } ;
1048
+
1050
1049
visit:: walk_item ( self , item) ;
1051
- self . in_fn = orig_in_fn ;
1050
+ self . in_block = orig_in_block ;
1052
1051
}
1053
1052
1054
- fn visit_fn ( & mut self , fk : visit:: FnKind < ' v > , fd : & ' v hir:: FnDecl ,
1055
- b : & ' v hir:: Block , s : Span , _: ast:: NodeId ) {
1056
- // This catches both functions and methods
1057
- let orig_in_fn = replace ( & mut self . in_fn , true ) ;
1058
- visit:: walk_fn ( self , fk, fd, b, s) ;
1059
- self . in_fn = orig_in_fn;
1053
+ fn visit_block ( & mut self , b : & ' v hir:: Block ) {
1054
+ let orig_in_block = replace ( & mut self . in_block , true ) ;
1055
+ visit:: walk_block ( self , b) ;
1056
+ self . in_block = orig_in_block;
1060
1057
}
1061
1058
}
1062
1059
@@ -1066,89 +1063,75 @@ impl<'a, 'tcx> SanePrivacyVisitor<'a, 'tcx> {
1066
1063
/// anything. In theory these qualifiers wouldn't parse, but that may happen
1067
1064
/// later on down the road...
1068
1065
fn check_sane_privacy ( & self , item : & hir:: Item ) {
1069
- let tcx = self . tcx ;
1070
- let check_inherited = |sp : Span , vis : hir:: Visibility , note : & str | {
1066
+ let check_inherited = |sp, vis, note : & str | {
1071
1067
if vis != hir:: Inherited {
1072
- span_err ! ( tcx. sess, sp, E0449 ,
1073
- "unnecessary visibility qualifier" ) ;
1068
+ span_err ! ( self . tcx. sess, sp, E0449 , "unnecessary visibility qualifier" ) ;
1074
1069
if !note. is_empty ( ) {
1075
- tcx. sess . span_note ( sp, note) ;
1070
+ self . tcx . sess . span_note ( sp, note) ;
1076
1071
}
1077
1072
}
1078
1073
} ;
1074
+
1079
1075
match item. node {
1080
1076
// implementations of traits don't need visibility qualifiers because
1081
1077
// that's controlled by having the trait in scope.
1082
1078
hir:: ItemImpl ( _, _, _, Some ( ..) , _, ref impl_items) => {
1083
1079
check_inherited ( item. span , item. vis ,
1084
- "visibility qualifiers have no effect on trait \
1085
- impls") ;
1080
+ "visibility qualifiers have no effect on trait impls" ) ;
1086
1081
for impl_item in impl_items {
1087
1082
check_inherited ( impl_item. span , impl_item. vis , "" ) ;
1088
1083
}
1089
1084
}
1090
-
1091
- hir:: ItemImpl ( ..) => {
1085
+ hir:: ItemImpl ( _, _, _, None , _, _) => {
1092
1086
check_inherited ( item. span , item. vis ,
1093
1087
"place qualifiers on individual methods instead" ) ;
1094
1088
}
1089
+ hir:: ItemDefaultImpl ( ..) => {
1090
+ check_inherited ( item. span , item. vis ,
1091
+ "visibility qualifiers have no effect on trait impls" ) ;
1092
+ }
1095
1093
hir:: ItemForeignMod ( ..) => {
1096
1094
check_inherited ( item. span , item. vis ,
1097
- "place qualifiers on individual functions \
1098
- instead") ;
1095
+ "place qualifiers on individual functions instead" ) ;
1099
1096
}
1100
-
1101
- hir:: ItemEnum ( ..) |
1102
- hir:: ItemTrait ( ..) | hir:: ItemDefaultImpl ( ..) |
1103
- hir:: ItemConst ( ..) | hir:: ItemStatic ( ..) | hir:: ItemStruct ( ..) |
1104
- hir:: ItemFn ( ..) | hir:: ItemMod ( ..) | hir:: ItemTy ( ..) |
1105
- hir:: ItemExternCrate ( _) | hir:: ItemUse ( _) => { }
1097
+ hir:: ItemStruct ( ..) | hir:: ItemEnum ( ..) | hir:: ItemTrait ( ..) |
1098
+ hir:: ItemConst ( ..) | hir:: ItemStatic ( ..) | hir:: ItemFn ( ..) |
1099
+ hir:: ItemMod ( ..) | hir:: ItemExternCrate ( ..) |
1100
+ hir:: ItemUse ( ..) | hir:: ItemTy ( ..) => { }
1106
1101
}
1107
1102
}
1108
1103
1109
1104
/// When inside of something like a function or a method, visibility has no
1110
1105
/// control over anything so this forbids any mention of any visibility
1111
1106
fn check_all_inherited ( & self , item : & hir:: Item ) {
1112
- let tcx = self . tcx ;
1113
- fn check_inherited ( tcx : & ty:: ctxt , sp : Span , vis : hir:: Visibility ) {
1107
+ let check_inherited = |sp, vis| {
1114
1108
if vis != hir:: Inherited {
1115
- span_err ! ( tcx. sess, sp, E0447 ,
1116
- "visibility has no effect inside functions" ) ;
1117
- }
1118
- }
1119
- let check_struct = |def : & hir:: VariantData | {
1120
- for f in def. fields ( ) {
1121
- match f. node . kind {
1122
- hir:: NamedField ( _, p) => check_inherited ( tcx, f. span , p) ,
1123
- hir:: UnnamedField ( ..) => { }
1124
- }
1109
+ span_err ! ( self . tcx. sess, sp, E0447 ,
1110
+ "visibility has no effect inside functions or block expressions" ) ;
1125
1111
}
1126
1112
} ;
1127
- check_inherited ( tcx, item. span , item. vis ) ;
1113
+
1114
+ check_inherited ( item. span , item. vis ) ;
1128
1115
match item. node {
1129
1116
hir:: ItemImpl ( _, _, _, _, _, ref impl_items) => {
1130
1117
for impl_item in impl_items {
1131
- match impl_item. node {
1132
- hir:: MethodImplItem ( ..) => {
1133
- check_inherited ( tcx, impl_item. span , impl_item. vis ) ;
1134
- }
1135
- _ => { }
1136
- }
1118
+ check_inherited ( impl_item. span , impl_item. vis ) ;
1137
1119
}
1138
1120
}
1139
1121
hir:: ItemForeignMod ( ref fm) => {
1140
- for i in & fm. items {
1141
- check_inherited ( tcx , i . span , i . vis ) ;
1122
+ for fi in & fm. items {
1123
+ check_inherited ( fi . span , fi . vis ) ;
1142
1124
}
1143
1125
}
1144
-
1145
- hir:: ItemStruct ( ref def, _) => check_struct ( def) ,
1146
-
1147
- hir:: ItemEnum ( ..) |
1148
- hir:: ItemExternCrate ( _) | hir:: ItemUse ( _) |
1149
- hir:: ItemTrait ( ..) | hir:: ItemDefaultImpl ( ..) |
1150
- hir:: ItemStatic ( ..) | hir:: ItemConst ( ..) |
1151
- hir:: ItemFn ( ..) | hir:: ItemMod ( ..) | hir:: ItemTy ( ..) => { }
1126
+ hir:: ItemStruct ( ref vdata, _) => {
1127
+ for f in vdata. fields ( ) {
1128
+ check_inherited ( f. span , f. node . kind . visibility ( ) ) ;
1129
+ }
1130
+ }
1131
+ hir:: ItemDefaultImpl ( ..) | hir:: ItemEnum ( ..) | hir:: ItemTrait ( ..) |
1132
+ hir:: ItemConst ( ..) | hir:: ItemStatic ( ..) | hir:: ItemFn ( ..) |
1133
+ hir:: ItemMod ( ..) | hir:: ItemExternCrate ( ..) |
1134
+ hir:: ItemUse ( ..) | hir:: ItemTy ( ..) => { }
1152
1135
}
1153
1136
}
1154
1137
}
@@ -1492,6 +1475,14 @@ pub fn check_crate(tcx: &ty::ctxt,
1492
1475
-> ( ExportedItems , PublicItems ) {
1493
1476
let krate = tcx. map . krate ( ) ;
1494
1477
1478
+ // Sanity check to make sure that all privacy usage and controls are
1479
+ // reasonable.
1480
+ let mut visitor = SanePrivacyVisitor {
1481
+ tcx : tcx,
1482
+ in_block : false ,
1483
+ } ;
1484
+ visit:: walk_crate ( & mut visitor, krate) ;
1485
+
1495
1486
// Figure out who everyone's parent is
1496
1487
let mut visitor = ParentVisitor {
1497
1488
parents : NodeMap ( ) ,
@@ -1509,14 +1500,6 @@ pub fn check_crate(tcx: &ty::ctxt,
1509
1500
} ;
1510
1501
visit:: walk_crate ( & mut visitor, krate) ;
1511
1502
1512
- // Sanity check to make sure that all privacy usage and controls are
1513
- // reasonable.
1514
- let mut visitor = SanePrivacyVisitor {
1515
- in_fn : false ,
1516
- tcx : tcx,
1517
- } ;
1518
- visit:: walk_crate ( & mut visitor, krate) ;
1519
-
1520
1503
tcx. sess . abort_if_errors ( ) ;
1521
1504
1522
1505
// Build up a set of all exported items in the AST. This is a set of all
0 commit comments