* fixes #13744 * improve style Co-authored-by: cooldome <ariabushenko@gmail.ru>
This commit is contained in:
parent
46c827be6a
commit
182d3c16e3
4 changed files with 44 additions and 1 deletions
|
|
@ -1941,6 +1941,8 @@ proc genSomeCast(p: BProc, e: PNode, d: var TLoc) =
|
||||||
elif optSeqDestructors in p.config.globalOptions and etyp.kind in {tySequence, tyString}:
|
elif optSeqDestructors in p.config.globalOptions and etyp.kind in {tySequence, tyString}:
|
||||||
putIntoDest(p, d, e, "(*($1*) (&$2))" %
|
putIntoDest(p, d, e, "(*($1*) (&$2))" %
|
||||||
[getTypeDesc(p.module, e.typ), rdCharLoc(a)], a.storage)
|
[getTypeDesc(p.module, e.typ), rdCharLoc(a)], a.storage)
|
||||||
|
elif etyp.kind == tyBool and srcTyp.kind in IntegralTypes:
|
||||||
|
putIntoDest(p, d, e, "(($1) != 0)" % [rdCharLoc(a)], a.storage)
|
||||||
else:
|
else:
|
||||||
putIntoDest(p, d, e, "(($1) ($2))" %
|
putIntoDest(p, d, e, "(($1) ($2))" %
|
||||||
[getTypeDesc(p.module, e.typ), rdCharLoc(a)], a.storage)
|
[getTypeDesc(p.module, e.typ), rdCharLoc(a)], a.storage)
|
||||||
|
|
|
||||||
|
|
@ -462,7 +462,7 @@ proc transformConv(c: PTransf, n: PNode): PNode =
|
||||||
var dest = skipTypes(n.typ, abstractVarRange)
|
var dest = skipTypes(n.typ, abstractVarRange)
|
||||||
var source = skipTypes(n[1].typ, abstractVarRange)
|
var source = skipTypes(n[1].typ, abstractVarRange)
|
||||||
case dest.kind
|
case dest.kind
|
||||||
of tyInt..tyInt64, tyEnum, tyChar, tyBool, tyUInt8..tyUInt32:
|
of tyInt..tyInt64, tyEnum, tyChar, tyUInt8..tyUInt32:
|
||||||
# we don't include uint and uint64 here as these are no ordinal types ;-)
|
# we don't include uint and uint64 here as these are no ordinal types ;-)
|
||||||
if not isOrdinalType(source):
|
if not isOrdinalType(source):
|
||||||
# float -> int conversions. ugh.
|
# float -> int conversions. ugh.
|
||||||
|
|
|
||||||
|
|
@ -463,6 +463,12 @@ proc opConv(c: PCtx; dest: var TFullReg, src: TFullReg, desttyp, srctyp: PType):
|
||||||
value = (value shl srcDist) shr srcDist
|
value = (value shl srcDist) shr srcDist
|
||||||
value = (value shl destDist) shr destDist
|
value = (value shl destDist) shr destDist
|
||||||
dest.intVal = cast[BiggestInt](value)
|
dest.intVal = cast[BiggestInt](value)
|
||||||
|
of tyBool:
|
||||||
|
dest.ensureKind(rkInt)
|
||||||
|
dest.intVal =
|
||||||
|
case skipTypes(srctyp, abstractRange).kind
|
||||||
|
of tyFloat..tyFloat64: int(src.floatVal != 0.0)
|
||||||
|
else: int(src.intVal != 0)
|
||||||
of tyFloat..tyFloat64:
|
of tyFloat..tyFloat64:
|
||||||
dest.ensureKind(rkFloat)
|
dest.ensureKind(rkFloat)
|
||||||
case skipTypes(srctyp, abstractRange).kind
|
case skipTypes(srctyp, abstractRange).kind
|
||||||
|
|
|
||||||
|
|
@ -19,3 +19,38 @@ proc remap2[T](s: seq[int], typ: typedesc[T]): seq[T] =
|
||||||
|
|
||||||
echo remap1(@[1,2,3], float)
|
echo remap1(@[1,2,3], float)
|
||||||
echo remap2(@[1,2,3], float)
|
echo remap2(@[1,2,3], float)
|
||||||
|
|
||||||
|
|
||||||
|
#--------------------------------------------------------------------
|
||||||
|
# conversion to bool, issue #13744
|
||||||
|
proc test_conv_to_bool =
|
||||||
|
var
|
||||||
|
i0 = 0
|
||||||
|
i1 = 1
|
||||||
|
ih = high(uint)
|
||||||
|
il = low(int)
|
||||||
|
|
||||||
|
f0 = 0.0
|
||||||
|
f1 = 1.0
|
||||||
|
fh = Inf
|
||||||
|
fl = -Inf
|
||||||
|
f_nan = NaN
|
||||||
|
|
||||||
|
doAssert(bool(i0) == false)
|
||||||
|
doAssert(bool(i1) == true)
|
||||||
|
doAssert(bool(-i1) == true)
|
||||||
|
doAssert(bool(il) == true)
|
||||||
|
doAssert(bool(ih) == true)
|
||||||
|
|
||||||
|
doAssert(bool(f0) == false)
|
||||||
|
doAssert(bool(-f0) == false)
|
||||||
|
doAssert(bool(f1) == true)
|
||||||
|
doAssert(bool(-f1) == true)
|
||||||
|
doAssert(bool(fh) == true)
|
||||||
|
doAssert(bool(fl) == true)
|
||||||
|
doAssert(bool(fnan) == true) # NaN to bool gives true according to standard
|
||||||
|
|
||||||
|
|
||||||
|
static:
|
||||||
|
test_conv_to_bool()
|
||||||
|
test_conv_to_bool()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue