Skip to content
Prev Previous commit
Next Next commit
test slight refactor
  • Loading branch information
metagn committed Nov 10, 2024
commit 21f6b4d0c9d594082571274439586893bd53616b
12 changes: 6 additions & 6 deletions compiler/sigmatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -643,14 +643,14 @@ proc genericParamPut(c: var TCandidate; last, fGenericOrigin: PType) =
put(c, fGenericOrigin[i], last[i])

proc isGenericObjectOf(f, a: PType): bool =
## checks if `f` is an unparametrized generic type
## that `a` is an instance of
if not (f.sym != nil and f.sym.typ.kind == tyGenericBody):
# covers the case where `f` is the last child (body) of the `tyGenericBody`
return false
if a.typeInst != nil:
let aBaseSym = a.typeInst.genericHead.sym
result = aBaseSym != nil and (f.sym == aBaseSym)
else:
# assume a is generic
result = a.sym != nil and a.sym.typ.kind == tyGenericBody and f.sym == a.sym
let aRoot = genericRoot(a)
# use sym equality to check if the `tyGenericBody` types are equal
result = aRoot != nil and f.sym == aRoot.sym

proc isObjectSubtype(c: var TCandidate; a, f, fGenericOrigin: PType): int =
var t = a
Expand Down
17 changes: 17 additions & 0 deletions compiler/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2000,3 +2000,20 @@ proc nominalRoot*(t: PType): PType =
# skips all typeclasses
# is this correct for `concept`?
result = nil

proc genericRoot*(t: PType): PType =
## gets the root generic type (`tyGenericBody`) from `t`,
## if `t` is a generic type or the body of a generic instantiation
case t.kind
of tyGenericBody:
result = t
of tyGenericInst, tyGenericInvocation:
result = t.genericHead
else:
if t.typeInst != nil:
result = t.typeInst.genericHead
elif t.sym != nil and t.sym.typ.kind == tyGenericBody:
# can happen if `t` is the last child (body) of the generic body
result = t.sym.typ
else:
result = nil