Skip to content

Commit

Permalink
fix logic for dcEqIgnoreDistinct in sameType
Browse files Browse the repository at this point in the history
fixes #22523
  • Loading branch information
metagn committed Sep 28, 2024
1 parent 4f5c0ef commit d9a6037
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
27 changes: 14 additions & 13 deletions compiler/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1250,26 +1250,27 @@ proc sameTypeAux(x, y: PType, c: var TSameTypeClosure): bool =
b = skipTypes(b.last, aliasSkipSet)
assert(a != nil)
assert(b != nil)
if a.kind != b.kind:
case c.cmp
of dcEq: return false
of dcEqIgnoreDistinct:
let distinctSkipSet = maybeSkipRange({tyDistinct, tyGenericInst})
a = a.skipTypes(distinctSkipSet)
b = b.skipTypes(distinctSkipSet)
if a.kind != b.kind: return false
of dcEqOrDistinctOf:
let distinctSkipSet = maybeSkipRange({tyDistinct, tyGenericInst})
a = a.skipTypes(distinctSkipSet)
if a.kind != b.kind: return false
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)
if a.kind != b.kind: return false
of dcEqOrDistinctOf:
let distinctSkipSet = maybeSkipRange({tyDistinct, tyGenericInst})
a = a.skipTypes(distinctSkipSet)
if a.kind != b.kind: return false

#[
The following code should not run in the case either side is an generic alias,
but it's not presently possible to distinguish the genericinsts from aliases of
objects ie `type A[T] = SomeObject`
]#
# this is required by tunique_type but makes no sense really:
if x.kind == tyGenericInst and IgnoreTupleFields notin c.flags and tyDistinct != y.kind:
if x.kind == tyGenericInst and IgnoreTupleFields notin c.flags and
c.cmp != dcEqIgnoreDistinct and tyDistinct != y.kind:
let
lhs = x.skipGenericAlias
rhs = y.skipGenericAlias
Expand Down
21 changes: 21 additions & 0 deletions tests/distinct/tcomplexaddressableconv.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# issue #22523

from std/typetraits import distinctBase

type
V[p: static int] = distinct int
D[p: static int] = distinct int
T = V[1]

proc f(y: var T) = discard

var a: D[0]

static:
doAssert distinctBase(T) is distinctBase(D[0])
doAssert distinctBase(T) is int
doAssert distinctBase(D[0]) is int
doAssert T(a) is T

f(cast[ptr T](addr a)[])
f(T(a))

0 comments on commit d9a6037

Please sign in to comment.