@@ -5518,12 +5518,11 @@ impl<'a> Parser<'a> {
5518
5518
} )
5519
5519
}
5520
5520
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.
5522
5523
fn parse_item_foreign_static ( & mut self , vis : ast:: Visibility , lo : Span , attrs : Vec < Attribute > )
5523
5524
-> PResult < ' a , ForeignItem > {
5524
- self . expect_keyword ( keywords:: Static ) ?;
5525
5525
let mutbl = self . eat_keyword ( keywords:: Mut ) ;
5526
-
5527
5526
let ident = self . parse_ident ( ) ?;
5528
5527
self . expect ( & token:: Colon ) ?;
5529
5528
let ty = self . parse_ty ( ) ?;
@@ -5997,21 +5996,22 @@ impl<'a> Parser<'a> {
5997
5996
let lo = self . span ;
5998
5997
let visibility = self . parse_visibility ( false ) ?;
5999
5998
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`
6002
6008
return Ok ( Some ( self . parse_item_foreign_static ( visibility, lo, attrs) ?) ) ;
6003
6009
}
6010
+ // FOREIGN FUNCTION ITEM
6004
6011
if self . check_keyword ( keywords:: Fn ) {
6005
- // FOREIGN FUNCTION ITEM
6006
6012
return Ok ( Some ( self . parse_item_foreign_fn ( visibility, lo, attrs) ?) ) ;
6007
6013
}
6008
6014
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
-
6015
6015
// FIXME #5668: this will occur for a macro invocation:
6016
6016
match self . parse_macro_use_or_failure ( attrs, true , false , lo, visibility) ? {
6017
6017
Some ( item) => {
0 commit comments