Skip to content

Commit

Permalink
fixes #20155; repr range with distinct types is broken in ORC (#21682)
Browse files Browse the repository at this point in the history
fixes #20155; repr range with distinct types is broken with ORC
  • Loading branch information
ringabout authored Apr 17, 2023
1 parent 8c4b712 commit 91e4381
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
5 changes: 5 additions & 0 deletions compiler/semmagic.nim
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ proc evalTypeTrait(c: PContext; traitCall: PNode, operand: PType, context: PSym)
arg = arg.base.skipTypes(skippedTypes + {tyGenericInst})
if not rec: break
result = getTypeDescNode(c, arg, operand.owner, traitCall.info)
of "rangeBase":
# return the base type of a range type
var arg = operand.skipTypes({tyGenericInst})
assert arg.kind == tyRange
result = getTypeDescNode(c, arg.base, operand.owner, traitCall.info)
else:
localError(c.config, traitCall.info, "unknown trait: " & s)
result = newNodeI(nkEmpty, traitCall.info)
Expand Down
12 changes: 10 additions & 2 deletions lib/system/repr_v2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ proc isNamedTuple(T: typedesc): bool {.magic: "TypeTrait".}
proc distinctBase(T: typedesc, recursive: static bool = true): typedesc {.magic: "TypeTrait".}
## imported from typetraits

proc rangeBase(T: typedesc): typedesc {.magic: "TypeTrait".}
# skip one level of range; return the base type of a range type

proc repr*(x: NimNode): string {.magic: "Repr", noSideEffect.}

proc repr*(x: int): string =
Expand Down Expand Up @@ -95,8 +98,13 @@ proc repr*(p: proc): string =
## repr of a proc as its address
repr(cast[ptr pointer](unsafeAddr p)[])

template repr*(x: distinct): string =
repr(distinctBase(typeof(x))(x))
template repr*[T: distinct|range](x: T): string =
when T is range: # add a branch to handle range
repr(rangeBase(typeof(x))(x))
elif T is distinct:
repr(distinctBase(typeof(x))(x))
else:
{.error: "cannot happen".}

template repr*(t: typedesc): string = $t

Expand Down
2 changes: 1 addition & 1 deletion tests/metatype/tmetatype_various.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
discard """
matrix: "--mm:refc"
matrix: "--mm:refc; --mm:refc"
output: '''[1, 0, 0, 0, 0, 0, 0, 0] CTBool[Ct[system.uint32]]'''
"""

Expand Down

0 comments on commit 91e4381

Please sign in to comment.