v-analyzer icon indicating copy to clipboard operation
v-analyzer copied to clipboard

Looping through generic enum at comptime with $for causes v-analyzer to crash

Open jeffbdavenport opened this issue 7 months ago • 6 comments

Describe the bug

Using $for comptime keyword anywhere in any file causes a crash

Expected Behavior

It doesn't crash

Current Behavior

It crashed.

Reproduction Steps

pub struct Game[T] {
  levels map[T]int
}

pub fn Game.new[T]() &Game[T] {
    mut levels := map[T]int{}
    $for data in T.values {
        levels[data.value] = 0
    }
    return &Game[T]{ levels: levels }
}

enum Levels {
    one
}

fn main() {
  game := Game.new[Levels]()
  println('${game}')
}

Possible Solution

No response

Additional Information/Context

No response

Environment details (v doctor output)

jeff@DESKTOP-N30T80W ~$ v doctor

V full version V 0.4.10 87d3a1d27229b64896cf3e22bab4822e1b75fb9a.87d3a1d
OS linux, Ubuntu 24.04.2 LTS
Processor 16 cpus, 64bit, little endian, Intel(R) Core(TM) i7-10870H CPU @ 2.20GHz
Memory 18.91GB/31.22GB
V executable /opt/v/v
V last modified time 2025-04-23 05:26:36
V home dir OK, value: /opt/v
VMODULES OK, value: /home/jeff/.vmodules
VTMP OK, value: /tmp/v_1000
Current working dir OK, value: /home/jeff
Git version git version 2.43.0
V git status weekly.2025.16-34-g87d3a1d2
.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 Homebrew clang version 15.0.7
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

Editor name

VS Code

v-analyzer Version

v-analyzer version 0.0.3-beta.1.a796f25

VS Code Extension Version

vosca.vscode-v-analyzer-0.0.3

jeffbdavenport avatar Apr 23 '25 19:04 jeffbdavenport

The example code doesn't compile for me - too many errors...

x.v:15:1: warning: unused variable: `game`
   13 | }
   14 | 
   15 | game := Game(Levels).new()
      | ~~~~
x.v:15:14: error: undefined ident: `Levels`
   13 | }
   14 | 
   15 | game := Game(Levels).new()
      |              ~~~~~~
x.v:15:14: error: expression does not return a value so it cannot be cast
   13 | }
   14 | 
   15 | game := Game(Levels).new()
      |              ~~~~~~
x.v:15:9: error: cannot cast `void` to struct
   13 | }
   14 | 
   15 | game := Game(Levels).new()
      |         ~~~~~~~~~~~~
x.v:15:22: error: unknown method or field: `Game.new`
   13 | }
   14 | 
   15 | game := Game(Levels).new()
      |                      ~~~~~
x.v:15:6: error: assignment mismatch: 1 variable but `new()` returns 0 values
   13 | }
   14 | 
   15 | game := Game(Levels).new()
      |      ~~

JalonSolov avatar Apr 23 '25 20:04 JalonSolov

@JalonSolov Thanks for your patience. I was just providing pseudo code, I didn't think anyone would try to run it. I have updated the code so it can be run now.

jeffbdavenport avatar Apr 24 '25 04:04 jeffbdavenport

If we can't run your example, how are we supposed to duplicate the problem, so we can try to track it down and fix it?

At any rate, with the corrected example code, it works fine on my system (or at least doesn't crash):

$ v run x.v
&Game[Levels]{
    levels: {one: 0}
}
$

JalonSolov avatar Apr 24 '25 11:04 JalonSolov

@JalonSolov Maybe you misunderstood the issue? The issue is not that the code doesn't run. The issue is that v-analyzer crashes when trying to index the file. At that point, no files can be analyzed anymore. The program runs just fine with V. This is why I didn't think it mattered to run the file as that is not the issue.

jeffbdavenport avatar Apr 24 '25 16:04 jeffbdavenport

My apologies. This showed up amongst other issues related to V itself, and the title didn't say v-analyzer, so I was checking V operation.

That aside, it is still always better to have real code rather than pseudo code which could cause different reactions in v-analyzer than real code.

Something that would still need to be fixed, but that's an important detail to know when looking for the problem.

JalonSolov avatar Apr 24 '25 17:04 JalonSolov

Agreed

jeffbdavenport avatar Apr 24 '25 21:04 jeffbdavenport