nim-works / nimskull

An in development statically typed systems programming language; with sustainability at its core. We, the community of users, maintain it.
https://nim-works.github.io/nimskull/index.html
Other
277 stars 39 forks source link

Forward declaration of generic types failing to compile with static evaluation #1446

Open shayanhabibi opened 2 months ago

shayanhabibi commented 2 months ago

Example

type
  Box*[T] = object
    when sizeof(T) != 0: value: int

  Obj* = object
    _*: Box[RefObj]
  RefObj* = ref Obj

Actual Output

PS C:\Users\shaya\Documents\GitHub\hyalos> nim_dbg c -o:"./bin/" -r "c:\Users\shaya\Documents\GitHub\hyalos\tests\test.nim"
...........................................................nim.nim(154)             nim
nim.nim(89)              handleCmdLine
main.nim(539)            mainCommand
main.nim(491)            compileToBackend
main.nim(214)            commandCompileToC
modules.nim(216)         compileProject
modules.nim(132)         compileModule
passes.nim(275)          processModule
passes.nim(102)          processTopLevelStmt
sem.nim(992)             myProcess
sem.nim(909)             semStmtAndGenerateGenerics nkStmtList 383830 c:\Users\shaya\Documents\GitHub\hyalos\tests\test.nim(2, 2)
semstmts.nim(3380)       semStmt
semexprs.nim(1440)       semExprNoType
semexprs.nim(3836)       semExpr nkStmtList 383830 c:\Users\shaya\Documents\GitHub\hyalos\tests\test.nim(2, 2)
semstmts.nim(3276)       semStmtList nkStmtList 383830 c:\Users\shaya\Documents\GitHub\hyalos\tests\test.nim(2, 2)
semexprs.nim(3843)       semExpr nkTypeSection 383780 c:\Users\shaya\Documents\GitHub\hyalos\tests\test.nim(2, 2)
semstmts.nim(2107)       semTypeSection
semstmts.nim(2007)       typeSectionRightSidePass
semtypes.nim(2314)       semTypeNode nkObjectTy 383811 c:\Users\shaya\Documents\GitHub\hyalos\tests\test.nim(5, 9)
semtypes.nim(1156)       semObjectNode
semtypes.nim(1041)       semRecordNodeAux
semtypes.nim(1051)       semRecordNodeAux
semtypes.nim(2239)       semTypeNode nkBracketExpr 383819 c:\Users\shaya\Documents\GitHub\hyalos\tests\test.nim(6, 11)
semtypes.nim(1786)       semGeneric
seminst.nim(206)         instGenericContainer
semtypinst.nim(170)      replaceTypeVarsT
semtypinst.nim(730)      replaceTypeVarsTAux
semtypinst.nim(622)      handleGenericInvocation
semtypinst.nim(535)      instantiate
semtypinst.nim(414)      instantiateRecord
semtypinst.nim(436)      instantiateRecord
sem.nim(846)             semConstBoolExpr
sem.nim(681)             semConstExpr nkStmtList 383891 c:\Users\shaya\Documents\GitHub\hyalos\tests\test.nim(3, 19)
sem.nim(652)             evalConstExpr nkStmtListExpr 383899 c:\Users\shaya\Documents\GitHub\hyalos\tests\test.nim(3, 19)
compilerbridge.nim(522)  evalConstExpr
compilerbridge.nim(518)  evalConstExprAux nkStmtListExpr 383995 c:\Users\shaya\Documents\GitHub\hyalos\tests\test.nim(3, 19)
compilerbridge.nim(482)  eval
vmjit.nim(242)           genExpr
vmjit.nim(216)           gen
vmjit.nim(173)           generateMirCode
mirgen.nim(2428)         exprToMir
mirgen.nim(1319)         genAsgnSource
mirgen.nim(2015)         genx
mirgen.nim(1192)         genCallOrMagic
mirgen.nim(1142)         genMagic
mirgen.nim(793)          genCall
mirgen.nim(779)          genArgs
mirgen.nim(728)          genArg
mirgen.nim(687)          emitOperandTree
mirgen.nim(682)          genArgExpression
mirgen.nim(2082)         genx
mirgen.nim(2015)         genx
mirgen.nim(1192)         genCallOrMagic
mirgen.nim(1188)         genMagic
mirgen.nim(793)          genCall
mirgen.nim(779)          genArgs
mirgen.nim(728)          genArg
mirgen.nim(687)          emitOperandTree
mirgen.nim(682)          genArgExpression
mirgen.nim(2082)         genx
mirgen.nim(2015)         genx
mirgen.nim(1192)         genCallOrMagic
mirgen.nim(998)          genMagic
mirgen.nim(211)          typeToMir
mirtypes.nim(1117)       add
mirtypes.nim(1110)       handleImported
mirtypes.nim(1062)       typeSymToMir
mirtypes.nim(885)        typeToMir
idioms.nim(42)           unreachableImpl
idioms.nim(36)           unreachableImpl
assertions.nim(28)       raiseAssert
fatal.nim(50)            sysFatal
Error: unhandled exception: mirtypes.nim(885, 16) unreachable: tyForward [AssertionDefect]

Expected Output

Compiles

Additional Information

Nimskull version:

Nimskull Compiler Version 0.1.0-dev.21413+dirty [windows: amd64]

Source hash: 73b9136359c77cbfe6f2bdd8a078bb5efcfbeb23
Source date: 2024-09-03

active boot switches: -d:release -d:danger

Reordering the type declaration, or removing the sizeof call both successfully compile.

type
  Box*[T] = object
    # when sizeof(T) != 0: value: int
    when T is int: value: int

  RefObj* = ref Obj
  Obj* = object
    _*: Box[RefObj]
  # Ref Obj* = ref Obj

References

Possibly related to other issues with type declaration order and static compiler hints such as #1413

zerbina commented 2 months ago

The problem is that Box[RefObj] is instantiated right away, even though RefObj is still a tyForward type. Since the sizeof can then not be collapsed (the size of the incomplete RefObj is not known yet), the expression is passed on to compile-time evaluation, where the unresolved forwarded type then causes trouble.