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
23rustc_error_messages::into_diag_arg_using_display!(ErrCode);
24
25macro_rules! define_error_code_constants_and_diagnostics_table {
26    ($($name:ident: $num:literal,)*) => (
27        $(
28            pub const $name: $crate::ErrCode = $crate::ErrCode::from_u32($num);
29        )*
30        pub static DIAGNOSTICS: &[($crate::ErrCode, &str)] = &[
31            $( (
32                $name,
33                include_str!(
34                    concat!("../../rustc_error_codes/src/error_codes/", stringify!($name), ".md")
35                )
36            ), )*
37        ];
38    )
39}
40
41rustc_error_codes::error_codes!(define_error_code_constants_and_diagnostics_table);