summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--lib/core/macros.nim10
-rw-r--r--tests/macros/tnewlit.nim20
2 files changed, 30 insertions, 0 deletions
diff --git a/lib/core/macros.nim b/lib/core/macros.nim
index a146117d0..3011eaa67 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 3ba1e09e1..194f035ba 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