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,8 +1,8 @@
use std::borrow::Cow;
use rustc_errors::{
DiagCtxt, DiagnosticArgValue, DiagnosticBuilder, DiagnosticMessage, EmissionGuarantee,
IntoDiagnostic, Level,
codes::*, DiagCtxt, DiagnosticArgValue, DiagnosticBuilder, DiagnosticMessage,
EmissionGuarantee, IntoDiagnostic, Level,
};
use rustc_hir::ConstContext;
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
@ -55,14 +55,14 @@ pub(crate) struct UnstableInStable {
}
#[derive(Diagnostic)]
#[diag(const_eval_thread_local_access, code = "E0625")]
#[diag(const_eval_thread_local_access, code = E0625)]
pub(crate) struct NonConstOpErr {
#[primary_span]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag(const_eval_static_access, code = "E0013")]
#[diag(const_eval_static_access, code = E0013)]
#[help]
pub(crate) struct StaticAccessErr {
#[primary_span]
@ -98,7 +98,7 @@ pub(crate) struct PanicNonStrErr {
}
#[derive(Diagnostic)]
#[diag(const_eval_mut_deref, code = "E0658")]
#[diag(const_eval_mut_deref, code = E0658)]
pub(crate) struct MutDerefErr {
#[primary_span]
pub span: Span,
@ -106,7 +106,7 @@ pub(crate) struct MutDerefErr {
}
#[derive(Diagnostic)]
#[diag(const_eval_transient_mut_borrow, code = "E0658")]
#[diag(const_eval_transient_mut_borrow, code = E0658)]
pub(crate) struct TransientMutBorrowErr {
#[primary_span]
pub span: Span,
@ -114,7 +114,7 @@ pub(crate) struct TransientMutBorrowErr {
}
#[derive(Diagnostic)]
#[diag(const_eval_transient_mut_raw, code = "E0658")]
#[diag(const_eval_transient_mut_raw, code = E0658)]
pub(crate) struct TransientMutRawErr {
#[primary_span]
pub span: Span,
@ -146,7 +146,7 @@ pub(crate) struct UnstableConstFn {
}
#[derive(Diagnostic)]
#[diag(const_eval_unallowed_mutable_refs, code = "E0764")]
#[diag(const_eval_unallowed_mutable_refs, code = E0764)]
pub(crate) struct UnallowedMutableRefs {
#[primary_span]
pub span: Span,
@ -156,7 +156,7 @@ pub(crate) struct UnallowedMutableRefs {
}
#[derive(Diagnostic)]
#[diag(const_eval_unallowed_mutable_raw, code = "E0764")]
#[diag(const_eval_unallowed_mutable_raw, code = E0764)]
pub(crate) struct UnallowedMutableRaw {
#[primary_span]
pub span: Span,
@ -165,7 +165,7 @@ pub(crate) struct UnallowedMutableRaw {
pub teach: Option<()>,
}
#[derive(Diagnostic)]
#[diag(const_eval_non_const_fmt_macro_call, code = "E0015")]
#[diag(const_eval_non_const_fmt_macro_call, code = E0015)]
pub(crate) struct NonConstFmtMacroCall {
#[primary_span]
pub span: Span,
@ -173,7 +173,7 @@ pub(crate) struct NonConstFmtMacroCall {
}
#[derive(Diagnostic)]
#[diag(const_eval_non_const_fn_call, code = "E0015")]
#[diag(const_eval_non_const_fn_call, code = E0015)]
pub(crate) struct NonConstFnCall {
#[primary_span]
pub span: Span,
@ -190,7 +190,7 @@ pub(crate) struct UnallowedOpInConstContext {
}
#[derive(Diagnostic)]
#[diag(const_eval_unallowed_heap_allocations, code = "E0010")]
#[diag(const_eval_unallowed_heap_allocations, code = E0010)]
pub(crate) struct UnallowedHeapAllocations {
#[primary_span]
#[label]
@ -201,7 +201,7 @@ pub(crate) struct UnallowedHeapAllocations {
}
#[derive(Diagnostic)]
#[diag(const_eval_unallowed_inline_asm, code = "E0015")]
#[diag(const_eval_unallowed_inline_asm, code = E0015)]
pub(crate) struct UnallowedInlineAsm {
#[primary_span]
pub span: Span,
@ -209,7 +209,7 @@ pub(crate) struct UnallowedInlineAsm {
}
#[derive(Diagnostic)]
#[diag(const_eval_interior_mutable_data_refer, code = "E0492")]
#[diag(const_eval_interior_mutable_data_refer, code = E0492)]
pub(crate) struct InteriorMutableDataRefer {
#[primary_span]
#[label]
@ -274,7 +274,7 @@ pub struct RawBytesNote {
// FIXME(fee1-dead) do not use stringly typed `ConstContext`
#[derive(Diagnostic)]
#[diag(const_eval_match_eq_non_const, code = "E0015")]
#[diag(const_eval_match_eq_non_const, code = E0015)]
#[note]
pub struct NonConstMatchEq<'tcx> {
#[primary_span]
@ -284,7 +284,7 @@ pub struct NonConstMatchEq<'tcx> {
}
#[derive(Diagnostic)]
#[diag(const_eval_for_loop_into_iter_non_const, code = "E0015")]
#[diag(const_eval_for_loop_into_iter_non_const, code = E0015)]
pub struct NonConstForLoopIntoIter<'tcx> {
#[primary_span]
pub span: Span,
@ -293,7 +293,7 @@ pub struct NonConstForLoopIntoIter<'tcx> {
}
#[derive(Diagnostic)]
#[diag(const_eval_question_branch_non_const, code = "E0015")]
#[diag(const_eval_question_branch_non_const, code = E0015)]
pub struct NonConstQuestionBranch<'tcx> {
#[primary_span]
pub span: Span,
@ -302,7 +302,7 @@ pub struct NonConstQuestionBranch<'tcx> {
}
#[derive(Diagnostic)]
#[diag(const_eval_question_from_residual_non_const, code = "E0015")]
#[diag(const_eval_question_from_residual_non_const, code = E0015)]
pub struct NonConstQuestionFromResidual<'tcx> {
#[primary_span]
pub span: Span,
@ -311,7 +311,7 @@ pub struct NonConstQuestionFromResidual<'tcx> {
}
#[derive(Diagnostic)]
#[diag(const_eval_try_block_from_output_non_const, code = "E0015")]
#[diag(const_eval_try_block_from_output_non_const, code = E0015)]
pub struct NonConstTryBlockFromOutput<'tcx> {
#[primary_span]
pub span: Span,
@ -320,7 +320,7 @@ pub struct NonConstTryBlockFromOutput<'tcx> {
}
#[derive(Diagnostic)]
#[diag(const_eval_await_non_const, code = "E0015")]
#[diag(const_eval_await_non_const, code = E0015)]
pub struct NonConstAwait<'tcx> {
#[primary_span]
pub span: Span,
@ -329,7 +329,7 @@ pub struct NonConstAwait<'tcx> {
}
#[derive(Diagnostic)]
#[diag(const_eval_closure_non_const, code = "E0015")]
#[diag(const_eval_closure_non_const, code = E0015)]
pub struct NonConstClosure {
#[primary_span]
pub span: Span,
@ -362,7 +362,7 @@ pub struct ConsiderDereferencing {
}
#[derive(Diagnostic)]
#[diag(const_eval_operator_non_const, code = "E0015")]
#[diag(const_eval_operator_non_const, code = E0015)]
pub struct NonConstOperator {
#[primary_span]
pub span: Span,
@ -372,7 +372,7 @@ pub struct NonConstOperator {
}
#[derive(Diagnostic)]
#[diag(const_eval_deref_coercion_non_const, code = "E0015")]
#[diag(const_eval_deref_coercion_non_const, code = E0015)]
#[note]
pub struct NonConstDerefCoercion<'tcx> {
#[primary_span]
@ -385,7 +385,7 @@ pub struct NonConstDerefCoercion<'tcx> {
}
#[derive(Diagnostic)]
#[diag(const_eval_live_drop, code = "E0493")]
#[diag(const_eval_live_drop, code = E0493)]
pub struct LiveDrop<'tcx> {
#[primary_span]
#[label]
@ -397,7 +397,7 @@ pub struct LiveDrop<'tcx> {
}
#[derive(Diagnostic)]
#[diag(const_eval_error, code = "E0080")]
#[diag(const_eval_error, code = E0080)]
pub struct ConstEvalError {
#[primary_span]
pub span: Span,
@ -423,7 +423,7 @@ pub struct NullaryIntrinsicError {
}
#[derive(Diagnostic)]
#[diag(const_eval_undefined_behavior, code = "E0080")]
#[diag(const_eval_undefined_behavior, code = E0080)]
pub struct UndefinedBehavior {
#[primary_span]
pub span: Span,

View file

@ -2,7 +2,7 @@
use hir::def_id::LocalDefId;
use hir::{ConstContext, LangItem};
use rustc_errors::{error_code, DiagnosticBuilder};
use rustc_errors::{codes::*, DiagnosticBuilder};
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_infer::infer::TyCtxtInferExt;
@ -372,7 +372,7 @@ impl<'tcx> NonConstOp<'tcx> for HeapAllocation {
ccx.dcx().create_err(errors::UnallowedHeapAllocations {
span,
kind: ccx.const_kind(),
teach: ccx.tcx.sess.teach(&error_code!(E0010)).then_some(()),
teach: ccx.tcx.sess.teach(E0010).then_some(()),
})
}
}
@ -434,14 +434,14 @@ impl<'tcx> NonConstOp<'tcx> for CellBorrow {
span,
opt_help: Some(()),
kind: ccx.const_kind(),
teach: ccx.tcx.sess.teach(&error_code!(E0492)).then_some(()),
teach: ccx.tcx.sess.teach(E0492).then_some(()),
})
} else {
ccx.dcx().create_err(errors::InteriorMutableDataRefer {
span,
opt_help: None,
kind: ccx.const_kind(),
teach: ccx.tcx.sess.teach(&error_code!(E0492)).then_some(()),
teach: ccx.tcx.sess.teach(E0492).then_some(()),
})
}
}
@ -469,12 +469,12 @@ impl<'tcx> NonConstOp<'tcx> for MutBorrow {
hir::BorrowKind::Raw => ccx.tcx.dcx().create_err(errors::UnallowedMutableRaw {
span,
kind: ccx.const_kind(),
teach: ccx.tcx.sess.teach(&error_code!(E0764)).then_some(()),
teach: ccx.tcx.sess.teach(E0764).then_some(()),
}),
hir::BorrowKind::Ref => ccx.dcx().create_err(errors::UnallowedMutableRefs {
span,
kind: ccx.const_kind(),
teach: ccx.tcx.sess.teach(&error_code!(E0764)).then_some(()),
teach: ccx.tcx.sess.teach(E0764).then_some(()),
}),
}
}
@ -588,7 +588,7 @@ impl<'tcx> NonConstOp<'tcx> for StaticAccess {
ccx.dcx().create_err(errors::StaticAccessErr {
span,
kind: ccx.const_kind(),
teach: ccx.tcx.sess.teach(&error_code!(E0013)).then_some(()),
teach: ccx.tcx.sess.teach(E0013).then_some(()),
})
}
}