Skip to content

Commit 72d4c1e

Browse files
authored
Rollup merge of #58984 - estebank:multi-treat-err-as-bug, r=oli-obk
Teach `-Z treat-err-as-bug` to take a number of errors to emit `-Z treat-err-as-bug` will cause `rustc` to panic after the first error is reported, like previously. `-Z treat-err-as-bug=2` will cause `rustc` to panic after 2 errors have been reported. Fix #58983.
2 parents c51c90c + 29716ef commit 72d4c1e

File tree

13 files changed

+56
-27
lines changed

13 files changed

+56
-27
lines changed

src/librustc/session/config.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,8 @@ macro_rules! options {
816816
Some("crate=integer");
817817
pub const parse_unpretty: Option<&str> =
818818
Some("`string` or `string=string`");
819+
pub const parse_treat_err_as_bug: Option<&str> =
820+
Some("either no value or a number bigger than 0");
819821
pub const parse_lto: Option<&str> =
820822
Some("either a boolean (`yes`, `no`, `on`, `off`, etc), `thin`, \
821823
`fat`, or omitted");
@@ -1022,6 +1024,13 @@ macro_rules! options {
10221024
}
10231025
}
10241026

1027+
fn parse_treat_err_as_bug(slot: &mut Option<usize>, v: Option<&str>) -> bool {
1028+
match v {
1029+
Some(s) => { *slot = s.parse().ok().filter(|&x| x != 0); slot.unwrap_or(0) != 0 }
1030+
None => { *slot = Some(1); true }
1031+
}
1032+
}
1033+
10251034
fn parse_lto(slot: &mut LtoCli, v: Option<&str>) -> bool {
10261035
if v.is_some() {
10271036
let mut bool_arg = None;
@@ -1236,8 +1245,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
12361245
"load proc macros for both target and host, but only link to the target"),
12371246
no_codegen: bool = (false, parse_bool, [TRACKED],
12381247
"run all passes except codegen; no output"),
1239-
treat_err_as_bug: bool = (false, parse_bool, [TRACKED],
1240-
"treat all errors that occur as bugs"),
1248+
treat_err_as_bug: Option<usize> = (None, parse_treat_err_as_bug, [TRACKED],
1249+
"treat error number `val` that occurs as bug"),
12411250
report_delayed_bugs: bool = (false, parse_bool, [TRACKED],
12421251
"immediately print bugs registered with `delay_span_bug`"),
12431252
external_macro_backtrace: bool = (false, parse_bool, [UNTRACKED],
@@ -3214,7 +3223,7 @@ mod tests {
32143223
assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());
32153224

32163225
opts = reference.clone();
3217-
opts.debugging_opts.treat_err_as_bug = true;
3226+
opts.debugging_opts.treat_err_as_bug = Some(1);
32183227
assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());
32193228

32203229
opts = reference.clone();

src/librustc/session/mod.rs

+2-2
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

+1-1
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

+2-2
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_driver/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ fn test_env_with_pool<F>(
113113
) where
114114
F: FnOnce(Env),
115115
{
116-
let diagnostic_handler = errors::Handler::with_emitter(true, false, emitter);
116+
let diagnostic_handler = errors::Handler::with_emitter(true, None, emitter);
117117
let sess = session::build_session_(
118118
options,
119119
None,

src/librustc_errors/diagnostic_builder.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ 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 ||
107+
self.handler.flags.treat_err_as_bug.is_some()
108+
{
107109
self.emit();
108110
return;
109111
}

src/librustc_errors/lib.rs

+27-9
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,8 +516,20 @@ impl Handler {
516516
}
517517

518518
fn panic_if_treat_err_as_bug(&self) {
519-
if self.flags.treat_err_as_bug {
520-
panic!("encountered error with `-Z treat_err_as_bug");
519+
if self.treat_err_as_bug() {
520+
let s = match (self.err_count(), self.flags.treat_err_as_bug.unwrap_or(0)) {
521+
(0, _) => return,
522+
(1, 1) => "aborting due to `-Z treat-err-as-bug=1`".to_string(),
523+
(1, _) => return,
524+
(count, as_bug) => {
525+
format!(
526+
"aborting after {} errors due to `-Z treat-err-as-bug={}`",
527+
count,
528+
as_bug,
529+
)
530+
}
531+
};
532+
panic!(s);
521533
}
522534
}
523535

@@ -558,7 +570,7 @@ impl Handler {
558570
panic!(ExplicitBug);
559571
}
560572
pub fn delay_span_bug<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
561-
if self.flags.treat_err_as_bug {
573+
if self.treat_err_as_bug() {
562574
// FIXME: don't abort here if report_delayed_bugs is off
563575
self.span_bug(sp, msg);
564576
}
@@ -593,14 +605,14 @@ impl Handler {
593605
DiagnosticBuilder::new(self, FailureNote, msg).emit()
594606
}
595607
pub fn fatal(&self, msg: &str) -> FatalError {
596-
if self.flags.treat_err_as_bug {
608+
if self.treat_err_as_bug() {
597609
self.bug(msg);
598610
}
599611
DiagnosticBuilder::new(self, Fatal, msg).emit();
600612
FatalError
601613
}
602614
pub fn err(&self, msg: &str) {
603-
if self.flags.treat_err_as_bug {
615+
if self.treat_err_as_bug() {
604616
self.bug(msg);
605617
}
606618
let mut db = DiagnosticBuilder::new(self, Error, msg);
@@ -610,6 +622,9 @@ impl Handler {
610622
let mut db = DiagnosticBuilder::new(self, Warning, msg);
611623
db.emit();
612624
}
625+
fn treat_err_as_bug(&self) -> bool {
626+
self.flags.treat_err_as_bug.map(|c| self.err_count() >= c).unwrap_or(false)
627+
}
613628
pub fn note_without_error(&self, msg: &str) {
614629
let mut db = DiagnosticBuilder::new(self, Note, msg);
615630
db.emit();
@@ -624,8 +639,8 @@ impl Handler {
624639
}
625640

626641
fn bump_err_count(&self) {
627-
self.panic_if_treat_err_as_bug();
628642
self.err_count.fetch_add(1, SeqCst);
643+
self.panic_if_treat_err_as_bug();
629644
}
630645

631646
pub fn err_count(&self) -> usize {
@@ -642,6 +657,9 @@ impl Handler {
642657
1 => "aborting due to previous error".to_string(),
643658
_ => format!("aborting due to {} previous errors", self.err_count())
644659
};
660+
if self.treat_err_as_bug() {
661+
return;
662+
}
645663

646664
let _ = self.fatal(&s);
647665

src/librustdoc/core.rs

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

src/librustdoc/test.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub fn run(mut options: Options) -> isize {
6767
let source_map = Lrc::new(SourceMap::new(sessopts.file_path_mapping()));
6868
let handler =
6969
errors::Handler::with_tty_emitter(ColorConfig::Auto,
70-
true, false,
70+
true, None,
7171
Some(source_map.clone()));
7272

7373
let mut sess = session::build_session_(
@@ -272,7 +272,7 @@ fn run_test(test: &str, cratename: &str, filename: &FileName, line: usize,
272272
false);
273273

274274
// Compile the code
275-
let diagnostic_handler = errors::Handler::with_emitter(true, false, box emitter);
275+
let diagnostic_handler = errors::Handler::with_emitter(true, None, box emitter);
276276

277277
let mut sess = session::build_session_(
278278
sessopts, None, diagnostic_handler, source_map, Default::default(),
@@ -424,7 +424,7 @@ pub fn make_test(s: &str,
424424
// send all the errors that libsyntax emits directly into a `Sink` instead of stderr.
425425
let cm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
426426
let emitter = EmitterWriter::new(box io::sink(), None, false, false);
427-
let handler = Handler::with_emitter(false, false, box emitter);
427+
let handler = Handler::with_emitter(false, None, box emitter);
428428
let sess = ParseSess::with_span_handler(handler, cm);
429429

430430
let mut found_main = false;

src/libsyntax/parse/lexer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1920,7 +1920,7 @@ mod tests {
19201920
false,
19211921
false);
19221922
ParseSess {
1923-
span_diagnostic: errors::Handler::with_emitter(true, false, Box::new(emitter)),
1923+
span_diagnostic: errors::Handler::with_emitter(true, None, Box::new(emitter)),
19241924
unstable_features: UnstableFeatures::from_environment(),
19251925
config: CrateConfig::default(),
19261926
included_mod_stack: Lock::new(Vec::new()),

src/libsyntax/parse/mod.rs

+1-1
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
}

src/libsyntax/test_snippet.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fn test_harness(file_text: &str, span_labels: Vec<SpanLabel>, expected_output: &
5858
Some(source_map.clone()),
5959
false,
6060
false);
61-
let handler = Handler::with_emitter(true, false, Box::new(emitter));
61+
let handler = Handler::with_emitter(true, None, Box::new(emitter));
6262
handler.span_err(msp, "foo");
6363

6464
assert!(expected_output.chars().next() == Some('\n'),

src/test/run-make-fulldeps/treat-err-as-bug/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
all:
44
$(RUSTC) err.rs -Z treat-err-as-bug 2>&1 \
5-
| $(CGREP) "panicked at 'encountered error with \`-Z treat_err_as_bug'"
5+
| $(CGREP) "panicked at 'aborting due to \`-Z treat-err-as-bug=1\`'"

0 commit comments

Comments
 (0)