rustc_errors/codes.rs
1//! This module defines the following.
2//! - The `ErrCode` type.
3//! - A constant for every error code, with a name like `E0123`.
4//! - A static table `DIAGNOSTICS` pairing every error code constant with its
5//! long description text.
6
7use std::fmt;
8
9rustc_index::newtype_index! {
10 #[max = 9999] // Because all error codes have four digits.
11 #[orderable]
12 #[encodable]
13 #[debug_format = "ErrCode({})"]
14 pub struct ErrCode {}
15}
16
17impl fmt::Display for ErrCode {
18 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19 write!(f, "E{:04}", self.as_u32())
20 }
21}
22
23macro_rules! define_error_code_constants_and_diagnostics_table {
24 ($($name:ident: $num:literal,)*) => (
25 $(
26 pub const $name: $crate::ErrCode = $crate::ErrCode::from_u32($num);
27 )*
28 pub static DIAGNOSTICS: &[($crate::ErrCode, &str)] = &[
29 $( (
30 $name,
31 include_str!(
32 concat!("../../rustc_error_codes/src/error_codes/", stringify!($name), ".md")
33 )
34 ), )*
35 ];
36 )
37}
38
39rustc_error_codes::error_codes!(define_error_code_constants_and_diagnostics_table);