Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
move to ast and try fix sameType, many issues though
  • Loading branch information
metagn committed Nov 7, 2024
commit 56e6d3b5dc8d1a325b8ed2f7ce0f2b4d3edcef99
14 changes: 14 additions & 0 deletions compiler/ast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1628,6 +1628,20 @@ proc skipTypesOrNil*(t: PType, kinds: TTypeKinds): PType =
if result.sons.len == 0: return nil
result = last(result)

proc skipStructuralGenerics*(t: PType, otherKinds: TTypeKinds = {}): PType =
## skips `otherKinds` and generic instantiations in `t`,
## given that the generic instantiations are not of direct
## object/enum/distinct types
# note: ref/ptr types are excluded (i.e. `type Foo[T] = ref object`)
# in practice this is not an issue since destructors are defined on
# direct `object` etc types, but in general the underlying `Foo:Obj`
# type will not have/use a corresponding `tyGenericInst`
result = t
while result.kind in otherKinds or
(result.kind == tyGenericInst and
result.skipModifier.kind notin {tyObject, tyEnum, tyDistinct}):
result = result.last

proc isGCedMem*(t: PType): bool {.inline.} =
result = t.kind in {tyString, tyRef, tySequence} or
t.kind == tyProc and t.callConv == ccClosure
Expand Down
30 changes: 7 additions & 23 deletions compiler/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1242,25 +1242,25 @@ proc sameTypeAux(x, y: PType, c: var TSameTypeClosure): bool =

if x == y: return true
let aliasSkipSet = maybeSkipRange({tyAlias})
var a = skipTypes(x, aliasSkipSet)
var a = skipStructuralGenerics(x, aliasSkipSet)
while a.kind == tyUserTypeClass and tfResolved in a.flags:
a = skipTypes(a.last, aliasSkipSet)
var b = skipTypes(y, aliasSkipSet)
a = skipStructuralGenerics(a.last, aliasSkipSet)
var b = skipStructuralGenerics(y, aliasSkipSet)
while b.kind == tyUserTypeClass and tfResolved in b.flags:
b = skipTypes(b.last, aliasSkipSet)
b = skipStructuralGenerics(b.last, aliasSkipSet)
assert(a != nil)
assert(b != nil)
case c.cmp
of dcEq:
if a.kind != b.kind: return false
of dcEqIgnoreDistinct:
let distinctSkipSet = maybeSkipRange({tyDistinct, tyGenericInst})
a = a.skipTypes(distinctSkipSet)
b = b.skipTypes(distinctSkipSet)
a = a.skipStructuralGenerics(distinctSkipSet)
b = b.skipStructuralGenerics(distinctSkipSet)
if a.kind != b.kind: return false
of dcEqOrDistinctOf:
let distinctSkipSet = maybeSkipRange({tyDistinct, tyGenericInst})
a = a.skipTypes(distinctSkipSet)
a = a.skipStructuralGenerics(distinctSkipSet)
if a.kind != b.kind: return false

#[
Expand Down Expand Up @@ -1998,19 +1998,3 @@ proc nominalRoot*(t: PType): PType =
# skips all typeclasses
# is this correct for `concept`?
result = nil

proc skipStructuralGenerics*(t: PType, otherKinds: TTypeKinds = {}): PType =
## skips `otherKinds` and generic instantiations in `t`,
## given that the generic instantiations are not of direct
## object/enum/distinct types
# note: ref/ptr types are excluded (i.e. `type Foo[T] = ref object`)
# in practice this is not an issue since destructors are defined on
# direct `object` etc types, but in general the underlying `Foo:Obj`
# type will not have/use a corresponding `tyGenericInst`
result = t
while result.kind in otherKinds or
(result.kind == tyGenericInst and
result.skipModifier.kind notin {tyObject, tyEnum, tyDistinct}):
result = result.last
if result.kind == tyGenericInst:
result = result.last.typeInst
Loading