Error: unhandled exception: value out of range [RangeDefect]
Hi, I get this error message using both Nim v1.6 and v2 (I also got more smaller errors but I fixed them fairly easy, mostly syntax errors or arithmetics on variables with different types):

```
> nim c -d:debug -r main.nim
Hint: used config file '/home/x/.choosenim/toolchains/nim-1.6.20/config/nim.cfg' [Conf]
Hint: used config file '/home/x/.choosenim/toolchains/nim-1.6.20/config/config.nims' [Conf]
Hint: gc: refc; opt: none (DEBUG BUILD, `-d:release` generates faster code)
9935 lines; 0.015s; 10.68MiB peakmem; proj: /home/x/hobby/nim/kaitai/main.nim; out: /home/x/hobby/nim/kaitai/main [SuccessX]
Hint: /home/x/hobby/nim/kaitai/main  [Exec]
/home/x/hobby/nim/kaitai/main.nim(541) main
/home/x/hobby/nim/kaitai/main.nim(538) main
/home/x/hobby/nim/kaitai/main.nim(172) fromFile
/home/x/hobby/nim/kaitai/main.nim(152) read
/home/x/hobby/nim/kaitai/main.nim(453) read
/home/x/hobby/nim/kaitai/main.nim(267) read
/home/x/.choosenim/toolchains/nim-1.6.20/lib/system/fatal.nim(54) sysFatal
Error: unhandled exception: value out of range [RangeDefect]
```

Caused by this generated Nim code:

```nim
proc read*(_: typedesc[Nvs_EntryStateBitmapT], io: KaitaiStream, root: KaitaiStruct, parent: Nvs_PageT): Nvs_EntryStateBitmapT =
  template this: untyped = result
  this = new(Nvs_EntryStateBitmapT)
  let root = if root == nil: cast[Nvs](this) else: cast[Nvs](root)
  this.io = io
  this.root = root
  this.parent = parent

  for i in 0 ..< int(((4 * 32) - 2)):
    let it = Nvs_EntryState(this.io.readBitsIntLe(2)) # <---- crashes here
    this.entry.add(it)
  let unnamed1Expr = this.io.readBitsIntLe(4)
  this.unnamed1 = unnamed1Expr
```

The source `.ksy` file works with `ksv` visualizer. I compiled the Nim code with https://ide.kaitai.io/# website.

Files to replicate this in zip below:
[kaitai_nim_doesnt_work.zip](https://github.com/user-attachments/files/16534974/kaitai_nim_doesnt_work.zip)
