diff --git a/compiler/types.nim b/compiler/types.nim index 66df653a2f..18f90b75ac 100644 --- a/compiler/types.nim +++ b/compiler/types.nim @@ -1306,26 +1306,34 @@ proc sameTypeAux(x, y: PType, c: var TSameTypeClosure): bool = cycleCheck() result = sameTypeAux(a.skipModifier, b.skipModifier, c) of tyObject: - withoutShallowFlags: + result = sameFlags(a, b) + if result: ifFastObjectTypeCheckFailed(a, b): cycleCheck() - if a.typeInst != nil and b.typeInst != nil: - # this is required because of `ref object`s, - # the value of their dereferences are not wrapped in `tyGenericInst`, - # so we need to check the generic parameters here - for ff, aa in underspecifiedPairs(a.typeInst, b.typeInst, 1, -1): - if not sameTypeAux(ff, aa, c): return false - # XXX should be removed in favor of above lines, - # structural equality is wrong in general: - result = sameObjectStructures(a, b, c) and sameFlags(a, b) + # should be generic, and belong to the same generic head type: + assert a.typeInst != nil, "generic object " & $a & " has no typeInst" + assert b.typeInst != nil, "generic object " & $b & " has no typeInst" + if result: + withoutShallowFlags: + # this is required because of generic `ref object`s, + # the value of their dereferences are not wrapped in `tyGenericInst`, + # so we need to check the generic parameters here + for ff, aa in underspecifiedPairs(a.typeInst, b.typeInst, 1, -1): + if not sameTypeAux(ff, aa, c): return false of tyDistinct: cycleCheck() if c.cmp == dcEq: - if sameFlags(a, b): + result = sameFlags(a, b) + if result: ifFastObjectTypeCheckFailed(a, b): - # XXX should be removed in favor of checking generic params, - # structural equality is wrong in general: - result = sameTypeAux(a.elementType, b.elementType, c) + # should be generic, and belong to the same generic head type: + assert a.typeInst != nil, "generic distinct type " & $a & " has no typeInst" + assert b.typeInst != nil, "generic distinct type " & $b & " has no typeInst" + withoutShallowFlags: + # just in case `tyGenericInst` was skipped at some point, + # we need to check the generic parameters here + for ff, aa in underspecifiedPairs(a.typeInst, b.typeInst, 1, -1): + if not sameTypeAux(ff, aa, c): return false else: result = sameTypeAux(a.elementType, b.elementType, c) and sameFlags(a, b) of tyEnum, tyForward: diff --git a/tests/objects/tgenericsubtype.nim b/tests/objects/tgenericsubtype.nim index c2618654c6..8c0596bb90 100644 --- a/tests/objects/tgenericsubtype.nim +++ b/tests/objects/tgenericsubtype.nim @@ -16,3 +16,37 @@ block: # no generic field proc foo(x: typedesc[Foo]) = discard foo(Bar) + +block: # issue #22445 + type + MyType = ref object of RootObj + MyChild[T] = ref object of MyType + + var curr = MyChild[int]() + doAssert not(curr of MyChild[float]) + doAssert curr of MyChild[int] + doAssert curr of MyType + +block: # issue #18861, original case + type + Connection = ref ConnectionObj + ConnectionObj = object of RootObj + + ConnectionRequest = ref ConnectionRequestObj + ConnectionRequestObj = object of RootObj + redis: Connection + + ConnectionStrBool = distinct bool + + ConnectionRequestT[T] = ref object of ConnectionRequest + + ConnectionSetRequest = ref object of ConnectionRequestT[ConnectionStrBool] + keepttl: bool + + proc execute(req: ConnectionRequest): bool = discard + proc execute(req: ConnectionRequestT[bool]): bool = discard + proc execute(req: ConnectionRequestT[ConnectionStrBool]): bool = discard + + proc testExecute() = + var connection: ConnectionSetRequest + let repl = connection.execute()