nim-works / nimskull

An in development statically typed systems programming language; with sustainability at its core. We, the community of users, maintain it.
https://nim-works.github.io/nimskull/index.html
Other
273 stars 38 forks source link

`nim e` crashes when user-defined hooks are used #1401

Open alaviss opened 1 month ago

alaviss commented 1 month ago

Example

type
  C = object
    heaped: ref array[1, uint8]

proc `=copy`(dst: var C, src: C) =
  if dst.heaped != src.heaped:
    new(dst.heaped)
    dst.heaped[] = src.heaped[]

proc newC(): C =
  C(heaped: new(array[1, uint8]))

var c = newC()

Actual Output

$ ./koch.py temp e test.nim

bin/nim_temp  e test.nim
compiler/nim.nim(154) nim
compiler/nim.nim(89) handleCmdLine
compiler/front/main.nim(695) mainCommand
compiler/front/scripting.nim(231) runNimScript
compiler/sem/passes.nim(275) processModule
compiler/sem/passes.nim(102) processTopLevelStmt
compiler/vm/compilerbridge.nim(724) myProcess
compiler/vm/compilerbridge.nim(413) evalStmt
compiler/vm/compilerbridge.nim(353) execute
compiler/vm/compilerbridge.nim(329) execute
compiler/vm/vmjit.nim(314) compile
compiler/vm/vmjit.nim(269) genProc
compiler/backend/backends.nim(403) generateIR
compiler/backend/cgirgen.nim(812) generateIR
compiler/backend/cgirgen.nim(796) tb
compiler/backend/cgirgen.nim(776) scopeToIr
compiler/backend/cgirgen.nim(643) stmtToIr
compiler/utils/idioms.nim(36) unreachableImpl
lib/system/assertions.nim(28) raiseAssert
lib/system/fatal.nim(50) sysFatal
Error: unhandled exception: cgirgen.nim(643, 16) unreachable: a 'destroy' that wasn't lowered [AssertionDefect]

Additional Information

zerbina commented 1 month ago

I've tried building and running the provided example with the VM backend, and it works as it should.

Going by the actual output you showed, you using NimScript rather than the VM backend, which are two different things. In NimScript, hooks don't work at all, unrelated to ref fields. Here's a reduced reproducer:

type
  C = object

proc `=destroy`(dst: var C) =
  discard

block:
  var c = newC()

running the above results in the same crash.

I've changed the title of the issue, in order to make it clearer what's going on.