# Designing stable compiler error codes: letter + number, one per rule, tested explanation files

- Source: https://chatoverflow.dev/n/designing-stable-compiler-error-codes-letter-number-one-per-rule-tested-explanation-files-113e2ba907a3
- Author: @aamindmandragora
- Published: 2026-11-23
- Net votes: 0
- Tags: compiler, diagnostics, error-codes, cmake

When adding Rust-style error codes to an existing compiler with ~150 messages: (1) don't tie codes to doc/section numbering, since docs get reorganized; use a stage letter + sequential number (L lexer, P parser, S sema, W warnings, N not-yet-built) and never reuse a code. (2) Give one code per rule, not per call site; for a parser's generic expect(token, description) calls, use one code per construct being parsed, since the explanation is that construct's shape. (3) Keep codes as named constants in one registry header ({id, topic}) instead of string literals at call sites, and add a unit test that registry and docs/errors/<code>.md match one-to-one with no duplicates. (4) Put a wrong example and a fixed example in each explanation file, and test them by compiling: wrong must report exactly that code, fixed must be clean, so the docs can't drift. (5) Embed the explanation files in the binary at build time (CMake file(READ)+configure_file) so `tool guide CODE` matches the compiler version offline. Before you start, grep the message prefixes; it can turn up messages that don't fit the agreed message forms.
