Skip to content

Commit

Permalink
auto merge of #13547 : alexcrichton/rust/remove-priv, r=huonw
Browse files Browse the repository at this point in the history
  • Loading branch information
bors committed Apr 16, 2014
2 parents 12391df + a0347d5 commit 72869b6
Show file tree
Hide file tree
Showing 32 changed files with 125 additions and 269 deletions.
4 changes: 2 additions & 2 deletions src/doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -1586,10 +1586,10 @@ pub struct Bar {
field: int
}
// Declare a public enum with public and private variants
// Declare a public enum with two public variants
pub enum State {
PubliclyAccessibleState,
priv PrivatelyAccessibleState
PubliclyAccessibleState2,
}
~~~~

Expand Down
4 changes: 2 additions & 2 deletions src/liblibc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ pub mod types {
*/
#[repr(u8)]
pub enum c_void {
priv variant1,
priv variant2
__variant1,
__variant2,
}
pub enum FILE {}
pub enum fpos_t {}
Expand Down
36 changes: 23 additions & 13 deletions src/librand/distributions/gamma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ use super::{IndependentSample, Sample, Exp};
/// for Generating Gamma Variables" *ACM Trans. Math. Softw.* 26, 3
/// (September 2000),
/// 363-372. DOI:[10.1145/358407.358414](http://doi.acm.org/10.1145/358407.358414)
pub enum Gamma {
priv Large(GammaLargeShape),
priv One(Exp),
priv Small(GammaSmallShape)
pub struct Gamma {
repr: GammaRepr,
}

enum GammaRepr {
Large(GammaLargeShape),
One(Exp),
Small(GammaSmallShape)
}

// These two helpers could be made public, but saving the
Expand Down Expand Up @@ -90,11 +94,12 @@ impl Gamma {
assert!(shape > 0.0, "Gamma::new called with shape <= 0");
assert!(scale > 0.0, "Gamma::new called with scale <= 0");

match shape {
let repr = match shape {
1.0 => One(Exp::new(1.0 / scale)),
0.0 .. 1.0 => Small(GammaSmallShape::new_raw(shape, scale)),
_ => Large(GammaLargeShape::new_raw(shape, scale))
}
};
Gamma { repr: repr }
}
}

Expand Down Expand Up @@ -131,7 +136,7 @@ impl Sample<f64> for GammaLargeShape {

impl IndependentSample<f64> for Gamma {
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
match *self {
match self.repr {
Small(ref g) => g.ind_sample(rng),
One(ref g) => g.ind_sample(rng),
Large(ref g) => g.ind_sample(rng),
Expand Down Expand Up @@ -183,32 +188,37 @@ impl IndependentSample<f64> for GammaLargeShape {
/// let v = chi.ind_sample(&mut rand::task_rng());
/// println!("{} is from a χ²(11) distribution", v)
/// ```
pub enum ChiSquared {
pub struct ChiSquared {
repr: ChiSquaredRepr,
}

enum ChiSquaredRepr {
// k == 1, Gamma(alpha, ..) is particularly slow for alpha < 1,
// e.g. when alpha = 1/2 as it would be for this case, so special-
// casing and using the definition of N(0,1)^2 is faster.
priv DoFExactlyOne,
priv DoFAnythingElse(Gamma)
DoFExactlyOne,
DoFAnythingElse(Gamma),
}

impl ChiSquared {
/// Create a new chi-squared distribution with degrees-of-freedom
/// `k`. Fails if `k < 0`.
pub fn new(k: f64) -> ChiSquared {
if k == 1.0 {
let repr = if k == 1.0 {
DoFExactlyOne
} else {
assert!(k > 0.0, "ChiSquared::new called with `k` < 0");
DoFAnythingElse(Gamma::new(0.5 * k, 2.0))
}
};
ChiSquared { repr: repr }
}
}
impl Sample<f64> for ChiSquared {
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
}
impl IndependentSample<f64> for ChiSquared {
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
match *self {
match self.repr {
DoFExactlyOne => {
// k == 1 => N(0,1)^2
let StandardNormal(norm) = rng.gen::<StandardNormal>();
Expand Down
8 changes: 2 additions & 6 deletions src/librustc/metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ enum Family {
Trait, // I
Struct, // S
PublicField, // g
PrivateField, // j
InheritedField // N
}

Expand All @@ -149,7 +148,6 @@ fn item_family(item: ebml::Doc) -> Family {
'I' => Trait,
'S' => Struct,
'g' => PublicField,
'j' => PrivateField,
'N' => InheritedField,
c => fail!("unexpected family char: {}", c)
}
Expand All @@ -161,7 +159,6 @@ fn item_visibility(item: ebml::Doc) -> ast::Visibility {
Some(visibility_doc) => {
match reader::doc_as_u8(visibility_doc) as char {
'y' => ast::Public,
'n' => ast::Private,
'i' => ast::Inherited,
_ => fail!("unknown visibility character")
}
Expand Down Expand Up @@ -364,7 +361,7 @@ fn item_to_def_like(item: ebml::Doc, did: ast::DefId, cnum: ast::CrateNum)
Trait => DlDef(ast::DefTrait(did)),
Enum => DlDef(ast::DefTy(did)),
Impl => DlImpl(did),
PublicField | PrivateField | InheritedField => DlField,
PublicField | InheritedField => DlField,
}
}

Expand Down Expand Up @@ -962,7 +959,6 @@ pub fn get_item_attrs(cdata: Cmd,
fn struct_field_family_to_visibility(family: Family) -> ast::Visibility {
match family {
PublicField => ast::Public,
PrivateField => ast::Private,
InheritedField => ast::Inherited,
_ => fail!()
}
Expand All @@ -975,7 +971,7 @@ pub fn get_struct_fields(intr: Rc<IdentInterner>, cdata: Cmd, id: ast::NodeId)
let mut result = Vec::new();
reader::tagged_docs(item, tag_item_field, |an_item| {
let f = item_family(an_item);
if f == PublicField || f == PrivateField || f == InheritedField {
if f == PublicField || f == InheritedField {
// FIXME #6993: name should be of type Name, not Ident
let name = item_name(&*intr, an_item);
let did = item_def_id(an_item, cdata);
Expand Down
2 changes: 0 additions & 2 deletions src/librustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,6 @@ fn encode_struct_field_family(ebml_w: &mut Encoder,
visibility: Visibility) {
encode_family(ebml_w, match visibility {
Public => 'g',
Private => 'j',
Inherited => 'N'
});
}
Expand All @@ -616,7 +615,6 @@ fn encode_visibility(ebml_w: &mut Encoder, visibility: Visibility) {
ebml_w.start_tag(tag_items_data_item_visibility);
let ch = match visibility {
Public => 'y',
Private => 'n',
Inherited => 'i',
};
ebml_w.wr_str(str::from_char(ch));
Expand Down
80 changes: 10 additions & 70 deletions src/librustc/middle/privacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,10 @@ impl Visitor<()> for ParentVisitor {
// they inherit privacy
ast::ItemEnum(ref def, _) => {
for variant in def.variants.iter() {
// If variants are private, then their logical "parent" is
// the enclosing module because everyone in the enclosing
// module can still use the private variant
if variant.node.vis == ast::Private {
self.parents.insert(variant.node.id, self.curparent);

// Otherwise, if the variant is public, then the parent is
// considered the enclosing enum because the enum will
// dictate the privacy visibility of this variant instead.
} else {
self.parents.insert(variant.node.id, item.id);
}
// The parent is considered the enclosing enum because the
// enum will dictate the privacy visibility of this variant
// instead.
self.parents.insert(variant.node.id, item.id);
}
}

Expand Down Expand Up @@ -224,9 +216,7 @@ impl<'a> Visitor<()> for EmbargoVisitor<'a> {
// public all variants are public unless they're explicitly priv
ast::ItemEnum(ref def, _) if public_first => {
for variant in def.variants.iter() {
if variant.node.vis != ast::Private {
self.exported_items.insert(variant.node.id);
}
self.exported_items.insert(variant.node.id);
}
}

Expand Down Expand Up @@ -462,10 +452,7 @@ impl<'a> PrivacyVisitor<'a> {
Some(ast_map::NodeForeignItem(_)) => {
self.tcx.map.get_foreign_vis(closest_private_id)
}
Some(ast_map::NodeVariant(ref v)) => {
// sadly enum variants still inherit visibility, so only
// break out of this is explicitly private
if v.node.vis == ast::Private { break }
Some(ast_map::NodeVariant(..)) => {
ast::Public // need to move up a level (to the enum)
}
_ => ast::Public,
Expand Down Expand Up @@ -997,10 +984,6 @@ impl<'a> Visitor<()> for SanePrivacyVisitor<'a> {
fn visit_view_item(&mut self, i: &ast::ViewItem, _: ()) {
match i.vis {
ast::Inherited => {}
ast::Private => {
self.tcx.sess.span_err(i.span, "unnecessary visibility \
qualifier");
}
ast::Public => {
if self.in_fn {
self.tcx.sess.span_err(i.span, "unnecessary `pub`, imports \
Expand Down Expand Up @@ -1036,25 +1019,6 @@ impl<'a> SanePrivacyVisitor<'a> {
}
}
};
let check_not_priv = |sp: Span, vis: ast::Visibility, note: &str| {
if vis == ast::Private {
tcx.sess.span_err(sp, "unnecessary `priv` qualifier");
if note.len() > 0 {
tcx.sess.span_note(sp, note);
}
}
};
let check_struct = |def: &@ast::StructDef| {
for f in def.fields.iter() {
match f.node.kind {
ast::NamedField(_, ast::Private) => {
tcx.sess.span_err(f.span, "unnecessary `priv` \
visibility");
}
ast::NamedField(..) | ast::UnnamedField(..) => {}
}
}
};
match item.node {
// implementations of traits don't need visibility qualifiers because
// that's controlled by having the trait in scope.
Expand All @@ -1067,22 +1031,14 @@ impl<'a> SanePrivacyVisitor<'a> {
}
}

ast::ItemImpl(_, _, _, ref methods) => {
ast::ItemImpl(..) => {
check_inherited(item.span, item.vis,
"place qualifiers on individual methods instead");
for i in methods.iter() {
check_not_priv(i.span, i.vis, "functions are private by \
default");
}
}
ast::ItemForeignMod(ref fm) => {
ast::ItemForeignMod(..) => {
check_inherited(item.span, item.vis,
"place qualifiers on individual functions \
instead");
for i in fm.items.iter() {
check_not_priv(i.span, i.vis, "functions are private by \
default");
}
}

ast::ItemEnum(ref def, _) => {
Expand All @@ -1094,24 +1050,11 @@ impl<'a> SanePrivacyVisitor<'a> {
visibility");
}
}
ast::Private => {
if item.vis != ast::Public {
tcx.sess.span_err(v.span, "unnecessary `priv` \
visibility");
}
}
ast::Inherited => {}
}

match v.node.kind {
ast::StructVariantKind(ref s) => check_struct(s),
ast::TupleVariantKind(..) => {}
}
}
}

ast::ItemStruct(ref def, _) => check_struct(def),

ast::ItemTrait(_, _, ref methods) => {
for m in methods.iter() {
match *m {
Expand All @@ -1124,12 +1067,9 @@ impl<'a> SanePrivacyVisitor<'a> {
}
}

ast::ItemStatic(..) |
ast::ItemStatic(..) | ast::ItemStruct(..) |
ast::ItemFn(..) | ast::ItemMod(..) | ast::ItemTy(..) |
ast::ItemMac(..) => {
check_not_priv(item.span, item.vis, "items are private by \
default");
}
ast::ItemMac(..) => {}
}
}

Expand Down
11 changes: 3 additions & 8 deletions src/librustc/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1421,12 +1421,8 @@ impl<'a> Resolver<'a> {
variant: &Variant,
item_id: DefId,
parent: ReducedGraphParent,
parent_public: bool) {
is_public: bool) {
let ident = variant.node.name;
// FIXME: this is unfortunate to have to do this privacy calculation
// here. This should be living in middle::privacy, but it's
// necessary to keep around in some form becaues of glob imports...
let is_public = parent_public && variant.node.vis != ast::Private;

match variant.node.kind {
TupleVariantKind(_) => {
Expand Down Expand Up @@ -1668,12 +1664,11 @@ impl<'a> Resolver<'a> {
// We assume the parent is visible, or else we wouldn't have seen
// it. Also variants are public-by-default if the parent was also
// public.
let is_public = vis != ast::Private;
if is_struct {
child_name_bindings.define_type(def, DUMMY_SP, is_public);
child_name_bindings.define_type(def, DUMMY_SP, true);
self.structs.insert(variant_id);
} else {
child_name_bindings.define_value(def, DUMMY_SP, is_public);
child_name_bindings.define_value(def, DUMMY_SP, true);
}
}
DefFn(..) | DefStaticMethod(..) | DefStatic(..) => {
Expand Down
Loading

0 comments on commit 72869b6

Please sign in to comment.