remove structural equality check for objects and distinct types (#24448)

follows up #24425, fixes #18861, fixes #22445

Since #24425 generic object and distinct types now accurately link back
to their generic instantiations. To work around this issue previously,
type matching checked if generic objects/distinct types were
*structurally* equal, which caused false positives with types that
didn't use generic parameters in their structures. This structural check
is now removed, in cases where generic objects/distinct types require a
nontrivial equality check, the generic parameters of the `typeInst`
fields are checked for equality instead.

The check is copied from `tyGenericInst`, but the check in
`tyGenericInst` is not always sufficient as this type can be skipped or
unreachable in the case of `ref object`s.
This commit is contained in:
metagn
2024-11-18 19:37:47 +03:00
committed by GitHub
parent 712f5be7eb
commit a2031ec6cf
2 changed files with 56 additions and 14 deletions

View File

@@ -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()