Skip to content

Commit

Permalink
Give TRACK_DIAGNOSTIC a return value.
Browse files Browse the repository at this point in the history
This means `DiagCtxtInner::emit_diagnostic` can return its result
directly, rather than having to modify a local variable.
  • Loading branch information
nnethercote committed Feb 9, 2024
1 parent 7bf52a3 commit fd40d67
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
19 changes: 11 additions & 8 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,12 +516,16 @@ pub enum StashKey {
UndeterminedMacroResolution,
}

fn default_track_diagnostic(diag: Diagnostic, f: &mut dyn FnMut(Diagnostic)) {
fn default_track_diagnostic<R>(diag: Diagnostic, f: &mut dyn FnMut(Diagnostic) -> R) -> R {
(*f)(diag)
}

pub static TRACK_DIAGNOSTIC: AtomicRef<fn(Diagnostic, &mut dyn FnMut(Diagnostic))> =
AtomicRef::new(&(default_track_diagnostic as _));
pub static TRACK_DIAGNOSTIC: AtomicRef<
fn(
Diagnostic,
&mut dyn FnMut(Diagnostic) -> Option<ErrorGuaranteed>,
) -> Option<ErrorGuaranteed>,
> = AtomicRef::new(&(default_track_diagnostic as _));

enum DelayedBugKind {
Normal,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_interface/src/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<R>(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 {
Expand All @@ -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);
})
}

Expand Down

0 comments on commit fd40d67

Please sign in to comment.