Skip to content

Diagnostic System ​

Error Code System ​

Error codes are grouped by category:

RangeCategoryDescription
E0xxxLexical/SyntaxLexical analysis and parsing errors
E1xxxType checkingType mismatch, 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,           // 错误码,如 "E1001"
    pub severity: Severity,     // Error / Warning / Info / Hint
    pub message: String,        // 渲染后的消息
    pub span: Option<Span>,     // 源码位置
    pub help: Option<String>,   // 修复建议
    pub related: Vec<Box<Diagnostic>>,  // 关联诊断
}

DiagnosticBuilder Pattern ​

Obtain a builder via ErrorCodeDefinition and set parameters through chained calls:

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-English switching. Message templates support {param} placeholders.

Emitter Output ​

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