Skip to content

Commit 7a55a00

Browse files
committed
Make -Z treat-err-as-bug take a number of errors to be emitted
`-Z treat-err-as-bug=0` will cause `rustc` to panic after the first error is reported. `-Z treat-err-as-bug=2` will cause `rustc` to panic after 3 errors have been reported.
1 parent f22dca0 commit 7a55a00

File tree

8 files changed

+28
-19
lines changed

8 files changed

+28
-19
lines changed

src/librustc/session/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
12341234
"parse only; do not compile, assemble, or link"),
12351235
no_codegen: bool = (false, parse_bool, [TRACKED],
12361236
"run all passes except codegen; no output"),
1237-
treat_err_as_bug: bool = (false, parse_bool, [TRACKED],
1237+
treat_err_as_bug: Option<usize> = (None, parse_opt_uint, [TRACKED],
12381238
"treat all errors that occur as bugs"),
12391239
report_delayed_bugs: bool = (false, parse_bool, [TRACKED],
12401240
"immediately print bugs registered with `delay_span_bug`"),
@@ -3212,7 +3212,7 @@ mod tests {
32123212
assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());
32133213

32143214
opts = reference.clone();
3215-
opts.debugging_opts.treat_err_as_bug = true;
3215+
opts.debugging_opts.treat_err_as_bug = Some(1);
32163216
assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());
32173217

32183218
opts = reference.clone();

src/librustc/session/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,7 +1315,7 @@ pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
13151315
Box::new(EmitterWriter::stderr(color_config, None, true, false))
13161316
}
13171317
};
1318-
let handler = errors::Handler::with_emitter(true, false, emitter);
1318+
let handler = errors::Handler::with_emitter(true, None, emitter);
13191319
handler.emit(&MultiSpan::new(), msg, errors::Level::Fatal);
13201320
errors::FatalError.raise();
13211321
}
@@ -1330,7 +1330,7 @@ pub fn early_warn(output: config::ErrorOutputType, msg: &str) {
13301330
Box::new(EmitterWriter::stderr(color_config, None, true, false))
13311331
}
13321332
};
1333-
let handler = errors::Handler::with_emitter(true, false, emitter);
1333+
let handler = errors::Handler::with_emitter(true, None, emitter);
13341334
handler.emit(&MultiSpan::new(), msg, errors::Level::Warning);
13351335
}
13361336

src/librustc_codegen_ssa/back/write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ pub struct CodegenContext<B: WriteBackendMethods> {
247247

248248
impl<B: WriteBackendMethods> CodegenContext<B> {
249249
pub fn create_diag_handler(&self) -> Handler {
250-
Handler::with_emitter(true, false, Box::new(self.diag_emitter.clone()))
250+
Handler::with_emitter(true, None, Box::new(self.diag_emitter.clone()))
251251
}
252252

253253
pub fn config(&self, kind: ModuleKind) -> &ModuleConfig {

src/librustc_driver/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ pub fn run<F>(run_compiler: F) -> isize
148148
true,
149149
false
150150
);
151-
let handler = errors::Handler::with_emitter(true, false, Box::new(emitter));
151+
let handler = errors::Handler::with_emitter(true, None, Box::new(emitter));
152152
handler.emit(&MultiSpan::new(),
153153
"aborting due to previous error(s)",
154154
errors::Level::Fatal);
@@ -1327,7 +1327,7 @@ pub fn monitor<F: FnOnce() + Send + 'static>(f: F) -> Result<(), CompilationFail
13271327
None,
13281328
false,
13291329
false));
1330-
let handler = errors::Handler::with_emitter(true, false, emitter);
1330+
let handler = errors::Handler::with_emitter(true, None, emitter);
13311331

13321332
// a .span_bug or .bug call has already printed what
13331333
// it wants to print.

src/librustc_errors/diagnostic_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl<'a> DiagnosticBuilder<'a> {
103103
/// Buffers the diagnostic for later emission, unless handler
104104
/// has disabled such buffering.
105105
pub fn buffer(mut self, buffered_diagnostics: &mut Vec<Diagnostic>) {
106-
if self.handler.flags.dont_buffer_diagnostics || self.handler.flags.treat_err_as_bug {
106+
if self.handler.flags.dont_buffer_diagnostics || self.handler.treat_err_as_bug() {
107107
self.emit();
108108
return;
109109
}

src/librustc_errors/lib.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ pub struct HandlerFlags {
330330
pub can_emit_warnings: bool,
331331
/// If true, error-level diagnostics are upgraded to bug-level.
332332
/// (rustc: see `-Z treat-err-as-bug`)
333-
pub treat_err_as_bug: bool,
333+
pub treat_err_as_bug: Option<usize>,
334334
/// If true, immediately emit diagnostics that would otherwise be buffered.
335335
/// (rustc: see `-Z dont-buffer-diagnostics` and `-Z treat-err-as-bug`)
336336
pub dont_buffer_diagnostics: bool,
@@ -360,7 +360,7 @@ impl Drop for Handler {
360360
impl Handler {
361361
pub fn with_tty_emitter(color_config: ColorConfig,
362362
can_emit_warnings: bool,
363-
treat_err_as_bug: bool,
363+
treat_err_as_bug: Option<usize>,
364364
cm: Option<Lrc<SourceMapperDyn>>)
365365
-> Handler {
366366
Handler::with_tty_emitter_and_flags(
@@ -382,7 +382,7 @@ impl Handler {
382382
}
383383

384384
pub fn with_emitter(can_emit_warnings: bool,
385-
treat_err_as_bug: bool,
385+
treat_err_as_bug: Option<usize>,
386386
e: Box<dyn Emitter + sync::Send>)
387387
-> Handler {
388388
Handler::with_emitter_and_flags(
@@ -516,7 +516,7 @@ impl Handler {
516516
}
517517

518518
fn panic_if_treat_err_as_bug(&self) {
519-
if self.flags.treat_err_as_bug {
519+
if self.treat_err_as_bug() {
520520
panic!("encountered error with `-Z treat_err_as_bug");
521521
}
522522
}
@@ -558,7 +558,7 @@ impl Handler {
558558
panic!(ExplicitBug);
559559
}
560560
pub fn delay_span_bug<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
561-
if self.flags.treat_err_as_bug {
561+
if self.treat_err_as_bug() {
562562
// FIXME: don't abort here if report_delayed_bugs is off
563563
self.span_bug(sp, msg);
564564
}
@@ -593,14 +593,14 @@ impl Handler {
593593
DiagnosticBuilder::new(self, FailureNote, msg).emit()
594594
}
595595
pub fn fatal(&self, msg: &str) -> FatalError {
596-
if self.flags.treat_err_as_bug {
596+
if self.treat_err_as_bug() {
597597
self.bug(msg);
598598
}
599599
DiagnosticBuilder::new(self, Fatal, msg).emit();
600600
FatalError
601601
}
602602
pub fn err(&self, msg: &str) {
603-
if self.flags.treat_err_as_bug {
603+
if self.treat_err_as_bug() {
604604
self.bug(msg);
605605
}
606606
let mut db = DiagnosticBuilder::new(self, Error, msg);
@@ -610,6 +610,9 @@ impl Handler {
610610
let mut db = DiagnosticBuilder::new(self, Warning, msg);
611611
db.emit();
612612
}
613+
fn treat_err_as_bug(&self) -> bool {
614+
self.flags.treat_err_as_bug.map(|c| self.err_count() >= c).unwrap_or(false)
615+
}
613616
pub fn note_without_error(&self, msg: &str) {
614617
let mut db = DiagnosticBuilder::new(self, Note, msg);
615618
db.emit();
@@ -624,8 +627,8 @@ impl Handler {
624627
}
625628

626629
fn bump_err_count(&self) {
627-
self.panic_if_treat_err_as_bug();
628630
self.err_count.fetch_add(1, SeqCst);
631+
self.panic_if_treat_err_as_bug();
629632
}
630633

631634
pub fn err_count(&self) -> usize {
@@ -643,7 +646,13 @@ impl Handler {
643646
_ => format!("aborting due to {} previous errors", self.err_count())
644647
};
645648

646-
let _ = self.fatal(&s);
649+
let _ = if self.treat_err_as_bug() {
650+
self.fatal(&s)
651+
} else {
652+
// only emit one backtrace when using `-Z treat-err-as-bug=X`
653+
DiagnosticBuilder::new(self, Fatal, &s).emit();
654+
FatalError
655+
};
647656

648657
let can_show_explain = self.emitter.borrow().should_show_explain();
649658
let are_there_diagnostics = !self.emitted_diagnostic_codes.borrow().is_empty();

src/librustdoc/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ impl DocAccessLevels for AccessLevels<DefId> {
266266
/// will be created for the handler.
267267
pub fn new_handler(error_format: ErrorOutputType,
268268
source_map: Option<Lrc<source_map::SourceMap>>,
269-
treat_err_as_bug: bool,
269+
treat_err_as_bug: Option<usize>,
270270
ui_testing: bool,
271271
) -> errors::Handler {
272272
// rustdoc doesn't override (or allow to override) anything from this that is relevant here, so

src/libsyntax/parse/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl ParseSess {
5353
let cm = Lrc::new(SourceMap::new(file_path_mapping));
5454
let handler = Handler::with_tty_emitter(ColorConfig::Auto,
5555
true,
56-
false,
56+
None,
5757
Some(cm.clone()));
5858
ParseSess::with_span_handler(handler, cm)
5959
}

0 commit comments

Comments
 (0)