@@ -113,6 +113,13 @@ pub enum BoundParsingMode {
113
113
Modified ,
114
114
}
115
115
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
+
116
123
/// Possibly accept an `token::Interpolated` expression (a pre-parsed expression
117
124
/// dropped into the token stream, which happens while parsing the result of
118
125
/// macro expansion). Placement of these is not as complex as I feared it would
@@ -4686,17 +4693,19 @@ impl<'a> Parser<'a> {
4686
4693
VariantData :: Unit ( ast:: DUMMY_NODE_ID )
4687
4694
} else {
4688
4695
// 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 )
4690
4698
}
4691
4699
// No `where` so: `struct Foo<T>;`
4692
4700
} else if try!( self . eat ( & token:: Semi ) ) {
4693
4701
VariantData :: Unit ( ast:: DUMMY_NODE_ID )
4694
4702
// Record-style struct definition
4695
4703
} 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 )
4697
4706
// Tuple-style struct definition with optional where-clause.
4698
4707
} 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 ) ) ,
4700
4709
ast:: DUMMY_NODE_ID ) ;
4701
4710
generics. where_clause = try!( self . parse_where_clause ( ) ) ;
4702
4711
try!( self . expect ( & token:: Semi ) ) ;
@@ -4710,11 +4719,11 @@ impl<'a> Parser<'a> {
4710
4719
Ok ( ( class_name, ItemStruct ( vdata, generics) , None ) )
4711
4720
}
4712
4721
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 > > {
4714
4723
let mut fields = Vec :: new ( ) ;
4715
4724
if try!( self . eat ( & token:: OpenDelim ( token:: Brace ) ) ) {
4716
4725
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 ) ) ) ;
4718
4727
}
4719
4728
4720
4729
try!( self . bump ( ) ) ;
@@ -4728,7 +4737,7 @@ impl<'a> Parser<'a> {
4728
4737
Ok ( fields)
4729
4738
}
4730
4739
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 > > {
4732
4741
// This is the case where we find `struct Foo<T>(T) where T: Copy;`
4733
4742
// Unit like structs are handled in parse_item_struct function
4734
4743
let fields = try!( self . parse_unspanned_seq (
@@ -4740,7 +4749,11 @@ impl<'a> Parser<'a> {
4740
4749
let lo = p. span . lo ;
4741
4750
let struct_field_ = ast:: StructField_ {
4742
4751
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
+ }
4744
4757
) ,
4745
4758
id : ast:: DUMMY_NODE_ID ,
4746
4759
ty : try!( p. parse_ty_sum ( ) ) ,
@@ -4776,12 +4789,12 @@ impl<'a> Parser<'a> {
4776
4789
}
4777
4790
4778
4791
/// 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 > {
4780
4793
4781
4794
let attrs = try!( self . parse_outer_attributes ( ) ) ;
4782
4795
4783
4796
if try!( self . eat_keyword ( keywords:: Pub ) ) {
4784
- if !allow_pub {
4797
+ if parse_pub == ParsePub :: No {
4785
4798
let span = self . last_span ;
4786
4799
self . span_err ( span, "`pub` is not allowed here" ) ;
4787
4800
}
@@ -5149,11 +5162,11 @@ impl<'a> Parser<'a> {
5149
5162
if self . check ( & token:: OpenDelim ( token:: Brace ) ) {
5150
5163
// Parse a struct variant.
5151
5164
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 ) ) ,
5153
5166
ast:: DUMMY_NODE_ID ) ;
5154
5167
} else if self . check ( & token:: OpenDelim ( token:: Paren ) ) {
5155
5168
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 ) ) ,
5157
5170
ast:: DUMMY_NODE_ID ) ;
5158
5171
} else if try!( self . eat ( & token:: Eq ) ) {
5159
5172
disr_expr = Some ( try!( self . parse_expr_nopanic ( ) ) ) ;
0 commit comments