Skip to content

struct comptime and generic array bug #23969

@kbkpbot

Description

@kbkpbot

Describe the bug

struct comptime and generic array can create bug.

Reproduction Steps

g.v

module main
struct MyStruct {
        f_array_string []string
        f_array_int    []int
        f_array_u8     []u8
}

fn arrarr[T](x []T) {
        $if T is $string {
                println('arrarr string => ${x}')
        } $else $if T is u8 {
                println('arrarr u8  => ${x}')
        } $else $if T is $int {
                println('arrarr int  => ${x}')
        } $else {
                println('arrarr unknown  => ${x}')
        }
}

fn main() {
        x := MyStruct{
                f_array_int:    [10244]
                f_array_u8:     [u8(13)]
                f_array_string: ['hello', 'world']
        }

       // use comptime will create wrong output
        $for f in MyStruct.fields {
                arrarr(x.$(f.name))
        }

       // correct output
        arrarr(x.f_array_string)
        arrarr(x.f_array_int)
        arrarr(x.f_array_u8)
}
v g.v

Expected Behavior

arrarr string => ['hello', 'world']
arrarr int  => [10244]
arrarr u8  => [13]
arrarr string => ['hello', 'world']
arrarr int  => [10244]
arrarr u8  => [13]

Current Behavior

arrarr u8  => [213, 219]
arrarr u8  => [4]
arrarr u8  => [13]
arrarr string => ['hello', 'world']
arrarr int  => [10244]
arrarr u8  => [13]

Possible Solution

No response

Additional Information/Context

Not sure it is a comptime bug or a generic bug.

V version

V 0.4.9 bd2ec67.bd2ec67

Environment details (OS name and version, etc.)

V full version V 0.4.9 bd2ec67.bd2ec67
OS linux, Ubuntu 24.04.2 LTS
Processor 8 cpus, 64bit, little endian, Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz
Memory 5.68GB/15.51GB
V executable /media/HD/github/kbkpbot/v/v
V last modified time 2025-03-18 02:26:51
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.11-53-gbd2ec679
.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

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