Can anyone familiar with the compiler offer some guidance?
After a function call as an optimization the compiler immediately tries to evaluate compile time nodes or ones that have no side effects in the VM (https://github.com/nim-lang/Nim/blob/devel/compiler/semexprs.nim#L873). When the call is a function argument it happens regardless of whether it actually typechecks, so given:
proc f():string {.compileTime.} =
launchMissiles()
"hello world"
proc g(i:int) = echo i
g(f())
launchMissiles() is invoked even though f() does not typecheck because the typechecker does overload resolution before matching up the operand with the argument.
If the optimization is disabled f() never gets evaluated by the VM and you get an error like:
Error: request to generate code for .compileTime proc: f
The proper behavior I think is to do the VM evaluation at code gen. time but I can't immediately see where to place that call.
Any help is appreciated.
Try with:
proc f():string {.compileTime.} =
echo "launch missiles!"
"hello world"
proc g(i:int) = echo i
g(f())
'launch missiles!' is written to the console even though the program doesn't type check.