Skip to content

Commit 649fc38

Browse files
committed
Use enum ParsePub instead of bool in field parsing + typo
1 parent 2a01e26 commit 649fc38

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

src/libsyntax/feature_gate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -867,8 +867,8 @@ impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> {
867867
self.context.span_handler.span_err(span, "empty tuple structs and enum variants \
868868
are not allowed, use unit structs and \
869869
enum variants instead");
870-
self.context.span_handler.span_help(span, "remove trailing () to make a unit \
871-
struct or unit enum varian");
870+
self.context.span_handler.span_help(span, "remove trailing `()` to make a unit \
871+
struct or unit enum variant");
872872
}
873873
}
874874
visit::walk_struct_def(self, s)

src/libsyntax/parse/parser.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ pub enum BoundParsingMode {
113113
Modified,
114114
}
115115

116+
/// `pub` should be parsed in struct fields and not parsed in variant fields
117+
#[derive(Clone, Copy, PartialEq)]
118+
pub enum ParsePub {
119+
Yes,
120+
No,
121+
}
122+
116123
/// Possibly accept an `token::Interpolated` expression (a pre-parsed expression
117124
/// dropped into the token stream, which happens while parsing the result of
118125
/// macro expansion). Placement of these is not as complex as I feared it would
@@ -4686,17 +4693,19 @@ impl<'a> Parser<'a> {
46864693
VariantData::Unit(ast::DUMMY_NODE_ID)
46874694
} else {
46884695
// If we see: `struct Foo<T> where T: Copy { ... }`
4689-
VariantData::Struct(try!(self.parse_record_struct_body(true)), ast::DUMMY_NODE_ID)
4696+
VariantData::Struct(try!(self.parse_record_struct_body(ParsePub::Yes)),
4697+
ast::DUMMY_NODE_ID)
46904698
}
46914699
// No `where` so: `struct Foo<T>;`
46924700
} else if try!(self.eat(&token::Semi) ){
46934701
VariantData::Unit(ast::DUMMY_NODE_ID)
46944702
// Record-style struct definition
46954703
} else if self.token == token::OpenDelim(token::Brace) {
4696-
VariantData::Struct(try!(self.parse_record_struct_body(true)), ast::DUMMY_NODE_ID)
4704+
VariantData::Struct(try!(self.parse_record_struct_body(ParsePub::Yes)),
4705+
ast::DUMMY_NODE_ID)
46974706
// Tuple-style struct definition with optional where-clause.
46984707
} else if self.token == token::OpenDelim(token::Paren) {
4699-
let body = VariantData::Tuple(try!(self.parse_tuple_struct_body(true)),
4708+
let body = VariantData::Tuple(try!(self.parse_tuple_struct_body(ParsePub::Yes)),
47004709
ast::DUMMY_NODE_ID);
47014710
generics.where_clause = try!(self.parse_where_clause());
47024711
try!(self.expect(&token::Semi));
@@ -4710,11 +4719,11 @@ impl<'a> Parser<'a> {
47104719
Ok((class_name, ItemStruct(vdata, generics), None))
47114720
}
47124721

4713-
pub fn parse_record_struct_body(&mut self, allow_pub: bool) -> PResult<Vec<StructField>> {
4722+
pub fn parse_record_struct_body(&mut self, parse_pub: ParsePub) -> PResult<Vec<StructField>> {
47144723
let mut fields = Vec::new();
47154724
if try!(self.eat(&token::OpenDelim(token::Brace)) ){
47164725
while self.token != token::CloseDelim(token::Brace) {
4717-
fields.push(try!(self.parse_struct_decl_field(allow_pub)));
4726+
fields.push(try!(self.parse_struct_decl_field(parse_pub)));
47184727
}
47194728

47204729
try!(self.bump());
@@ -4728,7 +4737,7 @@ impl<'a> Parser<'a> {
47284737
Ok(fields)
47294738
}
47304739

4731-
pub fn parse_tuple_struct_body(&mut self, allow_pub: bool) -> PResult<Vec<StructField>> {
4740+
pub fn parse_tuple_struct_body(&mut self, parse_pub: ParsePub) -> PResult<Vec<StructField>> {
47324741
// This is the case where we find `struct Foo<T>(T) where T: Copy;`
47334742
// Unit like structs are handled in parse_item_struct function
47344743
let fields = try!(self.parse_unspanned_seq(
@@ -4740,7 +4749,11 @@ impl<'a> Parser<'a> {
47404749
let lo = p.span.lo;
47414750
let struct_field_ = ast::StructField_ {
47424751
kind: UnnamedField (
4743-
if allow_pub { try!(p.parse_visibility()) } else { Inherited }
4752+
if parse_pub == ParsePub::Yes {
4753+
try!(p.parse_visibility())
4754+
} else {
4755+
Inherited
4756+
}
47444757
),
47454758
id: ast::DUMMY_NODE_ID,
47464759
ty: try!(p.parse_ty_sum()),
@@ -4776,12 +4789,12 @@ impl<'a> Parser<'a> {
47764789
}
47774790

47784791
/// Parse an element of a struct definition
4779-
fn parse_struct_decl_field(&mut self, allow_pub: bool) -> PResult<StructField> {
4792+
fn parse_struct_decl_field(&mut self, parse_pub: ParsePub) -> PResult<StructField> {
47804793

47814794
let attrs = try!(self.parse_outer_attributes());
47824795

47834796
if try!(self.eat_keyword(keywords::Pub) ){
4784-
if !allow_pub {
4797+
if parse_pub == ParsePub::No {
47854798
let span = self.last_span;
47864799
self.span_err(span, "`pub` is not allowed here");
47874800
}
@@ -5149,11 +5162,11 @@ impl<'a> Parser<'a> {
51495162
if self.check(&token::OpenDelim(token::Brace)) {
51505163
// Parse a struct variant.
51515164
all_nullary = false;
5152-
struct_def = VariantData::Struct(try!(self.parse_record_struct_body(false)),
5165+
struct_def = VariantData::Struct(try!(self.parse_record_struct_body(ParsePub::No)),
51535166
ast::DUMMY_NODE_ID);
51545167
} else if self.check(&token::OpenDelim(token::Paren)) {
51555168
all_nullary = false;
5156-
struct_def = VariantData::Tuple(try!(self.parse_tuple_struct_body(false)),
5169+
struct_def = VariantData::Tuple(try!(self.parse_tuple_struct_body(ParsePub::No)),
51575170
ast::DUMMY_NODE_ID);
51585171
} else if try!(self.eat(&token::Eq) ){
51595172
disr_expr = Some(try!(self.parse_expr_nopanic()));

src/test/compile-fail/issue-12560-1.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
enum Foo {
1616
Bar(), //~ ERROR empty tuple structs and enum variants are not allowed
17-
//~^ HELP remove trailing () to make a unit struct or unit enum varian
17+
//~^ HELP remove trailing `()` to make a unit struct or unit enum variant
1818
Baz(), //~ ERROR empty tuple structs and enum variants are not allowed
19-
//~^ HELP remove trailing () to make a unit struct or unit enum varian
19+
//~^ HELP remove trailing `()` to make a unit struct or unit enum variant
2020
Bazar
2121
}
2222

0 commit comments

Comments
 (0)