Skip to content

Commit e4e34a1

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 ee9c7c9 commit e4e34a1

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

compiler/rustc_errors/src/lib.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -531,12 +531,18 @@ pub enum StashKey {
531531
UndeterminedMacroResolution,
532532
}
533533

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

538-
pub static TRACK_DIAGNOSTIC: AtomicRef<fn(Diagnostic, &mut dyn FnMut(Diagnostic))> =
539-
AtomicRef::new(&(default_track_diagnostic as _));
538+
/// Diagnostics emitted by `DiagCtxtInner::emit_diagnostic` are passed through this function. Used
539+
/// for tracking by incremental, to replay diagnostics as necessary.
540+
pub static TRACK_DIAGNOSTIC: AtomicRef<
541+
fn(
542+
Diagnostic,
543+
&mut dyn FnMut(Diagnostic) -> Option<ErrorGuaranteed>,
544+
) -> Option<ErrorGuaranteed>,
545+
> = AtomicRef::new(&(default_track_diagnostic as _));
540546

541547
#[derive(Copy, Clone, Default)]
542548
pub struct DiagCtxtFlags {
@@ -1346,19 +1352,18 @@ impl DiagCtxtInner {
13461352
}
13471353
Warning if !self.flags.can_emit_warnings => {
13481354
if diagnostic.has_future_breakage() {
1349-
(*TRACK_DIAGNOSTIC)(diagnostic, &mut |_| {});
1355+
TRACK_DIAGNOSTIC(diagnostic, &mut |_| None);
13501356
}
13511357
return None;
13521358
}
13531359
Allow | Expect(_) => {
1354-
(*TRACK_DIAGNOSTIC)(diagnostic, &mut |_| {});
1360+
TRACK_DIAGNOSTIC(diagnostic, &mut |_| None);
13551361
return None;
13561362
}
13571363
_ => {}
13581364
}
13591365

1360-
let mut guaranteed = None;
1361-
(*TRACK_DIAGNOSTIC)(diagnostic, &mut |mut diagnostic| {
1366+
TRACK_DIAGNOSTIC(diagnostic, &mut |mut diagnostic| {
13621367
if let Some(code) = diagnostic.code {
13631368
self.emitted_diagnostic_codes.insert(code);
13641369
}
@@ -1422,17 +1427,17 @@ impl DiagCtxtInner {
14221427
// `ErrorGuaranteed` for errors and lint errors originates.
14231428
#[allow(deprecated)]
14241429
let guar = ErrorGuaranteed::unchecked_error_guaranteed();
1425-
guaranteed = Some(guar);
14261430
if is_lint {
14271431
self.lint_err_guars.push(guar);
14281432
} else {
14291433
self.err_guars.push(guar);
14301434
}
14311435
self.panic_if_treat_err_as_bug();
1436+
Some(guar)
1437+
} else {
1438+
None
14321439
}
1433-
});
1434-
1435-
guaranteed
1440+
})
14361441
}
14371442

14381443
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: 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)