Root cause of the 100 GB crash: a non-ANSI port loop took its trailing
comma from the element, an error element never got that far, the next
iteration failed on the same comma, consumed nothing and pushed another
empty error element - an allocating spin. Fix is structural: list_ends
ends the list whenever the cursor has not moved since the iteration
began; the two loops that read their own separator also eat it after an
error element. Truncate-anywhere + delete-any-character pass under
tools/guard.sh -m 4. Strict parity: element_or_recover declines on a
conditional-compilation directive so split.rs's verbatim rescue still
runs. cargo test -p vlog_parser 365 + 26 + 31 / 0 failed; workspace
suite NOT re-run since the last fixes. Not done: completion pin + the
three validity probes (32.6), measurements (32.7), README amendments,
coverage_gaps M10, TODO ticks, every gate. Handshake: three
vlog_linter arms (visit.rs walk_param_ports, signals.rs connection
loops) live in the earlier commit 6a15594.
Co-Authored-By: Claude Fable 5 <[email protected]>
241 lines
14 KiB
Markdown
241 lines
14 KiB
Markdown
# header-recovery
|
||
|
||
## Goal
|
||
|
||
`docs/features/error-recovery/README.md` recovers in item and statement
|
||
**lists** only, so an error anywhere inside a *header* escapes to the nearest
|
||
enclosing list and takes the whole construct with it: a typo in a port list,
|
||
parameter list, formal list or connection list destroys the `Module`,
|
||
`FunctionDecl` or `Instantiation` it was written in, the body then re-parses at
|
||
the wrong scope, and the orphaned `endmodule` becomes a second error item. Two
|
||
diagnostics and a misleading tree for one missing comma — the state an editor
|
||
buffer is in most of the time. This feature (TODO §32) adds recovery *inside*
|
||
those headers: a list element that does not parse becomes an
|
||
`ListElem::Error(RawText)` element of its list, a statement header's condition
|
||
that does not parse becomes an `ExprKind::Error(RawText)`, and the construct
|
||
around it keeps its name, the elements that were typed and its body. Strict
|
||
parsing is untouched — every new branch sits behind `self.tolerant` on the
|
||
error path — so the formatter's "zero errors or nothing" contract and the
|
||
byte-identity of every corpus file are unchanged.
|
||
|
||
## Assumptions
|
||
|
||
- **A header's error element is a leaf, not a guess.** The parser does not try
|
||
to work out what the user meant by the text it skipped; the element records
|
||
the bytes and its span, and every consumer decides what to do with it. The
|
||
variant exists so that consumers *must* decide: a `Vec<AnsiPort>` that
|
||
silently hid the hole would let the LSP mislabel what follows it.
|
||
- **The skip is list-local and bracket-seeded.** The bracket counter starts at
|
||
one — the list's own open bracket, already consumed — so a `;` inside a
|
||
`for (…; …; …)` header is invisible and no skip can run past the bracket that
|
||
closes the list it is in.
|
||
- **One line-based rule.** A skip stops at the first token of a new line. It is
|
||
the only line-based rule in the parser, and it is what makes
|
||
`module m (input logic a, inp⏎ output b);` cost one comma instead of the rest
|
||
of the list.
|
||
- **A recovered element ends its list unless a `,` follows it.** The skip stops
|
||
at the separator when there is one; no separator means the list is not there
|
||
any more and what follows belongs to the enclosing construct. That is what
|
||
leaves the `wire w;` under a half-typed `sub u (.a(` an item of the module.
|
||
- **Recovery declines on a conditional-compilation directive.** A list element
|
||
that fails on `` `ifdef ``/`` `else ``/`` `endif `` is one arm of a construct
|
||
written across the arms of a region, and the enclosing item or statement
|
||
captures the whole of it verbatim (`split.rs`). Recovering it here would take
|
||
that rescue away and give a file the strict parser reads *correctly* a
|
||
different tree.
|
||
- **Recovery is not silence.** Every shape still yields at least one
|
||
diagnostic; a list read on past a missing comma says so, and a bracket or `;`
|
||
missing behind an already-reported typo is the only thing taken silently.
|
||
- **A recovered header may still swallow the line the failure was on.** The
|
||
expression grammar is greedy: `for (int i = 0; i <⏎ x = 1;` reads `x` as the
|
||
right operand of `<`, so the error node covers that line. The loop survives;
|
||
the statement that was on the line does not.
|
||
|
||
## Handshakes
|
||
|
||
**Consumed from `docs/features/error-recovery/README.md`:** the tolerant flag,
|
||
`recover`, `skip_to_sync`, `at_sync_stop`, `at_body_end` and `expect_closer`
|
||
are unchanged and still own item- and statement-list recovery. This feature
|
||
adds a second, smaller mechanism beside them (`parser/recover_list.rs`) and
|
||
does not touch the first.
|
||
|
||
**Promised to `vlog_formatter`:** `format_source` still calls the strict
|
||
parser, so `ListElem::Error` and `ExprKind::Error` cannot appear in a tree the
|
||
formatter builds. Three arms were added anyway, because the new variants make
|
||
its exhaustive matches fail to compile: `ListItem::from_raw` (a header list's
|
||
error element, printed verbatim), the `ExprKind::Error` arm in `format/expr.rs`
|
||
(`raw_inline`), and `Fmt::list_elem`, the one-line adapter every header list
|
||
now maps through. Each says at its definition that it is unreachable from
|
||
`format_source`, exactly as `ItemKind::Error` does. `verify.rs` is untouched.
|
||
|
||
**Promised to `vlog_semantic`:** an error element declares nothing and binds
|
||
nothing — `bind_param_ports`, `bind_tf_args`, the port-list arms and the new
|
||
`bind_connections` all skip them, mirroring `bind/item.rs`'s `ItemKind::Error`
|
||
arm.
|
||
|
||
**Requested of the §33 linter lane (Handshake, not a silent edit):** three
|
||
places in `crates/vlog_linter` had to change for the new variants —
|
||
`visit.rs`'s `walk_param_ports` (one `filter_map(ListElem::ok)`, since an error
|
||
element has no name or type for a rule to judge) and `signals.rs`'s two
|
||
connection loops (an error element's span joins the same opaque-span bucket a
|
||
connection's does). The lead pre-agreed exhaustive-match arms in that crate;
|
||
these are that, plus the two-line `match` `signals.rs` needed to keep pushing a
|
||
span. Named here and in the report for the merge.
|
||
|
||
**`vlog_refactor` and `lucisv_server`:** both read tolerant trees (the server's
|
||
`FileFacts` is `tokenize` + `parse_tolerant` + `analyze`), so both had to
|
||
decide. Refactor skips error elements for every *name* question and keeps their
|
||
spans where it asks where the list's text *is* (`ports/list.rs::elem_spans`).
|
||
The server skips them in `type_def`, and `inlay` declines to hint a list that
|
||
holds one at all: which port a position names is exactly what the skipped text
|
||
decides, and a wrong hint is worse than none.
|
||
|
||
## Status / TODO
|
||
|
||
- [x] 32.1 `ListElem<T>` (`Ok` / `Error(RawText)`) in the five header lists —
|
||
a module's and a class's parameter ports, ANSI and non-ANSI ports, a
|
||
subroutine's formals, named and ordered connections — plus
|
||
`ExprKind::Error`, with the cross-crate arms above.
|
||
- [x] 32.2 `element_or_recover` and the list-local, bracket-seeded sync set
|
||
(`,`, the list's own `)`, `;`, a line change) in
|
||
`parser/recover_list.rs`.
|
||
- [x] 32.3 headers keep their node: the four list parsers are infallible in
|
||
tolerant mode, closers go through `close_list`, and a header that has
|
||
already reported something takes its `;` silently
|
||
(`expect_after_recovery`).
|
||
- [x] 32.4 `for` / `if` / `while` / `repeat` / `case` / `do…while` conditions
|
||
recover inside the parentheses (`cond_or_recover`, `error_expr`), with
|
||
`body_after_recovery` so a header truncated above an `end` does not eat
|
||
it.
|
||
- [x] 32.5 parser pins per shape with exact counts (9 new tests in
|
||
`tests/error_recovery.rs`, the half-typed-instantiation pin inverted as
|
||
planned); `error_recovery.rs`'s walkers and `tests/spans.rs`'s exhaustive
|
||
walker extended to the new shapes; the 550-source strict/tolerant
|
||
equality test green.
|
||
- [x] 32.6 server pins: one diagnostic, the module still in the outline with
|
||
the ports either side of the typo, `pairs::missing_end_diagnostics`
|
||
silent (`diagnostics.rs`), and the inlay rule (`inlay.rs`).
|
||
- [ ] 32.6 the completion pin — `visible_symbols` at a cursor inside a broken
|
||
header — **not written yet**.
|
||
- [ ] 32.6 re-check of the three validity probes (`inline.rs`,
|
||
`templates.rs`, `completion/tiers.rs`) — **not done**.
|
||
- [ ] 32.7 measurements: `perf.rs`'s `parse_tolerant` harness before/after, and
|
||
the truncated-corpus diagnostic census — **not run**.
|
||
- [ ] 32.7 docs: the error-recovery README's Assumptions sentence and its
|
||
inverted "Not done" line; `docs/coverage_gaps.md` M10; TODO §32 ticks —
|
||
**not done**.
|
||
- [ ] Gates: `cargo fmt -p` per crate, `cargo clippy --all-targets --workspace`,
|
||
`cargo test --workspace` counts, corpus 814/820 byte-identical, the
|
||
read-only opentitan sweep — **not run** (only the crate suites below).
|
||
|
||
## Work log
|
||
|
||
### 2026-09-03 — dev agent (paused; internet outage)
|
||
|
||
**Where this stands.** The tree **builds** (`cargo build --workspace` clean,
|
||
`cargo check --workspace --all-targets` silent). 32.1–32.5 are in and green;
|
||
32.6 is half in; 32.7 (docs, measurements) is not started. Nothing is
|
||
committed. The work sits on `wip-header-recovery` in
|
||
`.claude/worktrees/recovery`, on top of 6a15594.
|
||
|
||
**The two failures the lead reported are both fixed, and both were mine.**
|
||
|
||
1. **The runaway (6 GB, machine down).** Root cause, found by a probe that
|
||
printed every fuzz input before parsing it: deleting the `#` from
|
||
`sv_features.sv:268` turns `#(parameter …)` into a *port list*, which is
|
||
read as **non-ANSI** (it opens with the keyword `parameter`, not a type or a
|
||
direction). `parse_non_ansi_port` reads its own trailing `,`, so I took the
|
||
loop's `comma` from the element — and an **error** element never got that
|
||
far, leaving the `,` under the cursor. The next iteration then failed at the
|
||
same `,`, consumed nothing (a `,` is not a list end, so the always-advance
|
||
bump declined it, and the skip stops before it), and pushed another empty
|
||
error element: an allocating spin. My termination argument had assumed every
|
||
loop ate the comma, which two of the six did not.
|
||
**The fix is structural, not an argument.** `Parser::list_ends` now takes
|
||
the cursor the iteration began at and ends the list whenever it has not
|
||
moved — no reasoning about element parsers or skips can make one of these
|
||
loops spin, because the loop refuses to go round again on an unmoved cursor.
|
||
The two loops that read their own separator (non-ANSI ports, positional
|
||
connections) also eat the `,` after an error element now, so recovery still
|
||
resumes at the next element rather than merely stopping.
|
||
2. **The strict-mode parity failure.** `tolerant_parse_equals_strict_parse_on_
|
||
valid_sources` failed on `directive_in_statements.cases`: an instantiation
|
||
(and, in a second case, a formal list) written across the arms of a
|
||
`` `ifdef `` fails in strict mode *by design* and is rescued at item level by
|
||
`split.rs`, which captures the whole construct verbatim. My list recovery
|
||
swallowed that failure into an error element, so the rescue never ran and
|
||
the tolerant tree held an `Instantiation` where the strict tree holds a
|
||
`Verbatim`. Fix: `element_or_recover` declines — passes the error out
|
||
untouched — when the cursor stands on a conditional-compilation directive.
|
||
Balanced directives never reach it (`take_list_directives` harvests those),
|
||
so this costs nothing else.
|
||
|
||
**Measured shapes (a)–(g), after both fixes.** Each is now a pin in
|
||
`tests/error_recovery.rs`:
|
||
|
||
| shape | diagnostics | tree |
|
||
|---|---|---|
|
||
| `module m #(parameter W = , parameter X = 4) (input a); wire ok; endmodule` | 1 | one `Module m`, `param_ports` = [error, `X`], port `a`, body `wire ok;` |
|
||
| `module m (input logic a, inp⏎ output b);` | 1 (`expected \`,\``) | ports `a`, `inp`, `b` — no error element |
|
||
| `module m #(parameter W = 8,` at EOF | 2 (unclosed `)`, unclosed module) | `Module m` with parameter `W` |
|
||
| `for (i = 0; i < ; i = i + 1) x = 1;` | 1 (was 2) | `For` with an `Error` condition, body `x = 1;`, the next statement intact |
|
||
| `for (int i = 0; i <⏎ end` | 1 | `For` with an `Error` condition and an **empty** error body — the `end` its block waits for is not eaten |
|
||
| `sub u (.a(x), .b(⏎ wire w;` | 1 | the instantiation keeps `.a(x)` and one error element; `wire w;` is a **sibling item** (the old pin inverted, as planned) |
|
||
| `function int f(input int a,⏎ f = a;⏎ endfunction` | 1 | the function and the module both intact |
|
||
| `if (a &&⏎ x = 1;⏎ y = 2;` | 1 | `If` with an `Error` condition and `y = 2;` as its branch |
|
||
|
||
**Design decisions worth the lead's attention.**
|
||
|
||
- **One generic `ListElem<T>` rather than four bespoke enums.** Same two
|
||
variants in all five lists; one type means one place says what an error
|
||
element is. Consumers read it through `ok()` / `error()` / `is_error()`.
|
||
- **"Has this header already reported something?" is asked of the diagnostic
|
||
count** (`diags_mark` / `recovered_since`), not of the elements. It is always
|
||
0 in strict mode, so every question asked of it answers "no" there, and it
|
||
covers a missing closer as well as a broken element.
|
||
- **`body_after_recovery`** gives a recovered statement header an *empty* error
|
||
leaf for its body where the enclosing block has already ended, rather than
|
||
inventing a `;` the source never wrote.
|
||
- **Cost of the shape change.** `Vec<T>` → `Vec<ListElem<T>>` in five lists
|
||
touched ~60 sites across six crates. Most are one-line filters; the test
|
||
churn is absorbed by an `oks()` helper in each test module (strict parses
|
||
never build an error element, so it is the whole list).
|
||
|
||
**Fuzzers, under `tools/guard.sh`.** The truncate-anywhere *and*
|
||
delete-any-character sweeps over every formatter fixture complete with
|
||
`-m 4 -t 900` (they exceeded 3 GB before the fix, and 6 GB in the lead's run).
|
||
`cargo test -p vlog_parser` is green: 365 unit + 26 spans + 31 error-recovery,
|
||
0 failed. `cargo test --workspace` had one failure left when I stopped — the
|
||
inlay test below, which I then updated and re-ran green in isolation; the
|
||
workspace run has **not** been repeated since.
|
||
|
||
**Server pins.** `diagnostics.rs` gained
|
||
`a_broken_port_list_publishes_one_error_and_keeps_its_module` (one diagnostic,
|
||
`missing_end_diagnostics` silent, the module and the ports either side of the
|
||
typo in the outline). `inlay.rs`'s
|
||
`a_buffer_that_does_not_parse_hints_nothing_rather_than_guessing` was replaced
|
||
by `a_truncated_list_hints_and_a_recovered_one_does_not`: a truncated list now
|
||
parses, and every position in it is where it was written, so it is hinted; a
|
||
list with a *hole* is declined whole. `completion/module.rs`'s
|
||
`a_module_with_an_attribute_near_its_header_is_not_indexed_at_all` split in
|
||
two — an attribute on a **port** is now one error element and the module is
|
||
indexed and its header slices, which is the re-audit that test's old message
|
||
asked for; an attribute between the name and the `#(` is inside no list and is
|
||
unchanged.
|
||
|
||
**Next steps, in order:** the completion pin and the three validity probes
|
||
(32.6); the `parse_tolerant` timing and the truncated-corpus census (32.7); the
|
||
error-recovery README amendments, `coverage_gaps.md` M10 and the TODO ticks;
|
||
then the full gates — `cargo fmt -p` per touched crate,
|
||
`cargo clippy --all-targets --workspace`, `cargo test --workspace` with counts,
|
||
the corpus byte-identity run against `/tmp/lucisv-0.35.0-baseline`, and the
|
||
read-only opentitan sweep. All of them through `tools/guard.sh`.
|
||
|
||
## QA log
|
||
|
||
*(empty — QA has not run)*
|
||
|
||
## Review
|
||
|
||
*(reserved for the lead)*
|