Skip to content

Commit e175000

Browse files
committed
Put in -Z continue-parse-after-error
This works by adding a boolean flag, `continue_after_error`, to `syntax::errors::Handler` that can be imperatively set to `true` or `false` via a new `fn set_continue_after_error`. The flag starts off true (since we generally try to recover from compiler errors, and `Handler` is shared across all phases). Then, during the `phase_1_parse_input`, we consult the setting of the `-Z continue-parse-after-error` debug flag to determine whether we should leave the flag set to `true` or should change it to `false`. ---- (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.)
1 parent c016754 commit e175000

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
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

0 commit comments

Comments
 (0)