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 bootstrapping priv-by-default struct fields #8609

Closed
Closed
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
23 changes: 6 additions & 17 deletions src/librustc/middle/privacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,11 @@ pub fn check_crate<'mm>(tcx: ty::ctxt,
let fields = ty::lookup_struct_fields(tcx, id);
for field in fields.iter() {
if field.ident != ident { loop; }
if field.vis == private {
// NOTE: this means that the next snapshot will totally not respect
// visibility of struct fields at all. The plan is to move
// from public-by-default to private-by-default, and this is
// necessary for bootstrapping.
if !cfg!(stage3) && field.vis == private {
tcx.sess.span_err(span, fmt!("field `%s` is private",
token::ident_to_str(&ident)));
}
Expand Down Expand Up @@ -581,21 +585,6 @@ fn check_sane_privacy(tcx: ty::ctxt, item: @ast::item) {
}
}

ast::item_struct(ref def, _) => {
for f in def.fields.iter() {
match f.node.kind {
ast::named_field(_, ast::public) => {
tcx.sess.span_err(f.span, "unnecessary `pub` \
visibility");
}
ast::named_field(_, ast::private) => {
// Fields should really be private by default...
}
ast::named_field(*) | ast::unnamed_field => {}
}
}
}

ast::item_trait(_, _, ref methods) => {
for m in methods.iter() {
match *m {
Expand All @@ -616,6 +605,6 @@ fn check_sane_privacy(tcx: ty::ctxt, item: @ast::item) {

ast::item_impl(*) | ast::item_static(*) | ast::item_foreign_mod(*) |
ast::item_fn(*) | ast::item_mod(*) | ast::item_ty(*) |
ast::item_mac(*) => {}
ast::item_mac(*) | ast::item_struct(*) => {}
}
}
5 changes: 5 additions & 0 deletions src/libsyntax/parse/obsolete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pub enum ObsoleteSyntax {
ObsoleteExternVisibility,
ObsoleteUnsafeExternFn,
ObsoletePrivVisibility,
ObsoletePrivStructFieldVisibility,
ObsoleteTraitFuncVisibility,
}

Expand Down Expand Up @@ -259,6 +260,10 @@ impl ParserObsoleteMethods for Parser {
"`priv` not necessary",
"an item without a visibility qualifier is private by default"
),
ObsoletePrivStructFieldVisibility=> (
"`priv` not necessary",
"struct fields are private by default",
),
ObsoleteTraitFuncVisibility => (
"visibility not necessary",
"trait functions inherit the visibility of the trait itself"
Expand Down
39 changes: 20 additions & 19 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3824,9 +3824,9 @@ impl Parser {
is_tuple_like = false;
fields = ~[];
while *self.token != token::RBRACE {
let r = self.parse_struct_decl_field();
for struct_field in r.iter() {
fields.push(*struct_field)
match self.parse_struct_decl_field() {
Some(f) => fields.push(f),
None => {}
}
}
if fields.len() == 0 {
Expand Down Expand Up @@ -3913,27 +3913,28 @@ impl Parser {
}

// parse an element of a struct definition
fn parse_struct_decl_field(&self) -> ~[@struct_field] {

fn parse_struct_decl_field(&self) -> Option<@struct_field> {
let attrs = self.parse_outer_attributes();

if self.try_parse_obsolete_priv_section(attrs) {
return ~[];
}

if self.eat_keyword(keywords::Priv) {
return ~[self.parse_single_struct_field(private, attrs)]
}

if self.eat_keyword(keywords::Pub) {
return ~[self.parse_single_struct_field(public, attrs)];
return None;
}
let vis = if self.eat_keyword(keywords::Priv) {
// NOTE: after a snapshot, acrichto needs to make a pull request to
// finish this work.
// self.obsolete(*self.last_span, ObsoletePrivStructFieldVisibility);
private
} else if self.eat_keyword(keywords::Pub) {
public
} else {
inherited
};

if self.try_parse_obsolete_struct_ctor() {
return ~[];
return None;
}

return ~[self.parse_single_struct_field(inherited, attrs)];
return Some(self.parse_single_struct_field(vis, attrs));
}

// parse visiility: PUB, PRIV, or nothing
Expand Down Expand Up @@ -4341,9 +4342,9 @@ impl Parser {
fn parse_struct_def(&self) -> @struct_def {
let mut fields: ~[@struct_field] = ~[];
while *self.token != token::RBRACE {
let r = self.parse_struct_decl_field();
for struct_field in r.iter() {
fields.push(*struct_field);
match self.parse_struct_decl_field() {
Some(f) => fields.push(f),
None => {}
}
}
self.bump();
Expand Down