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

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

Open
shayanhabibi opened this issue Sep 4, 2024 · 1 comment
Labels
bug Something isn't working compiler/sem Related to semantic-analysis system of the compiler

Comments

@shayanhabibi
Copy link
Collaborator

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 zerbina added bug Something isn't working compiler/sem Related to semantic-analysis system of the compiler labels Sep 4, 2024
@zerbina
Copy link
Collaborator

zerbina commented Sep 4, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler/sem Related to semantic-analysis system of the compiler
Projects
None yet
Development

No branches or pull requests

2 participants