wasm: resolve fixed-array .len instead of aborting with "could not find field len" - #27518
Merged
Merged
Conversation
…find field len"
A fixed array's `.len` (e.g. `[8]int{}.len`) is typed as `int` by the checker
but carries no struct field, so the wasm backend's `SelectorExpr` handler fell
through to `get_field_offset` -> `find_field('len')`, aborting with the internal
`could not find field 'len' on init`:
fn main() {
a := [8]int{}
println(a.len) // wasm error: could not find field `len` on init
}
A fixed array's length is a compile-time constant (`ast.ArrayFixed.size`), so
resolve `.len` directly in the SelectorExpr arm and emit it as an i32 const.
Extends vlib/v/gen/wasm/tests/arrays.vv (+ .out) with a `.len` case.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Reading
.lenon a fixed-size array aborts the wasm backend with an internal error:Root cause
The checker types
array_fixed.lenasint(checker.v) but emits no actual field for it. In the wasm backend,expr()'sast.SelectorExprarm triesget_var_from_expr(fails for.len), then falls through tofield_offset→get_field_offset→ts.find_field('len'), which fails and callsw_error('could not find field len on init').Fix
A fixed array's length is a compile-time constant —
ast.ArrayFixed.size. Resolve.lendirectly in the SelectorExpr arm and push it as ani32const, before the field-offset path.ast.SelectorExpr { final := g.table.final_sym(node.expr_type) if node.field_name == 'len' && final.info is ast.ArrayFixed { g.func.i32_const(i32(final.info.size)) } else if v := g.get_var_from_expr(node) { ...After
Tests
Extends
vlib/v/gen/wasm/tests/arrays.vv(+.out) with a fixed-array.lencase (compiled and run viav -b wasm run, output compared). Fullvlib/v/gen/wasm/tests/suite passes;v fmtclean.Checklist
v test vlib/v/gen/wasm/tests/passesv fmt -wapplied