Files
lucisv/docs/features/types-decls
lucioandClaude Fable 5 0557933994 QA round 1: adversarial review logs and a with-block attack test
No blocking findings over the 8 round-1 clusters: whole-corpus idempotency
and comment-preservation sweep clean (796/820), 476-run mutation fuzz clean,
no swallowed errors in the diff. Adds
reindented_string_inside_a_with_block_is_rejected. Findings (non-blocking)
are in each feature README's QA log; the printer fits() / Doc::Inline gap
is fixed separately.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-08-21 01:37:33 -07:00
..

types-decls

Goal

Close the types-decls bucket of scripts/score.sh: the seven declaration shapes that real RTL writes and vfmt could not parse. All of them are about a type standing somewhere vfmt only accepted a name — an anonymous enum/struct/union used as a type in place, a typedef'd name carrying packed dimensions, a type parameter outside the #( ... ) list — plus a for loop whose init declares more than one variable.

Assumptions

  • An anonymous aggregate is the type: it never shares a DataType with a name, signing or packed dimensions of its own. Any dimensions written after the closing } belong to the declarator, which is where parse_declarators already puts them.
  • enum / struct / union at item and statement position always opens a data declaration. The typedef spelling is taken by an earlier arm, and there is no other construct those keywords can begin there.
  • At item position, an identifier followed by balanced [...] groups and then another identifier is a declaration, not an expression. A range select followed by a name is not a legal statement, so nothing is stolen from the expression grammar. The same test gates statement position, where it is what keeps x[i] = 1; an assignment: what follows the brackets there is =.
  • Enum member names introduced by an anonymous enum are not entered into the semantic model's scopes (only typedef enum members are). Nothing in the formatter needs them; see Handshakes.
  • An anonymous struct/union body always breaks one member per line, like the typedef struct path it shares. An anonymous enum body is a group: one line if it fits, else one member per line — again the typedef enum rule. Both are the layout the corpus already writes.

Handshakes

Consumed

  • vlog_parser::parser — parse_opt_data_type is the one door every type position goes through (declarations, ports, typedef bases, tf arguments, struct members, parameter type defaults). Teaching it about anonymous aggregates is what made shapes 1, 2 and 5 land together.
  • vlog_formatter::format — delimited_dangling (the fit-or-break list) and indent/Doc::HardLine for the struct body; both were already what the typedef path used.
  • vlog_formatter::verify — not modified. Every shape here adds tokens to the AST rather than normalizing them away, so the existing equivalence and convergence checks cover the new output unchanged.

Promised / changed for other modules

  • ast::DataType lost params: Option<Box<RawText>> and gained extra: Option<Box<TypeExtra>>, read through the accessors DataType::params() and DataType::inline(). The two rare tails share one box on purpose: DataType rides inside every Expr through ExprKind::Type, and a second Option<Box<..>> field pushed DimKind past clippy's large_enum_variant threshold. Only format.rs and two parser sites read the field.
  • ast::ParamDecl gained is_type: bool (localparam type payload_t = ...;). vlog_linter's explicit-parameter-storage-type now exempts it, matching the exemption the parameter-port path already made for parameter type T = int — a type parameter has no storage type to declare. Fixed here rather than filed, because the false positive is created by this change.
  • ast::{EnumType, EnumMember, StructType, StructMember, Declarator, ListDirective} now derive Clone, so DataType can keep deriving it.
  • No new ItemKind / StmtKind / ExprKind variants, so no exhaustive-match arms were needed in vlog_linter::visit, vlog_semantic or vfmt_server.

Requests for other modules (not done here)

  • vlog_semantic::bind declares typedef enum members and typedef struct members as symbols (bind.rs:395, bind.rs:406). The equivalent members of an anonymous aggregate — enum {A, B} x;, the nested struct packed {...} q; of a register-package typedef — are not declared, so go-to-definition and hover do not find them. Purely an LSP completeness gap; the formatter and the corpus metric do not depend on it.
  • vlog_linter's enum-name-style / struct-union-name-style only look at ItemKind::Typedef, so an anonymous aggregate is out of their reach by construction. That is correct today (an anonymous type has no name to style), but a future rule about member naming would want the inline bodies too.

Status / TODO

  • 1. Anonymous enum as a variable declaration, with and without a base type — corpus/common_cells/src/deprecated/clock_divider.sv:59, generic_fifo.sv:61, generic_fifo_adv.sv:39
  • 1b. Anonymous packed struct / union declarations (no corpus hit; same machinery, cases added)
  • 2. struct packed { ... } / enum { ... } as a parameter type default — corpus/common_cells/src/ecc_decode.sv:36, ecc_encode.sv:29
  • 3. A typedef'd type with packed dimensions before the declarator — corpus/common_cells/src/stream_omega_net.sv:162, stream_xbar.sv:98; also as a function local and a class property
  • 4. typedef enum <typedef'd_base_t> { ... } name_t; — corpus/ibex/vendor/lowrisc_ip/ip/prim/rtl/prim_count_pkg.sv:10
  • 5. A nested anonymous aggregate as a struct member — corpus/opentitan/hw/ip/uart/rtl/uart_reg_pkg.sv:30
  • 6. localparam type / parameter type as a body item — corpus/common_cells/test/lossy_valid_to_stream_tb.sv:26
  • 7. A for loop declaring several variables in its init — corpus/ibex/vendor/lowrisc_ip/ip/prim/rtl/prim_sparse_fsm_flop.sv:48
  • Cases in crates/vlog_formatter/tests/cases/types_decls.cases (22 cases: one per shape, the negative x[i] = 1; statement, a // verilog_format: off fence, comment attachment, and the line-length boundary), plus 9 parser unit tests
  • cargo fmt / cargo clippy --all-targets / cargo test clean
  • No corpus regression: the passing set after is a strict superset

Work log

2026-08-21 — dev

Baseline {"passing":740,"total":820,"pct":90.2,"failures_by_category": {"dv-statements":26,"expressions":21,"directive-split":14,"types-decls":11, "invalid-or-fragment":3,"lex-macro-number":2,"other":2,"not-verilog":1}}

After {"passing":751,"total":820,"pct":91.6,"failures_by_category": {"dv-statements":26,"expressions":21,"directive-split":14, "invalid-or-fragment":3,"lex-macro-number":2,"other":2,"not-verilog":1}}

All 11 types-decls files pass; the category is gone from the JSON. No file that passed before fails now (comm on the two .score-results.tsv pass sets is empty in the regression direction).

Parser. The shape of the change is one refactor plus six small openings:

  • parse_typedef's enum and struct bodies moved out into parse_enum_type / parse_struct_type, which run from the keyword through the closing }. typedef now calls them and appends the name; so does everything below.
  • parse_opt_data_type takes an enum/struct/union at the cursor and stores the body in DataType::extra. Because that function is the single door to every type position, this one hook delivered shapes 1, 2 (through parse_type_expr, which a parameter type default already used) and 5 (through parse_struct_type's own member loop — the recursion is free).
  • at_typed_declarator replaces the two hand-rolled copies of the "identifier type?" lookahead (in parse_opt_data_type and parse_assign_or_expr_stmt) with one that also skips a run of balanced [...] groups. Shape 3, at item, statement and class-member position at once. scan_matching is strictly increasing on a [, so the skip loop terminates.
  • parse_enum_type's base type goes through the new parse_type_ref, which falls back to a bare identifier (plus :: chain, #( ... ) and packed dims) when parse_opt_data_type declines — shape 4. parse_type_expr was a cut-down copy of that fallback and now delegates to it.
  • parse_param_decl eats an optional type keyword and parses each = ... as a type rather than an expression — shape 6. parse_declarators grew a parse_declarator_list(type_init) sibling for that.
  • The for init accepts an identifier type when at_typed_declarator says a declarator follows it — shape 7. The comma-separated step list already worked.
  • parse_keyword_item and parse_keyword_stmt route enum/struct/union to parse_data_decl instead of erroring.

Formatter. typedef's two big arms became enum_type / struct_type, which data_type also calls for an inline aggregate — so a nested struct member indents one more level for free, and an anonymous enum inherits the fit-or-break group the typedef path already had. param_decl and param_decl_cells print the type keyword; putting it in the prefix cell is what keeps a run of aligned localparam type lines aligned.

Decision: one box, not two. Adding inline: Option<Box<InlineType>> beside params: Option<Box<RawText>> grew DataType by 8 bytes, which grew Expr (it carries a DataType through ExprKind::Type), which took DimKind::Range { a: Expr, b: Option<Expr> } from 200 to 208 bytes and tripped clippy's large_enum_variant at its 200-byte threshold. Rather than #[allow] a lint on a shared AST node for a rare field, the two mutually exclusive tails were merged into extra: Option<Box<TypeExtra>> with params() / inline() accessors — which is what params' own doc comment had been asking for since it was written. DataType is back to its original size and clippy is clean with no suppressions.

Not fixed, and why. corpus/ibex/vendor/google_riscv-dv/src/isa/custom/riscv_custom_instr_enum.sv is an include fragment — a bare CUSTOM_1, at top level, meant to be pasted inside an enum body. It is not a translation unit and vfmt should keep rejecting it. It is classified invalid-or-fragment, not types-decls, in both the before and after TSVs, so it was never in this bucket's count.

QA log

2026-08-21 — round-1 adversarial QA

Verified: cargo test --workspace/clippy/fmt clean; corpus score 796/820; whole-corpus idempotency + comment-preservation sweep clean; 476-case mutation fuzz clean.

Targeted attacks: fed the statement-position ident [..] ... ident lookahead every shape from the hit-list plus variants — x[i] y;, a [b] = c;, foo [1] ;, x [i] = y;, arr[3] = val;, x[i][j] y;, x [7:0] y, z;, x[7:0] y = 1;, x[a+b] y;, x [1] y [2];. Findings:

  • a [b] = c;, foo [1] ;, arr[3] = val; correctly stay ordinary statements (array-index assignment / index expression) — not reinterpreted as declarations.
  • x[i] y; and friends (a trailing bare identifier before ; with no assignment) are reinterpreted as declarations (space normalized to x [i] y;). This is not a corruption risk: under real SV grammar, ident [expr] ident; with no operator between the two identifiers has no valid non-declaration reading (bare juxtaposition isn't a legal expression/statement), so there is nothing else this string could have meant — the old behavior was simply "reject it," the new behavior is "accept the only grammatical reading." Confirmed idempotent (x[i] y; case, cmp clean across two passes).
  • No case found where the lookahead grabs something that was a genuinely valid other statement before this change.

Also exercised parse_opt_data_type's inline-aggregate acceptance with DataType::extra via the corpus sweep and mutation fuzz (delete ;/paren, comment-in-every-gap, fenced) over 3 samples from types_decls.cases — no panics, no idempotency breaks.

No BLOCKING findings for this cluster.