From a904ba2dd2fd93f71e25fba7cf7d2fbe28479e5c Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Sun, 28 Feb 2016 15:58:47 +0000 Subject: [PATCH 1/2] Refactor away `ParsePub` and make errors for unnecessary visibility qualifiers consistent --- src/libsyntax/parse/parser.rs | 44 ++++++++--------------------------- 1 file changed, 10 insertions(+), 34 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index e166a367219..5e5991bfebf 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -99,13 +99,6 @@ pub enum BoundParsingMode { Modified, } -/// `pub` should be parsed in struct fields and not parsed in variant fields -#[derive(Clone, Copy, PartialEq)] -pub enum ParsePub { - Yes, - No, -} - #[derive(Clone, Copy, PartialEq)] pub enum SemiColonMode { Break, @@ -5093,20 +5086,17 @@ impl<'a> Parser<'a> { VariantData::Unit(ast::DUMMY_NODE_ID) } else { // If we see: `struct Foo where T: Copy { ... }` - VariantData::Struct(try!(self.parse_record_struct_body(ParsePub::Yes)), - ast::DUMMY_NODE_ID) + VariantData::Struct(try!(self.parse_record_struct_body()), ast::DUMMY_NODE_ID) } // No `where` so: `struct Foo;` } else if self.eat(&token::Semi) { VariantData::Unit(ast::DUMMY_NODE_ID) // Record-style struct definition } else if self.token == token::OpenDelim(token::Brace) { - VariantData::Struct(try!(self.parse_record_struct_body(ParsePub::Yes)), - ast::DUMMY_NODE_ID) + VariantData::Struct(try!(self.parse_record_struct_body()), ast::DUMMY_NODE_ID) // Tuple-style struct definition with optional where-clause. } else if self.token == token::OpenDelim(token::Paren) { - let body = VariantData::Tuple(try!(self.parse_tuple_struct_body(ParsePub::Yes)), - ast::DUMMY_NODE_ID); + let body = VariantData::Tuple(try!(self.parse_tuple_struct_body()), ast::DUMMY_NODE_ID); generics.where_clause = try!(self.parse_where_clause()); try!(self.expect(&token::Semi)); body @@ -5119,13 +5109,11 @@ impl<'a> Parser<'a> { Ok((class_name, ItemKind::Struct(vdata, generics), None)) } - pub fn parse_record_struct_body(&mut self, - parse_pub: ParsePub) - -> PResult<'a, Vec> { + pub fn parse_record_struct_body(&mut self) -> PResult<'a, Vec> { let mut fields = Vec::new(); if self.eat(&token::OpenDelim(token::Brace)) { while self.token != token::CloseDelim(token::Brace) { - fields.push(try!(self.parse_struct_decl_field(parse_pub))); + fields.push(try!(self.parse_struct_decl_field())); } self.bump(); @@ -5139,9 +5127,7 @@ impl<'a> Parser<'a> { Ok(fields) } - pub fn parse_tuple_struct_body(&mut self, - parse_pub: ParsePub) - -> PResult<'a, Vec> { + pub fn parse_tuple_struct_body(&mut self) -> PResult<'a, Vec> { // This is the case where we find `struct Foo(T) where T: Copy;` // Unit like structs are handled in parse_item_struct function let fields = try!(self.parse_unspanned_seq( @@ -5152,13 +5138,7 @@ impl<'a> Parser<'a> { let attrs = try!(p.parse_outer_attributes()); let lo = p.span.lo; let struct_field_ = ast::StructField_ { - kind: UnnamedField ( - if parse_pub == ParsePub::Yes { - try!(p.parse_visibility()) - } else { - Visibility::Inherited - } - ), + kind: UnnamedField(try!(p.parse_visibility())), id: ast::DUMMY_NODE_ID, ty: try!(p.parse_ty_sum()), attrs: attrs, @@ -5193,15 +5173,11 @@ impl<'a> Parser<'a> { } /// Parse an element of a struct definition - fn parse_struct_decl_field(&mut self, parse_pub: ParsePub) -> PResult<'a, StructField> { + fn parse_struct_decl_field(&mut self) -> PResult<'a, StructField> { let attrs = try!(self.parse_outer_attributes()); if self.eat_keyword(keywords::Pub) { - if parse_pub == ParsePub::No { - let span = self.last_span; - self.span_err(span, "`pub` is not allowed here"); - } return self.parse_single_struct_field(Visibility::Public, attrs); } @@ -5567,11 +5543,11 @@ impl<'a> Parser<'a> { if self.check(&token::OpenDelim(token::Brace)) { // Parse a struct variant. all_nullary = false; - struct_def = VariantData::Struct(try!(self.parse_record_struct_body(ParsePub::No)), + struct_def = VariantData::Struct(try!(self.parse_record_struct_body()), ast::DUMMY_NODE_ID); } else if self.check(&token::OpenDelim(token::Paren)) { all_nullary = false; - struct_def = VariantData::Tuple(try!(self.parse_tuple_struct_body(ParsePub::No)), + struct_def = VariantData::Tuple(try!(self.parse_tuple_struct_body()), ast::DUMMY_NODE_ID); } else if self.eat(&token::Eq) { disr_expr = Some(try!(self.parse_expr())); From 498059bb65e1be55d8952070a4cc5b4d06e9d695 Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Thu, 3 Mar 2016 23:31:44 +0000 Subject: [PATCH 2/2] Update tests --- src/test/compile-fail/useless-pub.rs | 7 +++++++ src/test/parse-fail/struct-variant-no-pub.rs | 19 ------------------- 2 files changed, 7 insertions(+), 19 deletions(-) delete mode 100644 src/test/parse-fail/struct-variant-no-pub.rs diff --git a/src/test/compile-fail/useless-pub.rs b/src/test/compile-fail/useless-pub.rs index fb6cdf7fa59..268b937c291 100644 --- a/src/test/compile-fail/useless-pub.rs +++ b/src/test/compile-fail/useless-pub.rs @@ -18,4 +18,11 @@ impl E for A { pub fn foo(&self) {} //~ ERROR: unnecessary visibility } +enum Foo { + V1 { pub f: i32 }, //~ ERROR unnecessary visibility qualifier + //| NOTE visibility qualifiers have no effect on variant fields + V2(pub i32), //~ ERROR unnecessary visibility qualifier + //| NOTE visibility qualifiers have no effect on variant fields +} + fn main() {} diff --git a/src/test/parse-fail/struct-variant-no-pub.rs b/src/test/parse-fail/struct-variant-no-pub.rs deleted file mode 100644 index 1824e32c425..00000000000 --- a/src/test/parse-fail/struct-variant-no-pub.rs +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2014 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -// compile-flags: -Z parse-only - -enum Foo { - Bar { - pub a: isize //~ ERROR: `pub` is not allowed here - } -} - -fn main() {}