Skip to content

Commit b1b1fa5

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 01d4197 commit b1b1fa5

File tree

2 files changed

+34
-29
lines changed
  • compiler

2 files changed

+34
-29
lines changed

compiler/rustc_errors/src/lib.rs

+24-19
Original file line numberDiff line numberDiff line change
@@ -775,11 +775,12 @@ impl DiagCtxt {
775775
match (errors.len(), warnings.len()) {
776776
(0, 0) => return,
777777
(0, _) => {
778-
// Use `inner.emitter` directly, otherwise the warning might not be emitted, e.g.
779-
// with a configuration like `--cap-lints allow --force-warn bare_trait_objects`.
780-
inner
781-
.emitter
782-
.emit_diagnostic(Diagnostic::new(Warning, DiagnosticMessage::Str(warnings)));
778+
// Use `ForceWarning` rather than `Warning` to guarantee emission, e.g. with a
779+
// configuration like `--cap-lints allow --force-warn bare_trait_objects`.
780+
inner.emit_diagnostic(Diagnostic::new(
781+
ForceWarning(None),
782+
DiagnosticMessage::Str(warnings),
783+
));
783784
}
784785
(_, 0) => {
785786
inner.emit_diagnostic(Diagnostic::new(Error, errors));
@@ -807,20 +808,23 @@ impl DiagCtxt {
807808
error_codes.sort();
808809
if error_codes.len() > 1 {
809810
let limit = if error_codes.len() > 9 { 9 } else { error_codes.len() };
810-
inner.failure_note(format!(
811+
let msg1 = format!(
811812
"Some errors have detailed explanations: {}{}",
812813
error_codes[..limit].join(", "),
813814
if error_codes.len() > 9 { "..." } else { "." }
814-
));
815-
inner.failure_note(format!(
815+
);
816+
let msg2 = format!(
816817
"For more information about an error, try `rustc --explain {}`.",
817818
&error_codes[0]
818-
));
819+
);
820+
inner.emit_diagnostic(Diagnostic::new(FailureNote, msg1));
821+
inner.emit_diagnostic(Diagnostic::new(FailureNote, msg2));
819822
} else {
820-
inner.failure_note(format!(
823+
let msg = format!(
821824
"For more information about this error, try `rustc --explain {}`.",
822825
&error_codes[0]
823-
));
826+
);
827+
inner.emit_diagnostic(Diagnostic::new(FailureNote, msg));
824828
}
825829
}
826830
}
@@ -843,10 +847,6 @@ impl DiagCtxt {
843847
self.inner.borrow_mut().taught_diagnostics.insert(code)
844848
}
845849

846-
pub fn force_print_diagnostic(&self, db: Diagnostic) {
847-
self.inner.borrow_mut().emitter.emit_diagnostic(db);
848-
}
849-
850850
pub fn emit_diagnostic(&self, diagnostic: Diagnostic) -> Option<ErrorGuaranteed> {
851851
self.inner.borrow_mut().emit_diagnostic(diagnostic)
852852
}
@@ -1203,6 +1203,15 @@ impl DiagCtxt {
12031203
DiagnosticBuilder::new(self, Help, msg)
12041204
}
12051205

1206+
#[rustc_lint_diagnostics]
1207+
#[track_caller]
1208+
pub fn struct_failure_note(
1209+
&self,
1210+
msg: impl Into<DiagnosticMessage>,
1211+
) -> DiagnosticBuilder<'_, ()> {
1212+
DiagnosticBuilder::new(self, FailureNote, msg)
1213+
}
1214+
12061215
#[rustc_lint_diagnostics]
12071216
#[track_caller]
12081217
pub fn struct_allow(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> {
@@ -1410,10 +1419,6 @@ impl DiagCtxtInner {
14101419
.or_else(|| self.delayed_bugs.get(0).map(|(_, guar)| guar).copied())
14111420
}
14121421

1413-
fn failure_note(&mut self, msg: impl Into<DiagnosticMessage>) {
1414-
self.emit_diagnostic(Diagnostic::new(FailureNote, msg));
1415-
}
1416-
14171422
fn flush_delayed(&mut self, kind: DelayedBugKind) {
14181423
let (bugs, note1) = match kind {
14191424
DelayedBugKind::Normal => (

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)