Skip to content

Commit f837383

Browse files
committed
Merge pull request #32795 from pnkfelix/parser-backports-for-beta
Parser backports for beta
2 parents ef6e1f1 + 9a32f6a commit f837383

29 files changed

+107
-25
lines changed

src/librustc/session/config.rs

+6
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ pub struct Options {
138138
pub no_trans: bool,
139139
pub error_format: ErrorOutputType,
140140
pub treat_err_as_bug: bool,
141+
pub continue_parse_after_error: bool,
141142
pub mir_opt_level: usize,
142143

143144
/// if true, build up the dep-graph
@@ -255,6 +256,7 @@ pub fn basic_options() -> Options {
255256
parse_only: false,
256257
no_trans: false,
257258
treat_err_as_bug: false,
259+
continue_parse_after_error: false,
258260
mir_opt_level: 1,
259261
build_dep_graph: false,
260262
dump_dep_graph: false,
@@ -629,6 +631,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
629631
"run all passes except translation; no output"),
630632
treat_err_as_bug: bool = (false, parse_bool,
631633
"treat all errors that occur as bugs"),
634+
continue_parse_after_error: bool = (false, parse_bool,
635+
"attempt to recover from parse errors (experimental)"),
632636
incr_comp: bool = (false, parse_bool,
633637
"enable incremental compilation (experimental)"),
634638
dump_dep_graph: bool = (false, parse_bool,
@@ -1039,6 +1043,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
10391043
let parse_only = debugging_opts.parse_only;
10401044
let no_trans = debugging_opts.no_trans;
10411045
let treat_err_as_bug = debugging_opts.treat_err_as_bug;
1046+
let continue_parse_after_error = debugging_opts.continue_parse_after_error;
10421047
let mir_opt_level = debugging_opts.mir_opt_level.unwrap_or(1);
10431048
let incremental_compilation = debugging_opts.incr_comp;
10441049
let dump_dep_graph = debugging_opts.dump_dep_graph;
@@ -1218,6 +1223,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
12181223
parse_only: parse_only,
12191224
no_trans: no_trans,
12201225
treat_err_as_bug: treat_err_as_bug,
1226+
continue_parse_after_error: continue_parse_after_error,
12211227
mir_opt_level: mir_opt_level,
12221228
build_dep_graph: incremental_compilation || dump_dep_graph,
12231229
dump_dep_graph: dump_dep_graph,

src/librustc_driver/driver.rs

+4
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,8 @@ pub fn phase_1_parse_input(sess: &Session, cfg: ast::CrateConfig, input: &Input)
421421
// memory, but they do not restore the initial state.
422422
syntax::ext::mtwt::reset_tables();
423423
token::reset_ident_interner();
424+
let continue_after_error = sess.opts.continue_parse_after_error;
425+
sess.diagnostic().set_continue_after_error(continue_after_error);
424426

425427
let krate = time(sess.time_passes(), "parsing", || {
426428
match *input {
@@ -436,6 +438,8 @@ pub fn phase_1_parse_input(sess: &Session, cfg: ast::CrateConfig, input: &Input)
436438
}
437439
});
438440

441+
sess.diagnostic().set_continue_after_error(true);
442+
439443
if sess.opts.debugging_opts.ast_json_noexpand {
440444
println!("{}", json::as_json(&krate));
441445
}

src/libsyntax/errors/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ pub struct Handler {
370370
emit: RefCell<Box<Emitter>>,
371371
pub can_emit_warnings: bool,
372372
treat_err_as_bug: bool,
373+
continue_after_error: Cell<bool>,
373374
delayed_span_bug: RefCell<Option<(MultiSpan, String)>>,
374375
}
375376

@@ -392,10 +393,15 @@ impl Handler {
392393
emit: RefCell::new(e),
393394
can_emit_warnings: can_emit_warnings,
394395
treat_err_as_bug: treat_err_as_bug,
396+
continue_after_error: Cell::new(true),
395397
delayed_span_bug: RefCell::new(None),
396398
}
397399
}
398400

401+
pub fn set_continue_after_error(&self, continue_after_error: bool) {
402+
self.continue_after_error.set(continue_after_error);
403+
}
404+
399405
pub fn struct_dummy<'a>(&'a self) -> DiagnosticBuilder<'a> {
400406
DiagnosticBuilder::new(&self.emit, Level::Cancelled, "")
401407
}
@@ -612,6 +618,7 @@ impl Handler {
612618
lvl: Level) {
613619
if lvl == Warning && !self.can_emit_warnings { return }
614620
self.emit.borrow_mut().emit(msp, msg, None, lvl);
621+
if !self.continue_after_error.get() { self.abort_if_errors(); }
615622
}
616623
pub fn emit_with_code(&self,
617624
msp: Option<&MultiSpan>,
@@ -620,10 +627,12 @@ impl Handler {
620627
lvl: Level) {
621628
if lvl == Warning && !self.can_emit_warnings { return }
622629
self.emit.borrow_mut().emit(msp, msg, Some(code), lvl);
630+
if !self.continue_after_error.get() { self.abort_if_errors(); }
623631
}
624632
pub fn custom_emit(&self, rsp: RenderSpan, msg: &str, lvl: Level) {
625633
if lvl == Warning && !self.can_emit_warnings { return }
626634
self.emit.borrow_mut().custom_emit(&rsp, msg, lvl);
635+
if !self.continue_after_error.get() { self.abort_if_errors(); }
627636
}
628637
}
629638

src/libsyntax/parse/parser.rs

+38-10
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ pub struct Parser<'a> {
261261
/// the previous token or None (only stashed sometimes).
262262
pub last_token: Option<Box<token::Token>>,
263263
last_token_interpolated: bool,
264+
last_token_eof: bool,
264265
pub buffer: [TokenAndSpan; 4],
265266
pub buffer_start: isize,
266267
pub buffer_end: isize,
@@ -369,6 +370,7 @@ impl<'a> Parser<'a> {
369370
last_span: span,
370371
last_token: None,
371372
last_token_interpolated: false,
373+
last_token_eof: false,
372374
buffer: [
373375
placeholder.clone(),
374376
placeholder.clone(),
@@ -939,7 +941,9 @@ impl<'a> Parser<'a> {
939941
{
940942
try!(self.expect(bra));
941943
let result = self.parse_seq_to_before_end(ket, sep, f);
942-
self.bump();
944+
if self.token == *ket {
945+
self.bump();
946+
}
943947
Ok(result)
944948
}
945949

@@ -982,6 +986,15 @@ impl<'a> Parser<'a> {
982986

983987
/// Advance the parser by one token
984988
pub fn bump(&mut self) {
989+
if self.last_token_eof {
990+
// Bumping after EOF is a bad sign, usually an infinite loop.
991+
self.bug("attempted to bump the parser past EOF (may be stuck in a loop)");
992+
}
993+
994+
if self.token == token::Eof {
995+
self.last_token_eof = true;
996+
}
997+
985998
self.last_span = self.span;
986999
// Stash token for error recovery (sometimes; clone is not necessarily cheap).
9871000
self.last_token = if self.token.is_ident() ||
@@ -1265,15 +1278,21 @@ impl<'a> Parser<'a> {
12651278
Ok(cua) => cua,
12661279
Err(e) => {
12671280
loop {
1268-
p.bump();
1269-
if p.token == token::Semi {
1270-
p.bump();
1271-
break;
1272-
}
1281+
match p.token {
1282+
token::Eof => break,
12731283

1274-
if p.token == token::OpenDelim(token::DelimToken::Brace) {
1275-
try!(p.parse_token_tree());
1276-
break;
1284+
token::CloseDelim(token::Brace) |
1285+
token::Semi => {
1286+
p.bump();
1287+
break;
1288+
}
1289+
1290+
token::OpenDelim(token::Brace) => {
1291+
try!(p.parse_token_tree());
1292+
break;
1293+
}
1294+
1295+
_ => p.bump()
12771296
}
12781297
}
12791298

@@ -3790,7 +3809,9 @@ impl<'a> Parser<'a> {
37903809
fn recover_stmt_(&mut self, break_on_semi: SemiColonMode) {
37913810
let mut brace_depth = 0;
37923811
let mut bracket_depth = 0;
3812+
debug!("recover_stmt_ enter loop");
37933813
loop {
3814+
debug!("recover_stmt_ loop {:?}", self.token);
37943815
match self.token {
37953816
token::OpenDelim(token::DelimToken::Brace) => {
37963817
brace_depth += 1;
@@ -3802,6 +3823,7 @@ impl<'a> Parser<'a> {
38023823
}
38033824
token::CloseDelim(token::DelimToken::Brace) => {
38043825
if brace_depth == 0 {
3826+
debug!("recover_stmt_ return - close delim {:?}", self.token);
38053827
return;
38063828
}
38073829
brace_depth -= 1;
@@ -3814,12 +3836,16 @@ impl<'a> Parser<'a> {
38143836
}
38153837
self.bump();
38163838
}
3817-
token::Eof => return,
3839+
token::Eof => {
3840+
debug!("recover_stmt_ return - Eof");
3841+
return;
3842+
}
38183843
token::Semi => {
38193844
self.bump();
38203845
if break_on_semi == SemiColonMode::Break &&
38213846
brace_depth == 0 &&
38223847
bracket_depth == 0 {
3848+
debug!("recover_stmt_ return - Semi");
38233849
return;
38243850
}
38253851
}
@@ -4008,6 +4034,8 @@ impl<'a> Parser<'a> {
40084034
while !self.eat(&token::CloseDelim(token::Brace)) {
40094035
let Spanned {node, span} = if let Some(s) = self.parse_stmt_() {
40104036
s
4037+
} else if self.token == token::Eof {
4038+
break;
40114039
} else {
40124040
// Found only `;` or `}`.
40134041
continue;

src/test/compile-fail/issue-12560-2.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// compile-flags: -Z continue-parse-after-error
12+
1113
// For style and consistency reasons, non-parametrized enum variants must
1214
// be used simply as `ident` instead of `ident ()`.
1315
// This test-case covers enum matching.

src/test/compile-fail/issue-28433.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// compile-flags: -Z continue-parse-after-error
12+
1113
enum bird {
1214
pub duck,
1315
//~^ ERROR: expected identifier, found keyword `pub`

src/test/compile-fail/issue-30715.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// compile-flags: -Z continue-parse-after-error
12+
1113
macro_rules! parallel {
1214
(
1315
// If future has `pred`/`moelarry` fragments (where "pred" is

src/test/compile-fail/macro-incomplete-parse.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// compile-flags: -Z continue-parse-after-error
12+
1113
macro_rules! ignored_item {
1214
() => {
1315
fn foo() {}

src/test/compile-fail/parse-error-correct.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// compile-flags: -Z continue-parse-after-error
12+
1113
// Test that the parser is error correcting missing idents. Despite a parsing
1214
// error (or two), we still run type checking (and don't get extra errors there).
1315

src/test/compile-fail/parser-recovery-1.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// compile-flags: -Z continue-parse-after-error
12+
1113
// Test that we can recover from missing braces in the parser.
1214

1315
trait Foo {

src/test/compile-fail/parser-recovery-2.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// compile-flags: -Z continue-parse-after-error
12+
1113
// Test that we can recover from mismatched braces in the parser.
1214

1315
trait Foo {

src/test/compile-fail/self_type_keyword.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// compile-flags: -Z continue-parse-after-error
12+
1113
struct Self;
1214
//~^ ERROR expected identifier, found keyword `Self`
1315

src/test/parse-fail/ascii-only-character-escape.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-flags: -Z parse-only
11+
// compile-flags: -Z parse-only -Z continue-parse-after-error
1212

1313
fn main() {
1414
let x = "\x80"; //~ ERROR may only be used

src/test/parse-fail/bad-char-literals.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-flags: -Z parse-only
11+
// compile-flags: -Z parse-only -Z continue-parse-after-error
1212

1313
// ignore-tidy-cr
1414
// ignore-tidy-tab

src/test/parse-fail/bad-lit-suffixes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-flags: -Z parse-only
11+
// compile-flags: -Z parse-only -Z continue-parse-after-error
1212

1313

1414
extern

src/test/parse-fail/byte-literals.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-flags: -Z parse-only
11+
// compile-flags: -Z parse-only -Z continue-parse-after-error
1212

1313

1414
// ignore-tidy-tab

src/test/parse-fail/byte-string-literals.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-flags: -Z parse-only
11+
// compile-flags: -Z parse-only -Z continue-parse-after-error
1212

1313

1414
// ignore-tidy-tab

src/test/parse-fail/issue-10412.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-flags: -Z parse-only
11+
// compile-flags: -Z parse-only -Z continue-parse-after-error
1212

1313

1414
trait Serializable<'self, T> { //~ ERROR no longer a special lifetime

src/test/parse-fail/issue-10636-2.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,4 @@ pub fn trace_option(option: Option<isize>) { //~ HELP did you mean to close this
1717
option.map(|some| 42; //~ NOTE: unclosed delimiter
1818
//~^ ERROR: expected one of
1919
} //~ ERROR: incorrect close delimiter
20-
//~^ ERROR: expected one of
21-
//~ ERROR: this file contains an un-closed delimiter
20+
//~^ ERROR: unexpected token

src/test/parse-fail/issue-23620-invalid-escapes.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// compile-flags: -Z parse-only -Z continue-parse-after-error
12+
1113
fn main() {
1214
let _ = b"\u{a66e}";
1315
//~^ ERROR unicode escape sequences cannot be used as a byte or in a byte string

src/test/parse-fail/issue-32446.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -Z parse-only
12+
13+
fn main() {}
14+
15+
// This used to end up in an infite loop trying to bump past EOF.
16+
trait T { ... } //~ ERROR

src/test/parse-fail/lex-bad-binary-literal.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// compile-flags: -Z parse-only -Z continue-parse-after-error
12+
1113
fn main() {
1214
0b121; //~ ERROR invalid digit for a base 2 literal
1315
0b10_10301; //~ ERROR invalid digit for a base 2 literal

src/test/parse-fail/lex-bad-char-literals-1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-flags: -Z parse-only
11+
// compile-flags: -Z parse-only -Z continue-parse-after-error
1212
static c3: char =
1313
'\x1' //~ ERROR: numeric character escape is too short
1414
;

0 commit comments

Comments
 (0)