nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.5k stars 1.46k forks source link

[Regression] Closure + Generics lead to stack overflow in VM #8432

Open timotheecour opened 6 years ago

timotheecour commented 6 years ago

to reproduce: checkout code from here: https://github.com/mratsim/Arraymancer/pull/259 (which itself fixes https://github.com/mratsim/Arraymancer/issues/258 to work with nim devel) (eg: git clone https://github.com/timotheecour/Arraymancer/ && git checkout fix_258) then: choosenim devel nimble test

it fails with: Error: Execution failed with exit code 35584 ... Command: "/Users/timothee/.nimble/bin/nim" c --noNimblePath --path:"/Users/timothee/.nimble/pkgs/nimblas-0.2.1" --path:"/Users/timothee/.nimble/pkgs/nimlapack-0.1.1" --path:"/Users/timothee/.nimble/pkgs/nimcuda-0.1.5" --path:"/Users/timothee/.nimble/pkgs/nimcl-0.1.3" --path:"/Users/timothee/.nimble/pkgs/opencl-1.0" --path:"/Users/timothee/.nimble/pkgs/clblast-0.0.1" --path:"/Users/timothee/.nimble/pkgs/opencl-1.0" --path:"/Users/timothee/.nimble/pkgs/stb_image-2.1" "--out:./build/tests_cpu" "--run" "--nimcache:"nimcache"" "tests/tests_cpu.nim"

and, after further simplification, this also fails:

nim c --noNimblePath --path:/Users/timothee/.nimble/pkgs/nimblas-0.2.1 --path:/Users/timothee/.nimble/pkgs/nimlapack-0.1.1 --path:/Users/timothee/.nimble/pkgs/nimcuda-0.1.5 --path:/Users/timothee/.nimble/pkgs/nimcl-0.1.3 --path:/Users/timothee/.nimble/pkgs/opencl-1.0 --path:/Users/timothee/.nimble/pkgs/clblast-0.0.1 --path:/Users/timothee/.nimble/pkgs/opencl-1.0 --path:/Users/timothee/.nimble/pkgs/stb_image-2.1 -o:build/tests_cpu4 tests/tests_cpu.nim && echo ok || echo error

prints: error at the end (just because I added && echo ok || echo error) (and build/tests_cpu4 isn't generated)

note: it works with choosenim stable, so looks like a regression

NOTE: after using nim_temp from ./koch temp --out:$nimc_D/bin/nim_temp, it fails with: segmentation fault (and no error)

NOTE: after further investigation, with lldb -- $nimc_D/bin/nim_temp ... I get following stacktrace under lldb (didn't work under gdb... not sure whether gdb is supported?):

this probably indicates stack overflow from infinite recursion:

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=2, address=0x7ffeef3ffec8)
    frame #0: 0x000000010007c04b nim_temp semTypeIdent_RiNpYDzSB8OUnjHue9b4s2g + 75
nim_temp`semTypeIdent_RiNpYDzSB8OUnjHue9b4s2g:
->  0x10007c04b <+75>: callq  0x1000c6170               ; pickSym_qEI19b8oSbENdUu5eGgUxew
    0x10007c050 <+80>: movq   %rax, %rbx
    0x10007c053 <+83>: testq  %rbx, %rbx
    0x10007c056 <+86>: je     0x10007c06f               ; <+111>
Target 0: (nim_temp) stopped.

(bt)

    frame #52267: 0x000000010008b481 nim_temp generateInstance_6DlcdI8m136DxICzC80OKg + 1905
    frame #52268: 0x0000000100096d10 nim_temp semResolvedCall_5MPXOtDbEsx5nrtQw8MPZQ + 1408
    frame #52269: 0x0000000100093acb nim_temp semOverloadedCall_nZLg3oTYOieG4PAqQ1zu0A + 1083
    frame #52270: 0x00000001000927bc nim_temp semOverloadedCallAnalyseEffects_DSb9bsrZYfDcxANKVwjl9chA + 44
    frame #52271: 0x0000000100078ceb nim_temp semExpr_tayBCUAEli3HMPwxdS9aMbg_2 + 3979
    frame #52272: 0x00000001000b1a3e nim_temp semOperand_tayBCUAEli3HMPwxdS9aMbg_26 + 30
    frame #52273: 0x00000001000df21e nim_temp matchesAux_5B328Lq3sbOWOpGv9bkDcTg + 2894
    frame #52274: 0x00000001000e0958 nim_temp matches_x3U2uVO0diqi4Z4tOU2y9aw_2 + 120
    frame #52275: 0x0000000100095643 nim_temp pickBestCandidate_WAPzOw27cCNyE9anuZ9a9cMaA + 611
    frame #52276: 0x0000000100095988 nim_temp resolveOverloads_wzM2ip7QP0Y3Z7WmKZZoBA + 312
    frame #52277: 0x0000000100093763 nim_temp semOverloadedCall_nZLg3oTYOieG4PAqQ1zu0A + 211
    frame #52278: 0x00000001000927bc nim_temp semOverloadedCallAnalyseEffects_DSb9bsrZYfDcxANKVwjl9chA + 44
    frame #52279: 0x0000000100078ceb nim_temp semExpr_tayBCUAEli3HMPwxdS9aMbg_2 + 3979
    frame #52280: 0x000000010007b04b nim_temp semExprWithType_tayBCUAEli3HMPwxdS9aMbg + 27
    frame #52281: 0x00000001000a8cad nim_temp semVarOrLet_6Rdn0jKaIRKP81jsTLDciw + 589
    frame #52282: 0x00000001000787cf nim_temp semExpr_tayBCUAEli3HMPwxdS9aMbg_2 + 2671
    frame #52283: 0x00000001000a7d50 nim_temp semStmtList_tayBCUAEli3HMPwxdS9aMbg_24 + 272
    frame #52284: 0x0000000100078225 nim_temp semExpr_tayBCUAEli3HMPwxdS9aMbg_2 + 1221
    frame #52285: 0x00000001000a7d50 nim_temp semStmtList_tayBCUAEli3HMPwxdS9aMbg_24 + 272
    frame #52286: 0x0000000100078225 nim_temp semExpr_tayBCUAEli3HMPwxdS9aMbg_2 + 1221
    frame #52287: 0x00000001000a7a2f nim_temp semBlock_NBTNEqzl5NBSHnN0VGYkSQ_42 + 255
    frame #52288: 0x0000000100078212 nim_temp semExpr_tayBCUAEli3HMPwxdS9aMbg_2 + 1202
    frame #52289: 0x0000000100082d1a nim_temp semAfterMacroCall_00z3ReZzIEXfRQL9czzyLtA + 266
    frame #52290: 0x0000000100085c46 nim_temp semMacroExpr_woULc1D9b2DCEYOx5yRZXRg + 342
    frame #52291: 0x000000010009267a nim_temp afterCallActions_9bA5HQzp5SlBuAZ9a1ZzejHQ + 58
    frame #52292: 0x0000000100078d00 nim_temp semExpr_tayBCUAEli3HMPwxdS9aMbg_2 + 4000
    frame #52293: 0x00000001000a7d50 nim_temp semStmtList_tayBCUAEli3HMPwxdS9aMbg_24 + 272
    frame #52294: 0x0000000100078225 nim_temp semExpr_tayBCUAEli3HMPwxdS9aMbg_2 + 1221
    frame #52295: 0x0000000100082d1a nim_temp semAfterMacroCall_00z3ReZzIEXfRQL9czzyLtA + 266
    frame #52296: 0x0000000100085c46 nim_temp semMacroExpr_woULc1D9b2DCEYOx5yRZXRg + 342
    frame #52297: 0x000000010009267a nim_temp afterCallActions_9bA5HQzp5SlBuAZ9a1ZzejHQ + 58
    frame #52298: 0x0000000100078d00 nim_temp semExpr_tayBCUAEli3HMPwxdS9aMbg_2 + 4000
    frame #52299: 0x00000001000a7d50 nim_temp semStmtList_tayBCUAEli3HMPwxdS9aMbg_24 + 272
    frame #52300: 0x0000000100078225 nim_temp semExpr_tayBCUAEli3HMPwxdS9aMbg_2 + 1221
    frame #52301: 0x00000001000a7d50 nim_temp semStmtList_tayBCUAEli3HMPwxdS9aMbg_24 + 272
    frame #52302: 0x0000000100078225 nim_temp semExpr_tayBCUAEli3HMPwxdS9aMbg_2 + 1221
    frame #52303: 0x00000001000acb6c nim_temp semTry_NBTNEqzl5NBSHnN0VGYkSQ_49 + 172
    frame #52304: 0x0000000100078914 nim_temp semExpr_tayBCUAEli3HMPwxdS9aMbg_2 + 2996
    frame #52305: 0x00000001000a7d50 nim_temp semStmtList_tayBCUAEli3HMPwxdS9aMbg_24 + 272
    frame #52306: 0x0000000100078225 nim_temp semExpr_tayBCUAEli3HMPwxdS9aMbg_2 + 1221
    frame #52307: 0x00000001000a71aa nim_temp semIf_NBTNEqzl5NBSHnN0VGYkSQ_38 + 330
    frame #52308: 0x0000000100078159 nim_temp semExpr_tayBCUAEli3HMPwxdS9aMbg_2 + 1017
    frame #52309: 0x00000001000a7d50 nim_temp semStmtList_tayBCUAEli3HMPwxdS9aMbg_24 + 272
    frame #52310: 0x0000000100078225 nim_temp semExpr_tayBCUAEli3HMPwxdS9aMbg_2 + 1221
    frame #52311: 0x0000000100082d5a nim_temp semAfterMacroCall_00z3ReZzIEXfRQL9czzyLtA + 330
    frame #52312: 0x0000000100082bd0 nim_temp semTemplateExpr_InSQ3LpBhnCqFgw1Hbb18Q + 160
    frame #52313: 0x000000010009269b nim_temp afterCallActions_9bA5HQzp5SlBuAZ9a1ZzejHQ + 91
    frame #52314: 0x0000000100078d00 nim_temp semExpr_tayBCUAEli3HMPwxdS9aMbg_2 + 4000
    frame #52315: 0x00000001000a7d50 nim_temp semStmtList_tayBCUAEli3HMPwxdS9aMbg_24 + 272
    frame #52316: 0x0000000100078225 nim_temp semExpr_tayBCUAEli3HMPwxdS9aMbg_2 + 1221
    frame #52317: 0x00000001000a7d50 nim_temp semStmtList_tayBCUAEli3HMPwxdS9aMbg_24 + 272
    frame #52318: 0x0000000100078225 nim_temp semExpr_tayBCUAEli3HMPwxdS9aMbg_2 + 1221
    frame #52319: 0x00000001000acb6c nim_temp semTry_NBTNEqzl5NBSHnN0VGYkSQ_49 + 172
    frame #52320: 0x0000000100078914 nim_temp semExpr_tayBCUAEli3HMPwxdS9aMbg_2 + 2996
    frame #52321: 0x00000001000a7d50 nim_temp semStmtList_tayBCUAEli3HMPwxdS9aMbg_24 + 272
    frame #52322: 0x0000000100078225 nim_temp semExpr_tayBCUAEli3HMPwxdS9aMbg_2 + 1221
    frame #52323: 0x00000001000a7a2f nim_temp semBlock_NBTNEqzl5NBSHnN0VGYkSQ_42 + 255
    frame #52324: 0x0000000100078212 nim_temp semExpr_tayBCUAEli3HMPwxdS9aMbg_2 + 1202
    frame #52325: 0x00000001000a7d50 nim_temp semStmtList_tayBCUAEli3HMPwxdS9aMbg_24 + 272
    frame #52326: 0x0000000100078225 nim_temp semExpr_tayBCUAEli3HMPwxdS9aMbg_2 + 1221
    frame #52327: 0x0000000100082d5a nim_temp semAfterMacroCall_00z3ReZzIEXfRQL9czzyLtA + 330
    frame #52328: 0x0000000100082bd0 nim_temp semTemplateExpr_InSQ3LpBhnCqFgw1Hbb18Q + 160
    frame #52329: 0x000000010009269b nim_temp afterCallActions_9bA5HQzp5SlBuAZ9a1ZzejHQ + 91
    frame #52330: 0x0000000100078d00 nim_temp semExpr_tayBCUAEli3HMPwxdS9aMbg_2 + 4000
    frame #52331: 0x00000001000b28f1 nim_temp semStmtAndGenerateGenerics_NBTNEqzl5NBSHnN0VGYkSQ_70 + 193
    frame #52332: 0x00000001000b2b85 nim_temp myProcess_9aYtClTzGl7gCIMK0iQSuhQ + 69
    frame #52333: 0x00000001000cdab2 nim_temp processModule_3A8vXpc6ePXdtcr9c1H1H9aA + 658
    frame #52334: 0x00000001001acde4 nim_temp compileModule_qzO5vYU9bpLpILLj3MVpbhg + 340
    frame #52335: 0x00000001001ace0e nim_temp importModule_re1epiGuwGWFjydVuzah3Q + 30
    frame #52336: 0x00000001000c32e3 nim_temp myImportModule_yJ71euPlK45hW8PmgqX9ahQ + 691
    frame #52337: 0x00000001000c3b8e nim_temp evalImport_rlI2ulrJgB3tjNcA8reHfg + 542
    frame #52338: 0x00000001000789a1 nim_temp semExpr_tayBCUAEli3HMPwxdS9aMbg_2 + 3137
    frame #52339: 0x00000001000b28f1 nim_temp semStmtAndGenerateGenerics_NBTNEqzl5NBSHnN0VGYkSQ_70 + 193
    frame #52340: 0x00000001000b2b85 nim_temp myProcess_9aYtClTzGl7gCIMK0iQSuhQ + 69
    frame #52341: 0x00000001000cdab2 nim_temp processModule_3A8vXpc6ePXdtcr9c1H1H9aA + 658
    frame #52342: 0x00000001001acde4 nim_temp compileModule_qzO5vYU9bpLpILLj3MVpbhg + 340
    frame #52343: 0x000000010005623c nim_temp commandCompileToC_ikP0vuP6oxqlYdG7q9cY7sA_2 + 188
    frame #52344: 0x0000000100057bad nim_temp mainCommand_ikP0vuP6oxqlYdG7q9cY7sA + 3197
    frame #52345: 0x0000000100001741 nim_temp handleCmdLine_I9aitIr13Z1B6wHv5vzMf2w + 1297
    frame #52346: 0x00000001000020d2 nim_temp NimMainModule + 114
    frame #52347: 0x000000010000219e nim_temp main + 94
    frame #52348: 0x00007fff53fdd015 libdyld.dylib start + 1
    frame #52349: 0x00007fff53fdd015 libdyld.dylib start + 1

NOTE: I'm on OSX

NOTE: the last msg (with --verbosity:3) was: /Users/timothee/git_clone/nim/Nim/lib/pure/unittest.nim(487, 9) Hint: global variable declared here [GlobalVar] var testStatusIMPL {.inject.} = OK

NOTE: this would be a perfect application of D's dustmite tool, if we adapted it to handle Nim (see https://github.com/nim-lang/Nim/issues/8276 )

mratsim commented 6 years ago

I can reproduce on my Mac.

ghost commented 6 years ago

@timotheecour btw, it's better to get stacktrace via ./koch temp <compiler options>, like ./koch temp c myfile.nim

timotheecour commented 6 years ago

thanks; indeed, it gives a better stacktrace and also tells "stack overflow" at then end; here:

/Users/timothee/git_clone/nim/Arraymancer/src/io/io_npy.nim(104, 55) template/generic instantiation from here
/Users/timothee/git_clone/nim/Arraymancer/src/io/io_npy.nim(36, 15) Warning: Cannot prove that 'result' is initialized. This will become a compile time error in the future. [ProveInit]
      of ">u8": result.parser = proc(x: FileStream): T = x.readUInt64BE.T
                ^
Traceback (most recent call last)
/Users/timothee/git_clone/nim/Nim/compiler/nim.nim(133) nim
/Users/timothee/git_clone/nim/Nim/compiler/nim.nim(97) handleCmdLine
/Users/timothee/git_clone/nim/Nim/compiler/main.nim(162) mainCommand
/Users/timothee/git_clone/nim/Nim/compiler/main.nim(73) commandCompileToC
/Users/timothee/git_clone/nim/Nim/compiler/modules.nim(124) compileProject
/Users/timothee/git_clone/nim/Nim/compiler/modules.nim(71) compileModule
/Users/timothee/git_clone/nim/Nim/compiler/passes.nim(194) processModule
/Users/timothee/git_clone/nim/Nim/compiler/passes.nim(103) processTopLevelStmt
/Users/timothee/git_clone/nim/Nim/compiler/sem.nim(592) myProcess
/Users/timothee/git_clone/nim/Nim/compiler/sem.nim(560) semStmtAndGenerateGenerics
/Users/timothee/git_clone/nim/Nim/compiler/semstmts.nim(1891) semStmt
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(912) semExprNoType
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2553) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/importer.nim(192) evalImport
/Users/timothee/git_clone/nim/Nim/compiler/importer.nim(168) impMod
/Users/timothee/git_clone/nim/Nim/compiler/importer.nim(150) myImportModule
/Users/timothee/git_clone/nim/Nim/compiler/modules.nim(84) importModule
/Users/timothee/git_clone/nim/Nim/compiler/modules.nim(71) compileModule
/Users/timothee/git_clone/nim/Nim/compiler/passes.nim(194) processModule
/Users/timothee/git_clone/nim/Nim/compiler/passes.nim(103) processTopLevelStmt
/Users/timothee/git_clone/nim/Nim/compiler/sem.nim(592) myProcess
/Users/timothee/git_clone/nim/Nim/compiler/sem.nim(560) semStmtAndGenerateGenerics
/Users/timothee/git_clone/nim/Nim/compiler/semstmts.nim(1891) semStmt
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(912) semExprNoType
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2413) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(894) semDirectOp
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(790) afterCallActions
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(31) semTemplateExpr
/Users/timothee/git_clone/nim/Nim/compiler/sem.nim(411) semAfterMacroCall
/Users/timothee/git_clone/nim/Nim/compiler/semstmts.nim(1891) semStmt
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(912) semExprNoType
(1874 calls omitted) ...
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2382) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2419) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(242) semConv
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(136) checkConvertible
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(337) cmpTypes
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(1751) typeRelImpl
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(800) tryResolvingStaticExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2382) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2419) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(242) semConv
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(136) checkConvertible
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(337) cmpTypes
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(1751) typeRelImpl
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(800) tryResolvingStaticExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2382) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2419) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(242) semConv
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(136) checkConvertible
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(337) cmpTypes
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(1751) typeRelImpl
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(800) tryResolvingStaticExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2382) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2419) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(242) semConv
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(136) checkConvertible
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(337) cmpTypes
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(1751) typeRelImpl
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(800) tryResolvingStaticExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2382) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2419) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(242) semConv
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(136) checkConvertible
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(337) cmpTypes
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(1751) typeRelImpl
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(800) tryResolvingStaticExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2382) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2419) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(242) semConv
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(136) checkConvertible
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(337) cmpTypes
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(1751) typeRelImpl
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(800) tryResolvingStaticExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2382) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2419) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(242) semConv
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(136) checkConvertible
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(337) cmpTypes
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(1751) typeRelImpl
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(800) tryResolvingStaticExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2382) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2419) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(242) semConv
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(136) checkConvertible
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(337) cmpTypes
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(1751) typeRelImpl
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(800) tryResolvingStaticExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2382) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2419) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(242) semConv
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(136) checkConvertible
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(337) cmpTypes
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(1751) typeRelImpl
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(800) tryResolvingStaticExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2382) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2419) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(242) semConv
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(136) checkConvertible
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(337) cmpTypes
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(1751) typeRelImpl
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(800) tryResolvingStaticExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2382) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2419) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(242) semConv
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(136) checkConvertible
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(337) cmpTypes
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(1751) typeRelImpl
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(800) tryResolvingStaticExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2382) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2419) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(242) semConv
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(136) checkConvertible
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(337) cmpTypes
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(1751) typeRelImpl
/Users/timothee/git_clone/nim/Nim/compiler/sigmatch.nim(798) tryResolvingStaticExpr
/Users/timothee/git_clone/nim/Nim/compiler/semtypinst.nim(566) replaceTypesInBody
/Users/timothee/git_clone/nim/Nim/compiler/semtypinst.nim initLayeredTypeMap
/Users/timothee/git_clone/nim/Nim/lib/system/assign.nim(230) genericReset
/Users/timothee/git_clone/nim/Nim/lib/system/assign.nim(217) genericResetAux
/Users/timothee/git_clone/nim/Nim/lib/system/assign.nim(215) genericResetAux
/Users/timothee/git_clone/nim/Nim/lib/system/assign.nim(230) genericReset
/Users/timothee/git_clone/nim/Nim/lib/system/assign.nim(217) genericResetAux
/Users/timothee/git_clone/nim/Nim/lib/system/assign.nim(215) genericResetAux
/Users/timothee/git_clone/nim/Nim/lib/system/assign.nim(228) genericReset
/Users/timothee/git_clone/nim/Nim/lib/system/gc.nim(284) unsureAsgnRef
/Users/timothee/git_clone/nim/Nim/lib/system/gc_common.nim(295) isOnStack
/Users/timothee/git_clone/nim/Nim/lib/system/gc_common.nim getActiveStack
Stack overflow
error
mratsim commented 6 years ago

Here is a smaller test case following my investigation in https://github.com/mratsim/Arraymancer/issues/260

proc relative_error*[T](y_true, y: T): T {.inline.} =
  ## Relative error, |y_true - y|/max(|y_true|, |y|)
  ## Normally the relative error is defined as |y_true - y| / |y_true|,
  ## but here max is used to make it symmetric and to prevent dividing by zero,
  ## guaranteed to return zero in the case when both values are zero.
  let denom = max(abs(y_true), abs(y))
  if denom == 0.T:
    return 0.T
  result = abs(y_true - y) / denom

proc numerical_gradient*[T](input: T, f: (proc(x: T): T), h: T = 1e-5.T): T {.inline.} =
  ## Compute numerical gradient for any function w.r.t. to an input value,
  ## useful for gradient checking, recommend using float64 types to assure
  ## numerical precision. The gradient is calculated as:
  ## (f(x + h) - f(x - h)) / (2*h)
  ## where h is a small number, typically 1e-5.
  result = (f(input + h) - f(input - h)) / (2.0.T * h)

proc f(x: float): float = x*x + x + 1.0

echo relative_error(numerical_gradient(2.0, f), 5.0) < 1e-8

The compiler just quits without any message.

Note that this is a completely different code path than result.parser = proc(x: FileStream): T = x.readUInt64BE.T reported that Timothée.

The common points are closure procs.

mratsim commented 6 years ago

Further investigation shows that closure + generics do not work anymore as the following works fine:

proc relative_error*(y_true, y: float): float {.inline.} =
  ## Relative error, |y_true - y|/max(|y_true|, |y|)
  ## Normally the relative error is defined as |y_true - y| / |y_true|,
  ## but here max is used to make it symmetric and to prevent dividing by zero,
  ## guaranteed to return zero in the case when both values are zero.
  let denom = max(abs(y_true), abs(y))
  if denom == 0.0:
    return 0.0
  result = abs(y_true - y) / denom

proc numerical_gradient*(input: float, f: (proc(x: float): float), h = 1e-5): float {.inline.} =
  ## Compute numerical gradient for any function w.r.t. to an input value,
  ## useful for gradient checking, recommend using float64 types to assure
  ## numerical precision. The gradient is calculated as:
  ## (f(x + h) - f(x - h)) / (2*h)
  ## where h is a small number, typically 1e-5.
  result = (f(input + h) - f(input - h)) / (2.0 * h)

proc f(x: float): float = x*x + x + 1.0

echo relative_error(numerical_gradient(2.0, f), 5.0) < 1e-8
skilchen commented 6 years ago

The problem of @mratsim was introduced via commit 7cec5d1cfb0ada682b8cc0b605adc6c1c8b32758

Merge pull request #7681 from nim-lang/typedesc-reforms

Typedesc reform

I think this is something @zah should take a look at

timotheecour commented 6 years ago

@skilchen did you use git bisect to find that commit?

skilchen commented 6 years ago

@timotheecour i tried, but for some reason it is impossible to find https://github.com/nim-lang/Nim/commit/7cec5d1cfb0ada682b8cc0b605adc6c1c8b32758 using git bisect as the bisecting will eventually find one of the single commits from @zah collectively merged by @araq (but keeping them in the history) and then you end up in an uncompilable state where the only way out (as far as i know) is git bisect skip until you have skipped all of @zah's single commits.

LemonBoy commented 6 years ago

Here's a minimal reproducer:

proc f[T](h: T = 1.T) = discard
f()

The problem is the 1.T argument that throws the typeRel in a loop as T is not known yet.

timotheecour commented 6 years ago

@LemonBoy I can't reproduce error on your minimal case:

rnim bugs/compiler/t07_bug_8432.nim
nim c --nimcache:/tmp/nim//nimcache/ -o:/tmp/nim//app -r bugs/compiler/t07_bug_8432.nim
# no error

nim --version Nim Compiler Version 0.18.1 [MacOSX: amd64] Compiled at 2018-08-06 Copyright (c) 2006-2018 by Andreas Rumpf

git hash: bbc404d80d77365a1c402a0862a3d26958b16e7b active boot switches: -d:release

(ie, latest)

mratsim commented 6 years ago

Following discussion with @Araq on IRC https://irclogs.nim-lang.org/07-09-2018.html#08:33:40

I fixed the issue and confirmed it has to do with default params + generics. There seems to be 2 ways this can happen according to this commit https://github.com/mratsim/Arraymancer/pull/275/commits/f278ea5f261e3b783a0249d42d103af84964d001 in Arraymancer:

  1. using this signature proc numerical_gradient*[T](input: T, f: (proc(x: T): T), h: T = 1e-5.T): T instead of this one proc numerical_gradient*[T](input: T, f: (proc(x: T): T), h = T(1e-5)): T. The :T throws the compiler in a loop. Minimal example already provided in this thread.

  2. Using let layer_3neurons = ctx.variable(randomTensor(3, 2, 2.0f) .- 1.0f, requires_grad = true) instead of let layer_3neurons = ctx.variable(randomTensor(3, 2, 2.0f) .- 1.0f, true) with the function signature being proc variable*[TT](ctx: Context[TT], value: TT, requires_grad = false): Variable[TT]. Unfortunately, I can't reproduce in a minimal working example for some reason.

Araq commented 6 years ago

Easy workaround exists, no showstopper.