From 91e4381a20a5b1af0f633ee8c7d0255a1530d082 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Mon, 17 Apr 2023 23:08:53 +0800 Subject: [PATCH] fixes #20155; repr range with distinct types is broken in ORC (#21682) fixes #20155; repr range with distinct types is broken with ORC --- compiler/semmagic.nim | 5 +++++ lib/system/repr_v2.nim | 12 ++++++++++-- tests/metatype/tmetatype_various.nim | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/compiler/semmagic.nim b/compiler/semmagic.nim index b4c6cd27557c..e72b27d52cad 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 f6c720e2c3d9..206b5c5f5658 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 c17410a06900..be70f37a7d60 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]]''' """