Skip to content

checker: cannot pass a fixed array [N]T to a generic function expecting []T #26899

@enghitalo

Description

@enghitalo

Describe the bug

A [N]T (fixed-size array) cannot be passed to a generic function that takes []T — V refuses to infer the inner element type, even though the relationship is mechanical ([N]T and []T share the element type).

This forces every generic decoder/encoder that wants a single code path for both array kinds to either copy via a sibling dynamic array (slow) or manually rewrite the dynamic-array header to alias the fixed-array storage (unsafe).

Reproduction Steps

fn fill[T](mut arr []T) {
	arr << T{}
}

fn fill_any[T](mut val T) {
	$if T is $array_fixed {
		fill(mut val)
	}
}

fn main() {
	mut fixed := [3]int{}
	fill_any(mut fixed)
	println(fixed)
}

Expected Behavior

Compiles. The checker infers T = int from [3]int matching []T.

Current Behavior

error: could not infer generic type `T` in call to `fill`
  10 |         fill(mut val)
     |         ~~~~~~~~~~~~~

Possible Solution

When inferring T for an mut []T parameter and the call-site argument has type [N]E, accept the inference T = E. The runtime can either coerce the fixed array into a []T view (length and cap = N, data = &fixed[0]) or generate a small adapter that copies in/out at the call boundary.

V version

V 0.5.1 (40b3711)

Environment details (OS name and version, etc.)

|V full version      |V 0.5.1 1b3385cc34ff783e793d1a26a8ec5be587c80fe0.40b3711
|:-------------------|:-------------------
|OS                  |linux, Ubuntu 24.04 LTS
|Processor           |16 cpus, 64bit, little endian, AMD Ryzen 7 5800H with Radeon Graphics
|Memory              |10.02GB/30.7GB
|                    |
|V executable        |/home/hitalo/Documents/v/v
|V last modified time|2026-04-18 09:18:00
|                    |
|V home dir          |OK, value: /home/hitalo/Documents/v
|VMODULES            |OK, value: /home/hitalo/.vmodules
|VTMP                |OK, value: /tmp/v_1000
|Current working dir |OK, value: /home/hitalo/Documents/v
|                    |
|Git version         |git version 2.43.0
|V git status        |0.5.1-1006-g40b3711b-dirty
|.git/config present |true
|                    |
|cc version          |cc (GCC) 14.2.0
|gcc version         |gcc (GCC) 14.2.0
|clang version       |Ubuntu clang version 18.1.3 (1)
|tcc version         |tcc version 0.9.28rc 2025-02-13 HEAD@f8bd136d (x86_64 Linux)
|tcc git status      |thirdparty-linux-amd64 696c1d84
|emcc version        |emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.6 ()
|glibc version       |ldd (Ubuntu GLIBC 2.39-0ubuntu8.3) 2.39

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugThis tag is applied to issues which reports bugs.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions