summary refs log tree commit diff stats
path: root/tests/vm
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2020-10-23 23:16:47 +0200
committerGitHub <noreply@github.com>2020-10-23 23:16:47 +0200
commitbf894ad3ebdd393a13b0ff69f5c7a221270639a7 (patch)
treee91979dd19d14f9a722e202dc0d744f3bb654570 /tests/vm
parentae320b4e7dfd07ed6ae85a2693da3bb8568520c6 (diff)
downloadNim-bf894ad3ebdd393a13b0ff69f5c7a221270639a7.tar.gz
close #8007 (#15695)
Diffstat (limited to 'tests/vm')
-rw-r--r--tests/vm/tvmmisc.nim51
1 files changed, 51 insertions, 0 deletions
diff --git a/tests/vm/tvmmisc.nim b/tests/vm/tvmmisc.nim
index 73faac495..61a187faf 100644
--- a/tests/vm/tvmmisc.nim
+++ b/tests/vm/tvmmisc.nim
@@ -230,3 +230,54 @@ block:
   doAssert d == @[]
   doAssert e == @[]
   doAssert f == @[]
+
+import tables
+
+block: # bug #8007
+  type
+    CostKind = enum
+      Fixed,
+      Dynamic
+
+    Cost = object
+      case kind*: CostKind
+      of Fixed:
+        cost*: int
+      of Dynamic:
+        handler*: proc(value: int): int {.nimcall.}
+
+  proc foo(value: int): int {.nimcall.} =
+    sizeof(value)
+
+  const a: array[2, Cost] =[
+    Cost(kind: Fixed, cost: 999),
+    Cost(kind: Dynamic, handler: foo)
+  ]
+
+  # OK with arrays & object variants
+  doAssert $a == "[(kind: Fixed, cost: 999), (kind: Dynamic, handler: ...)]"
+
+  const b: Table[int, Cost] = {
+    0: Cost(kind: Fixed, cost: 999),
+    1: Cost(kind: Dynamic, handler: foo)
+  }.toTable
+
+  # KO with Tables & object variants
+  # echo b # {0: (kind: Fixed, cost: 0), 1: (kind: Dynamic, handler: ...)} # <----- wrong behaviour
+  doAssert $b == "{0: (kind: Fixed, cost: 999), 1: (kind: Dynamic, handler: ...)}"
+
+  const c: Table[int, int] = {
+    0: 100,
+    1: 999
+  }.toTable
+
+  # OK with Tables and primitive int
+  doAssert $c == "{0: 100, 1: 999}"
+
+  # For some reason the following gives
+  #    Error: invalid type for const: Cost
+  const d0 = Cost(kind: Fixed, cost: 999)
+
+  # OK with seq & object variants
+  const d = @[Cost(kind: Fixed, cost: 999), Cost(kind: Dynamic, handler: foo)]
+  doAssert $d == "@[(kind: Fixed, cost: 999), (kind: Dynamic, handler: ...)]"