Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare for private fields by default #13145

Merged
merged 2 commits into from
Mar 27, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/librustc/metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1019,10 +1019,11 @@ pub fn get_struct_fields(intr: @IdentInterner, cdata: Cmd, id: ast::NodeId)
});
reader::tagged_docs(item, tag_item_unnamed_field, |an_item| {
let did = item_def_id(an_item, cdata);
let f = item_family(an_item);
result.push(ty::field_ty {
name: special_idents::unnamed_field.name,
id: did,
vis: ast::Inherited,
vis: struct_field_family_to_visibility(f),
});
true
});
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,9 @@ fn encode_struct_fields(ebml_w: &mut writer::Encoder,
encode_def_id(ebml_w, local_def(f.node.id));
ebml_w.end_tag();
}
UnnamedField => {
UnnamedField(vis) => {
ebml_w.start_tag(tag_item_unnamed_field);
encode_struct_field_family(ebml_w, vis);
encode_def_id(ebml_w, local_def(f.node.id));
ebml_w.end_tag();
}
Expand Down Expand Up @@ -513,8 +514,7 @@ fn each_auxiliary_node_id(item: @Item, callback: |NodeId| -> bool) -> bool {
// If this is a newtype struct, return the constructor.
match struct_def.ctor_id {
Some(ctor_id) if struct_def.fields.len() > 0 &&
struct_def.fields.get(0).node.kind ==
ast::UnnamedField => {
struct_def.fields.get(0).node.kind.is_unnamed() => {
continue_ = callback(ctor_id);
}
_ => {}
Expand Down Expand Up @@ -690,7 +690,7 @@ fn encode_info_for_struct(ecx: &EncodeContext,
for field in fields.iter() {
let (nm, vis) = match field.node.kind {
NamedField(nm, vis) => (nm, vis),
UnnamedField => (special_idents::unnamed_field, Inherited)
UnnamedField(vis) => (special_idents::unnamed_field, vis)
};

let id = field.node.id;
Expand Down
8 changes: 2 additions & 6 deletions src/librustc/middle/privacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1001,15 +1001,11 @@ impl<'a> SanePrivacyVisitor<'a> {
};
for f in def.fields.iter() {
match f.node.kind {
ast::NamedField(_, ast::Public) if public_def => {
tcx.sess.span_err(f.span, "unnecessary `pub` \
visibility");
}
ast::NamedField(_, ast::Private) if !public_def => {
tcx.sess.span_err(f.span, "unnecessary `priv` \
visibility");
}
ast::NamedField(..) | ast::UnnamedField => {}
ast::NamedField(..) | ast::UnnamedField(..) => {}
}
}
};
Expand Down Expand Up @@ -1106,7 +1102,7 @@ impl<'a> SanePrivacyVisitor<'a> {
for f in def.fields.iter() {
match f.node.kind {
ast::NamedField(_, p) => check_inherited(f.span, p),
ast::UnnamedField => {}
ast::UnnamedField(..) => {}
}
}
};
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3900,7 +3900,7 @@ impl VariantInfo {
let arg_names = fields.iter().map(|field| {
match field.node.kind {
NamedField(ident, _) => ident,
UnnamedField => cx.sess.bug(
UnnamedField(..) => cx.sess.bug(
"enum_variants: all fields in struct must have a name")
}
}).collect();
Expand Down Expand Up @@ -4264,11 +4264,11 @@ fn struct_field_tys(fields: &[StructField]) -> Vec<field_ty> {
vis: visibility,
}
}
UnnamedField => {
UnnamedField(visibility) => {
field_ty {
name: syntax::parse::token::special_idents::unnamed_field.name,
id: ast_util::local_def(field.node.id),
vis: ast::Public,
vis: visibility,
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/middle/typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,7 @@ pub fn convert_struct(ccx: &CrateCtxt,
write_ty_to_tcx(tcx, ctor_id, selfty);

tcx.tcache.borrow_mut().insert(local_def(ctor_id), tpt);
} else if struct_def.fields.get(0).node.kind ==
ast::UnnamedField {
} else if struct_def.fields.get(0).node.kind.is_unnamed() {
// Tuple-like.
let inputs = struct_def.fields.map(
|field| tcx.tcache.borrow().get(
Expand Down
11 changes: 10 additions & 1 deletion src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,16 @@ pub type StructField = Spanned<StructField_>;
#[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
pub enum StructFieldKind {
NamedField(Ident, Visibility),
UnnamedField // element of a tuple-like struct
UnnamedField(Visibility), // element of a tuple-like struct
}

impl StructFieldKind {
pub fn is_unnamed(&self) -> bool {
match *self {
UnnamedField(..) => true,
NamedField(..) => false,
}
}
}

#[deriving(Eq, TotalEq, Encodable, Decodable, Hash)]
Expand Down
3 changes: 1 addition & 2 deletions src/libsyntax/ast_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,7 @@ pub fn split_trait_methods(trait_methods: &[TraitMethod])

pub fn struct_field_visibility(field: ast::StructField) -> Visibility {
match field.node.kind {
ast::NamedField(_, visibility) => visibility,
ast::UnnamedField => ast::Public
ast::NamedField(_, v) | ast::UnnamedField(v) => v
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax/ext/deriving/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ impl<'a> TraitDef<'a> {
let sp = self.set_expn_info(cx, field.span);
match field.node.kind {
ast::NamedField(ident, _) => named_idents.push((ident, sp)),
ast::UnnamedField => just_spans.push(sp),
ast::UnnamedField(..) => just_spans.push(sp),
}
}

Expand Down Expand Up @@ -1061,8 +1061,8 @@ impl<'a> TraitDef<'a> {
struct_type = Record;
Some(ident)
}
ast::UnnamedField if (struct_type == Unknown ||
struct_type == Tuple) => {
ast::UnnamedField(..) if (struct_type == Unknown ||
struct_type == Tuple) => {
struct_type = Tuple;
None
}
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3985,7 +3985,7 @@ impl<'a> Parser<'a> {
let attrs = p.parse_outer_attributes();
let lo = p.span.lo;
let struct_field_ = ast::StructField_ {
kind: UnnamedField,
kind: UnnamedField(p.parse_visibility()),
id: ast::DUMMY_NODE_ID,
ty: p.parse_ty(false),
attrs: attrs,
Expand Down
5 changes: 3 additions & 2 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,8 @@ impl<'a> State<'a> {
|s, field| {
match field.node.kind {
ast::NamedField(..) => fail!("unexpected named field"),
ast::UnnamedField => {
ast::UnnamedField(vis) => {
try!(s.print_visibility(vis));
try!(s.maybe_print_comment(field.span.lo));
s.print_type(field.node.ty)
}
Expand All @@ -762,7 +763,7 @@ impl<'a> State<'a> {

for field in struct_def.fields.iter() {
match field.node.kind {
ast::UnnamedField => fail!("unexpected unnamed field"),
ast::UnnamedField(..) => fail!("unexpected unnamed field"),
ast::NamedField(ident, visibility) => {
try!(self.hardbreak_if_not_bol());
try!(self.maybe_print_comment(field.span.lo));
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/struct-field-privacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mod inner {
pub struct B {
a: int,
priv b: int,
pub c: int, //~ ERROR: unnecessary `pub` visibility
pub c: int,
}
}

Expand Down
20 changes: 0 additions & 20 deletions src/test/compile-fail/struct-variant-privacy.rs

This file was deleted.