Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nim crashes with SIGABRT after getting into a replaceTypeVars infinite loop. #10884

Closed
kayabaNerve opened this issue Mar 22, 2019 · 2 comments

Comments

Projects
None yet
2 participants
@kayabaNerve
Copy link
Collaborator

commented Mar 22, 2019

The code below has a proc with two arguments. The first can be an int or an uint. The second is a string which has a default argument which chains together two calls to a func.

func pad(
    data: string,
    len: int
): string =
    result = data
    while result.len < len:
        result = char(0) & result
    
proc test(
    a: int | uint,
    b: string = "".pad(64).pad(128)
) =
    discard

test(1)

On Nim stable, and Nim devel, this will cause a silent crash. On a Nim devel debug compiler, it prints the following:

Hint: TestDatabase [Processing]
Traceback (most recent call last)
nim.nim(98)              nim
nim.nim(75)              handleCmdLine
cmdlinehelper.nim(92)    loadConfigsAndRunMainCommand
main.nim(198)            mainCommand
main.nim(90)             commandCompileToC
modules.nim(137)         compileProject
modules.nim(78)          compileModule
passes.nim(202)          processModule
passes.nim(86)           processTopLevelStmt
sem.nim(603)             myProcess
sem.nim(571)             semStmtAndGenerateGenerics
semstmts.nim(2158)       semStmt
semexprs.nim(932)        semExprNoType
semexprs.nim(2645)       semExpr
importer.nim(212)        evalImport
importer.nim(182)        impMod
importer.nim(154)        myImportModule
modules.nim(92)          importModule
modules.nim(78)          compileModule
passes.nim(202)          processModule
passes.nim(86)           processTopLevelStmt
sem.nim(603)             myProcess
sem.nim(571)             semStmtAndGenerateGenerics
semstmts.nim(2158)       semStmt
semexprs.nim(932)        semExprNoType
semexprs.nim(2645)       semExpr
importer.nim(212)        evalImport
importer.nim(182)        impMod
importer.nim(154)        myImportModule
modules.nim(92)          importModule
modules.nim(78)          compileModule
(1874 calls omitted) ...
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(200)      replaceTypeVarsN
semtypinst.nim(121)      replaceTypeVarsT
semtypinst.nim(555)      replaceTypeVarsTAux
types.nim(1324)          containsGenericType
types.nim(173)           iterOverType
intsets.nim(161)         initIntSet
assign.nim(211)          genericReset
assign.nim(198)          genericResetAux
assign.nim(196)          genericResetAux
assign.nim(209)          genericReset
gc.nim(249)              unsureAsgnRef
gc_common.nim(300)       isOnStack
gc_common.nim            getActiveStack
Error: call depth limit reached in a debug build (2000 function calls). You can change it with -d:nimCallDepthLimit=<int> but really try to avoid deep recursions instead.
Traceback (most recent call last)
nim.nim(98)              nim
nim.nim(75)              handleCmdLine
cmdlinehelper.nim(92)    loadConfigsAndRunMainCommand
main.nim(198)            mainCommand
main.nim(90)             commandCompileToC
modules.nim(137)         compileProject
modules.nim(78)          compileModule
passes.nim(202)          processModule
passes.nim(86)           processTopLevelStmt
sem.nim(603)             myProcess
sem.nim(571)             semStmtAndGenerateGenerics
semstmts.nim(2158)       semStmt
semexprs.nim(932)        semExprNoType
semexprs.nim(2645)       semExpr
importer.nim(212)        evalImport
importer.nim(182)        impMod
importer.nim(154)        myImportModule
modules.nim(92)          importModule
modules.nim(78)          compileModule
passes.nim(202)          processModule
passes.nim(86)           processTopLevelStmt
sem.nim(603)             myProcess
sem.nim(571)             semStmtAndGenerateGenerics
semstmts.nim(2158)       semStmt
semexprs.nim(932)        semExprNoType
semexprs.nim(2645)       semExpr
importer.nim(212)        evalImport
importer.nim(182)        impMod
importer.nim(154)        myImportModule
modules.nim(92)          importModule
modules.nim(78)          compileModule
(1874 calls omitted) ...
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(206)      replaceTypeVarsN
semtypinst.nim(268)      replaceTypeVarsS
semtypinst.nim(243)      replaceTypeVarsN
semtypinst.nim(200)      replaceTypeVarsN
semtypinst.nim(121)      replaceTypeVarsT
semtypinst.nim(555)      replaceTypeVarsTAux
types.nim(1324)          containsGenericType
types.nim(173)           iterOverType
intsets.nim(161)         initIntSet
assign.nim(211)          genericReset
assign.nim(198)          genericResetAux
assign.nim(196)          genericResetAux
assign.nim(209)          genericReset
gc.nim(249)              unsureAsgnRef
gc_common.nim(300)       isOnStack
gc_common.nim            getActiveStack
SIGABRT: Abnormal termination.

The expected behavior is this compiles without crashing (or if I did violate the language spec, I get an error).

There are four ways to get this code to compile without changing the compiler:

  1. Removing | uint.
  2. Removing one of the calls to pad (func).
  3. Making pad a proc (proc -> proc).
  4. Making the first call to pad call a copy of pad which is a proc (proc -> func).
    func -> proc does crash.
@kayabaNerve

This comment has been minimized.

Copy link
Collaborator Author

commented Mar 22, 2019

After I posted this, I realized I didn't try changing pad to a proc to make this MWE even more minimal. I tried that and realized the fact it's a func is a key part to the bug, and have updated my original post accordingly.

@Araq Araq added the Crash label Mar 22, 2019

@Araq

This comment has been minimized.

Copy link
Member

commented Mar 22, 2019

Workaround:

proc pad(
    data: string,
    len: int
): string =
    result = data
    while result.len < len:
        result = char(0) & result

proc test(
    a: int | uint,
    b: string = "".pad(64).pad(128)
) =
    discard

test(1)

Araq added a commit that referenced this issue Mar 22, 2019

@Araq Araq closed this in 04ad200 Mar 22, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.