Search code examples
lambdaclosuresnim-lang

Nim: lambdas with void return type cause compiler crash


See this minimal example:

import sugar

type TestLambda = () -> void
type NamedTestLambda = tuple[name: string, lambda: TestLambda]

var testNil: TestLambda = () => void


var funcs: seq[NamedTestLambda] = @[
    (name: "nil", lambda: testNil),
]

This causes the following error output:

Error: internal error: getTypeDescAux(tyVoid)
No stack traceback available
To create a stacktrace, rerun compilation with './koch temp c <file>', see https://nim-lang.github.io/Nim/intern.html#debugging-the-compiler for details
     Error: Build failed for package: nim_snake
        ... Execution failed with exit code 256

While the code seems to compile without problem when we change the return type to int:

import sugar

type TestLambda = () -> int
type NamedTestLambda = tuple[name: string, lambda: TestLambda]

var testNil: TestLambda = () => 1


var funcs: seq[NamedTestLambda] = @[
    (name: "nil", lambda: testNil),
]

Should I report this to the nim language forum, or is there something I did wrong here?

Nim 1.6.16, compiling to C.


Solution

  • I think the issue lies in the testNil variable which returns void instead of just discarding with discard or not returning a value. As per this discussion on nim forum void is only useful to say that something has no type and if used as a return value in a proc/func, just means the function doesn't return a value. Using it in var testnil: TestLambda = () => void is wrong and should have been var testnil: TestLambda = () => (discard) or even var testnil: TestLambda = () => echo "void".