Skip to content

Commit 7bdb227

Browse files
committed
Avoid struct_diagnostic where possible.
It's necessary for `derive(Diagnostic)`, but is best avoided elsewhere because there are clearer alternatives. This required adding `Handler::struct_almost_fatal`.
1 parent dc05a30 commit 7bdb227

File tree

7 files changed

+26
-19
lines changed

7 files changed

+26
-19
lines changed

compiler/rustc_builtin_macros/src/errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ impl<'a> IntoDiagnostic<'a> for EnvNotDefinedWithUserMessage {
453453
rustc::untranslatable_diagnostic,
454454
reason = "cannot translate user-provided messages"
455455
)]
456-
let mut diag = handler.struct_diagnostic(self.msg_from_user.to_string());
456+
let mut diag = handler.struct_err(self.msg_from_user.to_string());
457457
diag.set_span(self.span);
458458
diag
459459
}
@@ -804,7 +804,7 @@ pub(crate) struct AsmClobberNoReg {
804804
impl<'a> IntoDiagnostic<'a> for AsmClobberNoReg {
805805
fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
806806
let mut diag =
807-
handler.struct_diagnostic(crate::fluent_generated::builtin_macros_asm_clobber_no_reg);
807+
handler.struct_err(crate::fluent_generated::builtin_macros_asm_clobber_no_reg);
808808
diag.set_span(self.spans.clone());
809809
// eager translation as `span_labels` takes `AsRef<str>`
810810
let lbl1 = handler.eagerly_translate_to_string(

compiler/rustc_codegen_llvm/src/errors.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ impl IntoDiagnostic<'_, FatalError> for ParseTargetMachineConfig<'_> {
107107
let (message, _) = diag.styled_message().first().expect("`LlvmError` with no message");
108108
let message = handler.eagerly_translate_to_string(message.clone(), diag.args());
109109

110-
let mut diag = handler.struct_diagnostic(fluent::codegen_llvm_parse_target_machine_config);
110+
let mut diag =
111+
handler.struct_almost_fatal(fluent::codegen_llvm_parse_target_machine_config);
111112
diag.set_arg("error", message);
112113
diag
113114
}

compiler/rustc_errors/src/lib.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,12 @@ impl Handler {
722722
self.inner.borrow_mut().emit_stashed_diagnostics()
723723
}
724724

725-
/// Construct a builder with the `msg` at the level appropriate for the specific `EmissionGuarantee`.
725+
/// Construct a builder with the `msg` at the level appropriate for the
726+
/// specific `EmissionGuarantee`.
727+
///
728+
/// Note: this is necessary for `derive(Diagnostic)`, but shouldn't be used
729+
/// outside of that. Instead use `struct_err`, `struct_warn`, etc., which
730+
/// make the diagnostic kind clearer.
726731
#[rustc_lint_diagnostics]
727732
#[track_caller]
728733
pub fn struct_diagnostic<G: EmissionGuarantee>(
@@ -937,13 +942,23 @@ impl Handler {
937942
result
938943
}
939944

940-
/// Construct a builder at the `Error` level with the `msg`.
945+
/// Construct a builder at the `Fatal` level with the `msg`.
941946
#[rustc_lint_diagnostics]
942947
#[track_caller]
943948
pub fn struct_fatal(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, !> {
944949
DiagnosticBuilder::new(self, Level::Fatal, msg)
945950
}
946951

952+
/// Construct a builder at the `Fatal` level with the `msg`, that doesn't abort.
953+
#[rustc_lint_diagnostics]
954+
#[track_caller]
955+
pub fn struct_almost_fatal(
956+
&self,
957+
msg: impl Into<DiagnosticMessage>,
958+
) -> DiagnosticBuilder<'_, FatalError> {
959+
DiagnosticBuilder::new(self, Level::Fatal, msg)
960+
}
961+
947962
/// Construct a builder at the `Help` level with the `msg`.
948963
#[rustc_lint_diagnostics]
949964
pub fn struct_help(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> {

compiler/rustc_mir_transform/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub(crate) struct RequiresUnsafe {
6565
impl<'sess> IntoDiagnostic<'sess> for RequiresUnsafe {
6666
#[track_caller]
6767
fn into_diagnostic(self, handler: &'sess Handler) -> DiagnosticBuilder<'sess, ErrorGuaranteed> {
68-
let mut diag = handler.struct_diagnostic(fluent::mir_transform_requires_unsafe);
68+
let mut diag = handler.struct_err(fluent::mir_transform_requires_unsafe);
6969
diag.code(rustc_errors::DiagnosticId::Error("E0133".to_string()));
7070
diag.set_span(self.span);
7171
diag.span_label(self.span, self.details.label());

compiler/rustc_parse/src/errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ impl<'a> IntoDiagnostic<'a> for ExpectedIdentifier {
10461046
) -> rustc_errors::DiagnosticBuilder<'a, ErrorGuaranteed> {
10471047
let token_descr = TokenDescription::from_token(&self.token);
10481048

1049-
let mut diag = handler.struct_diagnostic(match token_descr {
1049+
let mut diag = handler.struct_err(match token_descr {
10501050
Some(TokenDescription::ReservedIdentifier) => {
10511051
fluent::parse_expected_identifier_found_reserved_identifier_str
10521052
}
@@ -1103,7 +1103,7 @@ impl<'a> IntoDiagnostic<'a> for ExpectedSemi {
11031103
) -> rustc_errors::DiagnosticBuilder<'a, ErrorGuaranteed> {
11041104
let token_descr = TokenDescription::from_token(&self.token);
11051105

1106-
let mut diag = handler.struct_diagnostic(match token_descr {
1106+
let mut diag = handler.struct_err(match token_descr {
11071107
Some(TokenDescription::ReservedIdentifier) => {
11081108
fluent::parse_expected_semi_found_reserved_identifier_str
11091109
}

compiler/rustc_session/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl<'a> IntoDiagnostic<'a> for FeatureGateError {
1919
self,
2020
handler: &'a rustc_errors::Handler,
2121
) -> rustc_errors::DiagnosticBuilder<'a, ErrorGuaranteed> {
22-
let mut diag = handler.struct_diagnostic(self.explain);
22+
let mut diag = handler.struct_err(self.explain);
2323
diag.set_span(self.span);
2424
diag.code(error_code!(E0658));
2525
diag

compiler/rustc_session/src/parse.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_data_structures::sync::{AppendOnlyVec, Lock, Lrc};
1414
use rustc_errors::{emitter::SilentEmitter, Handler};
1515
use rustc_errors::{
1616
fallback_fluent_bundle, Diagnostic, DiagnosticBuilder, DiagnosticId, DiagnosticMessage,
17-
EmissionGuarantee, ErrorGuaranteed, IntoDiagnostic, MultiSpan, Noted, StashKey,
17+
ErrorGuaranteed, IntoDiagnostic, MultiSpan, Noted, StashKey,
1818
};
1919
use rustc_feature::{find_feature_issue, GateIssue, UnstableFeatures};
2020
use rustc_span::edition::Edition;
@@ -390,13 +390,4 @@ impl ParseSess {
390390
pub fn struct_fatal(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, !> {
391391
self.span_diagnostic.struct_fatal(msg)
392392
}
393-
394-
#[rustc_lint_diagnostics]
395-
#[track_caller]
396-
pub fn struct_diagnostic<G: EmissionGuarantee>(
397-
&self,
398-
msg: impl Into<DiagnosticMessage>,
399-
) -> DiagnosticBuilder<'_, G> {
400-
self.span_diagnostic.struct_diagnostic(msg)
401-
}
402393
}

0 commit comments

Comments
 (0)