Frontend

Diagnostics & Error Recovery

A compiler that stops at the first error is a bad tool. Spans, recovery, synchronization, and diagnostics that name what was expected instead of saying "syntax error".

Source Locations
▶ lab

File, line, column, offset — four numbers that sound trivial and are not. Byte offsets versus (line, column), and the UTF-16 code unit that LSP counts by and your compiler almost certainly does not.

Q · How does a compiler know which line and column an error is on, and why do the numbers sometimes disagree between tools?
Spans and Ranges
▶ lab

A span is two offsets, half-open, threaded through every stage of the compiler. This is the lesson where the discipline of carrying them finally pays: underlines, secondary labels, jump-to-definition, rename, quick fixes and source maps are all one data structure.

Q · Why do compilers store a range rather than a position, and what does carrying it through every phase actually buy?
Error Recovery
▶ lab

Detect, report, synchronize, continue. A compiler that stops at the first error costs a round trip per typo — and one that recovers badly turns a single missing brace into forty messages, which is worse.

Q · How does a compiler keep going after a syntax error, and why does one missing brace produce forty messages?
Parser Synchronization
▶ lab

The judgement call inside error recovery: which token to resume at. Too eager and a whole function is skipped in silence; too timid and one missing brace produces forty messages that are all the same mistake.

Q · After a parse error, how does the parser decide where to start parsing again?
Diagnostic Quality
▶ lab

`syntax error` versus `Expected ')' after function arguments. Found '{' instead.` The difference is not politeness — it is a span, a named expectation, and a note about the thing that caused it, each of which the compiler had to be built to keep.

Q · What separates an error message that helps from one that is technically correct?
Suggested Fixes
▶ lab

A diagnostic that carries an edit an editor can apply, and the discipline that keeps it honest: "did you mean" by edit distance, a confidence label on every suggestion, and a budget past which no suggestion is better than a confident wrong one.

Q · When should a compiler propose a fix, and when is proposing one actively harmful?