From 6608fcc60f2a47983eac28d878f090522976a20a Mon Sep 17 00:00:00 2001 From: Clay Sweetser Date: Mon, 26 May 2014 10:12:46 -0400 Subject: [PATCH 1/3] Update 'isCastable' and 'semCast' doc-comments --- compiler/semexprs.nim | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 9ea93a15e9..b7b5b30cec 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -176,13 +176,16 @@ proc checkConvertible(c: PContext, castDest, src: PType): TConvStatus = else: discard -proc isCastable(dst, src: PType): bool = +proc isCastable(dst, src: PType): bool = + ## Checks whether the source type can be casted to the destination type. + ## Casting is very unrestrictive; casts are allowed as long as + ## castDest.size >= src.size, and typeAllowed(dst, skParam) #const # castableTypeKinds = {tyInt, tyPtr, tyRef, tyCstring, tyString, # tySequence, tyPointer, tyNil, tyOpenArray, # tyProc, tySet, tyEnum, tyBool, tyChar} var ds, ss: BiggestInt - # this is very unrestrictive; cast is allowed if castDest.size >= src.size + ds = computeSize(dst) ss = computeSize(src) if ds < 0: @@ -254,6 +257,7 @@ proc semConv(c: PContext, n: PNode): PNode = localError(n.info, errUseQualifier, op.sons[0].sym.name.s) proc semCast(c: PContext, n: PNode): PNode = + ## Semantically analyze a casting ("cast[type](param)") if optSafeCode in gGlobalOptions: localError(n.info, errCastNotInSafeMode) #incl(c.p.owner.flags, sfSideEffect) checkSonsLen(n, 2) From c4f77ecd9ace7ba131438f325138aeecbf4ae490 Mon Sep 17 00:00:00 2001 From: Clay Sweetser Date: Mon, 26 May 2014 10:13:29 -0400 Subject: [PATCH 2/3] Fix issue #1203 --- compiler/semexprs.nim | 2 ++ 1 file changed, 2 insertions(+) diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index b7b5b30cec..faba700ca2 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -192,6 +192,8 @@ proc isCastable(dst, src: PType): bool = result = false elif ss < 0: result = false + elif not typeAllowed(dst, skParam): + result = false else: result = (ds >= ss) or (skipTypes(dst, abstractInst).kind in IntegralTypes) or From 9d68fe378203f4cdd9edc412b8d0b0f6b422fb14 Mon Sep 17 00:00:00 2001 From: Clay Sweetser Date: Mon, 26 May 2014 10:16:59 -0400 Subject: [PATCH 3/3] Rename some variables in the isCastable code --- compiler/semexprs.nim | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index faba700ca2..5d4dc33bf3 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -184,18 +184,18 @@ proc isCastable(dst, src: PType): bool = # castableTypeKinds = {tyInt, tyPtr, tyRef, tyCstring, tyString, # tySequence, tyPointer, tyNil, tyOpenArray, # tyProc, tySet, tyEnum, tyBool, tyChar} - var ds, ss: BiggestInt + var dstSize, srcSize: BiggestInt - ds = computeSize(dst) - ss = computeSize(src) - if ds < 0: + dstSize = computeSize(dst) + srcSize = computeSize(src) + if dstSize < 0: result = false - elif ss < 0: + elif srcSize < 0: result = false elif not typeAllowed(dst, skParam): result = false else: - result = (ds >= ss) or + result = (dstSize >= srcSize) or (skipTypes(dst, abstractInst).kind in IntegralTypes) or (skipTypes(src, abstractInst-{tyTypeDesc}).kind in IntegralTypes)