Continue bool conversion fixing (#13751)

* continue fixing #13744

* improve style

* improve test

Co-authored-by: cooldome <ariabushenko@bk.ru>
This commit is contained in:
cooldome 2020-03-25 20:36:22 +00:00 • committed by GitHub
commit 764a81ca25
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 2 deletions

View file

@ -147,7 +147,9 @@ proc checkConvertible(c: PContext, targetTyp: PType, src: PNode): TConvStatus =
result = checkConversionBetweenObjects(d.skipTypes(abstractInst), s.skipTypes(abstractInst), pointers) result = checkConversionBetweenObjects(d.skipTypes(abstractInst), s.skipTypes(abstractInst), pointers)
elif (targetBaseTyp.kind in IntegralTypes) and elif (targetBaseTyp.kind in IntegralTypes) and
(srcBaseTyp.kind in IntegralTypes): (srcBaseTyp.kind in IntegralTypes):
if targetTyp.isOrdinalType: if targetTyp.kind == tyBool:
discard "convOk"
elif targetTyp.isOrdinalType:
if src.kind in nkCharLit..nkUInt64Lit and if src.kind in nkCharLit..nkUInt64Lit and
src.getInt notin firstOrd(c.config, targetTyp)..lastOrd(c.config, targetTyp): src.getInt notin firstOrd(c.config, targetTyp)..lastOrd(c.config, targetTyp):
result = convNotInRange result = convNotInRange

View file

@ -433,6 +433,15 @@ proc foldConv(n, a: PNode; g: ModuleGraph; check = false): PNode =
# echo high(int64) # echo high(int64)
# writeStackTrace() # writeStackTrace()
case dstTyp.kind case dstTyp.kind
of tyBool:
case srcTyp.kind
of tyFloat..tyFloat64:
result = newIntNodeT(int(getFloat(a) != 0.0), n, g)
of tyChar, tyUInt..tyUInt64, tyInt..tyInt64:
result = newIntNodeT(int(a.getOrdValue != 0), n, g)
else:
result = a
result.typ = n.typ
of tyInt..tyInt64, tyUInt..tyUInt64: of tyInt..tyInt64, tyUInt..tyUInt64:
case srcTyp.kind case srcTyp.kind
of tyFloat..tyFloat64: of tyFloat..tyFloat64:

View file

@ -52,5 +52,12 @@ proc test_conv_to_bool =
static: static:
doAssert(bool(0) == false)
doAssert(bool(-1) == true)
doAssert(bool(2) == true)
doAssert(bool(NaN) == true)
doAssert(bool(0.0) == false)
doAssert(bool(-0.0) == false)
test_conv_to_bool() test_conv_to_bool()
test_conv_to_bool() test_conv_to_bool()