@@ -1866,48 +1866,51 @@ impl<'a> Parser<'a> {
1866
1866
1867
1867
/// Parses `ident (COLON expr)?`.
1868
1868
fn parse_field ( & mut self ) -> PResult < ' a , Field > {
1869
- let attrs = self . parse_outer_attributes ( ) ?;
1869
+ let attrs = self . parse_outer_attributes ( ) ?. into ( ) ;
1870
1870
let lo = self . token . span ;
1871
1871
1872
1872
// Check if a colon exists one ahead. This means we're parsing a fieldname.
1873
- let ( fieldname, expr, is_shorthand) =
1874
- if self . look_ahead ( 1 , |t| t == & token:: Colon || t == & token:: Eq ) {
1875
- let fieldname = self . parse_field_name ( ) ?;
1876
-
1877
- // Check for an equals token. This means the source incorrectly attempts to
1878
- // initialize a field with an eq rather than a colon.
1879
- if self . token == token:: Eq {
1880
- self . diagnostic ( )
1881
- . struct_span_err ( self . token . span , "expected `:`, found `=`" )
1882
- . span_suggestion (
1883
- fieldname. span . shrink_to_hi ( ) . to ( self . token . span ) ,
1884
- "replace equals symbol with a colon" ,
1885
- ":" . to_string ( ) ,
1886
- Applicability :: MachineApplicable ,
1887
- )
1888
- . emit ( ) ;
1889
- }
1890
- self . bump ( ) ; // `:`
1891
- ( fieldname, self . parse_expr ( ) ?, false )
1892
- } else {
1893
- let fieldname = self . parse_ident_common ( false ) ?;
1894
-
1895
- // Mimic `x: x` for the `x` field shorthand.
1896
- let path = ast:: Path :: from_ident ( fieldname) ;
1897
- let expr = self . mk_expr ( fieldname. span , ExprKind :: Path ( None , path) , AttrVec :: new ( ) ) ;
1898
- ( fieldname, expr, true )
1899
- } ;
1873
+ let is_shorthand = !self . look_ahead ( 1 , |t| t == & token:: Colon || t == & token:: Eq ) ;
1874
+ let ( ident, expr) = if is_shorthand {
1875
+ // Mimic `x: x` for the `x` field shorthand.
1876
+ let ident = self . parse_ident_common ( false ) ?;
1877
+ let path = ast:: Path :: from_ident ( ident) ;
1878
+ ( ident, self . mk_expr ( ident. span , ExprKind :: Path ( None , path) , AttrVec :: new ( ) ) )
1879
+ } else {
1880
+ let ident = self . parse_field_name ( ) ?;
1881
+ self . error_on_eq_field_init ( ident) ;
1882
+ self . bump ( ) ; // `:`
1883
+ ( ident, self . parse_expr ( ) ?)
1884
+ } ;
1900
1885
Ok ( ast:: Field {
1901
- ident : fieldname ,
1886
+ ident,
1902
1887
span : lo. to ( expr. span ) ,
1903
1888
expr,
1904
1889
is_shorthand,
1905
- attrs : attrs . into ( ) ,
1890
+ attrs,
1906
1891
id : DUMMY_NODE_ID ,
1907
1892
is_placeholder : false ,
1908
1893
} )
1909
1894
}
1910
1895
1896
+ /// Check for `=`. This means the source incorrectly attempts to
1897
+ /// initialize a field with an eq rather than a colon.
1898
+ fn error_on_eq_field_init ( & self , field_name : Ident ) {
1899
+ if self . token != token:: Eq {
1900
+ return ;
1901
+ }
1902
+
1903
+ self . diagnostic ( )
1904
+ . struct_span_err ( self . token . span , "expected `:`, found `=`" )
1905
+ . span_suggestion (
1906
+ field_name. span . shrink_to_hi ( ) . to ( self . token . span ) ,
1907
+ "replace equals symbol with a colon" ,
1908
+ ":" . to_string ( ) ,
1909
+ Applicability :: MachineApplicable ,
1910
+ )
1911
+ . emit ( ) ;
1912
+ }
1913
+
1911
1914
fn err_dotdotdot_syntax ( & self , span : Span ) {
1912
1915
self . struct_span_err ( span, "unexpected token: `...`" )
1913
1916
. span_suggestion (
0 commit comments