1
Fork 0

Stop using String for error codes.

Error codes are integers, but `String` is used everywhere to represent
them. Gross!

This commit introduces `ErrCode`, an integral newtype for error codes,
replacing `String`. It also introduces a constant for every error code,
e.g. `E0123`, and removes the `error_code!` macro. The constants are
imported wherever used with `use rustc_errors::codes::*`.

With the old code, we have three different ways to specify an error code
at a use point:
```
error_code!(E0123)  // macro call

struct_span_code_err!(dcx, span, E0123, "msg");  // bare ident arg to macro call

\#[diag(name, code = "E0123")]  // string
struct Diag;
```

With the new code, they all use the `E0123` constant.
```
E0123  // constant

struct_span_code_err!(dcx, span, E0123, "msg");  // constant

\#[diag(name, code = E0123)]  // constant
struct Diag;
```

The commit also changes the structure of the error code definitions:
- `rustc_error_codes` now just defines a higher-order macro listing the
  used error codes and nothing else.
- Because that's now the only thing in the `rustc_error_codes` crate, I
  moved it into the `lib.rs` file and removed the `error_codes.rs` file.
- `rustc_errors` uses that macro to define everything, e.g. the error
  code constants and the `DIAGNOSTIC_TABLES`. This is in its new
  `codes.rs` file.
This commit is contained in:
Nicholas Nethercote 2024-01-14 10:57:07 +11:00
parent 0321de2778
commit 5d9dfbd08f
110 changed files with 1624 additions and 1572 deletions

View file

@ -1,7 +1,7 @@
use crate::fluent_generated as fluent;
use rustc_errors::{
AddToDiagnostic, Applicability, DiagCtxt, Diagnostic, DiagnosticBuilder, EmissionGuarantee,
IntoDiagnostic, Level, SubdiagnosticMessage,
codes::*, AddToDiagnostic, Applicability, DiagCtxt, Diagnostic, DiagnosticBuilder,
EmissionGuarantee, IntoDiagnostic, Level, SubdiagnosticMessage,
};
use rustc_macros::Diagnostic;
use rustc_middle::ty::{self, ClosureKind, PolyTraitRef, Ty};
@ -25,7 +25,7 @@ pub struct UnableToConstructConstantValue<'a> {
}
#[derive(Diagnostic)]
#[diag(trait_selection_empty_on_clause_in_rustc_on_unimplemented, code = "E0232")]
#[diag(trait_selection_empty_on_clause_in_rustc_on_unimplemented, code = E0232)]
pub struct EmptyOnClauseInOnUnimplemented {
#[primary_span]
#[label]
@ -33,7 +33,7 @@ pub struct EmptyOnClauseInOnUnimplemented {
}
#[derive(Diagnostic)]
#[diag(trait_selection_invalid_on_clause_in_rustc_on_unimplemented, code = "E0232")]
#[diag(trait_selection_invalid_on_clause_in_rustc_on_unimplemented, code = E0232)]
pub struct InvalidOnClauseInOnUnimplemented {
#[primary_span]
#[label]
@ -41,7 +41,7 @@ pub struct InvalidOnClauseInOnUnimplemented {
}
#[derive(Diagnostic)]
#[diag(trait_selection_no_value_in_rustc_on_unimplemented, code = "E0232")]
#[diag(trait_selection_no_value_in_rustc_on_unimplemented, code = E0232)]
#[note]
pub struct NoValueInOnUnimplemented {
#[primary_span]
@ -59,17 +59,13 @@ pub struct NegativePositiveConflict<'tcx> {
impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for NegativePositiveConflict<'_> {
#[track_caller]
fn into_diagnostic(
self,
dcx: &DiagCtxt,
level: Level,
) -> rustc_errors::DiagnosticBuilder<'_, G> {
fn into_diagnostic(self, dcx: &DiagCtxt, level: Level) -> DiagnosticBuilder<'_, G> {
let mut diag =
DiagnosticBuilder::new(dcx, level, fluent::trait_selection_negative_positive_conflict);
diag.arg("trait_desc", self.trait_desc.print_only_trait_path().to_string());
diag.arg("self_desc", self.self_ty.map_or_else(|| "none".to_string(), |ty| ty.to_string()));
diag.span(self.impl_span);
diag.code(rustc_errors::error_code!(E0751));
diag.code(E0751);
match self.negative_impl_span {
Ok(span) => {
diag.span_label(span, fluent::trait_selection_negative_implementation_here);
@ -132,7 +128,7 @@ impl AddToDiagnostic for AdjustSignatureBorrow {
}
#[derive(Diagnostic)]
#[diag(trait_selection_closure_kind_mismatch, code = "E0525")]
#[diag(trait_selection_closure_kind_mismatch, code = E0525)]
pub struct ClosureKindMismatch {
#[primary_span]
#[label]

View file

@ -1,7 +1,7 @@
use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use crate::infer::InferCtxt;
use crate::traits::{Obligation, ObligationCause, ObligationCtxt};
use rustc_errors::{pluralize, struct_span_code_err, Applicability, DiagnosticBuilder};
use rustc_errors::{codes::*, pluralize, struct_span_code_err, Applicability, DiagnosticBuilder};
use rustc_hir as hir;
use rustc_hir::Node;
use rustc_middle::ty::{self, Ty};

View file

@ -6,7 +6,7 @@ use rustc_ast::AttrKind;
use rustc_ast::{Attribute, MetaItem, NestedMetaItem};
use rustc_attr as attr;
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::{struct_span_code_err, ErrorGuaranteed};
use rustc_errors::{codes::*, struct_span_code_err, ErrorGuaranteed};
use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_middle::ty::GenericArgsRef;

View file

@ -13,7 +13,7 @@ use hir::def::CtorOf;
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_errors::{
error_code, pluralize, struct_span_code_err, Applicability, Diagnostic, DiagnosticBuilder,
codes::*, pluralize, struct_span_code_err, Applicability, Diagnostic, DiagnosticBuilder,
MultiSpan, Style, SuggestionStyle,
};
use rustc_hir as hir;
@ -2014,7 +2014,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
return false;
};
err.code(error_code!(E0746));
err.code(E0746);
err.primary_message("return type cannot have an unboxed trait object");
err.children.clear();

View file

@ -19,8 +19,8 @@ use crate::traits::{
};
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
use rustc_errors::{
pluralize, struct_span_code_err, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed,
MultiSpan, StashKey, Style,
codes::*, pluralize, struct_span_code_err, Applicability, Diagnostic, DiagnosticBuilder,
ErrorGuaranteed, MultiSpan, StashKey, Style,
};
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Namespace, Res};
@ -2529,7 +2529,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
corresponding `impl` type",
),
);
err.code(rustc_errors::error_code!(E0790));
err.code(E0790);
if let Some(local_def_id) = data.trait_ref.def_id.as_local()
&& let Some(hir::Node::Item(hir::Item {
@ -3162,7 +3162,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
| ObligationCauseCode::ItemObligation(def_id)
if self.tcx.is_fn_trait(*def_id) =>
{
err.code(rustc_errors::error_code!(E0059));
err.code(E0059);
err.primary_message(format!(
"type parameter to bare `{}` trait must be a tuple",
self.tcx.def_path_str(*def_id)

View file

@ -20,7 +20,7 @@ use crate::traits::{
self, coherence, FutureCompatOverlapErrorKind, ObligationCause, ObligationCtxt,
};
use rustc_data_structures::fx::FxIndexSet;
use rustc_errors::{error_code, DelayDm, Diagnostic};
use rustc_errors::{codes::*, DelayDm, Diagnostic};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_middle::ty::{self, ImplSubject, Ty, TyCtxt, TypeVisitableExt};
use rustc_middle::ty::{GenericArgs, GenericArgsRef};
@ -449,7 +449,7 @@ fn report_conflicting_impls<'tcx>(
|| tcx.orphan_check_impl(impl_def_id).is_ok()
{
let mut err = tcx.dcx().struct_span_err(impl_span, msg);
err.code(error_code!(E0119));
err.code(E0119);
decorate(tcx, &overlap, impl_span, &mut err);
err.emit()
} else {