Skip to content

Commit fd40d67

Browse files
committed
Give TRACK_DIAGNOSTIC a return value.
This means `DiagCtxtInner::emit_diagnostic` can return its result directly, rather than having to modify a local variable.
1 parent 7bf52a3 commit fd40d67

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

compiler/rustc_errors/src/lib.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -516,12 +516,16 @@ pub enum StashKey {
516516
UndeterminedMacroResolution,
517517
}
518518

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

523-
pub static TRACK_DIAGNOSTIC: AtomicRef<fn(Diagnostic, &mut dyn FnMut(Diagnostic))> =
524-
AtomicRef::new(&(default_track_diagnostic as _));
523+
pub static TRACK_DIAGNOSTIC: AtomicRef<
524+
fn(
525+
Diagnostic,
526+
&mut dyn FnMut(Diagnostic) -> Option<ErrorGuaranteed>,
527+
) -> Option<ErrorGuaranteed>,
528+
> = AtomicRef::new(&(default_track_diagnostic as _));
525529

526530
enum DelayedBugKind {
527531
Normal,
@@ -1303,7 +1307,6 @@ impl DiagCtxtInner {
13031307
_ => {}
13041308
}
13051309

1306-
let mut guaranteed = None;
13071310
TRACK_DIAGNOSTIC(diagnostic, &mut |mut diagnostic| {
13081311
if let Some(code) = diagnostic.code {
13091312
self.emitted_diagnostic_codes.insert(code);
@@ -1365,11 +1368,11 @@ impl DiagCtxtInner {
13651368

13661369
#[allow(deprecated)]
13671370
if level == Level::Error {
1368-
guaranteed = Some(ErrorGuaranteed::unchecked_error_guaranteed());
1371+
Some(ErrorGuaranteed::unchecked_error_guaranteed())
1372+
} else {
1373+
None
13691374
}
1370-
});
1371-
1372-
guaranteed
1375+
})
13731376
}
13741377

13751378
fn treat_err_as_bug(&self) -> bool {

compiler/rustc_interface/src/callbacks.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn track_span_parent(def_id: rustc_span::def_id::LocalDefId) {
2929
/// This is a callback from `rustc_errors` as it cannot access the implicit state
3030
/// in `rustc_middle` otherwise. It is used when diagnostic messages are
3131
/// emitted and stores them in the current query, if there is one.
32-
fn track_diagnostic(diagnostic: Diagnostic, f: &mut dyn FnMut(Diagnostic)) {
32+
fn track_diagnostic<R>(diagnostic: Diagnostic, f: &mut dyn FnMut(Diagnostic) -> R) -> R {
3333
tls::with_context_opt(|icx| {
3434
if let Some(icx) = icx {
3535
if let Some(diagnostics) = icx.diagnostics {
@@ -38,11 +38,11 @@ fn track_diagnostic(diagnostic: Diagnostic, f: &mut dyn FnMut(Diagnostic)) {
3838

3939
// Diagnostics are tracked, we can ignore the dependency.
4040
let icx = tls::ImplicitCtxt { task_deps: TaskDepsRef::Ignore, ..icx.clone() };
41-
return tls::enter_context(&icx, move || (*f)(diagnostic));
41+
tls::enter_context(&icx, move || (*f)(diagnostic))
42+
} else {
43+
// In any other case, invoke diagnostics anyway.
44+
(*f)(diagnostic)
4245
}
43-
44-
// In any other case, invoke diagnostics anyway.
45-
(*f)(diagnostic);
4646
})
4747
}
4848

0 commit comments

Comments
 (0)