Skip to content

Commit 6743c65

Browse files
committed
auto merge of #11777 : alexcrichton/rust/issue-11522, r=pnkfelix
See #11522, but the idea is for private structs to have private fields by default, whereas public structs will continue to have public fields by default.
2 parents 838b5a4 + 4d6836f commit 6743c65

35 files changed

+187
-83
lines changed

doc/guide-container.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Reaching the end of the iterator is signalled by returning `None` instead of
9090
# fn main() {}
9191
/// A stream of N zeroes
9292
struct ZeroStream {
93-
priv remaining: uint
93+
remaining: uint
9494
}
9595

9696
impl ZeroStream {
@@ -305,7 +305,7 @@ The `ZeroStream` from earlier can provide an exact lower and upper bound:
305305
# fn main() {}
306306
/// A stream of N zeroes
307307
struct ZeroStream {
308-
priv remaining: uint
308+
remaining: uint
309309
}
310310
311311
impl ZeroStream {

src/libextra/arc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ impl<T:Freeze + Send> Clone for Arc<T> {
148148
****************************************************************************/
149149

150150
#[doc(hidden)]
151-
struct MutexArcInner<T> { priv lock: Mutex, priv failed: bool, priv data: T }
151+
struct MutexArcInner<T> { lock: Mutex, failed: bool, data: T }
152152

153153
/// An Arc with mutable data protected by a blocking mutex.
154154
#[no_freeze]
@@ -312,7 +312,7 @@ impl PoisonOnFail {
312312
****************************************************************************/
313313

314314
#[doc(hidden)]
315-
struct RWArcInner<T> { priv lock: RWLock, priv failed: bool, priv data: T }
315+
struct RWArcInner<T> { lock: RWLock, failed: bool, data: T }
316316
/**
317317
* A dual-mode Arc protected by a reader-writer lock. The data can be accessed
318318
* mutably or immutably, and immutably-accessing tasks may run concurrently.

src/libextra/dlist.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ pub struct DList<T> {
3838
}
3939

4040
type Link<T> = Option<~Node<T>>;
41-
struct Rawlink<T> { priv p: *mut T }
41+
struct Rawlink<T> { p: *mut T }
4242

4343
struct Node<T> {
44-
priv next: Link<T>,
45-
priv prev: Rawlink<Node<T>>,
46-
priv value: T,
44+
next: Link<T>,
45+
prev: Rawlink<Node<T>>,
46+
value: T,
4747
}
4848

4949
/// Double-ended DList iterator

src/libextra/lru_cache.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ use std::to_bytes::Cb;
4343
use std::ptr;
4444
use std::cast;
4545

46-
struct KeyRef<K> { priv k: *K }
46+
struct KeyRef<K> { k: *K }
4747

4848
struct LruEntry<K, V> {
49-
priv key: Option<K>,
50-
priv value: Option<V>,
51-
priv next: *mut LruEntry<K, V>,
52-
priv prev: *mut LruEntry<K, V>,
49+
key: Option<K>,
50+
value: Option<V>,
51+
next: *mut LruEntry<K, V>,
52+
prev: *mut LruEntry<K, V>,
5353
}
5454

5555
/// An LRU Cache.

src/libextra/sync.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -709,8 +709,8 @@ pub struct Barrier {
709709

710710
// The inner state of a double barrier
711711
struct BarrierState {
712-
priv count: uint,
713-
priv generation_id: uint,
712+
count: uint,
713+
generation_id: uint,
714714
}
715715

716716
impl Barrier {

src/librustc/middle/privacy.rs

+66-32
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use std::hashmap::{HashSet, HashMap};
1616
use std::util;
1717

18+
use metadata::csearch;
1819
use middle::resolve;
1920
use middle::ty;
2021
use middle::typeck::{method_map, method_origin, method_param};
@@ -123,22 +124,7 @@ impl Visitor<()> for ParentVisitor {
123124
// While we have the id of the struct definition, go ahead and parent
124125
// all the fields.
125126
for field in s.fields.iter() {
126-
let vis = match field.node.kind {
127-
ast::NamedField(_, vis) => vis,
128-
ast::UnnamedField => continue
129-
};
130-
131-
// Private fields are scoped to this module, so parent them directly
132-
// to the module instead of the struct. This is similar to the case
133-
// of private enum variants.
134-
if vis == ast::Private {
135-
self.parents.insert(field.node.id, self.curparent);
136-
137-
// Otherwise public fields are scoped to the visibility of the
138-
// struct itself
139-
} else {
140-
self.parents.insert(field.node.id, n);
141-
}
127+
self.parents.insert(field.node.id, self.curparent);
142128
}
143129
visit::walk_struct_def(self, s, i, g, n, ())
144130
}
@@ -558,12 +544,48 @@ impl<'a> PrivacyVisitor<'a> {
558544

559545
// Checks that a field is in scope.
560546
// FIXME #6993: change type (and name) from Ident to Name
561-
fn check_field(&mut self, span: Span, id: ast::DefId, ident: ast::Ident) {
547+
fn check_field(&mut self, span: Span, id: ast::DefId, ident: ast::Ident,
548+
enum_id: Option<ast::DefId>) {
562549
let fields = ty::lookup_struct_fields(self.tcx, id);
550+
let struct_vis = if is_local(id) {
551+
match self.tcx.items.get(id.node) {
552+
ast_map::NodeItem(ref it, _) => it.vis,
553+
ast_map::NodeVariant(ref v, ref it, _) => {
554+
if v.node.vis == ast::Inherited {it.vis} else {v.node.vis}
555+
}
556+
_ => {
557+
self.tcx.sess.span_bug(span,
558+
format!("not an item or variant def"));
559+
}
560+
}
561+
} else {
562+
let cstore = self.tcx.sess.cstore;
563+
match enum_id {
564+
Some(enum_id) => {
565+
let v = csearch::get_enum_variants(self.tcx, enum_id);
566+
match v.iter().find(|v| v.id == id) {
567+
Some(variant) => {
568+
if variant.vis == ast::Inherited {
569+
csearch::get_item_visibility(cstore, enum_id)
570+
} else {
571+
variant.vis
572+
}
573+
}
574+
None => {
575+
self.tcx.sess.span_bug(span, "no xcrate variant");
576+
}
577+
}
578+
}
579+
None => csearch::get_item_visibility(cstore, id)
580+
}
581+
};
582+
563583
for field in fields.iter() {
564584
if field.name != ident.name { continue; }
565-
// public fields are public everywhere
566-
if field.vis != ast::Private { break }
585+
// public structs have public fields by default, and private structs
586+
// have private fields by default.
587+
if struct_vis == ast::Public && field.vis != ast::Private { break }
588+
if struct_vis != ast::Public && field.vis == ast::Public { break }
567589
if !is_local(field.id) ||
568590
!self.private_accessible(field.id.node) {
569591
self.tcx.sess.span_err(span, format!("field `{}` is private",
@@ -661,7 +683,7 @@ impl<'a> Visitor<()> for PrivacyVisitor<'a> {
661683
let t = ty::type_autoderef(ty::expr_ty(self.tcx, base));
662684
match ty::get(t).sty {
663685
ty::ty_struct(id, _) => {
664-
self.check_field(expr.span, id, ident);
686+
self.check_field(expr.span, id, ident, None);
665687
}
666688
_ => {}
667689
}
@@ -690,16 +712,18 @@ impl<'a> Visitor<()> for PrivacyVisitor<'a> {
690712
match ty::get(ty::expr_ty(self.tcx, expr)).sty {
691713
ty::ty_struct(id, _) => {
692714
for field in (*fields).iter() {
693-
self.check_field(expr.span, id, field.ident.node);
715+
self.check_field(expr.span, id, field.ident.node,
716+
None);
694717
}
695718
}
696719
ty::ty_enum(_, _) => {
697720
let def_map = self.tcx.def_map.borrow();
698721
match def_map.get().get_copy(&expr.id) {
699-
ast::DefVariant(_, variant_id, _) => {
722+
ast::DefVariant(enum_id, variant_id, _) => {
700723
for field in fields.iter() {
701724
self.check_field(expr.span, variant_id,
702-
field.ident.node);
725+
field.ident.node,
726+
Some(enum_id));
703727
}
704728
}
705729
_ => self.tcx.sess.span_bug(expr.span,
@@ -763,16 +787,17 @@ impl<'a> Visitor<()> for PrivacyVisitor<'a> {
763787
match ty::get(ty::pat_ty(self.tcx, pattern)).sty {
764788
ty::ty_struct(id, _) => {
765789
for field in fields.iter() {
766-
self.check_field(pattern.span, id, field.ident);
790+
self.check_field(pattern.span, id, field.ident,
791+
None);
767792
}
768793
}
769794
ty::ty_enum(_, _) => {
770795
let def_map = self.tcx.def_map.borrow();
771796
match def_map.get().find(&pattern.id) {
772-
Some(&ast::DefVariant(_, variant_id, _)) => {
797+
Some(&ast::DefVariant(enum_id, variant_id, _)) => {
773798
for field in fields.iter() {
774799
self.check_field(pattern.span, variant_id,
775-
field.ident);
800+
field.ident, Some(enum_id));
776801
}
777802
}
778803
_ => self.tcx.sess.span_bug(pattern.span,
@@ -888,15 +913,22 @@ impl SanePrivacyVisitor {
888913
}
889914
}
890915
};
891-
let check_struct = |def: &@ast::StructDef| {
916+
let check_struct = |def: &@ast::StructDef,
917+
vis: ast::Visibility,
918+
parent_vis: Option<ast::Visibility>| {
919+
let public_def = match vis {
920+
ast::Public => true,
921+
ast::Inherited | ast::Private => parent_vis == Some(ast::Public),
922+
};
892923
for f in def.fields.iter() {
893924
match f.node.kind {
894-
ast::NamedField(_, ast::Public) => {
925+
ast::NamedField(_, ast::Public) if public_def => {
895926
tcx.sess.span_err(f.span, "unnecessary `pub` \
896927
visibility");
897928
}
898-
ast::NamedField(_, ast::Private) => {
899-
// Fields should really be private by default...
929+
ast::NamedField(_, ast::Private) if !public_def => {
930+
tcx.sess.span_err(f.span, "unnecessary `priv` \
931+
visibility");
900932
}
901933
ast::NamedField(..) | ast::UnnamedField => {}
902934
}
@@ -951,13 +983,15 @@ impl SanePrivacyVisitor {
951983
}
952984

953985
match v.node.kind {
954-
ast::StructVariantKind(ref s) => check_struct(s),
986+
ast::StructVariantKind(ref s) => {
987+
check_struct(s, v.node.vis, Some(item.vis));
988+
}
955989
ast::TupleVariantKind(..) => {}
956990
}
957991
}
958992
}
959993

960-
ast::ItemStruct(ref def, _) => check_struct(def),
994+
ast::ItemStruct(ref def, _) => check_struct(def, item.vis, None),
961995

962996
ast::ItemTrait(_, _, ref methods) => {
963997
for m in methods.iter() {

src/librustc/middle/ty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ pub type ctxt = @ctxt_;
257257
/// The data structure to keep track of all the information that typechecker
258258
/// generates so that so that it can be reused and doesn't have to be redone
259259
/// later on.
260-
struct ctxt_ {
260+
pub struct ctxt_ {
261261
diag: @syntax::diagnostic::SpanHandler,
262262
interner: RefCell<HashMap<intern_key, ~t_box_>>,
263263
next_id: Cell<uint>,

src/librustc/util/sha2.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ trait FixedBuffer {
109109

110110
/// A FixedBuffer of 64 bytes useful for implementing Sha256 which has a 64 byte blocksize.
111111
struct FixedBuffer64 {
112-
priv buffer: [u8, ..64],
113-
priv buffer_idx: uint,
112+
buffer: [u8, ..64],
113+
buffer_idx: uint,
114114
}
115115

116116
impl FixedBuffer64 {

src/librustpkg/package_source.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ fn prefixes(p: &Path) -> Prefixes {
7878
}
7979

8080
struct Prefixes {
81-
priv components: ~[~str],
82-
priv remaining: ~[~str]
81+
components: ~[~str],
82+
remaining: ~[~str]
8383
}
8484

8585
impl Iterator<(Path, Path)> for Prefixes {

src/librustuv/homing.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ pub trait HomingIO {
126126
/// task back to its appropriate home (if applicable). The field is used to
127127
/// assert that we are where we think we are.
128128
struct HomingMissile {
129-
priv io_home: uint,
129+
io_home: uint,
130130
}
131131

132132
impl HomingMissile {

src/libstd/comm/select.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub struct Handle<'port, T> {
9595
priv port: &'port mut Port<T>,
9696
}
9797

98-
struct Packets { priv cur: *mut Packet }
98+
struct Packets { cur: *mut Packet }
9999

100100
impl Select {
101101
/// Creates a new selection structure. This set is initially empty and

src/libsyntax/parse/parser.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ struct PathSegmentAndBoundSet {
122122
}
123123

124124
/// A path paired with optional type bounds.
125-
struct PathAndBounds {
125+
pub struct PathAndBounds {
126126
path: ast::Path,
127127
bounds: Option<OptVec<TyParamBound>>,
128128
}

src/test/auxiliary/iss.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// part of issue-6919.rs
1414

1515
struct C<'a> {
16-
k: 'a ||,
16+
pub k: 'a ||,
1717
}
1818

1919
fn no_op() { }
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct A {
12+
a: int,
13+
pub b: int,
14+
}
15+
16+
pub struct B {
17+
a: int,
18+
priv b: int,
19+
}

src/test/bench/shootout-meteor.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ fn iterate<'a, T>(x: T, f: 'a |&T| -> T) -> Iterate<'a, T> {
1818
Iterate {f: f, next: x}
1919
}
2020
struct Iterate<'a, T> {
21-
priv f: 'a |&T| -> T,
22-
priv next: T
21+
f: 'a |&T| -> T,
22+
next: T
2323
}
2424
impl<'a, T> Iterator<T> for Iterate<'a, T> {
2525
fn next(&mut self) -> Option<T> {
@@ -35,7 +35,7 @@ enum List<'a, T> {
3535
Cons(T, &'a List<'a, T>)
3636
}
3737
struct ListIterator<'a, T> {
38-
priv cur: &'a List<'a, T>
38+
cur: &'a List<'a, T>
3939
}
4040
impl<'a, T> List<'a, T> {
4141
fn iter(&'a self) -> ListIterator<'a, T> {

src/test/compile-fail/lint-missing-doc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
struct Foo {
2222
a: int,
23-
priv b: int,
23+
b: int,
2424
}
2525

2626
pub struct PubFoo { //~ ERROR: missing documentation
@@ -99,7 +99,7 @@ mod a {
9999
enum Baz {
100100
BazA {
101101
a: int,
102-
priv b: int
102+
b: int
103103
},
104104
BarB
105105
}

src/test/compile-fail/mutable-class-fields-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
struct cat {
12-
priv meows : uint,
12+
meows : uint,
1313

1414
how_hungry : int,
1515
}

0 commit comments

Comments
 (0)