From fd40d6792ea25edcac1fa914408bc22445b9f8ba Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 9 Feb 2024 16:16:22 +1100 Subject: [PATCH] Give `TRACK_DIAGNOSTIC` a return value. This means `DiagCtxtInner::emit_diagnostic` can return its result directly, rather than having to modify a local variable. --- compiler/rustc_errors/src/lib.rs | 19 +++++++++++-------- compiler/rustc_interface/src/callbacks.rs | 10 +++++----- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs index 9c3ec3cbb1032..c7ee588bd6def 100644 --- a/compiler/rustc_errors/src/lib.rs +++ b/compiler/rustc_errors/src/lib.rs @@ -516,12 +516,16 @@ pub enum StashKey { UndeterminedMacroResolution, } -fn default_track_diagnostic(diag: Diagnostic, f: &mut dyn FnMut(Diagnostic)) { +fn default_track_diagnostic(diag: Diagnostic, f: &mut dyn FnMut(Diagnostic) -> R) -> R { (*f)(diag) } -pub static TRACK_DIAGNOSTIC: AtomicRef = - AtomicRef::new(&(default_track_diagnostic as _)); +pub static TRACK_DIAGNOSTIC: AtomicRef< + fn( + Diagnostic, + &mut dyn FnMut(Diagnostic) -> Option, + ) -> Option, +> = AtomicRef::new(&(default_track_diagnostic as _)); enum DelayedBugKind { Normal, @@ -1303,7 +1307,6 @@ impl DiagCtxtInner { _ => {} } - let mut guaranteed = None; TRACK_DIAGNOSTIC(diagnostic, &mut |mut diagnostic| { if let Some(code) = diagnostic.code { self.emitted_diagnostic_codes.insert(code); @@ -1365,11 +1368,11 @@ impl DiagCtxtInner { #[allow(deprecated)] if level == Level::Error { - guaranteed = Some(ErrorGuaranteed::unchecked_error_guaranteed()); + Some(ErrorGuaranteed::unchecked_error_guaranteed()) + } else { + None } - }); - - guaranteed + }) } fn treat_err_as_bug(&self) -> bool { diff --git a/compiler/rustc_interface/src/callbacks.rs b/compiler/rustc_interface/src/callbacks.rs index 8c7e49b51f9b2..eb2b484f6b14c 100644 --- a/compiler/rustc_interface/src/callbacks.rs +++ b/compiler/rustc_interface/src/callbacks.rs @@ -29,7 +29,7 @@ fn track_span_parent(def_id: rustc_span::def_id::LocalDefId) { /// This is a callback from `rustc_errors` as it cannot access the implicit state /// in `rustc_middle` otherwise. It is used when diagnostic messages are /// emitted and stores them in the current query, if there is one. -fn track_diagnostic(diagnostic: Diagnostic, f: &mut dyn FnMut(Diagnostic)) { +fn track_diagnostic(diagnostic: Diagnostic, f: &mut dyn FnMut(Diagnostic) -> R) -> R { tls::with_context_opt(|icx| { if let Some(icx) = icx { if let Some(diagnostics) = icx.diagnostics { @@ -38,11 +38,11 @@ fn track_diagnostic(diagnostic: Diagnostic, f: &mut dyn FnMut(Diagnostic)) { // Diagnostics are tracked, we can ignore the dependency. let icx = tls::ImplicitCtxt { task_deps: TaskDepsRef::Ignore, ..icx.clone() }; - return tls::enter_context(&icx, move || (*f)(diagnostic)); + tls::enter_context(&icx, move || (*f)(diagnostic)) + } else { + // In any other case, invoke diagnostics anyway. + (*f)(diagnostic) } - - // In any other case, invoke diagnostics anyway. - (*f)(diagnostic); }) }