From 573d02760ecf38082c5bae0bdc2b42e4c9f5e0d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Fri, 9 Nov 2018 12:15:00 +0100 Subject: [PATCH] newLit works on enum (#9662) * newLit works on enum * remove debugging echo --- lib/core/macros.nim | 10 ++++++++++ tests/macros/tnewlit.nim | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/core/macros.nim b/lib/core/macros.nim index a146117d0e..3011eaa673 100644 --- a/lib/core/macros.nim +++ b/lib/core/macros.nim @@ -696,6 +696,16 @@ proc newLit*[T](arg: seq[T]): NimNode {.compileTime.} = ), bracket ) +proc newLit*(arg: enum): NimNode {.compileTime.} = + result = newCall( + arg.type.getTypeInst[1], + newLit(int(arg)) + ) + +proc newLit*[T](s: set[T]): NimNode {.compileTime.} = + result = nnkCurly.newTree + for x in s: + result.add newLit(x) proc newLit*(arg: tuple): NimNode {.compileTime.} = result = nnkPar.newTree diff --git a/tests/macros/tnewlit.nim b/tests/macros/tnewlit.nim index 3ba1e09e19..194f035ba7 100644 --- a/tests/macros/tnewlit.nim +++ b/tests/macros/tnewlit.nim @@ -147,3 +147,23 @@ block: # x needs to be of type seq[string] var x = test_newLit_empty_seq_string x.add("xyz") + +type + MyEnum = enum + meA + meB + +macro test_newLit_Enum: untyped = + result = newLit(meA) + +block: + let tmp: MyEnum = meA + doAssert tmp == test_newLit_Enum + +macro test_newLit_set: untyped = + let myset = {MyEnum.low .. MyEnum.high} + result = newLit(myset) + +block: + let tmp: set[MyEnum] = {MyEnum.low .. MyEnum.high} + doAssert tmp == test_newLit_set