This is a boiled down test case of something I noticed the other day. Consider the following code with Person and Employee derived from Person.
type
EmployeeCode = enum
ecCode1,
ecCode2
Person* = object of RootObj
name* : string
last_name*: string
Employee* = object of Person
empl_code* : EmployeeCode
mgr_name* : string
proc test() =
var
empl1 = Employee(name: "fred", last_name: "smith", mgr_name: "sally", empl_code: ecCode1)
empl2 = Employee(name: "sally", last_name: "jones", mgr_name: "bob", empl_code: ecCode2)
echo "Hello ", empl1.name, " , managed by ", empl1.mgr_name
echo "Hello ", empl2.name, " , managed by ", empl2.mgr_name
const do_static = false
when do_static:
static:
test()
else:
test()
Running that produces the output you'd expect
Hello fred , managed by sally Hello sally , managed by bob
If you change do_static to true so that the code runs at compile time, then it crashes
SIGSEGV: Illegal storage access. (Attempt to read from nil?) Exited with status 1
What's interesting is that if you move the name and last_name attributes from Person into Employee (but still having Employee = object of Person), then it works fine. That's actually the workaround that we used in the more complex use case that this example is derived from.
It seems to be related to the EmployeeCode enum though. If you change the example to not supply the ecode1 and ecode2 parameters when constructing the objects, then the example runs fine.
I don't know how many folks are doing a lot of compile time logic, but I suppose that this warrants opening an issue on Github. I did want to put this out here first to see if anyone had any thoughts as to what might be going on here. The workaround that I mentioned makes this a low priority for us, but I am kind of curious as to the underlying issue. Or if anyone has any good pointers as to where in the compiler would be a good place to look, I might poke around some more this weekend on it.
Thanks
I hunted this down a bit more. The crash is coming from a nil value being passed to asgnComplex in compiler/vm.nim.
I've opened https://github.com/nim-lang/Nim/issues/3973 to properly track this.