Skip to content

Commit f2a5c7a

Browse files
committed
rustc: Switch struct fields to private by default
This commit switches privacy's checking of fields to have *all* fields be private by default. This does not yet change tuple structs, this only affects structs with named fields. The fallout of this change will follow shortly. RFC: 0004-private-fields cc rust-lang#8122 Closes rust-lang#11809
1 parent a7e057d commit f2a5c7a

File tree

2 files changed

+20
-77
lines changed

2 files changed

+20
-77
lines changed

src/librustc/middle/lint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1506,7 +1506,7 @@ fn check_missing_doc_ty_method(cx: &Context, tm: &ast::TypeMethod) {
15061506

15071507
fn check_missing_doc_struct_field(cx: &Context, sf: &ast::StructField) {
15081508
match sf.node.kind {
1509-
ast::NamedField(_, vis) if vis != ast::Private =>
1509+
ast::NamedField(_, vis) if vis == ast::Public =>
15101510
check_missing_doc_attrs(cx,
15111511
Some(cx.cur_struct_def_id),
15121512
sf.node.attrs.as_slice(),

src/librustc/middle/privacy.rs

+19-76
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
1515
use std::mem::replace;
1616

17-
use metadata::csearch;
1817
use middle::lint;
1918
use middle::resolve;
2019
use middle::ty;
@@ -562,53 +561,10 @@ impl<'a> PrivacyVisitor<'a> {
562561

563562
// Checks that a field is in scope.
564563
// FIXME #6993: change type (and name) from Ident to Name
565-
fn check_field(&mut self, span: Span, id: ast::DefId, ident: ast::Ident,
566-
enum_id: Option<ast::DefId>) {
567-
let fields = ty::lookup_struct_fields(self.tcx, id);
568-
let struct_vis = if is_local(id) {
569-
match self.tcx.map.get(id.node) {
570-
ast_map::NodeItem(ref it) => it.vis,
571-
ast_map::NodeVariant(ref v) => {
572-
if v.node.vis == ast::Inherited {
573-
let parent = self.tcx.map.get_parent(id.node);
574-
self.tcx.map.expect_item(parent).vis
575-
} else {
576-
v.node.vis
577-
}
578-
}
579-
_ => {
580-
self.tcx.sess.span_bug(span,
581-
format!("not an item or variant def"));
582-
}
583-
}
584-
} else {
585-
let cstore = &self.tcx.sess.cstore;
586-
match enum_id {
587-
Some(enum_id) => {
588-
let v = csearch::get_enum_variants(self.tcx, enum_id);
589-
match v.iter().find(|v| v.id == id) {
590-
Some(variant) => {
591-
if variant.vis == ast::Inherited {
592-
csearch::get_item_visibility(cstore, enum_id)
593-
} else {
594-
variant.vis
595-
}
596-
}
597-
None => {
598-
self.tcx.sess.span_bug(span, "no xcrate variant");
599-
}
600-
}
601-
}
602-
None => csearch::get_item_visibility(cstore, id)
603-
}
604-
};
605-
606-
for field in fields.iter() {
564+
fn check_field(&mut self, span: Span, id: ast::DefId, ident: ast::Ident) {
565+
for field in ty::lookup_struct_fields(self.tcx, id).iter() {
607566
if field.name != ident.name { continue; }
608-
// public structs have public fields by default, and private structs
609-
// have private fields by default.
610-
if struct_vis == ast::Public && field.vis != ast::Private { break }
611-
if struct_vis != ast::Public && field.vis == ast::Public { break }
567+
if field.vis == ast::Public { break }
612568
if !is_local(field.id) ||
613569
!self.private_accessible(field.id.node) {
614570
self.tcx.sess.span_err(span,
@@ -770,7 +726,7 @@ impl<'a> Visitor<()> for PrivacyVisitor<'a> {
770726
match ty::get(ty::expr_ty_adjusted(self.tcx, base,
771727
&*self.method_map.borrow())).sty {
772728
ty::ty_struct(id, _) => {
773-
self.check_field(expr.span, id, ident, None);
729+
self.check_field(expr.span, id, ident);
774730
}
775731
_ => {}
776732
}
@@ -793,17 +749,15 @@ impl<'a> Visitor<()> for PrivacyVisitor<'a> {
793749
match ty::get(ty::expr_ty(self.tcx, expr)).sty {
794750
ty::ty_struct(id, _) => {
795751
for field in (*fields).iter() {
796-
self.check_field(expr.span, id, field.ident.node,
797-
None);
752+
self.check_field(expr.span, id, field.ident.node);
798753
}
799754
}
800755
ty::ty_enum(_, _) => {
801756
match self.tcx.def_map.borrow().get_copy(&expr.id) {
802-
ast::DefVariant(enum_id, variant_id, _) => {
757+
ast::DefVariant(_, variant_id, _) => {
803758
for field in fields.iter() {
804759
self.check_field(expr.span, variant_id,
805-
field.ident.node,
806-
Some(enum_id));
760+
field.ident.node);
807761
}
808762
}
809763
_ => self.tcx.sess.span_bug(expr.span,
@@ -867,16 +821,15 @@ impl<'a> Visitor<()> for PrivacyVisitor<'a> {
867821
match ty::get(ty::pat_ty(self.tcx, pattern)).sty {
868822
ty::ty_struct(id, _) => {
869823
for field in fields.iter() {
870-
self.check_field(pattern.span, id, field.ident,
871-
None);
824+
self.check_field(pattern.span, id, field.ident);
872825
}
873826
}
874827
ty::ty_enum(_, _) => {
875828
match self.tcx.def_map.borrow().find(&pattern.id) {
876-
Some(&ast::DefVariant(enum_id, variant_id, _)) => {
829+
Some(&ast::DefVariant(_, variant_id, _)) => {
877830
for field in fields.iter() {
878831
self.check_field(pattern.span, variant_id,
879-
field.ident, Some(enum_id));
832+
field.ident);
880833
}
881834
}
882835
_ => self.tcx.sess.span_bug(pattern.span,
@@ -992,16 +945,10 @@ impl<'a> SanePrivacyVisitor<'a> {
992945
}
993946
}
994947
};
995-
let check_struct = |def: &@ast::StructDef,
996-
vis: ast::Visibility,
997-
parent_vis: Option<ast::Visibility>| {
998-
let public_def = match vis {
999-
ast::Public => true,
1000-
ast::Inherited | ast::Private => parent_vis == Some(ast::Public),
1001-
};
948+
let check_struct = |def: &@ast::StructDef| {
1002949
for f in def.fields.iter() {
1003-
match f.node.kind {
1004-
ast::NamedField(_, ast::Private) if !public_def => {
950+
match f.node.kind {
951+
ast::NamedField(_, ast::Private) => {
1005952
tcx.sess.span_err(f.span, "unnecessary `priv` \
1006953
visibility");
1007954
}
@@ -1058,15 +1005,13 @@ impl<'a> SanePrivacyVisitor<'a> {
10581005
}
10591006

10601007
match v.node.kind {
1061-
ast::StructVariantKind(ref s) => {
1062-
check_struct(s, v.node.vis, Some(item.vis));
1063-
}
1008+
ast::StructVariantKind(ref s) => check_struct(s),
10641009
ast::TupleVariantKind(..) => {}
10651010
}
10661011
}
10671012
}
10681013

1069-
ast::ItemStruct(ref def, _) => check_struct(def, item.vis, None),
1014+
ast::ItemStruct(ref def, _) => check_struct(def),
10701015

10711016
ast::ItemTrait(_, _, ref methods) => {
10721017
for m in methods.iter() {
@@ -1372,12 +1317,10 @@ impl<'a> Visitor<()> for VisiblePrivateTypesVisitor<'a> {
13721317

13731318
fn visit_struct_field(&mut self, s: &ast::StructField, _: ()) {
13741319
match s.node.kind {
1375-
// the only way to get here is by being inside a public
1376-
// struct/enum variant, so the only way to have a private
1377-
// field is with an explicit `priv`.
1378-
ast::NamedField(_, ast::Private) => {}
1379-
1380-
_ => visit::walk_struct_field(self, s, ())
1320+
ast::NamedField(_, ast::Public) => {
1321+
visit::walk_struct_field(self, s, ());
1322+
}
1323+
_ => {}
13811324
}
13821325
}
13831326

0 commit comments

Comments
 (0)