Skip to content

Compiler crashes with infinite recursion for nested generic instantiation with static[int] #23628

Open
@Nimaoth

Description

@Nimaoth

Description

When compiling the following code, the compiler just stops without any error messages.

type
  Bar[T] = object

  Foo[T; C: static int] = object
    bar: Bar[Foo[T, C]]

var f: Foo[int, 5]

Another failing example

type
  Bar[T; C: static int] = object
    arr: array[C, ptr T]

  Foo[T; C: static int] = object
    bar: Bar[Foo[T, C], C]

var f: Foo[int, 5]
echo f.bar.arr.len

After investigating a bit it looks like an infinite recursion when trying to instantiate Foo[T, C] inside of the field declaration of bar: Bar[Foo[T, C]].

The cached instantiation is never used, although new versions of it are repeatedly added to type instantiation cache.

The stack trace when it crashes looks like this:

/home/nimaoth/dev/Nim/compiler/nim.nim(169) nim
/home/nimaoth/dev/Nim/compiler/nim.nim(124) handleCmdLine
/home/nimaoth/dev/Nim/compiler/main.nim(338) mainCommand
/home/nimaoth/dev/Nim/compiler/main.nim(304) compileToBackend
/home/nimaoth/dev/Nim/compiler/main.nim(142) commandCompileToC
/home/nimaoth/dev/Nim/compiler/pipelines.nim(325) compilePipelineProject
/home/nimaoth/dev/Nim/compiler/pipelines.nim(245) compilePipelineModule
/home/nimaoth/dev/Nim/compiler/pipelines.nim(187) processPipelineModule
/home/nimaoth/dev/Nim/compiler/sem.nim(881) semWithPContext
/home/nimaoth/dev/Nim/compiler/sem.nim(843) semStmtAndGenerateGenerics
/home/nimaoth/dev/Nim/compiler/semstmts.nim(2769) semStmt
/home/nimaoth/dev/Nim/compiler/semexprs.nim(1190) semExprNoType
/home/nimaoth/dev/Nim/compiler/semexprs.nim(3354) semExpr /home/nimaoth/dev/nimsumtree/test.nim(3, 3) nkStmtList
/home/nimaoth/dev/Nim/lib/system.nim(918) semStmtList
/home/nimaoth/dev/Nim/compiler/semexprs.nim(3356) semExpr /home/nimaoth/dev/nimsumtree/test.nim(16, 1) nkVarSection
/home/nimaoth/dev/Nim/compiler/semstmts.nim(705) semVarOrLet
/home/nimaoth/dev/Nim/compiler/semtypes.nim(2092) semTypeNode
/home/nimaoth/dev/Nim/compiler/semtypes.nim(1623) semGeneric
/home/nimaoth/dev/Nim/compiler/seminst.nim(209) instGenericContainer
/home/nimaoth/dev/Nim/compiler/semtypinst.nim(118) replaceTypeVarsT
/home/nimaoth/dev/Nim/compiler/semtypinst.nim(567) replaceTypeVarsTAux
/home/nimaoth/dev/Nim/compiler/semtypinst.nim(432) handleGenericInvocation
/home/nimaoth/dev/Nim/compiler/semtypinst.nim(118) replaceTypeVarsT
/home/nimaoth/dev/Nim/compiler/semtypinst.nim(660) replaceTypeVarsTAux
/home/nimaoth/dev/Nim/compiler/semtypinst.nim(272) replaceTypeVarsN
/home/nimaoth/dev/Nim/compiler/semtypinst.nim(221) replaceTypeVarsN
/home/nimaoth/dev/Nim/compiler/semtypinst.nim(118) replaceTypeVarsT
/home/nimaoth/dev/Nim/compiler/semtypinst.nim(567) replaceTypeVarsTAux
/home/nimaoth/dev/Nim/compiler/semtypinst.nim(413) handleGenericInvocation
/home/nimaoth/dev/Nim/compiler/semtypinst.nim(118) replaceTypeVarsT
/home/nimaoth/dev/Nim/compiler/semtypinst.nim(567) replaceTypeVarsTAux
(1874 calls omitted) ...
/home/nimaoth/dev/Nim/compiler/semtypinst.nim(221) replaceTypeVarsN
/home/nimaoth/dev/Nim/compiler/semtypinst.nim(118) replaceTypeVarsT
/home/nimaoth/dev/Nim/compiler/semtypinst.nim(567) replaceTypeVarsTAux
/home/nimaoth/dev/Nim/compiler/semtypinst.nim(413) handleGenericInvocation
/home/nimaoth/dev/Nim/compiler/semtypinst.nim(118) replaceTypeVarsT
/home/nimaoth/dev/Nim/compiler/semtypinst.nim(567) replaceTypeVarsTAux
/home/nimaoth/dev/Nim/compiler/semtypinst.nim(432) handleGenericInvocation
/home/nimaoth/dev/Nim/compiler/semtypinst.nim(118) replaceTypeVarsT
/home/nimaoth/dev/Nim/compiler/semtypinst.nim(660) replaceTypeVarsTAux
/home/nimaoth/dev/Nim/compiler/semtypinst.nim(272) replaceTypeVarsN
/home/nimaoth/dev/Nim/compiler/semtypinst.nim(221) replaceTypeVarsN
/home/nimaoth/dev/Nim/compiler/semtypinst.nim(118) replaceTypeVarsT
/home/nimaoth/dev/Nim/compiler/semtypinst.nim(567) replaceTypeVarsTAux
/home/nimaoth/dev/Nim/compiler/semtypinst.nim(413) handleGenericInvocation
/home/nimaoth/dev/Nim/compiler/semtypinst.nim(118) replaceTypeVarsT
/home/nimaoth/dev/Nim/compiler/semtypinst.nim(567) replaceTypeVarsTAux
/home/nimaoth/dev/Nim/compiler/semtypinst.nim(432) handleGenericInvocation

Nim Version

Nim Compiler Version 2.1.1 [Windows: amd64]
Compiled at 2024-02-09
Copyright (c) 2006-2024 by Andreas Rumpf

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

Current Output

No output from the compiler

Expected Output

No response

Possible Solution

No response

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions