Skip to content

Commit fa42726

Browse files
committed
Some drive-by improvements to SanePrivacyVisitor
Check that variant fields are not marked public by syntax extensions
1 parent df48216 commit fa42726

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/librustc_privacy/lib.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -1139,10 +1139,9 @@ impl<'a, 'tcx, 'v> Visitor<'v> for SanePrivacyVisitor<'a, 'tcx> {
11391139
}
11401140

11411141
impl<'a, 'tcx> SanePrivacyVisitor<'a, 'tcx> {
1142-
/// Validates all of the visibility qualifiers placed on the item given. This
1143-
/// ensures that there are no extraneous qualifiers that don't actually do
1144-
/// anything. In theory these qualifiers wouldn't parse, but that may happen
1145-
/// 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.
11461145
fn check_sane_privacy(&self, item: &hir::Item) {
11471146
let check_inherited = |sp, vis, note: &str| {
11481147
if vis != hir::Inherited {
@@ -1156,13 +1155,12 @@ impl<'a, 'tcx> SanePrivacyVisitor<'a, 'tcx> {
11561155
};
11571156

11581157
match item.node {
1159-
// implementations of traits don't need visibility qualifiers because
1160-
// that's controlled by having the trait in scope.
11611158
hir::ItemImpl(_, _, _, Some(..), _, ref impl_items) => {
11621159
check_inherited(item.span, item.vis,
11631160
"visibility qualifiers have no effect on trait impls");
11641161
for impl_item in impl_items {
1165-
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");
11661164
}
11671165
}
11681166
hir::ItemImpl(_, _, _, None, _, _) => {
@@ -1177,7 +1175,15 @@ impl<'a, 'tcx> SanePrivacyVisitor<'a, 'tcx> {
11771175
check_inherited(item.span, item.vis,
11781176
"place qualifiers on individual functions instead");
11791177
}
1180-
hir::ItemStruct(..) | hir::ItemEnum(..) | hir::ItemTrait(..) |
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+
}
1184+
}
1185+
}
1186+
hir::ItemStruct(..) | hir::ItemTrait(..) |
11811187
hir::ItemConst(..) | hir::ItemStatic(..) | hir::ItemFn(..) |
11821188
hir::ItemMod(..) | hir::ItemExternCrate(..) |
11831189
hir::ItemUse(..) | hir::ItemTy(..) => {}
@@ -1764,8 +1770,7 @@ pub fn check_crate(tcx: &ty::ctxt,
17641770

17651771
let krate = tcx.map.krate();
17661772

1767-
// Sanity check to make sure that all privacy usage and controls are
1768-
// reasonable.
1773+
// Sanity check to make sure that all privacy usage is reasonable.
17691774
let mut visitor = SanePrivacyVisitor { tcx: tcx };
17701775
krate.visit_all_items(&mut visitor);
17711776

0 commit comments

Comments
 (0)