Skip to content

Commit bf62d59

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 1a1876c commit bf62d59

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

compiler/rustc_errors/src/lib.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -526,12 +526,15 @@ pub enum StashKey {
526526
UndeterminedMacroResolution,
527527
}
528528

529-
fn default_track_diagnostic(diag: DiagInner, f: &mut dyn FnMut(DiagInner)) {
529+
fn default_track_diagnostic<R>(diag: DiagInner, f: &mut dyn FnMut(DiagInner) -> R) -> R {
530530
(*f)(diag)
531531
}
532532

533-
pub static TRACK_DIAGNOSTIC: AtomicRef<fn(DiagInner, &mut dyn FnMut(DiagInner))> =
534-
AtomicRef::new(&(default_track_diagnostic as _));
533+
/// Diagnostics emitted by `DiagCtxtInner::emit_diagnostic` are passed through this function. Used
534+
/// for tracking by incremental, to replay diagnostics as necessary.
535+
pub static TRACK_DIAGNOSTIC: AtomicRef<
536+
fn(DiagInner, &mut dyn FnMut(DiagInner) -> Option<ErrorGuaranteed>) -> Option<ErrorGuaranteed>,
537+
> = AtomicRef::new(&(default_track_diagnostic as _));
535538

536539
#[derive(Copy, Clone, Default)]
537540
pub struct DiagCtxtFlags {
@@ -1406,19 +1409,18 @@ impl DiagCtxtInner {
14061409
}
14071410
Warning if !self.flags.can_emit_warnings => {
14081411
if diagnostic.has_future_breakage() {
1409-
(*TRACK_DIAGNOSTIC)(diagnostic, &mut |_| {});
1412+
TRACK_DIAGNOSTIC(diagnostic, &mut |_| None);
14101413
}
14111414
return None;
14121415
}
14131416
Allow | Expect(_) => {
1414-
(*TRACK_DIAGNOSTIC)(diagnostic, &mut |_| {});
1417+
TRACK_DIAGNOSTIC(diagnostic, &mut |_| None);
14151418
return None;
14161419
}
14171420
_ => {}
14181421
}
14191422

1420-
let mut guaranteed = None;
1421-
(*TRACK_DIAGNOSTIC)(diagnostic, &mut |mut diagnostic| {
1423+
TRACK_DIAGNOSTIC(diagnostic, &mut |mut diagnostic| {
14221424
if let Some(code) = diagnostic.code {
14231425
self.emitted_diagnostic_codes.insert(code);
14241426
}
@@ -1481,17 +1483,17 @@ impl DiagCtxtInner {
14811483
// `ErrorGuaranteed` for errors and lint errors originates.
14821484
#[allow(deprecated)]
14831485
let guar = ErrorGuaranteed::unchecked_error_guaranteed();
1484-
guaranteed = Some(guar);
14851486
if is_lint {
14861487
self.lint_err_guars.push(guar);
14871488
} else {
14881489
self.err_guars.push(guar);
14891490
}
14901491
self.panic_if_treat_err_as_bug();
1492+
Some(guar)
1493+
} else {
1494+
None
14911495
}
1492-
});
1493-
1494-
guaranteed
1496+
})
14951497
}
14961498

14971499
fn treat_err_as_bug(&self) -> bool {

compiler/rustc_interface/src/callbacks.rs

Lines changed: 5 additions & 5 deletions
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: DiagInner, f: &mut dyn FnMut(DiagInner)) {
32+
fn track_diagnostic<R>(diagnostic: DiagInner, f: &mut dyn FnMut(DiagInner) -> 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: DiagInner, f: &mut dyn FnMut(DiagInner)) {
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)