Skip to content

internal error: expr: var not init - in custom finalizer #19231

@izackp

Description

@izackp

Using a variable inside of an iterator does not work.

Example

type
  Game* = ref object

proc free*(game: Game) =
  var mixNumOpened:cint = 0
  for i in 0..<mixNumOpened:
    mixNumOpened -= 1

proc newGame*(): Game =
  new result, free

var
  game*: Game

Current Output

It results in this error.

/opt/homebrew/Cellar/nim/1.6.0/nim/lib/system/iterators_1.nim(129, 17) Error: internal error: expr: var not init mixNumOpened_452984858

Expected Output

It's expected to compile successfully or with an error that's not internal

Possible Solution

Changing the object around to use =destroy seems to fix the issue

type
  GameObj = object
    fTest: cint
  Game* = ref GameObj

proc `=destroy`(obj: var GameObj) =
    echo "destroy"
    var mixNumOpened:cint = 0
    for i in 0..<mixNumOpened:
        mixNumOpened -= 1

var
  game*: Game

echo "hello"
game = new Game
game.fTest = 3
echo game.fTest

Additional Information

This was an attempt at compiling nimgame2 using 1.6.0

Tested mostly on Nim Compiler Version 1.6.0 [MacOSX: arm64], but issue also appears on the latest devel branch at 99f8793

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions