Skip to content

Commit 764a81c

Browse files
cooldomecooldome
andauthored
Continue bool conversion fixing (#13751)
* continue fixing #13744 * improve style * improve test Co-authored-by: cooldome <[email protected]>
1 parent e1e0621 commit 764a81c

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

compiler/semexprs.nim

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ proc checkConvertible(c: PContext, targetTyp: PType, src: PNode): TConvStatus =
147147
result = checkConversionBetweenObjects(d.skipTypes(abstractInst), s.skipTypes(abstractInst), pointers)
148148
elif (targetBaseTyp.kind in IntegralTypes) and
149149
(srcBaseTyp.kind in IntegralTypes):
150-
if targetTyp.isOrdinalType:
150+
if targetTyp.kind == tyBool:
151+
discard "convOk"
152+
elif targetTyp.isOrdinalType:
151153
if src.kind in nkCharLit..nkUInt64Lit and
152154
src.getInt notin firstOrd(c.config, targetTyp)..lastOrd(c.config, targetTyp):
153155
result = convNotInRange

compiler/semfold.nim

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,15 @@ proc foldConv(n, a: PNode; g: ModuleGraph; check = false): PNode =
433433
# echo high(int64)
434434
# writeStackTrace()
435435
case dstTyp.kind
436+
of tyBool:
437+
case srcTyp.kind
438+
of tyFloat..tyFloat64:
439+
result = newIntNodeT(int(getFloat(a) != 0.0), n, g)
440+
of tyChar, tyUInt..tyUInt64, tyInt..tyInt64:
441+
result = newIntNodeT(int(a.getOrdValue != 0), n, g)
442+
else:
443+
result = a
444+
result.typ = n.typ
436445
of tyInt..tyInt64, tyUInt..tyUInt64:
437446
case srcTyp.kind
438447
of tyFloat..tyFloat64:

tests/types/tcast1.nim

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,12 @@ proc test_conv_to_bool =
5252

5353

5454
static:
55+
doAssert(bool(0) == false)
56+
doAssert(bool(-1) == true)
57+
doAssert(bool(2) == true)
58+
doAssert(bool(NaN) == true)
59+
doAssert(bool(0.0) == false)
60+
doAssert(bool(-0.0) == false)
5561
test_conv_to_bool()
56-
test_conv_to_bool()
62+
test_conv_to_bool()
63+

0 commit comments

Comments
 (0)