I have this code
proc Tokenize(Command: string): seq[Token] =
var
cmd = toLower(strip(Command))
c = 0
ch = cmd[c]
word = ""
tokenStart: int
result = seq[Token]
and later
result.add(Token(kind: tkNum, value: word.parseInt()))
With that code I get the following error:
dice.nim(101, 14) Error: type mismatch: got (typedesc[seq[Token]]) but expected 'seq[Token]'
if I remove the assignment to result I get this at add:
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
Anyone of you smart people who know what I am doing wrong.
result = seq[Token]
should be:
result = @[]
Thank you def :)
That solved it!
But what does the Illegal storage access error mean? I tend to run into it in wierd places and I am always a bit confused?
Does it mean that I am accessing a ref to something undefined or?
But what does the Illegal storage access error mean?
That should be simple: When you have a proc which returns a sequence, and you do not assign a value to result, I really would assume that the result is nil in Nim. And when you later try to use that nil value you can have illegal storage access. (Other languages may prefer to return an empty sequence when there is no assignment, but I really think Araq decided for nil with good reasons. You may test return value yourself to proof my guess or check the manual.)
You are correct Stefan.
It came from me using unassigned result vars. Would be great for adoption if the error messages could say what variable caused it and perhaps suggest checking if it is unassigned. Just to be nice to new coders wanting in on the Nim action.
@jgoodgive That's quite hard, unfortunately. Nim depends on the operating system to tell it when there's a bad pointer, and that doesn't give it much information. If you don't mind some memory overhead, you can pass --debuginfo --linedir:on --passC:-fsanitize=address --passL:-fsanitize=address to Nim in order to tell the C compiler to insert checks for this sort of stuff.
Even that isn't much more useful in many cases:
ASAN:SIGSEGV
=================================================================
==23833==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x0000004c4768 bp 0x7ffff05c2d50 sp 0x7ffff05c2c90 T0)
#0 0x4c4767 in testInit /home/user/tmp/test.nim:2:19
#1 0x4c4a06 in NimMain /home/user/dev/nim/nimlang/lib/system.nim:2251:2
#2 0x4c4a06 in main /home/user/dev/nim/nimlang/lib/system.nim:2258
#3 0x7fee0f3d003f in __libc_start_main (/usr/lib/libc.so.6+0x2003f)
#4 0x4b8d7c in _start (/home/user/tmp/test+0x4b8d7c)
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /home/user/tmp/test.nim:2 testInit
==23833==ABORTING