summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorOscar NihlgÄrd <oscarnihlgard@gmail.com>2018-06-30 09:16:46 +0200
committerAndreas Rumpf <rumpf_a@web.de>2018-06-30 09:16:46 +0200
commitd146045ed55a45428991ece55e13afb742b2fdc4 (patch)
treeda972bf0a73307e85c7bc39c88d9a9a5719a87bf
parent0a14b3d1980e116bd40a98d5d2b414a0e1d05a1c (diff)
downloadNim-d146045ed55a45428991ece55e13afb742b2fdc4.tar.gz
Fixes #6689 (#8135)
-rw-r--r--compiler/vm.nim10
-rw-r--r--tests/vm/tvmmisc.nim17
2 files changed, 24 insertions, 3 deletions
diff --git a/compiler/vm.nim b/compiler/vm.nim
index c8e595f5d..9e5db978c 100644
--- a/compiler/vm.nim
+++ b/compiler/vm.nim
@@ -1211,8 +1211,14 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
     of opcIsNil:
       decodeB(rkInt)
       let node = regs[rb].node
-      regs[ra].intVal = ord(node.kind == nkNilLit or
-        (node.kind in {nkStrLit..nkTripleStrLit} and node.strVal.isNil))
+      regs[ra].intVal = ord(
+        # Note that `nfIsRef` + `nkNilLit` represents an allocated
+        # reference with the value `nil`, so `isNil` should be false!
+        (node.kind == nkNilLit and nfIsRef notin node.flags) or
+        (node.kind in {nkStrLit..nkTripleStrLit} and node.strVal.isNil) or
+        (not node.typ.isNil and node.typ.kind == tyProc and
+          node.typ.callConv == ccClosure and node.sons[0].kind == nkNilLit and
+          node.sons[1].kind == nkNilLit))
     of opcNBindSym:
       decodeBx(rkNode)
       regs[ra].node = copyTree(c.constants.sons[rbx])
diff --git a/tests/vm/tvmmisc.nim b/tests/vm/tvmmisc.nim
index 7e4af8b75..d82c2cf5e 100644
--- a/tests/vm/tvmmisc.nim
+++ b/tests/vm/tvmmisc.nim
@@ -91,6 +91,21 @@ block:
       result = size
     doAssert f(4) == 4
 
+# #6689
+block:
+  static:
+    proc foo(): int = 0
+    var f: proc(): int
+    doAssert f.isNil
+    f = foo
+    doAssert(not f.isNil)
+
+block:
+  static:
+    var x: ref ref int
+    new(x)
+    doAssert(not x.isNil)
+
 # #7871
 static:
   type Obj = object
@@ -119,4 +134,4 @@ static:
   o.names = ""
   o.pushName()
   o.pushName()
-  doAssert o.names == "FOOBARFOOBAR"
\ No newline at end of file
+  doAssert o.names == "FOOBARFOOBAR"