Skip to content

Converting derived type to distinct base type crashes compiler #1598

@bptato

Description

@bptato

When converting a derived ref object to a distinct type of its base type, the compiler asserts.

Example

type
  A = ref object of RootObj
  B = ref object of A
  C = distinct A

discard C(B())

Actual Output

/src/nimskull/compiler/nim.nim(156) nim
/src/nimskull/compiler/nim.nim(91) handleCmdLine
/src/nimskull/compiler/front/main.nim(541) mainCommand
/src/nimskull/compiler/front/main.nim(493) compileToBackend
/src/nimskull/compiler/front/main.nim(219) commandCompileToC
/src/nimskull/compiler/backend/cbackend.nim(409) generateCode
/src/nimskull/compiler/backend/backends.nim(815) generateCode
/src/nimskull/compiler/backend/backends.nim(397) translate
/src/nimskull/compiler/mir/mirgen.nim(2552) generateCode
/src/nimskull/compiler/mir/mirgen.nim(2302) gen
/src/nimskull/compiler/mir/mirgen.nim(2365) gen
/src/nimskull/compiler/mir/mirgen.nim(411) exprToPmir
/src/nimskull/compiler/mir/proto_mir.nim(819) exprToPmir
/src/nimskull/compiler/mir/proto_mir.nim(672) exprToPmir
/src/nimskull/compiler/mir/proto_mir.nim(496) analyseObjConv
/src/nimskull/compiler/ast/types.nim(922) inheritanceDiff
/src/nimskull/lib/system/assertions.nim(35) failedAssertImpl
/src/nimskull/lib/system/assertions.nim(25) raiseAssert
/src/nimskull/lib/system/fatal.nim(50) sysFatal
Error: unhandled exception: /src/nimskull/compiler/ast/types.nim(922, 10) `a.kind in {tyObject} + skipPtrs`  [AssertionDefect]

Expected Output

Compiles.

Possible Solution

I don't know if it's a good solution, but skipping tyDistinct seems to work:

diff --git a/compiler/ast/types.nim b/compiler/ast/types.nim
index 0db66576e..96d685a7c 100644
--- a/compiler/ast/types.nim
+++ b/compiler/ast/types.nim
@@ -919,19 +919,19 @@ proc inheritanceDiff*(a, b: PType): int =
   # | returns: +x iff `a` is the x'th direct subclass of `b`
   # | returns: `maxint` iff `a` and `b` are not compatible at all
   if a == b or a.kind == tyError or b.kind == tyError: return 0
-  assert a.kind in {tyObject} + skipPtrs
-  assert b.kind in {tyObject} + skipPtrs
+  assert a.kind in {tyObject, tyDistinct} + skipPtrs
+  assert b.kind in {tyObject, tyDistinct} + skipPtrs
   var x = a
   result = 0
   while x != nil:
-    x = skipTypes(x, skipPtrs)
+    x = skipTypes(x, skipPtrs + {tyDistinct})
     if sameObjectTypes(x, b): return
     x = x[0]
     inc(result)
   var y = b
   result = 0
   while y != nil:
-    y = skipTypes(y, skipPtrs)
+    y = skipTypes(y, skipPtrs + {tyDistinct})
     if sameObjectTypes(y, a): return
     y = y[0]
     dec(result)

Additional Information

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcompilerGeneral compiler tagcompiler/semRelated to semantic-analysis system of the compiler

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions