mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-20 06:20:38 +00:00
* 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
This commit is contained in:
committed by
Andreas Rumpf
parent
210955c3b6
commit
bbf9ef606d
23
tests/misc/tcast.nim
Normal file
23
tests/misc/tcast.nim
Normal file
@@ -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))
|
||||
Reference in New Issue
Block a user