summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBung <crc32@qq.com>2020-11-12 01:41:49 +0800
committerGitHub <noreply@github.com>2020-11-11 17:41:49 +0000
commita81434a890d70e7146c440c59caadc701c6099b3 (patch)
treed471d54ad917f5ca01f16a3a369403e2bed4cacc
parentf02c7542fcd21d4b3d7b82908325512be4e5404a (diff)
downloadNim-a81434a890d70e7146c440c59caadc701c6099b3.tar.gz
Fix 14127 js from int to int casting (#15918)
* fix #14127 from int to int casting

* add test for #14127

* use template for test, also test uint2int

* move to tests/types/t14127_cast_number.nim targets:c cpp js
-rw-r--r--compiler/jsgen.nim3
-rw-r--r--tests/types/t14127_cast_number.nim30
2 files changed, 31 insertions, 2 deletions
diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim
index d124ae5fe..87021de11 100644
--- a/compiler/jsgen.nim
+++ b/compiler/jsgen.nim
@@ -2406,8 +2406,7 @@ proc genCast(p: PProc, n: PNode, r: var TCompRes) =
     r.res = "($1 $2)" % [r.res, trimmer]
   elif toInt:
     if fromInt:
-      let trimmer = unsignedTrimmer(dest.size)
-      r.res = "($1 $2)" % [r.res, trimmer]
+      return
     elif fromUint:
       if src.size == 4 and dest.size == 4:
         # XXX prevent multi evaluations
diff --git a/tests/types/t14127_cast_number.nim b/tests/types/t14127_cast_number.nim
new file mode 100644
index 000000000..4bb23f22f
--- /dev/null
+++ b/tests/types/t14127_cast_number.nim
@@ -0,0 +1,30 @@
+discard """
+  targets: "c cpp js"
+"""
+block: # bug #14127
+  template int2uint(T) =
+    var a = -1
+    let b = cast[T](a)
+    doAssert b < 0
+    let c = b + 1
+    doAssert c is T
+    doAssert c == 0
+
+  int2uint(int8)
+  int2uint(int16)
+  int2uint(int32)
+  int2uint(int64)
+
+block: # maybe related
+  template uint2int(T) =
+    var a = 3
+    let b = cast[T](a)
+    doAssert b > 0
+    let c = b - 1
+    doAssert c is T
+    doAssert c == 2
+
+  uint2int(uint8)
+  uint2int(uint16)
+  uint2int(uint32)
+  uint2int(uint64)