Reject casts to builtin typeclasses (#14788)
* Closes #14231, closes #14452 * Merge test to tcast.nim
This commit is contained in:
parent
3e5743d6f7
commit
80952cadaa
2 changed files with 10 additions and 1 deletions
|
|
@ -191,6 +191,8 @@ proc isCastable(conf: ConfigRef; dst, src: PType): bool =
|
||||||
return false
|
return false
|
||||||
if skipTypes(src, abstractInst-{tyTypeDesc}).kind == tyTypeDesc:
|
if skipTypes(src, abstractInst-{tyTypeDesc}).kind == tyTypeDesc:
|
||||||
return false
|
return false
|
||||||
|
if skipTypes(dst, abstractInst).kind == tyBuiltInTypeClass:
|
||||||
|
return false
|
||||||
if conf.selectedGC in {gcArc, gcOrc}:
|
if conf.selectedGC in {gcArc, gcOrc}:
|
||||||
let d = skipTypes(dst, abstractInst)
|
let d = skipTypes(dst, abstractInst)
|
||||||
let s = skipTypes(src, abstractInst)
|
let s = skipTypes(src, abstractInst)
|
||||||
|
|
|
||||||
|
|
@ -10,15 +10,22 @@ type MyProc3 = proc() #{.closure.} is implicit
|
||||||
|
|
||||||
proc testProc() = echo "Hello World"
|
proc testProc() = echo "Hello World"
|
||||||
|
|
||||||
|
template reject(x) = doAssert(not compiles(x))
|
||||||
|
|
||||||
proc callPointer(p: pointer) =
|
proc callPointer(p: pointer) =
|
||||||
# can cast to proc(){.cdecl.}
|
# can cast to proc(){.cdecl.}
|
||||||
let ffunc0 = cast[MyProc](p)
|
let ffunc0 = cast[MyProc](p)
|
||||||
# can cast to proc(){.nimcall.}
|
# can cast to proc(){.nimcall.}
|
||||||
let ffunc1 = cast[MyProc2](p)
|
let ffunc1 = cast[MyProc2](p)
|
||||||
# cannot cast to proc(){.closure.}
|
# cannot cast to proc(){.closure.}
|
||||||
doAssert(not compiles(cast[MyProc3](p)))
|
reject: cast[MyProc3](p)
|
||||||
|
|
||||||
ffunc0()
|
ffunc0()
|
||||||
ffunc1()
|
ffunc1()
|
||||||
|
|
||||||
callPointer(cast[pointer](testProc))
|
callPointer(cast[pointer](testProc))
|
||||||
|
|
||||||
|
reject: discard cast[enum](0)
|
||||||
|
proc a = echo "hi"
|
||||||
|
|
||||||
|
reject: discard cast[ptr](a)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue