Skip to content

Computed types violate isa transitivity #25340

@yglukhov

Description

@yglukhov

Nim Version

Nim Compiler Version 2.3.1 [Linux: amd64]
Compiled at 2025-12-06
Copyright (c) 2006-2025 by Andreas Rumpf

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

Description

Playing with computed types I wanted to express smth like the following (pseudocode):

type Foo[T] = object of Foo[getParentType(T)]

However the resulting types seem to have broken isa relation. The following code shows a runtime problem, where A is B, B is C, but A is not C, which violates transitivity common sense.

import macros

type
  JSObj {.inheritable.} = object
  JSExternObjBase {.inheritable.} = object
  Document = object

macro parent(t: typedesc): untyped =
  let n = t.getType()[1]
  result = case $n
           of "JSObj": ident "JSExternObjBase"
           of "Document": ident "JSExternObjDocument"
           else: assert(false); nil
  echo "parent(", n, ") = ", repr result

type
  JSExternObj[T] = parent(T)
  JSExternObjDocument = object of JSExternObj[JSObj]

assert JSExternObj[Document] is JSExternObjDocument
assert JSExternObjDocument is JSExternObj[JSObj]
assert JSExternObj[Document] is JSExternObj[JSObj] # FAILS HERE

Current Output

Error: unhandled exception: .../test.nim(22, 1) `JSExternObj[Document] is JSExternObj[JSObj]`  [AssertionDefect]

Expected Output

No errors

Known Workarounds

No response

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions