@@ -5518,12 +5518,11 @@ impl<'a> Parser<'a> {
55185518 } )
55195519 }
55205520
5521- /// Parse a static item from a foreign module
5521+ /// Parse a static item from a foreign module.
5522+ /// Assumes that the `static` keyword is already parsed.
55225523 fn parse_item_foreign_static ( & mut self , vis : ast:: Visibility , lo : Span , attrs : Vec < Attribute > )
55235524 -> PResult < ' a , ForeignItem > {
5524- self . expect_keyword ( keywords:: Static ) ?;
55255525 let mutbl = self . eat_keyword ( keywords:: Mut ) ;
5526-
55275526 let ident = self . parse_ident ( ) ?;
55285527 self . expect ( & token:: Colon ) ?;
55295528 let ty = self . parse_ty ( ) ?;
@@ -5997,21 +5996,22 @@ impl<'a> Parser<'a> {
59975996 let lo = self . span ;
59985997 let visibility = self . parse_visibility ( false ) ?;
59995998
6000- if self . check_keyword ( keywords:: Static ) {
6001- // FOREIGN STATIC ITEM
5999+ // FOREIGN STATIC ITEM
6000+ // Treat `const` as `static` for error recovery, but don't add it to expected tokens.
6001+ if self . check_keyword ( keywords:: Static ) || self . token . is_keyword ( keywords:: Const ) {
6002+ if self . token . is_keyword ( keywords:: Const ) {
6003+ self . diagnostic ( )
6004+ . struct_span_err ( self . span , "extern items cannot be `const`" )
6005+ . span_label ( self . span , "use `static` instead" ) . emit ( ) ;
6006+ }
6007+ self . bump ( ) ; // `static` or `const`
60026008 return Ok ( Some ( self . parse_item_foreign_static ( visibility, lo, attrs) ?) ) ;
60036009 }
6010+ // FOREIGN FUNCTION ITEM
60046011 if self . check_keyword ( keywords:: Fn ) {
6005- // FOREIGN FUNCTION ITEM
60066012 return Ok ( Some ( self . parse_item_foreign_fn ( visibility, lo, attrs) ?) ) ;
60076013 }
60086014
6009- if self . check_keyword ( keywords:: Const ) {
6010- let mut err = self . span_fatal ( self . span , "extern items cannot be `const`" ) ;
6011- err. help ( "use `static` instead" ) ;
6012- return Err ( err) ;
6013- }
6014-
60156015 // FIXME #5668: this will occur for a macro invocation:
60166016 match self . parse_macro_use_or_failure ( attrs, true , false , lo, visibility) ? {
60176017 Some ( item) => {
0 commit comments