summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorsolo989 <solo989@yahoo.com>2020-06-16 13:35:41 -0700
committerAndreas Rumpf <rumpf_a@web.de>2020-06-19 09:53:06 +0200
commit408518c9fed9757fd1cc246e3ced05ecbb3bcaab (patch)
tree6411af58c1b02a0f5732187c8ddefd105a520c6d
parent99ad65fdd616e2bc21a688acffb9f5f310b70066 (diff)
downloadNim-408518c9fed9757fd1cc246e3ced05ecbb3bcaab.tar.gz
Update tuple newLit
-rw-r--r--lib/core/macros.nim17
-rw-r--r--tests/macros/tmacros_various.nim32
2 files changed, 44 insertions, 5 deletions
diff --git a/lib/core/macros.nim b/lib/core/macros.nim
index 856c4f2c1..04a85d355 100644
--- a/lib/core/macros.nim
+++ b/lib/core/macros.nim
@@ -760,7 +760,7 @@ proc newLit*(arg: enum): NimNode {.compileTime.} =
 proc newLit*[N,T](arg: array[N,T]): NimNode {.compileTime.}
 proc newLit*[T](arg: seq[T]): NimNode {.compileTime.}
 proc newLit*[T](s: set[T]): NimNode {.compileTime.}
-proc newLit*(arg: tuple): NimNode {.compileTime.}
+proc newLit*[T: tuple](arg: T): NimNode {.compileTime.}
 
 proc newLit*(arg: object): NimNode {.compileTime.} =
   result = nnkObjConstr.newTree(arg.type.getTypeInst[1])
@@ -800,10 +800,17 @@ proc newLit*[T](s: set[T]): NimNode {.compileTime.} =
     var typ = getTypeInst(typeof(s))[1]
     result = newCall(typ,result)
 
-proc newLit*(arg: tuple): NimNode {.compileTime.} =
-  result = nnkPar.newTree
-  for a,b in arg.fieldPairs:
-    result.add nnkExprColonExpr.newTree(newIdentNode(a), newLit(b))
+proc isNamedTuple(T: typedesc): bool {.magic: "TypeTrait".}
+  ## See typetraits.isNamedTuple
+
+proc newLit*[T: tuple](arg: T): NimNode {.compileTime.} =
+  result = nnkTupleConstr.newTree
+  when isNamedTuple(T):
+    for a,b in arg.fieldPairs:
+      result.add nnkExprColonExpr.newTree(newIdentNode(a), newLit(b))
+  else:
+    for b in arg.fields:
+      result.add newLit(b)
 
 proc nestList*(op: NimNode; pack: NimNode): NimNode {.compileTime.} =
   ## Nests the list `pack` into a tree of call expressions:
diff --git a/tests/macros/tmacros_various.nim b/tests/macros/tmacros_various.nim
index 3991a5e30..695353b0c 100644
--- a/tests/macros/tmacros_various.nim
+++ b/tests/macros/tmacros_various.nim
@@ -194,3 +194,35 @@ static:
     doAssert(v == a)
 
   echo "macrocache ok"
+  
+block tupleNewLitTests:
+  macro t0(): untyped =
+    result = newLit(())
+  doAssert t0 == ()
+  macro t1(): untyped =
+    result = newLit((5,))
+  doAssert t1 == (5,)
+  macro t2(): untyped =
+    result = newLit((a: 5))
+  doAssert t2 == (a: 5)
+  macro t3(): untyped =
+    result = newLit((5, "5"))
+  doAssert t3 == (5, "5")
+  macro t4(): untyped =
+    result = newLit((a: 5, b: "5"))
+  doAssert t4 == (a: 5, b: "5")
+  macro t5(): untyped =
+    result = newLit(@[(5,)])
+  doAssert t5 == @[(5,)]
+  macro t6(): untyped =
+    result = newLit(@[(a: 5)])
+  doAssert t6 == @[(a: 5)]
+  macro t7(): untyped =
+    result = newLit(@[(5, "5")])
+  doAssert t7 == @[(5, "5")]
+  macro t8(): untyped =
+    result = newLit(@[(a: 5, b: "5")])
+  doAssert t8 == @[(a: 5, b: "5")]
+  macro t9(): untyped =
+    result = newLit(@[(a: (5, 6), b: ())])
+  doAssert t9 == @[(a: (5, 6), b: ())]