Skip to content

checker report error while use generic array in struct method #24104

@kbkpbot

Description

@kbkpbot

Describe the bug

This should compile

Reproduction Steps

gg.v

module main

struct Test {
}

fn (mut t Test) decode_array[X](x []X)[]X {
	println('second: x.typename = ${typeof(x).name}')
	return x
}

fn (mut t Test) decode[T]() T {
	return t.decode_array(T{})
}

fn main() {
	mut x := Test {}
	x.decode[[]u8]()
	x.decode[[]int]()
}

Expected Behavior

it should compile ok

Current Behavior

gg.v:12:11: error: cannot use `[]u8` as type `[]int` in return argument
   10 |
   11 | fn (mut t Test) decode[T]() T {
   12 |     return t.decode_array(T{})
      |              ~~~~~~~~~~~~~~~~~
   13 | }
   14 |

Possible Solution

By removing the struct from code, it can compile:

gg.v

module main

fn decode_array[X](x []X)[]X {
	println('second: x.typename = ${typeof(x).name}')
	return x
}

fn decode[T]() T {
	return decode_array(T{})
}

fn main() {
	decode[[]u8]()
	decode[[]int]()
}

Additional Information/Context

No response

V version

V 0.4.10 86536e4.4a2b722

Environment details (OS name and version, etc.)

V full version V 0.4.10 86536e4.4a2b722
OS linux, Ubuntu 24.04.2 LTS
Processor 8 cpus, 64bit, little endian, Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz
Memory 6.2GB/15.51GB
V executable /media/HD/github/kbkpbot/v/v
V last modified time 2025-04-01 06:43:24
V home dir OK, value: /media/HD/github/kbkpbot/v
VMODULES OK, value: /home/mars/.vmodules
VTMP OK, value: /tmp/v_1000
Current working dir OK, value: /home/mars/v/bug
Git version git version 2.43.0
V git status weekly.2025.13-45-g83f3960e (4 commit(s) behind V master)
.git/config present true
cc version cc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
gcc version gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
clang version Ubuntu clang version 18.1.3 (1ubuntu1)
tcc version tcc version 0.9.28rc 2025-02-13 HEAD@f8bd136d (x86_64 Linux)
tcc git status thirdparty-linux-amd64 696c1d84
emcc version N/A
glibc version ldd (Ubuntu GLIBC 2.39-0ubuntu8.4) 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

Labels

BugThis tag is applied to issues which reports bugs.Generics[T]Bugs/feature requests, that are related to the V generics.Unit: CheckerBugs/feature requests, that are related to the type checker.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions