When trying to run complex code at compilation time, I'm getting a weird error, with no stack trace, that I don't know how to further debug. Nimvm compiler bug?
$ mkdir dali
$ cd dali
$ git clone https://github.com/akavel/dali -b comptime-exp1 .
$ nim c q_comptime.nim
[...]
fatal.nim(49) sysFatal
Error: unhandled exception: 'sons' is not accessible using discriminant 'kind' of type 'TNode' [FieldError]
No stacktrace, no idea where this comes from :( running git grep sons on my code doesn't seem to show up any occurences of sons :/
A q_comptime.dex file should be silently created and written to.
Nim version:
C:\prog\dali>nim -v
Nim Compiler Version 1.2.0 [Windows: amd64]
Compiled at 2020-04-03
Copyright (c) 2006-2020 by Andreas Rumpf
git hash: 7e83adff84be5d0c401a213eccb61e321a3fb1ff
active boot switches: -d:release
Same with choosenim devel:
fatal.nim(49) sysFatal
Error: unhandled exception: 'sons' is not accessible using discriminant 'kind' of type 'TNode' [FieldDefect]
C:\prog\tmp01>nim -v
Nim Compiler Version 1.3.5 [Windows: amd64]
Compiled at 2020-05-13
Copyright (c) 2006-2020 by Andreas Rumpf
git hash: a3719df8b327ce33d4fdbfc7be06b058e9a2ea48
active boot switches: -d:release
I "fixed" the crash but your dex code is better with staticExec on a helper tool you provide. Nim's VM isn't capable of running your dex.render and once it is, it might be too slow.
No stacktrace, no idea where this comes from :(
@akavel here are some debugging tips for next time:
koch temp), it should print a stacktrace when nim crashesand some more advanced flags when --stacktrace isn't enough:
running git grep sons on my code doesn't seem to show up any occurences of sons
it's inside compiler code (as would've been apparent with a nim compiled in debug mode)
anyway, after araq's fix, you should now get a cleaner stacktrace.
Also, always try to minimize your issues, even if a bit more work for your (eg you have Circular dependency detected warnings which may point to some other issue)
@timotheecour thanks; can I do it somehow quickly if I'm just using choosenim? or do I have to git clone nim, then find and follow the instructions to build it, then somehow inject to nimble or something?
@Araq @timotheecour Also, after some thinking, I realized I'm honestly somewhat confused as to how in particular I could best understand this issue being closed now; if that's not too much to ask for, I'd be super grateful if you could try and help me better understand a few things! Please assume no ill will in those questions, they're 100% sincere; I fully understand you guys are busy, and I'd just like to try and be helpful in a way that's best use of your time, and mine as well! Therefore, in particular:
Thanks!
I'm sorry, the reason this was closed was mostly an accident. That were two bugs here, one of which I fixed but that doesn't help you. :-)
However, whether your code can work with the VM is unclear, the VM doesn't support copyMem for example. So yeah, a small reproducible example helps.
@Araq thanks! :)
As a side note, I'm happy to try and use workarounds for features not available in nimvm when possible, for example I specifically:
But when the compiler itself is breaking, it's waaaay over my head, so all I can do for now is try and report an issue, and hope one day it might get fixed... :) Will try to minimize it If I Find Time At Some Point™; for now, I find consolation for myself in that what I reported seems to be at least a deterministic reproducer ;)
Uh oh, so I actually couldn't resist and tried to at least find the line of code which triggers this, and it seems to be a call to sha1's secureHash. If you checkout the new branch comptime-exp2, I seem to have isolated it to a code block like below:
#...
when defined(comptime02):
if true: return
let tmp03 = secureHash(tmp02)
if true: return
#...
With it, I'm getting the following results
C:\prog\dali>nim c -r q_comptime.nim
[...]
fatal.nim(49) sysFatal
Error: unhandled exception: 'sons' is not accessible using discriminant 'kind' of type 'TNode' [FieldDefect]
C:\prog\dali>nim c -r -d:comptime02 q_comptime.nim
[...]
CC: stdlib_io.nim
CC: stdlib_system.nim
CC: q_comptime.nim
Hint: [Link]
Hint: 85514 LOC; 2.713 sec; 117.832MiB peakmem; Debug build; proj: C:\prog\dali\q_comptime.nim; out: C:\prog\dali\q_comptime.exe [SuccessX]
Hint: C:\prog\dali\q_comptime.exe [Exec]
Surprisingly to me, when I tried to extract just the problematic lines to a separate file (included in the new branch), I got a very different error:
C:\prog\dali>nim c q_ct02.nim
[...]
gcc!
C:\Users\Mateusz\.choosenim\toolchains\nim-#devel\lib\pure\endians.nim(59, 24) Error: cannot 'importc' variable at compile time; builtin_bswap64
That's all I could get to for now; maybe this helps you understand more about it already? Trying to reduce the code gradually while still reproducing the error would be more work than the "line bisect" I did for now, so again not sure when and if I'll have time for that. Cheers!
@akavel thanks for not giving up! bugs should be fixed, not worked around (otherwise it'll just impact the next person).
A good reduction removes as much code as possible, including stdlib modules (right now you still have import std/sha1, which hides the problem)
=> I've reduced the problem here: https://github.com/nim-lang/Nim/issues/14368
note (for next time): reducing this was really a mechanical operation. All i did was add some debugging statement echo (data.len, ctx.buf.len, i, j) right above the line that gives a stacktrace error copyMem(addr ctx.buf[i], unsafeAddr data[j], 64 - i); then that led me to try on a similar input in a standalone file, and that was it; after reducing the next step was to modify the test to see the boundary between fails/succeeds.
@timotheecour Um, but #14368 seems to print a different error message than what I reported here, no? so I'm now confused how #14368 is a reduction of this one? Also what copyMem are you referring to in the original code?
In addition to that, with regards to the side comments, if this is about modifying stdlib or compiler, please kindly note that jumping into koch territory is definitely not trivial from a casual nim & choosenim user's persective. I know there are better reports and worse reports, but please also note this is a spectrum, and I believe reporting a poor reproducer is still better than not reporting any, so that's what I did... and also I must go with workarounds first for best effectiveness on both mine and Araq's side. I'm happy to also report simpler issues once the tougher ones are out of the way.
Um, but #14368 seems to print a different error message than what I reported here, no?
it's the same as one you get in original post: following your OP instructions, and updating to devel >= https://github.com/nim-lang/Nim/commit/278b4582f5c7cc3471bccc4ba587030edf7a50e2 (I don't think you're using latest devel since Error: unhandled exception: 'sons' is not accessible using discriminant 'kind' was already fixed in https://github.com/nim-lang/Nim/commit/278b4582f5c7cc3471bccc4ba587030edf7a50e2)
git clone https://github.com/akavel/dali -b comptime-exp1 && cd dali
nim c q_comptime.nim
stack trace: (most recent call last)
/Users/timothee/git_clone/nim/Nim_devel/dali/q_comptime.nim(39, 34) q_comptime
/Users/timothee/git_clone/nim/Nim_devel/dali/src/dali/dex.nim(358, 24) render
/Users/timothee/git_clone/nim/Nim_devel/lib/std/sha1.nim(207, 8) secureHash
/Users/timothee/git_clone/nim/Nim_devel/lib/std/sha1.nim(155, 45) update
/Users/timothee/git_clone/nim/Nim_devel/dali/q_comptime.nim(5, 1) template/generic instantiation from here
/Users/timothee/git_clone/nim/Nim_devel/lib/std/sha1.nim(155, 45) Error: index out of bounds, the container is empty
copyMem(addr ctx.buf[i], unsafeAddr data[j], 64 - i)
^
which is the same error and same root cause as in reduction I gave in https://github.com/nim-lang/Nim/issues/14368
different error message than what I reported here
nim c -r -d:comptime02 q_comptime.nim is a different issue altogether (even if originating from your same initial use case)
Also what copyMem are you referring to in the original code?
the bug happens before copyMem is called as shown in stacktrace: it's happening while evaluating one of its arguments, unsafeAddr data[j]
jumping into koch territory is definitely not trivial
not judging whether it's easy/hard but koch territory just means (in this context) building nim from source with --stacktrace:on to get stacktraces in nim compiler:
it's really just: ./koch temp (once you've built koch); or, if starting from a fresh clone, it's:
git clone https://github.com/nim-lang/Nim
sh build_all.sh
./koch temp
if this is about modifying stdlib or compiler [...]
I was just trying to give helpful tips so that you can make progress debugging on your end if you're the impatient kind (like I am and some other ppl as well) and would rather dig into a problem than wait for someone to fix it. The issue you've reported is real and thanks for reporting them (which will happen in time regardless). Fixing a bug is often a sequence of steps, and anyone (including reporter) is welcome to help along those steps:
reporting reproducible bug => minimizing (ideally with minimal dependencies) => finding root cause => fixing it locally => PR => merge
Error: cannot 'importc' variable at compile time; builtin_bswap64
there is experimental support for CT FFI but it's not default enabled (and currently doesn't support intrinsics,like __builtin_bswap64, but definitely could); until then, there are 3 choices:
when nimvm: fallbackCT() else: usualCodeRT() (simplest fix; the question is where to branch in the code)@timotheecour 鉂わ笍 Thanks for your patience. After writing my last reply, I started thinking about it more, what is the reason I'm whining and complaining so much, and I realized what would probably help me overcome it and ease the testing for me would be, I think, if I had a good simple guide how to build nim devel from source in a choosenim-based environment, in a way where I could be sure it won't break my usual working Nim setup. I will try to test the koch temp commands you provided for this function (when I have time). If they work for me, I will try and open a PR for including them as a guidance/hint in the Nim bug report template.
open a PR for including them as a guidance/hint in the Nim bug report template
I think we already have guides for that so you could link to it instead of creating such guide
https://github.com/nim-lang/Nim/blob/devel/.github/ISSUE_TEMPLATE/bug_report.md could IMO emphasize more on need for generating minimized reproducible examples tested against latest devel, which puts a bit more burden on the person who's writing the bug report but less burden on whoever's gonna fix it.
Another thing that could help is if compiler messages start linking to relevant doc section, so that instead of
No stack traceback available
To create a stacktrace, rerun compilation with ./koch temp r <file>
you'll get:
No stack traceback available
To create a stacktrace, rerun compilation with ./koch temp r <file>, see https://nim-lang.github.io/Nim/intern.html#bootstrapping-the-compiler
likewise with many more error messages. Good docs, like good code, should be DRY (only explained in a single place, and linked to where needed)
Hello, I am writing a program and got the same error message. I can't give a condensed example, as I don't know what in about 33k lines of code causes it.
Versions of Nim used: 1.0.6, 1.4.0, devel @61352d5a203e76e3334b21766bdbd4ebc71c300a
Version 1.0.6 does not seem to be affected by the problem.
1.4.0 and devel create the same error.
Using the koch temp method, I got a stacktrace, which points to the file ast.nim of the compiler, at line 1113.
This is part of proc len*(n: Indexable): int {.inline.} =
The stacktrace can be found here: stacktrace.txt
I don't know what in about 33k lines of code causes it
compile nim with -d:debug --stacktrace:on --excessiveStackTrace --stackTraceMsgs -d:nimCompilerStackraceHints
(introduced in #13351, see PR for what this does)
and show the stacktrace again
Ok, here is the new stacktrace2.txt