Skip to content

Commit 56c9077

Browse files
committed
Make sure the compiler actually panics on delay_span_bug
Even if that is just happening because of `abort_if_errors`
1 parent 0fc4501 commit 56c9077

File tree

5 files changed

+36
-7
lines changed

5 files changed

+36
-7
lines changed

src/librustc/session/config.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
11931193
"run all passes except codegen; no output"),
11941194
treat_err_as_bug: bool = (false, parse_bool, [TRACKED],
11951195
"treat all errors that occur as bugs"),
1196+
report_delayed_bugs: bool = (false, parse_bool, [TRACKED],
1197+
"immediately print bugs registered with `delay_span_bug`"),
11961198
external_macro_backtrace: bool = (false, parse_bool, [UNTRACKED],
11971199
"show macro backtraces even for non-local macros"),
11981200
teach: bool = (false, parse_bool, [TRACKED],
@@ -3133,6 +3135,10 @@ mod tests {
31333135
opts.debugging_opts.treat_err_as_bug = true;
31343136
assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());
31353137

3138+
opts = reference.clone();
3139+
opts.debugging_opts.report_delayed_bugs = true;
3140+
assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());
3141+
31363142
opts = reference.clone();
31373143
opts.debugging_opts.continue_parse_after_error = true;
31383144
assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());

src/librustc/session/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,7 @@ pub fn build_session_with_codemap(
10001000
let can_emit_warnings = !(warnings_allow || cap_lints_allow);
10011001

10021002
let treat_err_as_bug = sopts.debugging_opts.treat_err_as_bug;
1003+
let report_delayed_bugs = sopts.debugging_opts.report_delayed_bugs;
10031004

10041005
let external_macro_backtrace = sopts.debugging_opts.external_macro_backtrace;
10051006

@@ -1045,6 +1046,7 @@ pub fn build_session_with_codemap(
10451046
errors::HandlerFlags {
10461047
can_emit_warnings,
10471048
treat_err_as_bug,
1049+
report_delayed_bugs,
10481050
external_macro_backtrace,
10491051
..Default::default()
10501052
},

src/librustc_errors/diagnostic_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl<'a> DiagnosticBuilder<'a> {
132132
/// locally in whichever way makes the most sense.
133133
pub fn delay_as_bug(&mut self) {
134134
self.level = Level::Bug;
135-
*self.handler.delayed_span_bug.borrow_mut() = Some(self.diagnostic.clone());
135+
self.handler.delay_as_bug(self.diagnostic.clone());
136136
self.cancel();
137137
}
138138

src/librustc_errors/lib.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ pub struct Handler {
274274
err_count: AtomicUsize,
275275
emitter: Lock<Box<dyn Emitter + sync::Send>>,
276276
continue_after_error: LockCell<bool>,
277-
delayed_span_bug: Lock<Option<Diagnostic>>,
277+
delayed_span_bug: Lock<Vec<Diagnostic>>,
278278

279279
// This set contains the `DiagnosticId` of all emitted diagnostics to avoid
280280
// emitting the same diagnostic with extended help (`--teach`) twice, which
@@ -299,9 +299,25 @@ thread_local!(pub static TRACK_DIAGNOSTICS: Cell<fn(&Diagnostic)> =
299299
pub struct HandlerFlags {
300300
pub can_emit_warnings: bool,
301301
pub treat_err_as_bug: bool,
302+
pub report_delayed_bugs: bool,
302303
pub external_macro_backtrace: bool,
303304
}
304305

306+
impl Drop for Handler {
307+
fn drop(&mut self) {
308+
if self.err_count() == 0 {
309+
let mut bugs = self.delayed_span_bug.borrow_mut();
310+
let has_bugs = !bugs.is_empty();
311+
for bug in bugs.drain(..) {
312+
DiagnosticBuilder::new_diagnostic(self, bug).emit();
313+
}
314+
if has_bugs {
315+
panic!("no errors encountered even though `delay_span_bug` issued");
316+
}
317+
}
318+
}
319+
}
320+
305321
impl Handler {
306322
pub fn with_tty_emitter(color_config: ColorConfig,
307323
can_emit_warnings: bool,
@@ -346,7 +362,7 @@ impl Handler {
346362
err_count: AtomicUsize::new(0),
347363
emitter: Lock::new(e),
348364
continue_after_error: LockCell::new(true),
349-
delayed_span_bug: Lock::new(None),
365+
delayed_span_bug: Lock::new(Vec::new()),
350366
taught_diagnostics: Lock::new(FxHashSet()),
351367
emitted_diagnostic_codes: Lock::new(FxHashSet()),
352368
emitted_diagnostics: Lock::new(FxHashSet()),
@@ -503,11 +519,18 @@ impl Handler {
503519
}
504520
pub fn delay_span_bug<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
505521
if self.flags.treat_err_as_bug {
522+
// FIXME: don't abort here if report_delayed_bugs is off
506523
self.span_bug(sp, msg);
507524
}
508525
let mut diagnostic = Diagnostic::new(Level::Bug, msg);
509526
diagnostic.set_span(sp.into());
510-
*self.delayed_span_bug.borrow_mut() = Some(diagnostic);
527+
self.delay_as_bug(diagnostic);
528+
}
529+
fn delay_as_bug(&self, diagnostic: Diagnostic) {
530+
if self.flags.report_delayed_bugs {
531+
DiagnosticBuilder::new_diagnostic(self, diagnostic.clone()).emit();
532+
}
533+
self.delayed_span_bug.borrow_mut().push(diagnostic);
511534
}
512535
pub fn span_bug_no_panic<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
513536
self.emit(&sp.into(), msg, Bug);
@@ -615,9 +638,6 @@ impl Handler {
615638

616639
pub fn abort_if_errors(&self) {
617640
if self.err_count() == 0 {
618-
if let Some(bug) = self.delayed_span_bug.borrow_mut().take() {
619-
DiagnosticBuilder::new_diagnostic(self, bug).emit();
620-
}
621641
return;
622642
}
623643
FatalError.raise();

src/librustdoc/core.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ pub fn new_handler(error_format: ErrorOutputType, codemap: Option<Lrc<codemap::C
161161
errors::HandlerFlags {
162162
can_emit_warnings: true,
163163
treat_err_as_bug: false,
164+
report_delayed_bugs: false,
164165
external_macro_backtrace: false,
165166
..Default::default()
166167
},

0 commit comments

Comments
 (0)