Skip to content

Commit cf2a0d1

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 8b21296 commit cf2a0d1

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
@@ -534,12 +534,18 @@ pub enum StashKey {
534534
UndeterminedMacroResolution,
535535
}
536536

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

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

544550
#[derive(Copy, Clone, Default)]
545551
pub struct DiagCtxtFlags {
@@ -1349,19 +1355,18 @@ impl DiagCtxtInner {
13491355
}
13501356
Warning if !self.flags.can_emit_warnings => {
13511357
if diagnostic.has_future_breakage() {
1352-
(*TRACK_DIAGNOSTIC)(diagnostic, &mut |_| {});
1358+
TRACK_DIAGNOSTIC(diagnostic, &mut |_| None);
13531359
}
13541360
return None;
13551361
}
13561362
Allow | Expect(_) => {
1357-
(*TRACK_DIAGNOSTIC)(diagnostic, &mut |_| {});
1363+
TRACK_DIAGNOSTIC(diagnostic, &mut |_| None);
13581364
return None;
13591365
}
13601366
_ => {}
13611367
}
13621368

1363-
let mut guaranteed = None;
1364-
(*TRACK_DIAGNOSTIC)(diagnostic, &mut |mut diagnostic| {
1369+
TRACK_DIAGNOSTIC(diagnostic, &mut |mut diagnostic| {
13651370
if let Some(code) = diagnostic.code {
13661371
self.emitted_diagnostic_codes.insert(code);
13671372
}
@@ -1425,17 +1430,17 @@ impl DiagCtxtInner {
14251430
// `ErrorGuaranteed` for errors and lint errors originates.
14261431
#[allow(deprecated)]
14271432
let guar = ErrorGuaranteed::unchecked_error_guaranteed();
1428-
guaranteed = Some(guar);
14291433
if is_lint {
14301434
self.lint_err_guars.push(guar);
14311435
} else {
14321436
self.err_guars.push(guar);
14331437
}
14341438
self.panic_if_treat_err_as_bug();
1439+
Some(guar)
1440+
} else {
1441+
None
14351442
}
1436-
});
1437-
1438-
guaranteed
1443+
})
14391444
}
14401445

14411446
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)