Skip to content

Commit c1ffb0b

Browse files
committed
Remove force_print_diagnostic.
There are a couple of places where we call `inner.emitter.emit_diagnostic` directly rather than going through `inner.emit_diagnostic`, to guarantee the diagnostic is printed. This feels dubious to me, particularly the bypassing of `TRACK_DIAGNOSTIC`. This commit removes those. - In `print_error_count`, it uses `ForceWarning` instead of `Warning`. - It removes `DiagCtxtInner::failure_note`, because it only has three uses and direct use of `emit_diagnostic` is consistent with other similar locations. - It removes `force_print_diagnostic`, and adds `struct_failure_note`, and updates `print_query_stack` accordingly, which makes it more normal. That location doesn't seem to need forced printing anyway.
1 parent bdc6d82 commit c1ffb0b

File tree

2 files changed

+34
-29
lines changed
  • compiler

2 files changed

+34
-29
lines changed

Diff for: compiler/rustc_errors/src/lib.rs

+24-19
Original file line numberDiff line numberDiff line change
@@ -780,11 +780,12 @@ impl DiagCtxt {
780780
match (errors.len(), warnings.len()) {
781781
(0, 0) => return,
782782
(0, _) => {
783-
// Use `inner.emitter` directly, otherwise the warning might not be emitted, e.g.
784-
// with a configuration like `--cap-lints allow --force-warn bare_trait_objects`.
785-
inner
786-
.emitter
787-
.emit_diagnostic(Diagnostic::new(Warning, DiagnosticMessage::Str(warnings)));
783+
// Use `ForceWarning` rather than `Warning` to guarantee emission, e.g. with a
784+
// configuration like `--cap-lints allow --force-warn bare_trait_objects`.
785+
inner.emit_diagnostic(Diagnostic::new(
786+
ForceWarning(None),
787+
DiagnosticMessage::Str(warnings),
788+
));
788789
}
789790
(_, 0) => {
790791
inner.emit_diagnostic(Diagnostic::new(Error, errors));
@@ -812,20 +813,23 @@ impl DiagCtxt {
812813
error_codes.sort();
813814
if error_codes.len() > 1 {
814815
let limit = if error_codes.len() > 9 { 9 } else { error_codes.len() };
815-
inner.failure_note(format!(
816+
let msg1 = format!(
816817
"Some errors have detailed explanations: {}{}",
817818
error_codes[..limit].join(", "),
818819
if error_codes.len() > 9 { "..." } else { "." }
819-
));
820-
inner.failure_note(format!(
820+
);
821+
let msg2 = format!(
821822
"For more information about an error, try `rustc --explain {}`.",
822823
&error_codes[0]
823-
));
824+
);
825+
inner.emit_diagnostic(Diagnostic::new(FailureNote, msg1));
826+
inner.emit_diagnostic(Diagnostic::new(FailureNote, msg2));
824827
} else {
825-
inner.failure_note(format!(
828+
let msg = format!(
826829
"For more information about this error, try `rustc --explain {}`.",
827830
&error_codes[0]
828-
));
831+
);
832+
inner.emit_diagnostic(Diagnostic::new(FailureNote, msg));
829833
}
830834
}
831835
}
@@ -848,10 +852,6 @@ impl DiagCtxt {
848852
self.inner.borrow_mut().taught_diagnostics.insert(code)
849853
}
850854

851-
pub fn force_print_diagnostic(&self, db: Diagnostic) {
852-
self.inner.borrow_mut().emitter.emit_diagnostic(db);
853-
}
854-
855855
pub fn emit_diagnostic(&self, diagnostic: Diagnostic) -> Option<ErrorGuaranteed> {
856856
self.inner.borrow_mut().emit_diagnostic(diagnostic)
857857
}
@@ -1207,6 +1207,15 @@ impl DiagCtxt {
12071207
DiagnosticBuilder::new(self, Help, msg)
12081208
}
12091209

1210+
#[rustc_lint_diagnostics]
1211+
#[track_caller]
1212+
pub fn struct_failure_note(
1213+
&self,
1214+
msg: impl Into<DiagnosticMessage>,
1215+
) -> DiagnosticBuilder<'_, ()> {
1216+
DiagnosticBuilder::new(self, FailureNote, msg)
1217+
}
1218+
12101219
#[rustc_lint_diagnostics]
12111220
#[track_caller]
12121221
pub fn struct_allow(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> {
@@ -1406,10 +1415,6 @@ impl DiagCtxtInner {
14061415
.or_else(|| self.delayed_bugs.get(0).map(|(_, guar)| guar).copied())
14071416
}
14081417

1409-
fn failure_note(&mut self, msg: impl Into<DiagnosticMessage>) {
1410-
self.emit_diagnostic(Diagnostic::new(FailureNote, msg));
1411-
}
1412-
14131418
fn flush_delayed(&mut self) {
14141419
if self.delayed_bugs.is_empty() {
14151420
return;

Diff for: compiler/rustc_query_system/src/query/job.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::query::plumbing::CycleError;
44
use crate::query::DepKind;
55
use crate::query::{QueryContext, QueryStackFrame};
66
use rustc_data_structures::fx::FxHashMap;
7-
use rustc_errors::{DiagCtxt, Diagnostic, DiagnosticBuilder, Level};
7+
use rustc_errors::{DiagCtxt, DiagnosticBuilder};
88
use rustc_hir::def::DefKind;
99
use rustc_session::Session;
1010
use rustc_span::Span;
@@ -628,15 +628,15 @@ pub fn print_query_stack<Qcx: QueryContext>(
628628
};
629629
if Some(count_printed) < num_frames || num_frames.is_none() {
630630
// Only print to stderr as many stack frames as `num_frames` when present.
631-
let mut diag = Diagnostic::new(
632-
Level::FailureNote,
633-
format!(
634-
"#{} [{:?}] {}",
635-
count_printed, query_info.query.dep_kind, query_info.query.description
636-
),
637-
);
638-
diag.span = query_info.job.span.into();
639-
dcx.force_print_diagnostic(diag);
631+
// FIXME: needs translation
632+
#[allow(rustc::diagnostic_outside_of_impl)]
633+
#[allow(rustc::untranslatable_diagnostic)]
634+
dcx.struct_failure_note(format!(
635+
"#{} [{:?}] {}",
636+
count_printed, query_info.query.dep_kind, query_info.query.description
637+
))
638+
.with_span(query_info.job.span)
639+
.emit();
640640
count_printed += 1;
641641
}
642642

0 commit comments

Comments
 (0)