From b2d28096927839548f4a556aba644530f4549f06 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sun, 18 Aug 2013 22:12:22 -0700 Subject: [PATCH] Prepare bootstrapping priv-by-default struct fields --- src/librustc/middle/privacy.rs | 23 +++++-------------- src/libsyntax/parse/obsolete.rs | 5 +++++ src/libsyntax/parse/parser.rs | 39 +++++++++++++++++---------------- 3 files changed, 31 insertions(+), 36 deletions(-) diff --git a/src/librustc/middle/privacy.rs b/src/librustc/middle/privacy.rs index 54e7c79e97cc2..09ff02011c72a 100644 --- a/src/librustc/middle/privacy.rs +++ b/src/librustc/middle/privacy.rs @@ -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))); } @@ -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 { @@ -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(*) => {} } } diff --git a/src/libsyntax/parse/obsolete.rs b/src/libsyntax/parse/obsolete.rs index 01c1af7464db6..e386916596802 100644 --- a/src/libsyntax/parse/obsolete.rs +++ b/src/libsyntax/parse/obsolete.rs @@ -65,6 +65,7 @@ pub enum ObsoleteSyntax { ObsoleteExternVisibility, ObsoleteUnsafeExternFn, ObsoletePrivVisibility, + ObsoletePrivStructFieldVisibility, ObsoleteTraitFuncVisibility, } @@ -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" diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index de7abb8d1f46d..689fcf529b3c2 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -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 { @@ -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 @@ -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();