mirror of
https://github.com/nim-lang/Nim.git
synced 2026-08-29 01:51:43 +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
@@ -152,6 +152,8 @@ proc isCastable(dst, src: PType): bool =
|
||||
result = false
|
||||
elif typeAllowed(dst, skParam) != nil:
|
||||
result = false
|
||||
elif dst.kind == tyProc and dst.callConv == ccClosure:
|
||||
result = src.kind == tyProc and src.callConv == ccClosure
|
||||
else:
|
||||
result = (dstSize >= srcSize) or
|
||||
(skipTypes(dst, abstractInst).kind in IntegralTypes) or
|
||||
@@ -227,7 +229,10 @@ proc semCast(c: PContext, n: PNode): PNode =
|
||||
if tfHasMeta in targetType.flags:
|
||||
localError(n.sons[0].info, errCastToANonConcreteType, $targetType)
|
||||
if not isCastable(targetType, castedExpr.typ):
|
||||
localError(n.info, errExprCannotBeCastToX, $targetType)
|
||||
let tar = $targetType
|
||||
let alt = typeToString(targetType, preferDesc)
|
||||
let msg = if tar != alt: tar & "=" & alt else: tar
|
||||
localError(n.info, errExprCannotBeCastToX, msg)
|
||||
result = newNodeI(nkCast, n.info)
|
||||
result.typ = targetType
|
||||
addSon(result, copyTree(n.sons[0]))
|
||||
|
||||
Reference in New Issue
Block a user