summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorcooldome <cdome@bk.ru>2020-03-25 20:36:22 +0000
committerGitHub <noreply@github.com>2020-03-25 21:36:22 +0100
commit764a81ca253b78d0faa75a24dffc8d111362974f (patch)
tree1238759a7266ab5dcf5a11b074c28084b33d68e5
parente1e062197ec13da4516642270e2057b0c26248cb (diff)
downloadNim-764a81ca253b78d0faa75a24dffc8d111362974f.tar.gz
Continue bool conversion fixing (#13751)
* continue fixing #13744

* improve style

* improve test

Co-authored-by: cooldome <ariabushenko@bk.ru>
-rw-r--r--compiler/semexprs.nim4
-rw-r--r--compiler/semfold.nim9
-rw-r--r--tests/types/tcast1.nim9
3 files changed, 20 insertions, 2 deletions
diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim
index c678d3674..c2f117efa 100644
--- a/compiler/semexprs.nim
+++ b/compiler/semexprs.nim
@@ -147,7 +147,9 @@ proc checkConvertible(c: PContext, targetTyp: PType, src: PNode): TConvStatus =
     result = checkConversionBetweenObjects(d.skipTypes(abstractInst), s.skipTypes(abstractInst), pointers)
   elif (targetBaseTyp.kind in IntegralTypes) and
       (srcBaseTyp.kind in IntegralTypes):
-    if targetTyp.isOrdinalType:
+    if targetTyp.kind == tyBool:
+      discard "convOk"
+    elif targetTyp.isOrdinalType:
       if src.kind in nkCharLit..nkUInt64Lit and
           src.getInt notin firstOrd(c.config, targetTyp)..lastOrd(c.config, targetTyp):
         result = convNotInRange
diff --git a/compiler/semfold.nim b/compiler/semfold.nim
index a8d42fe7a..4131347a2 100644
--- a/compiler/semfold.nim
+++ b/compiler/semfold.nim
@@ -433,6 +433,15 @@ proc foldConv(n, a: PNode; g: ModuleGraph; check = false): PNode =
   #   echo high(int64)
   #   writeStackTrace()
   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:
     case srcTyp.kind
     of tyFloat..tyFloat64:
diff --git a/tests/types/tcast1.nim b/tests/types/tcast1.nim
index 04596baff..ab375b8b2 100644
--- a/tests/types/tcast1.nim
+++ b/tests/types/tcast1.nim
@@ -52,5 +52,12 @@ proc test_conv_to_bool =
 
 
 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()
\ No newline at end of file
+test_conv_to_bool()
+