Nim icon indicating copy to clipboard operation
Nim copied to clipboard

Compiler segfault in codegen when putting nested importCpp type into seq

Open RedBeard0531 opened this issue 3 years ago • 1 comments

Example

The following code causes a segfault in the nim compiler itself.

type
  Foo* {.importC, nodecl.} = object # doesn't matter if this is importC or importCpp
    value*: int64
  Bar* {.importCpp, nodecl.} = object # no segfault with importC
    foo*: Foo

discard @[Bar()]

This appears to be a minimal repo. In particular, Foo needs to have a member, and Bar needs to be put into a seq. I don't know what other uses of Bar would also cause a compiler segfault, but discard Bar() was insufficient.

Current Output

> ~/nim/nim/bin/nim cpp repro.nim
Hint: used config file '/home/mstearn/nim/nim/config/nim.cfg' [Conf]
Hint: used config file '/home/mstearn/nim/nim/config/config.nims' [Conf]
.................................................................Traceback (most recent call last)
/home/mstearn/nim/nim/compiler/nim.nim(143) nim
/home/mstearn/nim/nim/compiler/nim.nim(98) handleCmdLine
/home/mstearn/nim/nim/compiler/main.nim(285) mainCommand
/home/mstearn/nim/nim/compiler/main.nim(255) compileToBackend
/home/mstearn/nim/nim/compiler/main.nim(110) commandCompileToC
/home/mstearn/nim/nim/compiler/modules.nim(136) compileProject
/home/mstearn/nim/nim/compiler/modules.nim(56) compileModule
/home/mstearn/nim/nim/compiler/passes.nim(183) processModule
/home/mstearn/nim/nim/compiler/passes.nim(76) processTopLevelStmt
/home/mstearn/nim/nim/compiler/cgen.nim(1991) myProcess
/home/mstearn/nim/nim/compiler/cgen.nim(1985) genTopLevelStmt
/home/mstearn/nim/nim/compiler/cgen.nim(1021) genProcBody
/home/mstearn/nim/nim/compiler/ccgstmts.nim(1607) genStmts
/home/mstearn/nim/nim/compiler/ccgexprs.nim(2883) expr
/home/mstearn/nim/nim/compiler/ccgexprs.nim(2612) genStmtList
/home/mstearn/nim/nim/compiler/ccgstmts.nim(1607) genStmts
/home/mstearn/nim/nim/compiler/ccgexprs.nim(2927) expr
/home/mstearn/nim/nim/compiler/cgen.nim(655) initLocExprSingleUse
/home/mstearn/nim/nim/compiler/ccgexprs.nim(2851) expr
/home/mstearn/nim/nim/compiler/ccgexprs.nim(2442) genMagicExpr
/home/mstearn/nim/nim/compiler/ccgexprs.nim(1561) genArrToSeq
/home/mstearn/nim/nim/compiler/ccgexprs.nim(1544) genSeqConstr
/home/mstearn/nim/nim/compiler/ccgexprs.nim(1405) genNewSeqAux
/home/mstearn/nim/nim/compiler/ccgtypes.nim(1487) genTypeInfoV1
/home/mstearn/nim/nim/compiler/ccgtrav.nim(156) genTraverseProc
/home/mstearn/nim/nim/compiler/ccgtrav.nim(130) genTraverseProcSeq
/home/mstearn/nim/nim/compiler/ccgtrav.nim(94) genTraverseProc
/home/mstearn/nim/nim/compiler/ccgtrav.nim(31) genTraverseProc
/home/mstearn/nim/nim/compiler/ccgtrav.nim(57) genTraverseProc
/home/mstearn/nim/nim/compiler/ccgtrav.nim(94) genTraverseProc
/home/mstearn/nim/nim/compiler/ccgtrav.nim(31) genTraverseProc
/home/mstearn/nim/nim/compiler/ccgtrav.nim(55) genTraverseProc
/home/mstearn/nim/nim/compiler/cgen.nim(78) t
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
zsh: segmentation fault  ~/nim/nim/bin/nim cpp repro.nim

Expected Output

The compiler shouldn't segfault? Ideally this would generate working code. If there is a problem with the source file (I don't think there is), it should output a clear error.

Possible Solution

🤷

Additional Information

Repros at least in 1.6.6 and devel as of https://github.com/nim-lang/Nim/commit/efcb89fa702da5bd5d2cf000ace759df90152895

RedBeard0531 avatar Jul 20 '22 09:07 RedBeard0531