Module comma_paren

Module comma_paren 

Source
Expand description

Pass: lower C-style comma operator inside parenthesised expressions.

HLSL’s (a, b, c) is the comma operator — evaluate left-to-right, yield the rightmost operand. The MD2 corpus uses this sparingly but it surfaces in shapes like:

uv2 = uv + texsize.zx * (q3, q3);

(a redundant-but-valid double of q3; HLSL treats the parens as scalar q3 so the surrounding texsize.zx * scalar is well-typed). WGSL has no comma operator — the parens parse q3, q3 as two arguments and the containing * rejects with expected ')'; found ','.

The pass is purely token-driven: detect every ( whose previous non-whitespace token is not Ident, Keyword, RParen, RBracket (those are call / index / cast contexts where commas separate arguments, not operator operands), then scan for top-level commas inside the matching pair and emit a single edit that strips every token up to and including the final top-level comma.

Skipping Keyword covers the for(init; cond; step) / if(...) / while(...) / do {...} while(...) / switch(...) / return (...) / cast contexts in one rule. The cost is missing the (very rare) return (a, b); idiom; we judged that acceptable next to the breakage potential of rewriting commas inside for(int i = 0, j = 0; ...) init lists.

Found in 17 warp + 2 comp presets on the 2 000-sample, all of which presented as the expected ')'; found ';' cluster — once the comma op is collapsed to its rightmost operand the surrounding expression type-checks and naga validates.

Idempotent: a second run sees no top-level commas inside any paren expression and emits no edits.

Structs§

ParenState 🔒

Functions§

collect_edits 🔒
For every paren-expression (a, b, …, z) (i.e. ( not preceded by an identifier / keyword / closing bracket), emit one edit removing a, b, …, so only z remains inside the parens.
is_call_like_prev 🔒
( is part of a call / index / cast context when preceded by one of these tokens. Everything else is treated as a paren-expression where inner top-level commas would be the C-style comma operator.
rewrite_comma_paren 🔒