Nim
Nim copied to clipboard
Illegal storage access on distinct type definition with a macro generated enum
Hi all, i'm new to Nim. Really like the language ! I'm experimenting a bit with it to understand what is / is not possible with it.
I was trying this snippet, and i noticed that if i added distinct before the toEnum macro invocation, the compiler segfaults.
Example
import macros
import strutils
macro toEnum(s : static[string]) : untyped =
result = newTree(nnkEnumTy, newEmptyNode())
for x in splitWhitespace(s):
result.add newIdentNode(x)
type
Color = distinct toEnum "Red Green Blue"
var x = Red
echo $x, " ", type(x)
Current Output
...........SIGSEGV: Illegal storage access. (Attempt to read from nil?)
Expected Output
Not sure what i should expect, probably not a compiler crash. Removing distinct will work as expected.
Additional Information
Nim Compiler Version 1.4.6 [MacOSX: amd64]
Compiled at 2021-04-29
distinct enum in general breaks it, the parser just normally doesn't allow it, the reason is the t.sym of the enum type becomes nil. An option is just to disallow these.