Skip to content

Commit 82e1750

Browse files
committed
Add -Z dont-buffer-diagnostics, a way to force NLL to immediately emit its diagnostics.
This is mainly intended for `rustc` developers who want to see a diagnostic in its original context in the control flow. Two uses cases for that are: * `-Z treat-err-as-bug` which then allows extraction of a stack-trace to the origin of the error (a case that is so important that we make that flag imply this one, effectively). * `RUST_LOG=... rustc`, in which case it is often useful to see the logging statements that occurred immediately prior to the point where the diagnostic was signalled. Drive-by: Added some documentation pointing future devs at HandlerFlags, and documented the fields of `HandlerFlags` itself.
1 parent f789b6b commit 82e1750

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

src/librustc/session/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
13311331
"disable user provided type assertion in NLL"),
13321332
nll_dont_emit_read_for_match: bool = (false, parse_bool, [UNTRACKED],
13331333
"in match codegen, do not include ReadForMatch statements (used by mir-borrowck)"),
1334+
dont_buffer_diagnostics: bool = (false, parse_bool, [UNTRACKED],
1335+
"emit diagnostics rather than buffering (breaks NLL error downgrading, sorting)."),
13341336
polonius: bool = (false, parse_bool, [UNTRACKED],
13351337
"enable polonius-based borrow-checker"),
13361338
codegen_time_graph: bool = (false, parse_bool, [UNTRACKED],

src/librustc/session/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,7 @@ pub fn build_session_with_source_map(
10121012
let can_emit_warnings = !(warnings_allow || cap_lints_allow);
10131013

10141014
let treat_err_as_bug = sopts.debugging_opts.treat_err_as_bug;
1015+
let dont_buffer_diagnostics = sopts.debugging_opts.dont_buffer_diagnostics;
10151016
let report_delayed_bugs = sopts.debugging_opts.report_delayed_bugs;
10161017

10171018
let external_macro_backtrace = sopts.debugging_opts.external_macro_backtrace;
@@ -1059,6 +1060,7 @@ pub fn build_session_with_source_map(
10591060
can_emit_warnings,
10601061
treat_err_as_bug,
10611062
report_delayed_bugs,
1063+
dont_buffer_diagnostics,
10621064
external_macro_backtrace,
10631065
..Default::default()
10641066
},

src/librustc_errors/diagnostic_builder.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ use std::thread::panicking;
2121
use syntax_pos::{MultiSpan, Span};
2222

2323
/// Used for emitting structured error messages and other diagnostic information.
24+
///
25+
/// If there is some state in a downstream crate you would like to
26+
/// access in the methods of `DiagnosticBuilder` here, consider
27+
/// extending `HandlerFlags`, accessed via `self.handler.flags`.
2428
#[must_use]
2529
#[derive(Clone)]
2630
pub struct DiagnosticBuilder<'a> {
@@ -89,8 +93,14 @@ impl<'a> DiagnosticBuilder<'a> {
8993
self.cancel();
9094
}
9195

92-
/// Buffers the diagnostic for later emission.
93-
pub fn buffer(self, buffered_diagnostics: &mut Vec<Diagnostic>) {
96+
/// Buffers the diagnostic for later emission, unless handler
97+
/// has disabled such buffering.
98+
pub fn buffer(mut self, buffered_diagnostics: &mut Vec<Diagnostic>) {
99+
if self.handler.flags.dont_buffer_diagnostics || self.handler.flags.treat_err_as_bug {
100+
self.emit();
101+
return;
102+
}
103+
94104
// We need to use `ptr::read` because `DiagnosticBuilder`
95105
// implements `Drop`.
96106
let diagnostic;

src/librustc_errors/lib.rs

+11
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,20 @@ thread_local!(pub static TRACK_DIAGNOSTICS: Cell<fn(&Diagnostic)> =
303303

304304
#[derive(Default)]
305305
pub struct HandlerFlags {
306+
/// If false, warning-level lints are suppressed.
307+
/// (rustc: see `--allow warnings` and `--cap-lints`)
306308
pub can_emit_warnings: bool,
309+
/// If true, error-level diagnostics are upgraded to bug-level.
310+
/// (rustc: see `-Z treat-err-as-bug`)
307311
pub treat_err_as_bug: bool,
312+
/// If true, immediately emit diagnostics that would otherwise be buffered.
313+
/// (rustc: see `-Z dont-buffer-diagnostics` and `-Z treat-err-as-bug`)
314+
pub dont_buffer_diagnostics: bool,
315+
/// If true, immediately print bugs registered with `delay_span_bug`.
316+
/// (rustc: see `-Z report-delayed-bugs`)
308317
pub report_delayed_bugs: bool,
318+
/// show macro backtraces even for non-local macros.
319+
/// (rustc: see `-Z external-macro-backtrace`)
309320
pub external_macro_backtrace: bool,
310321
}
311322

0 commit comments

Comments
 (0)