1
Fork 0

Add level arg to into_diagnostic.

And make all hand-written `IntoDiagnostic` impls generic, by using
`DiagnosticBuilder::new(dcx, level, ...)` instead of e.g.
`dcx.struct_err(...)`.

This means the `create_*` functions are the source of the error level.
This change will let us remove `struct_diagnostic`.

Note: `#[rustc_lint_diagnostics]` is added to `DiagnosticBuilder::new`,
it's necessary to pass diagnostics tests now that it's used in
`into_diagnostic` functions.
This commit is contained in:
Nicholas Nethercote 2023-12-18 14:12:39 +11:00
parent 31df50c897
commit e7724a2e31
24 changed files with 307 additions and 288 deletions

View file

@ -6,8 +6,8 @@ use std::{
use crate::fluent_generated as fluent;
use rustc_ast::Label;
use rustc_errors::{
error_code, AddToDiagnostic, Applicability, Diagnostic, DiagnosticSymbolList, ErrorGuaranteed,
IntoDiagnostic, MultiSpan,
error_code, AddToDiagnostic, Applicability, DiagCtxt, Diagnostic, DiagnosticBuilder,
DiagnosticSymbolList, EmissionGuarantee, IntoDiagnostic, Level, MultiSpan,
};
use rustc_hir::{self as hir, ExprKind, Target};
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
@ -863,13 +863,11 @@ pub struct ItemFollowingInnerAttr {
pub kind: &'static str,
}
impl IntoDiagnostic<'_> for InvalidAttrAtCrateLevel {
impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for InvalidAttrAtCrateLevel {
#[track_caller]
fn into_diagnostic(
self,
dcx: &'_ rustc_errors::DiagCtxt,
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
let mut diag = dcx.struct_err(fluent::passes_invalid_attr_at_crate_level);
fn into_diagnostic(self, dcx: &'_ DiagCtxt, level: Level) -> DiagnosticBuilder<'_, G> {
let mut diag =
DiagnosticBuilder::new(dcx, level, fluent::passes_invalid_attr_at_crate_level);
diag.set_span(self.span);
diag.set_arg("name", self.name);
// Only emit an error with a suggestion if we can create a string out
@ -879,7 +877,7 @@ impl IntoDiagnostic<'_> for InvalidAttrAtCrateLevel {
span,
fluent::passes_suggestion,
String::new(),
rustc_errors::Applicability::MachineApplicable,
Applicability::MachineApplicable,
);
}
if let Some(item) = self.item {
@ -1016,17 +1014,12 @@ pub struct BreakNonLoop<'a> {
pub break_expr_span: Span,
}
impl<'a> IntoDiagnostic<'_> for BreakNonLoop<'a> {
impl<'a, G: EmissionGuarantee> IntoDiagnostic<'_, G> for BreakNonLoop<'a> {
#[track_caller]
fn into_diagnostic(
self,
dcx: &rustc_errors::DiagCtxt,
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
let mut diag = dcx.struct_span_err_with_code(
self.span,
fluent::passes_break_non_loop,
error_code!(E0571),
);
fn into_diagnostic(self, dcx: &DiagCtxt, level: Level) -> DiagnosticBuilder<'_, G> {
let mut diag = DiagnosticBuilder::new(dcx, level, fluent::passes_break_non_loop);
diag.set_span(self.span);
diag.code(error_code!(E0571));
diag.set_arg("kind", self.kind);
diag.span_label(self.span, fluent::passes_label);
if let Some(head) = self.head {
@ -1165,17 +1158,12 @@ pub struct NakedFunctionsAsmBlock {
pub non_asms: Vec<Span>,
}
impl IntoDiagnostic<'_> for NakedFunctionsAsmBlock {
impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for NakedFunctionsAsmBlock {
#[track_caller]
fn into_diagnostic(
self,
dcx: &rustc_errors::DiagCtxt,
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
let mut diag = dcx.struct_span_err_with_code(
self.span,
fluent::passes_naked_functions_asm_block,
error_code!(E0787),
);
fn into_diagnostic(self, dcx: &DiagCtxt, level: Level) -> DiagnosticBuilder<'_, G> {
let mut diag = DiagnosticBuilder::new(dcx, level, fluent::passes_naked_functions_asm_block);
diag.set_span(self.span);
diag.code(error_code!(E0787));
for span in self.multiple_asms.iter() {
diag.span_label(*span, fluent::passes_label_multiple_asm);
}
@ -1281,17 +1269,12 @@ pub struct NoMainErr {
pub add_teach_note: bool,
}
impl<'a> IntoDiagnostic<'a> for NoMainErr {
impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for NoMainErr {
#[track_caller]
fn into_diagnostic(
self,
dcx: &'a rustc_errors::DiagCtxt,
) -> rustc_errors::DiagnosticBuilder<'a, ErrorGuaranteed> {
let mut diag = dcx.struct_span_err_with_code(
DUMMY_SP,
fluent::passes_no_main_function,
error_code!(E0601),
);
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> DiagnosticBuilder<'a, G> {
let mut diag = DiagnosticBuilder::new(dcx, level, fluent::passes_no_main_function);
diag.set_span(DUMMY_SP);
diag.code(error_code!(E0601));
diag.set_arg("crate_name", self.crate_name);
diag.set_arg("filename", self.filename);
diag.set_arg("has_filename", self.has_filename);
@ -1344,20 +1327,19 @@ pub struct DuplicateLangItem {
pub(crate) duplicate: Duplicate,
}
impl IntoDiagnostic<'_> for DuplicateLangItem {
impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for DuplicateLangItem {
#[track_caller]
fn into_diagnostic(
self,
dcx: &rustc_errors::DiagCtxt,
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
let mut diag = dcx.struct_err_with_code(
fn into_diagnostic(self, dcx: &DiagCtxt, level: Level) -> DiagnosticBuilder<'_, G> {
let mut diag = DiagnosticBuilder::new(
dcx,
level,
match self.duplicate {
Duplicate::Plain => fluent::passes_duplicate_lang_item,
Duplicate::Crate => fluent::passes_duplicate_lang_item_crate,
Duplicate::CrateDepends => fluent::passes_duplicate_lang_item_crate_depends,
},
error_code!(E0152),
);
diag.code(error_code!(E0152));
diag.set_arg("lang_item_name", self.lang_item_name);
diag.set_arg("crate_name", self.crate_name);
diag.set_arg("dependency_of", self.dependency_of);