Skip to content

Rust target: emit 1u64 (not 1usize) in bitset comparisons - #48

Closed
knoguchi wants to merge 2 commits into
antlr4rust:rust-targetfrom
knoguchi:fix/rust-bitmask-u64
Closed

knoguchi wants to merge 2 commits into
antlr4rust:rust-targetfrom
knoguchi:fix/rust-bitmask-u64

Conversation

@knoguchi

@knoguchi knoguchi commented Aug 9, 2026

Copy link
Copy Markdown

Fixes #47.

bitsetBitfieldComparison guards the shift with testShiftInRange's 0..63 check (64-bit word semantics) but shifts 1usize. On 32-bit targets (wasm32), usize is 32 bits and the shift amount wraps mod 32, so e.g. 1usize << 35 evaluates to 1 << 3 — false set-membership, and ATN prediction takes the wrong alternative for any token whose offset lands in 32..63 of a decision's mask. Same input parses correctly on 64-bit targets, which is what makes this easy to miss.

Widening the literal to u64 matches the guard's assumption and is semantically identical on 64-bit (the mask literal infers u64 from the lhs). Verified downstream on a large community grammar: applying exactly this widening to the generated output fixed all wasm32 misparses with no behavior change on 64-bit.

bitsetBitfieldComparison guards the shift with testShiftInRange's
0..63 check (64-bit word semantics) but shifts 1usize. On 32-bit
targets (wasm32), usize is 32 bits and the shift amount wraps mod 32,
so e.g. 1usize << 35 evaluates to 1 << 3 -- false set-membership, and
ATN prediction takes the wrong alternative for any token whose offset
lands in 32..63. Widening the literal to u64 matches the guard's
assumption and is semantically identical on 64-bit targets.

Fixes #47
Scans tests/gen for '1usize <<' -- enforceable on 64-bit CI (where the
behavioral bug is invisible) because build.rs regenerates tests/gen
from the grammars whenever the tool jar is present, so a template
regression trips this on the next generating run. Also widens the one
occurrence in the checked-in csvparser.rs, which was produced by the
unfixed template. Full behavioral repro requires a 32-bit target
(e.g. cargo test --target wasm32-wasip1); documented in the test.
@knoguchi knoguchi closed this by deleting the head repository Aug 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rust target emits 1usize << for token bitmasks — generated parsers misparse on 32-bit targets (wasm32)

1 participant