Skip to content

v compiler crash when compile generic array code #23910

Description

@kbkpbot

Describe the bug

v compiler crash when compile generic array code

Reproduction Steps

crash.v

module main

fn decode[T](b []u8) !T {
	$if T is $array {
		val := T{0}
		return decode_array(val)
	}
	return error('error')
}

pub fn decode_array[T](_ []T) []T {
	mut arr := []T{cap : 10}
	v := T(0)
	for _ in 0..10 {
			arr << v
	}
	return arr
}

fn main() {
	x := decode[[]u8]([u8(0),0])!
	dump(x)
}

Expected Behavior

compile ok or report error

Current Behavior

compiler crash:

$ v crash.v
V panic: array.get: index out of range (i,a.len):0, 0
v hash: 50b716b
/tmp/v_1000/v2.01JNZ04TMETS8NWZB8CCRK9D1B.tmp.c:19371: at _v_panic: Backtrace
/tmp/v_1000/v2.01JNZ04TMETS8NWZB8CCRK9D1B.tmp.c:19419: by panic_n2
/tmp/v_1000/v2.01JNZ04TMETS8NWZB8CCRK9D1B.tmp.c:18925: by array_get
/tmp/v_1000/v2.01JNZ04TMETS8NWZB8CCRK9D1B.tmp.c:17399: by v__gen__c__Gen_array_init
/tmp/v_1000/v2.01JNZ04TMETS8NWZB8CCRK9D1B.tmp.c:27770: by v__gen__c__Gen_expr
/tmp/v_1000/v2.01JNZ04TMETS8NWZB8CCRK9D1B.tmp.c:28154: by v__gen__c__Gen_expr
/tmp/v_1000/v2.01JNZ04TMETS8NWZB8CCRK9D1B.tmp.c:20987: by v__gen__c__Gen_assign_stmt
/tmp/v_1000/v2.01JNZ04TMETS8NWZB8CCRK9D1B.tmp.c:26582: by v__gen__c__Gen_stmt
/tmp/v_1000/v2.01JNZ04TMETS8NWZB8CCRK9D1B.tmp.c:26380: by v__gen__c__Gen_stmts_with_tmp_var
/tmp/v_1000/v2.01JNZ04TMETS8NWZB8CCRK9D1B.tmp.c:26228: by v__gen__c__Gen_stmts
/tmp/v_1000/v2.01JNZ04TMETS8NWZB8CCRK9D1B.tmp.c:34110: by v__gen__c__Gen_comptime_if
/tmp/v_1000/v2.01JNZ04TMETS8NWZB8CCRK9D1B.tmp.c:27894: by v__gen__c__Gen_expr
/tmp/v_1000/v2.01JNZ04TMETS8NWZB8CCRK9D1B.tmp.c:26609: by v__gen__c__Gen_stmt
/tmp/v_1000/v2.01JNZ04TMETS8NWZB8CCRK9D1B.tmp.c:26380: by v__gen__c__Gen_stmts_with_tmp_var
/tmp/v_1000/v2.01JNZ04TMETS8NWZB8CCRK9D1B.tmp.c:26228: by v__gen__c__Gen_stmts
/tmp/v_1000/v2.01JNZ04TMETS8NWZB8CCRK9D1B.tmp.c:36876: by v__gen__c__Gen_gen_fn_decl
/tmp/v_1000/v2.01JNZ04TMETS8NWZB8CCRK9D1B.tmp.c:36500: by v__gen__c__Gen_gen_fn_decl
/tmp/v_1000/v2.01JNZ04TMETS8NWZB8CCRK9D1B.tmp.c:36407: by v__gen__c__Gen_fn_decl
/tmp/v_1000/v2.01JNZ04TMETS8NWZB8CCRK9D1B.tmp.c:26568: by v__gen__c__Gen_stmt
/tmp/v_1000/v2.01JNZ04TMETS8NWZB8CCRK9D1B.tmp.c:26380: by v__gen__c__Gen_stmts_with_tmp_var
/tmp/v_1000/v2.01JNZ04TMETS8NWZB8CCRK9D1B.tmp.c:26228: by v__gen__c__Gen_stmts
/tmp/v_1000/v2.01JNZ04TMETS8NWZB8CCRK9D1B.tmp.c:25079: by v__gen__c__Gen_gen_file
/tmp/v_1000/v2.01JNZ04TMETS8NWZB8CCRK9D1B.tmp.c:25016: by v__gen__c__cgen_process_one_file_cb
/tmp/v_1000/v2.01JNZ04TMETS8NWZB8CCRK9D1B.tmp.c:33136: by sync__pool__process_in_thread
/tmp/v_1000/v2.01JNZ04TMETS8NWZB8CCRK9D1B.tmp.c:15797: by sync__pool__process_in_thread_thread_wrapper

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.9 ab2eb00.50b716b

Environment details (OS name and version, etc.)

V full version V 0.4.9 ab2eb00.50b716b
OS linux, Ubuntu 24.04.2 LTS
Processor 8 cpus, 64bit, little endian, Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz
Memory 4.82GB/15.51GB
V executable /media/HD/github/kbkpbot/v/v
V last modified time 2025-03-10 03:04:03
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/serial
Git version git version 2.43.0
V git status weekly.2025.10-55-g50b716b8-dirty (7 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 2024-07-31 HEAD@1cee0908 (x86_64 Linux)
tcc git status thirdparty-linux-amd64 0134e9b9-dirty
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.Compiler PanicThe V compiler itself paniced. It should not, it should produce nice and readable errors instead.Generics[T]Bugs/feature requests, that are related to the V generics.Unit: CheckerBugs/feature requests, that are related to the type checker.Unit: cgenBugs/feature requests, that are related to the default C generating backend.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions