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

@@ -14,6 +14,7 @@
#![feature(error_reporter)]
#![feature(extract_if)]
#![feature(let_chains)]
#![feature(min_specialization)]
#![feature(negative_impls)]
#![feature(never_type)]
#![feature(rustc_attrs)]
@@ -30,6 +31,7 @@ extern crate tracing;
extern crate self as rustc_errors;
pub use codes::*;
pub use diagnostic::{
AddToDiagnostic, DecorateLint, Diagnostic, DiagnosticArg, DiagnosticArgValue,
DiagnosticStyledString, IntoDiagnosticArg, SubDiagnostic,
@@ -78,6 +80,7 @@ use std::path::{Path, PathBuf};
use Level::*;
pub mod annotate_snippet_emitter_writer;
pub mod codes;
mod diagnostic;
mod diagnostic_builder;
mod diagnostic_impls;
@@ -446,10 +449,10 @@ struct DiagCtxtInner {
/// This set contains the code of all emitted diagnostics to avoid
/// emitting the same diagnostic with extended help (`--teach`) twice, which
/// would be unnecessary repetition.
taught_diagnostics: FxHashSet<String>,
taught_diagnostics: FxHashSet<ErrCode>,
/// Used to suggest rustc --explain `<error code>`
emitted_diagnostic_codes: FxIndexSet<String>,
emitted_diagnostic_codes: FxIndexSet<ErrCode>,
/// This set contains a hash of every diagnostic that has been emitted by
/// this `DiagCtxt`. These hashes is used to avoid emitting the same error
@@ -1004,9 +1007,9 @@ impl DiagCtxt {
let mut error_codes = inner
.emitted_diagnostic_codes
.iter()
.filter_map(|code| {
.filter_map(|&code| {
if registry.try_find_description(code).is_ok().clone() {
Some(code.clone())
Some(code.to_string())
} else {
None
}
@@ -1052,8 +1055,8 @@ impl DiagCtxt {
///
/// Used to suppress emitting the same error multiple times with extended explanation when
/// calling `-Zteach`.
pub fn must_teach(&self, code: &str) -> bool {
self.inner.borrow_mut().taught_diagnostics.insert(code.to_string())
pub fn must_teach(&self, code: ErrCode) -> bool {
self.inner.borrow_mut().taught_diagnostics.insert(code)
}
pub fn force_print_diagnostic(&self, db: Diagnostic) {
@@ -1313,8 +1316,8 @@ impl DiagCtxtInner {
let mut guaranteed = None;
(*TRACK_DIAGNOSTIC)(diagnostic, &mut |mut diagnostic| {
if let Some(ref code) = diagnostic.code {
self.emitted_diagnostic_codes.insert(code.clone());
if let Some(code) = diagnostic.code {
self.emitted_diagnostic_codes.insert(code);
}
let already_emitted = {