From bbf9ef606d08a6ae91fdf1efccd6f37aadc41237 Mon Sep 17 00:00:00 2001 From: Parashurama Date: Wed, 7 Jun 2017 08:52:50 +0200 Subject: 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 --- tests/misc/tcast.nim | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 tests/misc/tcast.nim (limited to 'tests') 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)) -- cgit 1.4.1-2-gfad0 ='/ahoang/Nim/refs/?h=devel&id=a9de33643b1503b87359bda0828beeea2ed0162e'>refs log blame commit diff stats
path: root/tests/test_nimscript.nims
blob: 2500bac73852680919bc21894362c2933c2fa29d (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16