Skip to content

Commit 5cfbc0e

Browse files
committed
rustc: Remove private enum variants
This removes the `priv` keyword from the language and removes private enum variants as a result. The remaining use cases of private enum variants were all updated to be a struct with one private field that is a private enum. RFC: 0006-remove-priv Closes #13535
1 parent 83351fa commit 5cfbc0e

27 files changed

+31
-198
lines changed

src/doc/rust.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,10 +1586,10 @@ pub struct Bar {
15861586
field: int
15871587
}
15881588
1589-
// Declare a public enum with public and private variants
1589+
// Declare a public enum with two public variants
15901590
pub enum State {
15911591
PubliclyAccessibleState,
1592-
priv PrivatelyAccessibleState
1592+
PubliclyAccessibleState2,
15931593
}
15941594
~~~~
15951595

src/librustc/metadata/decoder.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ enum Family {
124124
Trait, // I
125125
Struct, // S
126126
PublicField, // g
127-
PrivateField, // j
128127
InheritedField // N
129128
}
130129

@@ -149,7 +148,6 @@ fn item_family(item: ebml::Doc) -> Family {
149148
'I' => Trait,
150149
'S' => Struct,
151150
'g' => PublicField,
152-
'j' => PrivateField,
153151
'N' => InheritedField,
154152
c => fail!("unexpected family char: {}", c)
155153
}
@@ -161,7 +159,6 @@ fn item_visibility(item: ebml::Doc) -> ast::Visibility {
161159
Some(visibility_doc) => {
162160
match reader::doc_as_u8(visibility_doc) as char {
163161
'y' => ast::Public,
164-
'n' => ast::Private,
165162
'i' => ast::Inherited,
166163
_ => fail!("unknown visibility character")
167164
}
@@ -364,7 +361,7 @@ fn item_to_def_like(item: ebml::Doc, did: ast::DefId, cnum: ast::CrateNum)
364361
Trait => DlDef(ast::DefTrait(did)),
365362
Enum => DlDef(ast::DefTy(did)),
366363
Impl => DlImpl(did),
367-
PublicField | PrivateField | InheritedField => DlField,
364+
PublicField | InheritedField => DlField,
368365
}
369366
}
370367

@@ -962,7 +959,6 @@ pub fn get_item_attrs(cdata: Cmd,
962959
fn struct_field_family_to_visibility(family: Family) -> ast::Visibility {
963960
match family {
964961
PublicField => ast::Public,
965-
PrivateField => ast::Private,
966962
InheritedField => ast::Inherited,
967963
_ => fail!()
968964
}
@@ -975,7 +971,7 @@ pub fn get_struct_fields(intr: Rc<IdentInterner>, cdata: Cmd, id: ast::NodeId)
975971
let mut result = Vec::new();
976972
reader::tagged_docs(item, tag_item_field, |an_item| {
977973
let f = item_family(an_item);
978-
if f == PublicField || f == PrivateField || f == InheritedField {
974+
if f == PublicField || f == InheritedField {
979975
// FIXME #6993: name should be of type Name, not Ident
980976
let name = item_name(&*intr, an_item);
981977
let did = item_def_id(an_item, cdata);

src/librustc/metadata/encoder.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,6 @@ fn encode_struct_field_family(ebml_w: &mut Encoder,
607607
visibility: Visibility) {
608608
encode_family(ebml_w, match visibility {
609609
Public => 'g',
610-
Private => 'j',
611610
Inherited => 'N'
612611
});
613612
}
@@ -616,7 +615,6 @@ fn encode_visibility(ebml_w: &mut Encoder, visibility: Visibility) {
616615
ebml_w.start_tag(tag_items_data_item_visibility);
617616
let ch = match visibility {
618617
Public => 'y',
619-
Private => 'n',
620618
Inherited => 'i',
621619
};
622620
ebml_w.wr_str(str::from_char(ch));

src/librustc/middle/privacy.rs

Lines changed: 10 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,10 @@ impl Visitor<()> for ParentVisitor {
6767
// they inherit privacy
6868
ast::ItemEnum(ref def, _) => {
6969
for variant in def.variants.iter() {
70-
// If variants are private, then their logical "parent" is
71-
// the enclosing module because everyone in the enclosing
72-
// module can still use the private variant
73-
if variant.node.vis == ast::Private {
74-
self.parents.insert(variant.node.id, self.curparent);
75-
76-
// Otherwise, if the variant is public, then the parent is
77-
// considered the enclosing enum because the enum will
78-
// dictate the privacy visibility of this variant instead.
79-
} else {
80-
self.parents.insert(variant.node.id, item.id);
81-
}
70+
// The parent is considered the enclosing enum because the
71+
// enum will dictate the privacy visibility of this variant
72+
// instead.
73+
self.parents.insert(variant.node.id, item.id);
8274
}
8375
}
8476

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

@@ -462,10 +452,7 @@ impl<'a> PrivacyVisitor<'a> {
462452
Some(ast_map::NodeForeignItem(_)) => {
463453
self.tcx.map.get_foreign_vis(closest_private_id)
464454
}
465-
Some(ast_map::NodeVariant(ref v)) => {
466-
// sadly enum variants still inherit visibility, so only
467-
// break out of this is explicitly private
468-
if v.node.vis == ast::Private { break }
455+
Some(ast_map::NodeVariant(..)) => {
469456
ast::Public // need to move up a level (to the enum)
470457
}
471458
_ => ast::Public,
@@ -997,10 +984,6 @@ impl<'a> Visitor<()> for SanePrivacyVisitor<'a> {
997984
fn visit_view_item(&mut self, i: &ast::ViewItem, _: ()) {
998985
match i.vis {
999986
ast::Inherited => {}
1000-
ast::Private => {
1001-
self.tcx.sess.span_err(i.span, "unnecessary visibility \
1002-
qualifier");
1003-
}
1004987
ast::Public => {
1005988
if self.in_fn {
1006989
self.tcx.sess.span_err(i.span, "unnecessary `pub`, imports \
@@ -1036,25 +1019,6 @@ impl<'a> SanePrivacyVisitor<'a> {
10361019
}
10371020
}
10381021
};
1039-
let check_not_priv = |sp: Span, vis: ast::Visibility, note: &str| {
1040-
if vis == ast::Private {
1041-
tcx.sess.span_err(sp, "unnecessary `priv` qualifier");
1042-
if note.len() > 0 {
1043-
tcx.sess.span_note(sp, note);
1044-
}
1045-
}
1046-
};
1047-
let check_struct = |def: &@ast::StructDef| {
1048-
for f in def.fields.iter() {
1049-
match f.node.kind {
1050-
ast::NamedField(_, ast::Private) => {
1051-
tcx.sess.span_err(f.span, "unnecessary `priv` \
1052-
visibility");
1053-
}
1054-
ast::NamedField(..) | ast::UnnamedField(..) => {}
1055-
}
1056-
}
1057-
};
10581022
match item.node {
10591023
// implementations of traits don't need visibility qualifiers because
10601024
// that's controlled by having the trait in scope.
@@ -1067,22 +1031,14 @@ impl<'a> SanePrivacyVisitor<'a> {
10671031
}
10681032
}
10691033

1070-
ast::ItemImpl(_, _, _, ref methods) => {
1034+
ast::ItemImpl(..) => {
10711035
check_inherited(item.span, item.vis,
10721036
"place qualifiers on individual methods instead");
1073-
for i in methods.iter() {
1074-
check_not_priv(i.span, i.vis, "functions are private by \
1075-
default");
1076-
}
10771037
}
1078-
ast::ItemForeignMod(ref fm) => {
1038+
ast::ItemForeignMod(..) => {
10791039
check_inherited(item.span, item.vis,
10801040
"place qualifiers on individual functions \
10811041
instead");
1082-
for i in fm.items.iter() {
1083-
check_not_priv(i.span, i.vis, "functions are private by \
1084-
default");
1085-
}
10861042
}
10871043

10881044
ast::ItemEnum(ref def, _) => {
@@ -1094,24 +1050,11 @@ impl<'a> SanePrivacyVisitor<'a> {
10941050
visibility");
10951051
}
10961052
}
1097-
ast::Private => {
1098-
if item.vis != ast::Public {
1099-
tcx.sess.span_err(v.span, "unnecessary `priv` \
1100-
visibility");
1101-
}
1102-
}
11031053
ast::Inherited => {}
11041054
}
1105-
1106-
match v.node.kind {
1107-
ast::StructVariantKind(ref s) => check_struct(s),
1108-
ast::TupleVariantKind(..) => {}
1109-
}
11101055
}
11111056
}
11121057

1113-
ast::ItemStruct(ref def, _) => check_struct(def),
1114-
11151058
ast::ItemTrait(_, _, ref methods) => {
11161059
for m in methods.iter() {
11171060
match *m {
@@ -1124,12 +1067,9 @@ impl<'a> SanePrivacyVisitor<'a> {
11241067
}
11251068
}
11261069

1127-
ast::ItemStatic(..) |
1070+
ast::ItemStatic(..) | ast::ItemStruct(..) |
11281071
ast::ItemFn(..) | ast::ItemMod(..) | ast::ItemTy(..) |
1129-
ast::ItemMac(..) => {
1130-
check_not_priv(item.span, item.vis, "items are private by \
1131-
default");
1132-
}
1072+
ast::ItemMac(..) => {}
11331073
}
11341074
}
11351075

src/librustc/middle/resolve.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,12 +1421,8 @@ impl<'a> Resolver<'a> {
14211421
variant: &Variant,
14221422
item_id: DefId,
14231423
parent: ReducedGraphParent,
1424-
parent_public: bool) {
1424+
is_public: bool) {
14251425
let ident = variant.node.name;
1426-
// FIXME: this is unfortunate to have to do this privacy calculation
1427-
// here. This should be living in middle::privacy, but it's
1428-
// necessary to keep around in some form becaues of glob imports...
1429-
let is_public = parent_public && variant.node.vis != ast::Private;
14301426

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

src/librustdoc/doctree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl Module {
4141
Module {
4242
name : name,
4343
id: 0,
44-
vis: ast::Private,
44+
vis: ast::Inherited,
4545
where: syntax::codemap::DUMMY_SP,
4646
attrs : Vec::new(),
4747
structs : Vec::new(),

src/librustdoc/html/format.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,6 @@ impl fmt::Show for VisSpace {
507507
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
508508
match self.get() {
509509
Some(ast::Public) => write!(f.buf, "pub "),
510-
Some(ast::Private) => write!(f.buf, "priv "),
511510
Some(ast::Inherited) | None => Ok(())
512511
}
513512
}

src/libsyntax/ast.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,15 +1038,14 @@ pub struct TraitRef {
10381038
#[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
10391039
pub enum Visibility {
10401040
Public,
1041-
Private,
10421041
Inherited,
10431042
}
10441043

10451044
impl Visibility {
10461045
pub fn inherit_from(&self, parent_visibility: Visibility) -> Visibility {
10471046
match self {
10481047
&Inherited => parent_visibility,
1049-
&Public | &Private => *self
1048+
&Public => *self
10501049
}
10511050
}
10521051
}

src/libsyntax/parse/parser.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use ast::{MutImmutable, MutMutable, Mac_, MacInvocTT, Matcher, MatchNonterminal}
3939
use ast::{MatchSeq, MatchTok, Method, MutTy, BiMul, Mutability};
4040
use ast::{NamedField, UnNeg, NoReturn, UnNot, P, Pat, PatEnum};
4141
use ast::{PatIdent, PatLit, PatRange, PatRegion, PatStruct};
42-
use ast::{PatTup, PatUniq, PatWild, PatWildMulti, Private};
42+
use ast::{PatTup, PatUniq, PatWild, PatWildMulti};
4343
use ast::{BiRem, Required};
4444
use ast::{RetStyle, Return, BiShl, BiShr, Stmt, StmtDecl};
4545
use ast::{StmtExpr, StmtSemi, StmtMac, StructDef, StructField};
@@ -3953,10 +3953,6 @@ impl<'a> Parser<'a> {
39533953

39543954
let attrs = self.parse_outer_attributes();
39553955

3956-
if self.eat_keyword(keywords::Priv) {
3957-
return self.parse_single_struct_field(Private, attrs);
3958-
}
3959-
39603956
if self.eat_keyword(keywords::Pub) {
39613957
return self.parse_single_struct_field(Public, attrs);
39623958
}
@@ -3967,7 +3963,6 @@ impl<'a> Parser<'a> {
39673963
// parse visiility: PUB, PRIV, or nothing
39683964
fn parse_visibility(&mut self) -> Visibility {
39693965
if self.eat_keyword(keywords::Pub) { Public }
3970-
else if self.eat_keyword(keywords::Priv) { Private }
39713966
else { Inherited }
39723967
}
39733968

src/libsyntax/print/pprust.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ pub fn variant_to_str(var: &ast::Variant) -> ~str {
230230

231231
pub fn visibility_qualified(vis: ast::Visibility, s: &str) -> ~str {
232232
match vis {
233-
ast::Private => format!("priv {}", s),
234233
ast::Public => format!("pub {}", s),
235234
ast::Inherited => s.to_owned()
236235
}
@@ -731,7 +730,6 @@ impl<'a> State<'a> {
731730

732731
pub fn print_visibility(&mut self, vis: ast::Visibility) -> IoResult<()> {
733732
match vis {
734-
ast::Private => self.word_nbsp("priv"),
735733
ast::Public => self.word_nbsp("pub"),
736734
ast::Inherited => Ok(())
737735
}

src/test/auxiliary/private_variant_xc.rs

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/test/auxiliary/private_variant_1.rs renamed to src/test/auxiliary/unreachable-variant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010

1111
mod super_sekrit {
1212
pub enum sooper_sekrit {
13-
quux, priv baz
13+
quux, baz
1414
}
1515
}

src/test/compile-fail/assign-to-method.rs

Lines changed: 1 addition & 1 deletion
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
}

src/test/compile-fail/class-cast-to-trait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ trait noisy {
1313
}
1414

1515
struct cat {
16-
priv meows : uint,
16+
meows : uint,
1717

1818
how_hungry : int,
1919
name : ~str,

src/test/compile-fail/class-missing-self.rs

Lines changed: 1 addition & 1 deletion
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

1515
impl cat {

src/test/compile-fail/issue-3993-2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use zoo::{duck, goose}; //~ ERROR: variant `goose` is private
11+
use zoo::{duck, goose};
1212

1313
mod zoo {
1414
pub enum bird {
1515
pub duck, //~ ERROR: unnecessary `pub` visibility
16-
priv goose
16+
goose
1717
}
1818
}
1919

0 commit comments

Comments
 (0)