Skip to content

Diagnostic System

Error Code System

Error codes are grouped by category:

RangeCategoryDescription
E0xxxLexical/SyntaxLexical analysis and syntax errors
E1xxxType CheckingType mismatches, undefined variables, etc.
E2xxxSemantic AnalysisSemantic errors
E4xxxGenerics/TraitGenerics and trait system errors
E5xxxModule/ImportModule system errors
E6xxxRuntimeRuntime errors
E7xxxI/OI/O and system errors
E8xxxInternalInternal compiler errors
W1xxxWarningDead code, unused variables, etc.

Diagnostic Data Structure

rust
pub struct Diagnostic {
    pub code: String,           // Error code, e.g., "E1001"
    pub severity: Severity,     // Error / Warning / Info / Hint
    pub message: String,        // Rendered message
    pub span: Option<Span>,     // Source code location
    pub help: Option<String>,   // Fix suggestion
    pub related: Vec<Box<Diagnostic>>,  // Related diagnostics
}

DiagnosticBuilder Pattern

Obtain a builder through ErrorCodeDefinition, chain calls to set parameters:

rust
let diagnostic = ErrorCodeDefinition::unknown_variable("x")
    .at(span)
    .help("did you mean 'y'?")
    .build();

i18n Support

Titles and help text for all error codes are managed through I18nRegistry, supporting Chinese and English switching. Message templates support {param} placeholders.

Emitter Output

  • TextEmitter:Text format output, supports colors and Unicode symbols
  • JsonEmitter:JSON format output, used for CI and LSP