Skip to content

Commit 99f4bfc

Browse files
committed
Auto merge of #119097 - nnethercote:fix-EmissionGuarantee, r=compiler-errors
Fix `EmissionGuarantee` There are some problems with the `DiagCtxt` API related to `EmissionGuarantee`. This PR fixes them. r? `@compiler-errors`
2 parents 767453e + 006446e commit 99f4bfc

File tree

45 files changed

+605
-701
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+605
-701
lines changed

compiler/rustc_attr/src/session_diagnostics.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use std::num::IntErrorKind;
22

33
use rustc_ast as ast;
44
use rustc_errors::{
5-
error_code, Applicability, DiagCtxt, DiagnosticBuilder, ErrorGuaranteed, IntoDiagnostic,
5+
error_code, Applicability, DiagCtxt, DiagnosticBuilder, EmissionGuarantee, IntoDiagnostic,
6+
Level,
67
};
78
use rustc_macros::Diagnostic;
89
use rustc_span::{Span, Symbol};
@@ -50,14 +51,12 @@ pub(crate) struct UnknownMetaItem<'a> {
5051
}
5152

5253
// Manual implementation to be able to format `expected` items correctly.
53-
impl<'a> IntoDiagnostic<'a> for UnknownMetaItem<'_> {
54-
fn into_diagnostic(self, dcx: &'a DiagCtxt) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
54+
impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for UnknownMetaItem<'_> {
55+
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> DiagnosticBuilder<'a, G> {
5556
let expected = self.expected.iter().map(|name| format!("`{name}`")).collect::<Vec<_>>();
56-
let mut diag = dcx.struct_span_err_with_code(
57-
self.span,
58-
fluent::attr_unknown_meta_item,
59-
error_code!(E0541),
60-
);
57+
let mut diag = DiagnosticBuilder::new(dcx, level, fluent::attr_unknown_meta_item);
58+
diag.set_span(self.span);
59+
diag.code(error_code!(E0541));
6160
diag.set_arg("item", self.item);
6261
diag.set_arg("expected", expected.join(", "));
6362
diag.span_label(self.span, fluent::attr_label);
@@ -200,10 +199,11 @@ pub(crate) struct UnsupportedLiteral {
200199
pub start_point_span: Span,
201200
}
202201

203-
impl<'a> IntoDiagnostic<'a> for UnsupportedLiteral {
204-
fn into_diagnostic(self, dcx: &'a DiagCtxt) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
205-
let mut diag = dcx.struct_span_err_with_code(
206-
self.span,
202+
impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for UnsupportedLiteral {
203+
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> DiagnosticBuilder<'a, G> {
204+
let mut diag = DiagnosticBuilder::new(
205+
dcx,
206+
level,
207207
match self.reason {
208208
UnsupportedLiteralReason::Generic => fluent::attr_unsupported_literal_generic,
209209
UnsupportedLiteralReason::CfgString => fluent::attr_unsupported_literal_cfg_string,
@@ -214,8 +214,9 @@ impl<'a> IntoDiagnostic<'a> for UnsupportedLiteral {
214214
fluent::attr_unsupported_literal_deprecated_kv_pair
215215
}
216216
},
217-
error_code!(E0565),
218217
);
218+
diag.set_span(self.span);
219+
diag.code(error_code!(E0565));
219220
if self.is_bytestr {
220221
diag.span_suggestion(
221222
self.start_point_span,

compiler/rustc_builtin_macros/src/errors.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_errors::{
2-
AddToDiagnostic, DiagCtxt, DiagnosticBuilder, ErrorGuaranteed, IntoDiagnostic, MultiSpan,
3-
SingleLabelManySpans,
2+
AddToDiagnostic, DiagCtxt, DiagnosticBuilder, EmissionGuarantee, IntoDiagnostic, Level,
3+
MultiSpan, SingleLabelManySpans,
44
};
55
use rustc_macros::{Diagnostic, Subdiagnostic};
66
use rustc_span::{symbol::Ident, Span, Symbol};
@@ -446,14 +446,14 @@ pub(crate) struct EnvNotDefinedWithUserMessage {
446446
}
447447

448448
// Hand-written implementation to support custom user messages.
449-
impl<'a> IntoDiagnostic<'a> for EnvNotDefinedWithUserMessage {
449+
impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for EnvNotDefinedWithUserMessage {
450450
#[track_caller]
451-
fn into_diagnostic(self, dcx: &'a DiagCtxt) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
451+
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> DiagnosticBuilder<'a, G> {
452452
#[expect(
453453
rustc::untranslatable_diagnostic,
454454
reason = "cannot translate user-provided messages"
455455
)]
456-
let mut diag = dcx.struct_err(self.msg_from_user.to_string());
456+
let mut diag = DiagnosticBuilder::new(dcx, level, self.msg_from_user.to_string());
457457
diag.set_span(self.span);
458458
diag
459459
}
@@ -801,9 +801,13 @@ pub(crate) struct AsmClobberNoReg {
801801
pub(crate) clobbers: Vec<Span>,
802802
}
803803

804-
impl<'a> IntoDiagnostic<'a> for AsmClobberNoReg {
805-
fn into_diagnostic(self, dcx: &'a DiagCtxt) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
806-
let mut diag = dcx.struct_err(crate::fluent_generated::builtin_macros_asm_clobber_no_reg);
804+
impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for AsmClobberNoReg {
805+
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> DiagnosticBuilder<'a, G> {
806+
let mut diag = DiagnosticBuilder::new(
807+
dcx,
808+
level,
809+
crate::fluent_generated::builtin_macros_asm_clobber_no_reg,
810+
);
807811
diag.set_span(self.spans.clone());
808812
// eager translation as `span_labels` takes `AsRef<str>`
809813
let lbl1 = dcx.eagerly_translate_to_string(

compiler/rustc_codegen_gcc/src/errors.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_errors::{
2-
DiagCtxt, DiagnosticArgValue, DiagnosticBuilder, ErrorGuaranteed, IntoDiagnostic,
3-
IntoDiagnosticArg,
2+
DiagCtxt, DiagnosticArgValue, DiagnosticBuilder, EmissionGuarantee, IntoDiagnostic,
3+
IntoDiagnosticArg, Level,
44
};
55
use rustc_macros::{Diagnostic, Subdiagnostic};
66
use rustc_span::Span;
@@ -111,9 +111,13 @@ pub(crate) struct TargetFeatureDisableOrEnable<'a> {
111111
#[help(codegen_gcc_missing_features)]
112112
pub(crate) struct MissingFeatures;
113113

114-
impl IntoDiagnostic<'_, ErrorGuaranteed> for TargetFeatureDisableOrEnable<'_> {
115-
fn into_diagnostic(self, dcx: &'_ DiagCtxt) -> DiagnosticBuilder<'_, ErrorGuaranteed> {
116-
let mut diag = dcx.struct_err(fluent::codegen_gcc_target_feature_disable_or_enable);
114+
impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for TargetFeatureDisableOrEnable<'_> {
115+
fn into_diagnostic(self, dcx: &'_ DiagCtxt, level: Level) -> DiagnosticBuilder<'_, G> {
116+
let mut diag = DiagnosticBuilder::new(
117+
dcx,
118+
level,
119+
fluent::codegen_gcc_target_feature_disable_or_enable
120+
);
117121
if let Some(span) = self.span {
118122
diag.set_span(span);
119123
};

compiler/rustc_codegen_llvm/src/errors.rs

+15-12
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ use std::path::Path;
44

55
use crate::fluent_generated as fluent;
66
use rustc_data_structures::small_c_str::SmallCStr;
7-
use rustc_errors::{
8-
DiagCtxt, DiagnosticBuilder, EmissionGuarantee, ErrorGuaranteed, FatalError, IntoDiagnostic,
9-
};
7+
use rustc_errors::{DiagCtxt, DiagnosticBuilder, EmissionGuarantee, IntoDiagnostic, Level};
108
use rustc_macros::{Diagnostic, Subdiagnostic};
119
use rustc_span::Span;
1210

@@ -101,13 +99,14 @@ pub(crate) struct DynamicLinkingWithLTO;
10199

102100
pub(crate) struct ParseTargetMachineConfig<'a>(pub LlvmError<'a>);
103101

104-
impl IntoDiagnostic<'_, FatalError> for ParseTargetMachineConfig<'_> {
105-
fn into_diagnostic(self, dcx: &'_ DiagCtxt) -> DiagnosticBuilder<'_, FatalError> {
106-
let diag: DiagnosticBuilder<'_, FatalError> = self.0.into_diagnostic(dcx);
102+
impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for ParseTargetMachineConfig<'_> {
103+
fn into_diagnostic(self, dcx: &'_ DiagCtxt, level: Level) -> DiagnosticBuilder<'_, G> {
104+
let diag: DiagnosticBuilder<'_, G> = self.0.into_diagnostic(dcx, level);
107105
let (message, _) = diag.styled_message().first().expect("`LlvmError` with no message");
108106
let message = dcx.eagerly_translate_to_string(message.clone(), diag.args());
109107

110-
let mut diag = dcx.struct_almost_fatal(fluent::codegen_llvm_parse_target_machine_config);
108+
let mut diag =
109+
DiagnosticBuilder::new(dcx, level, fluent::codegen_llvm_parse_target_machine_config);
111110
diag.set_arg("error", message);
112111
diag
113112
}
@@ -123,9 +122,13 @@ pub(crate) struct TargetFeatureDisableOrEnable<'a> {
123122
#[help(codegen_llvm_missing_features)]
124123
pub(crate) struct MissingFeatures;
125124

126-
impl IntoDiagnostic<'_, ErrorGuaranteed> for TargetFeatureDisableOrEnable<'_> {
127-
fn into_diagnostic(self, dcx: &'_ DiagCtxt) -> DiagnosticBuilder<'_, ErrorGuaranteed> {
128-
let mut diag = dcx.struct_err(fluent::codegen_llvm_target_feature_disable_or_enable);
125+
impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for TargetFeatureDisableOrEnable<'_> {
126+
fn into_diagnostic(self, dcx: &'_ DiagCtxt, level: Level) -> DiagnosticBuilder<'_, G> {
127+
let mut diag = DiagnosticBuilder::new(
128+
dcx,
129+
level,
130+
fluent::codegen_llvm_target_feature_disable_or_enable,
131+
);
129132
if let Some(span) = self.span {
130133
diag.set_span(span);
131134
};
@@ -184,7 +187,7 @@ pub enum LlvmError<'a> {
184187
pub(crate) struct WithLlvmError<'a>(pub LlvmError<'a>, pub String);
185188

186189
impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for WithLlvmError<'_> {
187-
fn into_diagnostic(self, dcx: &'_ DiagCtxt) -> DiagnosticBuilder<'_, G> {
190+
fn into_diagnostic(self, dcx: &'_ DiagCtxt, level: Level) -> DiagnosticBuilder<'_, G> {
188191
use LlvmError::*;
189192
let msg_with_llvm_err = match &self.0 {
190193
WriteOutput { .. } => fluent::codegen_llvm_write_output_with_llvm_err,
@@ -201,7 +204,7 @@ impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for WithLlvmError<'_> {
201204
PrepareThinLtoModule => fluent::codegen_llvm_prepare_thin_lto_module_with_llvm_err,
202205
ParseBitcode => fluent::codegen_llvm_parse_bitcode_with_llvm_err,
203206
};
204-
let mut diag = self.0.into_diagnostic(dcx);
207+
let mut diag = self.0.into_diagnostic(dcx, level);
205208
diag.set_primary_message(msg_with_llvm_err);
206209
diag.set_arg("llvm_err", self.1);
207210
diag

0 commit comments

Comments
 (0)