summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authorParashurama <Rhagdamaziel@ymail.com>2017-06-07 08:52:50 +0200
committerAndreas Rumpf <rumpf_a@web.de>2017-06-07 08:52:50 +0200
commitbbf9ef606d08a6ae91fdf1efccd6f37aadc41237 (patch)
tree7a5721bd93d3b4c71a079a3810e06bd5c89cf867 /tests
parent210955c3b6bfbb1d27c426f78cd9315bab2dd0ec (diff)
downloadNim-bbf9ef606d08a6ae91fdf1efccd6f37aadc41237.tar.gz
restrict casting for closure. (#5948); fixes #5742
* restrict casting for closure.

This commit forbid casting a closure to anything other than another
closure. use rawEnv/rawProc to access underlaying pointers.

* better error message for closure cast
* fixes #5742
Diffstat (limited to 'tests')
-rw-r--r--tests/misc/tcast.nim23
1 files changed, 23 insertions, 0 deletions
diff --git a/tests/misc/tcast.nim b/tests/misc/tcast.nim
new file mode 100644
index 000000000..4e27040fb
--- /dev/null
+++ b/tests/misc/tcast.nim
@@ -0,0 +1,23 @@
+discard """
+  output: '''
+Hello World
+Hello World'''
+"""
+type MyProc = proc() {.cdecl.}
+type MyProc2 = proc() {.nimcall.}
+type MyProc3 = proc() #{.closure.} is implicit
+
+proc testProc()  = echo "Hello World"
+
+proc callPointer(p: pointer) =
+  # can cast to proc(){.cdecl.}
+  let ffunc0 = cast[MyProc](p)
+  # can cast to proc(){.nimcall.}
+  let ffunc1 = cast[MyProc2](p)
+  # cannot cast to proc(){.closure.}
+  doAssert(not compiles(cast[MyProc3](p)))
+
+  ffunc0()
+  ffunc1()
+
+callPointer(cast[pointer](testProc))