Skip to content

wasm: resolve fixed-array .len instead of aborting with "could not find field len" - #27518

Merged
medvednikov merged 1 commit into
vlang:masterfrom
enghitalo:fix-wasm-fixed-array-len
Jun 21, 2026
Merged

wasm: resolve fixed-array .len instead of aborting with "could not find field len"#27518
medvednikov merged 1 commit into
vlang:masterfrom
enghitalo:fix-wasm-fixed-array-len

Conversation

@enghitalo

Copy link
Copy Markdown
Contributor

Description

Reading .len on a fixed-size array aborts the wasm backend with an internal error:

fn main() {
	a := [8]int{}
	println(a.len)
}
wasm error: could not find field `len` on init

Root cause

The checker types array_fixed.len as int (checker.v) but emits no actual field for it. In the wasm backend, expr()'s ast.SelectorExpr arm tries get_var_from_expr (fails for .len), then falls through to field_offsetget_field_offsetts.find_field('len'), which fails and calls w_error('could not find field len on init').

Fix

A fixed array's length is a compile-time constant — ast.ArrayFixed.size. Resolve .len directly in the SelectorExpr arm and push it as an i32 const, 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

$ v -b wasm run a.v
8

Tests

Extends vlib/v/gen/wasm/tests/arrays.vv (+ .out) with a fixed-array .len case (compiled and run via v -b wasm run, output compared). Full vlib/v/gen/wasm/tests/ suite passes; v fmt clean.

Checklist

  • v test vlib/v/gen/wasm/tests/ passes
  • v fmt -w applied

…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.
@medvednikov
medvednikov merged commit 287d33e into vlang:master Jun 21, 2026
85 checks passed
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.

2 participants