Skip to content

Commit 1c5d1c7

Browse files
committed
Reinstate emit_stashed_diagnostics in DiagCtxtInner::drop.
I removed it in rust-lang#121206 because I thought thought it wasn't necessary. But then I had to add an `emit_stashed_diagnostics` call elsewhere in rustfmt to avoid the assertion failure (which took two attempts to get right, rust-lang#121487 and rust-lang#121615), and now there's an assertion failure in clippy as well (rust-lang/rust-clippy#12364). So this commit just reinstates the call in `DiagCtxtInner::drop`. It also reverts the rustfmt changes from rust-lang#121487 and rust-lang#121615, though it keeps the tests added for those PRs.
1 parent 759785b commit 1c5d1c7

File tree

3 files changed

+13
-24
lines changed

3 files changed

+13
-24
lines changed

compiler/rustc_errors/src/lib.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -556,12 +556,14 @@ pub struct DiagCtxtFlags {
556556

557557
impl Drop for DiagCtxtInner {
558558
fn drop(&mut self) {
559-
// Any stashed diagnostics should have been handled by
560-
// `emit_stashed_diagnostics` by now.
559+
// For tools using `interface::run_compiler` (e.g. rustc, rustdoc)
560+
// stashed diagnostics will have already been emitted. But for others
561+
// that don't use `interface::run_compiler` (e.g. rustfmt, some clippy
562+
// lints) this fallback is necessary.
561563
//
562564
// Important: it is sound to produce an `ErrorGuaranteed` when stashing
563-
// errors because they are guaranteed to have been emitted by here.
564-
assert!(self.stashed_diagnostics.is_empty());
565+
// errors because they are guaranteed to be emitted here or earlier.
566+
self.emit_stashed_diagnostics();
565567

566568
// Important: it is sound to produce an `ErrorGuaranteed` when emitting
567569
// delayed bugs because they are guaranteed to be emitted here if

src/tools/rustfmt/src/formatting.rs

+6-15
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ fn format_project<T: FormatHandler>(
109109
let main_file = input.file_name();
110110
let input_is_stdin = main_file == FileName::Stdin;
111111

112-
let mut parse_session = ParseSess::new(config)?;
112+
let parse_session = ParseSess::new(config)?;
113113
if config.skip_children() && parse_session.ignore_file(&main_file) {
114114
return Ok(FormatReport::new());
115115
}
@@ -118,20 +118,11 @@ fn format_project<T: FormatHandler>(
118118
let mut report = FormatReport::new();
119119
let directory_ownership = input.to_directory_ownership();
120120

121-
// rustfmt doesn't use `run_compiler` like other tools, so it must emit any
122-
// stashed diagnostics itself, otherwise the `DiagCtxt` will assert when
123-
// dropped. The final result here combines the parsing result and the
124-
// `emit_stashed_diagnostics` result.
125-
let parse_res = Parser::parse_crate(input, &parse_session);
126-
let stashed_res = parse_session.emit_stashed_diagnostics();
127-
let krate = match (parse_res, stashed_res) {
128-
(Ok(krate), None) => krate,
129-
(parse_res, _) => {
130-
// Surface parse error via Session (errors are merged there from report).
131-
let forbid_verbose = match parse_res {
132-
Err(e) if e != ParserError::ParsePanicError => true,
133-
_ => input_is_stdin,
134-
};
121+
let krate = match Parser::parse_crate(input, &parse_session) {
122+
Ok(krate) => krate,
123+
// Surface parse error via Session (errors are merged there from report)
124+
Err(e) => {
125+
let forbid_verbose = input_is_stdin || e != ParserError::ParsePanicError;
135126
should_emit_verbose(forbid_verbose, config, || {
136127
eprintln!("The Rust parser panicked");
137128
});

src/tools/rustfmt/src/parse/session.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_data_structures::sync::{IntoDynSyncSend, Lrc};
66
use rustc_errors::emitter::{DynEmitter, Emitter, HumanEmitter};
77
use rustc_errors::translation::Translate;
88
use rustc_errors::{
9-
ColorConfig, DiagCtxt, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, Level as DiagnosticLevel,
9+
ColorConfig, DiagCtxt, Diagnostic, DiagnosticBuilder, Level as DiagnosticLevel,
1010
};
1111
use rustc_session::parse::ParseSess as RawParseSess;
1212
use rustc_span::{
@@ -230,10 +230,6 @@ impl ParseSess {
230230
self.ignore_path_set.as_ref().is_match(path)
231231
}
232232

233-
pub(crate) fn emit_stashed_diagnostics(&mut self) -> Option<ErrorGuaranteed> {
234-
self.parse_sess.dcx.emit_stashed_diagnostics()
235-
}
236-
237233
pub(crate) fn set_silent_emitter(&mut self) {
238234
self.parse_sess.dcx = DiagCtxt::with_emitter(silent_emitter());
239235
}

0 commit comments

Comments
 (0)