User failures and internal failures travel separate lanes as structured payloads with deferred rendering.

Compiler errors as structured data

Suppose the page mistypes the dependency selection:

@greeting great

The failure is not "something went wrong." It is a precise event: an unknown dependency-bound symbol at a known location, in a known stage, with a stable code a tool can recognise.

Moth treats diagnostics as durable structured data. Rendering into English is a later, separate act.

The stage with the best context

Each stage owns the failures it is uniquely equipped to see.

Stage

Example failure

Stage 0, graph and config

unresolved provider, invalid root role, or a command-required entry missing

Stage 1, tokenizer

bad escape, broken delimiter

Stage 2, headers and interface binding

malformed shell, dependency symbol not visible

Stage 3, local declaration ordering

a declaration cycle inside one module

Stage 4, AST

unknown name, type mismatch, bad template slot

Stage 5, HIR validation

broken lowering invariant, malformed control flow

Stage 6, borrow validation

conflicting exclusive access

Lifetime analysis, after Stage 6 and unnumbered

a retained edge into shorter-lived storage

Target validation

unsupported capability on a reachable function

A missing provider and a missing symbol look alike in a terminal and belong to different owners. Stage 0 resolves whether the provider exists at all. Stage 2 decides whether a name that provider publishes is visible here.

A single global "parser error" funnel would throw away context. The type checker already knows the expected type. The borrow checker already knows the place. Let the owner who holds the fact build its typed diagnostic draft.

The split is about who should act. One lane asks the author to change their code. One reports that the build could not run. One says the compiler broke and later results cannot be trusted.

Diagnostic data lanes

Three lanes matter.

User-facing diagnostics are typed DiagnosticDraft values. Producers create them through the owning family schemas, and DiagnosticBag accumulates them locally.

Owning boundaries capture typed facts and identity context, then freeze compact DiagnosticStore records and TypeDisplaySnapshot data into DiagnosticReports. Renderers read family facts through DiagnosticViews.

InfrastructureFailure carries operational failures: filesystem, tooling, provider, output and dev-server problems, plus internal failures whose impossibility is not yet proven. It holds a kind, stage, optional host path, message and typed details, never source spans or compact IDs. These mean "the build could not run," not "rename your variable."

Compiler bugs are proven-impossible states. compiler_bug! panics with a CompilerBugReport carrying the stage, Rust caller and invariant message; the host boundary renders it. A compiler bug is never a Result variant.

Compilation boundaries return CompilationResult<T>: Ok(CompilationOutcome::Success { value, warnings }), Ok(CompilationOutcome::Diagnosed(reports)) or Err(InfrastructureErrors). Warnings travel only with success; when infrastructure failures and diagnostics coexist, the infrastructure failure wins.

Stable diagnostic codes and typed family facts keep semantic identity separate from prose. Foreign-site references are report-owned frozen identity-context references indexed by ForeignSiteRecord. This storage change leaves diagnostic output unchanged.

Deferred-feature diagnostics stay distinct from outside-design-scope diagnostics. "Not implemented yet" and "not part of the language" are different sentences.

Semantic identities in messages

Type diagnostics begin with semantic TypeIds and context. Owning boundaries snapshot only the display facts needed for rendering into the report's TypeDisplaySnapshot; family views resolve them without retaining the module's full type environment.

Why wait?

Because the same identity might print differently in different tools, and because pretty names are not the semantic authority. Source spelling, canonical paths and human wording remain distinct layers. A stable code must not be repurposed for a different semantic family when someone rewrites a sentence.

Carrying and rendering

Stages accumulate typed drafts in local bags. Builds merge prepared facts in canonical order: file order and module order, never worker-completion order.

Rendering happens at CLI or tooling boundaries from frozen report views. The same typed event can feed a terminal, a future editor integration or a test harness.

Module outcomes split cleanly:

A consumer blocked by a diagnosed required interface never compiles as if the provider half-worked. A failed root does not feed its dependents. Independent graph branches may continue under build-system orchestration for check or tooling. A backend never receives a partial linkable project.

Parallel failures

Workers may finish in any order. Merge order ignores that race. Canonical module order and original source order decide how humans and tools see the list.

Per-stage articles still own their contracts. This page owns the transport and lane model, not a catalogue of every code.

Hints never decide structure

A hint may only follow a proven structural error. It must never decide scope, insert a closer or replace the source-accurate diagnostic.

Scope errors show the rule. MOTH-SYNTAX-0041 reports a ; placed where a scope closer cannot go. MOTH-SYNTAX-0042 reports a source scope that never closes, names its construct, labels its opener and labels any visible ; that closed an inner scope instead. Those facts come from the parser that balanced the scope, so they are exact.

A suggestion such as "did you mean to close this if before this line?" based on indentation was investigated and rejected:

A dedented line can still sit inside the body. This program leaves the function unclosed, so it reports MOTH-SYNTAX-0042:

-- Invalid: the function never closes.
run |ready Bool|:
    if ready:
        io.line("Priya")
io.line("Rob")
    ;

Here io.line("Rob") belongs to the if even though it is dedented, and the ; closes the if, leaving the function open. An indentation heuristic would suggest closing the function before io.line("Rob"), which would move a valid body line out of its scope.

Revisiting this needs a token-side indentation fact and an exact trigger, not a guess from whitespace.

What can fail here

Exact wording in examples should track current constructors when you verify against the tree. Do not fossilise a poetic error string that the code never emits.

Why Moth does it this way

Alternative: construct final prose inside each parser or checker branch.

Choice: typed family drafts compacted into report-owned stores and rendered through read-only views.

Benefit: CLI, dev server and tooling render the same semantic event. Codes stay stable while wording improves.

Cost: family schemas, fact codecs and renderers become maintained data, not afterthoughts.

Roadmap and current status

Exit state

You leave knowing how failures travel, and what a trusted successful semantic result implies. Next: how successful meaning becomes explicit runtime control flow in HIR.

Compiler design / Previous / Next