Skip to content

Commit c83994e

Browse files
committed
auto merge of #13145 : alexcrichton/rust/flip-some-defaults, r=brson
This change prepares `rustc` to accept private fields by default. These changes will have to go through a snapshot before the rest of the changes can happen.
2 parents 533a526 + 7de4841 commit c83994e

File tree

12 files changed

+31
-46
lines changed

12 files changed

+31
-46
lines changed

src/librustc/metadata/decoder.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1019,10 +1019,11 @@ pub fn get_struct_fields(intr: @IdentInterner, cdata: Cmd, id: ast::NodeId)
10191019
});
10201020
reader::tagged_docs(item, tag_item_unnamed_field, |an_item| {
10211021
let did = item_def_id(an_item, cdata);
1022+
let f = item_family(an_item);
10221023
result.push(ty::field_ty {
10231024
name: special_idents::unnamed_field.name,
10241025
id: did,
1025-
vis: ast::Inherited,
1026+
vis: struct_field_family_to_visibility(f),
10261027
});
10271028
true
10281029
});

src/librustc/metadata/encoder.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,9 @@ fn encode_struct_fields(ebml_w: &mut writer::Encoder,
310310
encode_def_id(ebml_w, local_def(f.node.id));
311311
ebml_w.end_tag();
312312
}
313-
UnnamedField => {
313+
UnnamedField(vis) => {
314314
ebml_w.start_tag(tag_item_unnamed_field);
315+
encode_struct_field_family(ebml_w, vis);
315316
encode_def_id(ebml_w, local_def(f.node.id));
316317
ebml_w.end_tag();
317318
}
@@ -513,8 +514,7 @@ fn each_auxiliary_node_id(item: @Item, callback: |NodeId| -> bool) -> bool {
513514
// If this is a newtype struct, return the constructor.
514515
match struct_def.ctor_id {
515516
Some(ctor_id) if struct_def.fields.len() > 0 &&
516-
struct_def.fields.get(0).node.kind ==
517-
ast::UnnamedField => {
517+
struct_def.fields.get(0).node.kind.is_unnamed() => {
518518
continue_ = callback(ctor_id);
519519
}
520520
_ => {}
@@ -690,7 +690,7 @@ fn encode_info_for_struct(ecx: &EncodeContext,
690690
for field in fields.iter() {
691691
let (nm, vis) = match field.node.kind {
692692
NamedField(nm, vis) => (nm, vis),
693-
UnnamedField => (special_idents::unnamed_field, Inherited)
693+
UnnamedField(vis) => (special_idents::unnamed_field, vis)
694694
};
695695

696696
let id = field.node.id;

src/librustc/middle/privacy.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1001,15 +1001,11 @@ impl<'a> SanePrivacyVisitor<'a> {
10011001
};
10021002
for f in def.fields.iter() {
10031003
match f.node.kind {
1004-
ast::NamedField(_, ast::Public) if public_def => {
1005-
tcx.sess.span_err(f.span, "unnecessary `pub` \
1006-
visibility");
1007-
}
10081004
ast::NamedField(_, ast::Private) if !public_def => {
10091005
tcx.sess.span_err(f.span, "unnecessary `priv` \
10101006
visibility");
10111007
}
1012-
ast::NamedField(..) | ast::UnnamedField => {}
1008+
ast::NamedField(..) | ast::UnnamedField(..) => {}
10131009
}
10141010
}
10151011
};
@@ -1106,7 +1102,7 @@ impl<'a> SanePrivacyVisitor<'a> {
11061102
for f in def.fields.iter() {
11071103
match f.node.kind {
11081104
ast::NamedField(_, p) => check_inherited(f.span, p),
1109-
ast::UnnamedField => {}
1105+
ast::UnnamedField(..) => {}
11101106
}
11111107
}
11121108
};

src/librustc/middle/ty.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3900,7 +3900,7 @@ impl VariantInfo {
39003900
let arg_names = fields.iter().map(|field| {
39013901
match field.node.kind {
39023902
NamedField(ident, _) => ident,
3903-
UnnamedField => cx.sess.bug(
3903+
UnnamedField(..) => cx.sess.bug(
39043904
"enum_variants: all fields in struct must have a name")
39053905
}
39063906
}).collect();
@@ -4264,11 +4264,11 @@ fn struct_field_tys(fields: &[StructField]) -> Vec<field_ty> {
42644264
vis: visibility,
42654265
}
42664266
}
4267-
UnnamedField => {
4267+
UnnamedField(visibility) => {
42684268
field_ty {
42694269
name: syntax::parse::token::special_idents::unnamed_field.name,
42704270
id: ast_util::local_def(field.node.id),
4271-
vis: ast::Public,
4271+
vis: visibility,
42724272
}
42734273
}
42744274
}

src/librustc/middle/typeck/collect.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -695,8 +695,7 @@ pub fn convert_struct(ccx: &CrateCtxt,
695695
write_ty_to_tcx(tcx, ctor_id, selfty);
696696

697697
tcx.tcache.borrow_mut().insert(local_def(ctor_id), tpt);
698-
} else if struct_def.fields.get(0).node.kind ==
699-
ast::UnnamedField {
698+
} else if struct_def.fields.get(0).node.kind.is_unnamed() {
700699
// Tuple-like.
701700
let inputs = struct_def.fields.map(
702701
|field| tcx.tcache.borrow().get(

src/libsyntax/ast.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,16 @@ pub type StructField = Spanned<StructField_>;
10801080
#[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
10811081
pub enum StructFieldKind {
10821082
NamedField(Ident, Visibility),
1083-
UnnamedField // element of a tuple-like struct
1083+
UnnamedField(Visibility), // element of a tuple-like struct
1084+
}
1085+
1086+
impl StructFieldKind {
1087+
pub fn is_unnamed(&self) -> bool {
1088+
match *self {
1089+
UnnamedField(..) => true,
1090+
NamedField(..) => false,
1091+
}
1092+
}
10841093
}
10851094

10861095
#[deriving(Eq, TotalEq, Encodable, Decodable, Hash)]

src/libsyntax/ast_util.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,7 @@ pub fn split_trait_methods(trait_methods: &[TraitMethod])
290290

291291
pub fn struct_field_visibility(field: ast::StructField) -> Visibility {
292292
match field.node.kind {
293-
ast::NamedField(_, visibility) => visibility,
294-
ast::UnnamedField => ast::Public
293+
ast::NamedField(_, v) | ast::UnnamedField(v) => v
295294
}
296295
}
297296

src/libsyntax/ext/deriving/generic.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ impl<'a> TraitDef<'a> {
10071007
let sp = self.set_expn_info(cx, field.span);
10081008
match field.node.kind {
10091009
ast::NamedField(ident, _) => named_idents.push((ident, sp)),
1010-
ast::UnnamedField => just_spans.push(sp),
1010+
ast::UnnamedField(..) => just_spans.push(sp),
10111011
}
10121012
}
10131013

@@ -1061,8 +1061,8 @@ impl<'a> TraitDef<'a> {
10611061
struct_type = Record;
10621062
Some(ident)
10631063
}
1064-
ast::UnnamedField if (struct_type == Unknown ||
1065-
struct_type == Tuple) => {
1064+
ast::UnnamedField(..) if (struct_type == Unknown ||
1065+
struct_type == Tuple) => {
10661066
struct_type = Tuple;
10671067
None
10681068
}

src/libsyntax/parse/parser.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3985,7 +3985,7 @@ impl<'a> Parser<'a> {
39853985
let attrs = p.parse_outer_attributes();
39863986
let lo = p.span.lo;
39873987
let struct_field_ = ast::StructField_ {
3988-
kind: UnnamedField,
3988+
kind: UnnamedField(p.parse_visibility()),
39893989
id: ast::DUMMY_NODE_ID,
39903990
ty: p.parse_ty(false),
39913991
attrs: attrs,

src/libsyntax/print/pprust.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,8 @@ impl<'a> State<'a> {
743743
|s, field| {
744744
match field.node.kind {
745745
ast::NamedField(..) => fail!("unexpected named field"),
746-
ast::UnnamedField => {
746+
ast::UnnamedField(vis) => {
747+
try!(s.print_visibility(vis));
747748
try!(s.maybe_print_comment(field.span.lo));
748749
s.print_type(field.node.ty)
749750
}
@@ -762,7 +763,7 @@ impl<'a> State<'a> {
762763

763764
for field in struct_def.fields.iter() {
764765
match field.node.kind {
765-
ast::UnnamedField => fail!("unexpected unnamed field"),
766+
ast::UnnamedField(..) => fail!("unexpected unnamed field"),
766767
ast::NamedField(ident, visibility) => {
767768
try!(self.hardbreak_if_not_bol());
768769
try!(self.maybe_print_comment(field.span.lo));

src/test/compile-fail/struct-field-privacy.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ mod inner {
2525
pub struct B {
2626
a: int,
2727
priv b: int,
28-
pub c: int, //~ ERROR: unnecessary `pub` visibility
28+
pub c: int,
2929
}
3030
}
3131

src/test/compile-fail/struct-variant-privacy.rs

-20
This file was deleted.

0 commit comments

Comments
 (0)