summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorringabout <43030857+ringabout@users.noreply.github.com>2023-04-17 23:08:53 +0800
committerGitHub <noreply@github.com>2023-04-17 17:08:53 +0200
commit91e4381a20a5b1af0f633ee8c7d0255a1530d082 (patch)
tree4edf8387380cdea3bad55ed9932ff4084c4adb3c
parent8c4b7129b5b92b6cb56639c7fdf0669f7d13e542 (diff)
downloadNim-91e4381a20a5b1af0f633ee8c7d0255a1530d082.tar.gz
fixes #20155; repr range with distinct types is broken in ORC (#21682)
fixes #20155; repr range with distinct types is broken with ORC
-rw-r--r--compiler/semmagic.nim5
-rw-r--r--lib/system/repr_v2.nim12
-rw-r--r--tests/metatype/tmetatype_various.nim2
3 files changed, 16 insertions, 3 deletions
diff --git a/compiler/semmagic.nim b/compiler/semmagic.nim
index b4c6cd275..e72b27d52 100644
--- a/compiler/semmagic.nim
+++ b/compiler/semmagic.nim
@@ -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)
diff --git a/lib/system/repr_v2.nim b/lib/system/repr_v2.nim
index f6c720e2c..206b5c5f5 100644
--- a/lib/system/repr_v2.nim
+++ b/lib/system/repr_v2.nim
@@ -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 =
@@ -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
 
diff --git a/tests/metatype/tmetatype_various.nim b/tests/metatype/tmetatype_various.nim
index c17410a06..be70f37a7 100644
--- a/tests/metatype/tmetatype_various.nim
+++ b/tests/metatype/tmetatype_various.nim
@@ -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]]'''
 """