diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 82715f263c96..c6a237d38279 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -254,6 +254,7 @@ pub struct Parser<'a> { /// the previous token or None (only stashed sometimes). pub last_token: Option>, last_token_interpolated: bool, + last_token_eof: bool, pub buffer: [TokenAndSpan; 4], pub buffer_start: isize, pub buffer_end: isize, @@ -366,6 +367,7 @@ impl<'a> Parser<'a> { last_span: span, last_token: None, last_token_interpolated: false, + last_token_eof: false, buffer: [ placeholder.clone(), placeholder.clone(), @@ -998,6 +1000,15 @@ impl<'a> Parser<'a> { /// Advance the parser by one token pub fn bump(&mut self) { + if self.last_token_eof { + // Bumping after EOF is a bad sign, usually an infinite loop. + self.bug("attempted to bump the parser past EOF (may be stuck in a loop)"); + } + + if self.token == token::Eof { + self.last_token_eof = true; + } + self.last_span = self.span; // Stash token for error recovery (sometimes; clone is not necessarily cheap). self.last_token = if self.token.is_ident() ||