Skip to content

Commit 74546e8

Browse files
committed
Rollup merge of rust-lang#32494 - pnkfelix:gate-parser-recovery-via-debugflag, r=nrc
Gate parser recovery via debugflag Gate parser recovery via debugflag Put in `-Z continue_parse_after_error` This works by adding a method, `fn abort_if_no_parse_recovery`, to the diagnostic handler in `syntax::errors`, and calling it after each error is emitted in the parser. (We might consider adding a debugflag to do such aborts in other places where we are currently attempting recovery, such as resolve, but I think the parser is the really important case to handle in the face of rust-lang#31994 and the parser bugs of varying degrees that were injected by parse error recovery.) r? @nikomatsakis
2 parents 9957081 + e1d8ad3 commit 74546e8

24 files changed

+50
-11
lines changed

src/librustc/session/config.rs

+6
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ pub struct Options {
136136
pub no_trans: bool,
137137
pub error_format: ErrorOutputType,
138138
pub treat_err_as_bug: bool,
139+
pub continue_parse_after_error: bool,
139140
pub mir_opt_level: usize,
140141

141142
/// if true, build up the dep-graph
@@ -257,6 +258,7 @@ pub fn basic_options() -> Options {
257258
parse_only: false,
258259
no_trans: false,
259260
treat_err_as_bug: false,
261+
continue_parse_after_error: false,
260262
mir_opt_level: 1,
261263
build_dep_graph: false,
262264
dump_dep_graph: false,
@@ -631,6 +633,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
631633
"run all passes except translation; no output"),
632634
treat_err_as_bug: bool = (false, parse_bool,
633635
"treat all errors that occur as bugs"),
636+
continue_parse_after_error: bool = (false, parse_bool,
637+
"attempt to recover from parse errors (experimental)"),
634638
incr_comp: bool = (false, parse_bool,
635639
"enable incremental compilation (experimental)"),
636640
dump_dep_graph: bool = (false, parse_bool,
@@ -1045,6 +1049,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
10451049
let parse_only = debugging_opts.parse_only;
10461050
let no_trans = debugging_opts.no_trans;
10471051
let treat_err_as_bug = debugging_opts.treat_err_as_bug;
1052+
let continue_parse_after_error = debugging_opts.continue_parse_after_error;
10481053
let mir_opt_level = debugging_opts.mir_opt_level.unwrap_or(1);
10491054
let incremental_compilation = debugging_opts.incr_comp;
10501055
let dump_dep_graph = debugging_opts.dump_dep_graph;
@@ -1224,6 +1229,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
12241229
parse_only: parse_only,
12251230
no_trans: no_trans,
12261231
treat_err_as_bug: treat_err_as_bug,
1232+
continue_parse_after_error: continue_parse_after_error,
12271233
mir_opt_level: mir_opt_level,
12281234
build_dep_graph: incremental_compilation || dump_dep_graph,
12291235
dump_dep_graph: dump_dep_graph,

src/librustc_driver/driver.rs

+4
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,8 @@ pub fn phase_1_parse_input<'a>(sess: &'a Session,
429429
// memory, but they do not restore the initial state.
430430
syntax::ext::mtwt::reset_tables();
431431
token::reset_ident_interner();
432+
let continue_after_error = sess.opts.continue_parse_after_error;
433+
sess.diagnostic().set_continue_after_error(continue_after_error);
432434

433435
let krate = time(sess.time_passes(), "parsing", || {
434436
match *input {
@@ -444,6 +446,8 @@ pub fn phase_1_parse_input<'a>(sess: &'a Session,
444446
}
445447
})?;
446448

449+
sess.diagnostic().set_continue_after_error(true);
450+
447451
if sess.opts.debugging_opts.ast_json_noexpand {
448452
println!("{}", json::as_json(&krate));
449453
}

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/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-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/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
;

src/test/parse-fail/lex-bad-numeric-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
fn main() {
1414
0o1.0; //~ ERROR: octal float literal is not supported

src/test/parse-fail/lex-bare-cr-string-literal-doc-comment.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

src/test/parse-fail/new-unicode-escapes-4.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
pub fn main() {
1414
let s = "\u{lol}";

src/test/parse-fail/no-unsafe-self.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
trait A {
1414
fn foo(*mut self); //~ ERROR cannot pass self by raw pointer

0 commit comments

Comments
 (0)