Skip to content

Commit

Permalink
Fix 14127 js from int to int casting (nim-lang#15918)
Browse files Browse the repository at this point in the history
* fix nim-lang#14127 from int to int casting

* add test for nim-lang#14127

* use template for test, also test uint2int

* move to tests/types/t14127_cast_number.nim targets:c cpp js
  • Loading branch information
bung87 authored and irdassis committed Feb 12, 2021
1 parent 1a4c968 commit 5e22477
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
3 changes: 1 addition & 2 deletions compiler/jsgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions tests/types/t14127_cast_number.nim
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 5e22477

Please sign in to comment.