summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorflywind <43030857+xflywind@users.noreply.github.com>2020-11-17 18:47:01 +0800
committerGitHub <noreply@github.com>2020-11-17 10:47:01 +0000
commitfec19c980e36e62747eb119f0740cb785e858ac3 (patch)
tree373e56a6d724f7a8ff7022d748edc2a20c317739
parent50d035b7896b85184fc6e97e22f32a189d76b81b (diff)
downloadNim-fec19c980e36e62747eb119f0740cb785e858ac3.tar.gz
follow #16009 VM supports cast nil to ptr (#16012)
* follow #16009 VM supports cast nil to ptr

* more testcase
-rw-r--r--compiler/vmgen.nim3
-rw-r--r--tests/vm/tcastnil.nim30
2 files changed, 33 insertions, 0 deletions
diff --git a/compiler/vmgen.nim b/compiler/vmgen.nim
index bcb7faa47..069b8cbf5 100644
--- a/compiler/vmgen.nim
+++ b/compiler/vmgen.nim
@@ -901,6 +901,9 @@ proc genCastIntFloat(c: PCtx; n: PNode; dest: var TDest) =
     c.gABx(n, opcSetType, dest, c.genType(dst))
     c.gABC(n, opcCastIntToPtr, dest, tmp)
     c.freeTemp(tmp)
+  elif src.kind == tyNil and dst.kind in PtrLikeKinds:
+    if dest < 0: dest = c.getTemp(n[0].typ)
+    genLit(c, n[1], dest)
   else:
     # todo: support cast from tyInt to tyRef
     globalError(c.config, n.info, "VM does not support 'cast' from " & $src.kind & " to " & $dst.kind)
diff --git a/tests/vm/tcastnil.nim b/tests/vm/tcastnil.nim
new file mode 100644
index 000000000..5a4684f95
--- /dev/null
+++ b/tests/vm/tcastnil.nim
@@ -0,0 +1,30 @@
+discard """
+  nimout: '''nil
+nil
+nil
+nil
+nil
+'''
+"""
+
+block:
+  static:
+    let a = cast[pointer](nil)
+    echo a.repr
+
+block:
+  static:
+    echo cast[ptr int](nil).repr
+
+block:
+  const str = cast[ptr int](nil)
+  static:
+    echo str.repr
+
+block:
+  static:
+    echo cast[ptr int](nil).repr
+
+block:
+  static:
+    echo cast[RootRef](nil).repr