Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #8404 JS backend doesn't handle float->int type conversion #15950

Merged
merged 8 commits into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions compiler/jsgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2208,11 +2208,17 @@ proc genConv(p: PProc, n: PNode, r: var TCompRes) =
if dest.kind == src.kind:
# no-op conversion
return
case dest.kind:
of tyBool:
let toInt = (dest.kind in tyInt..tyInt32)
let fromInt = (src.kind in tyInt..tyInt32)
let toUint = (dest.kind in tyUInt..tyUInt32)
let fromUint = (src.kind in tyUInt..tyUInt32)
if toUint and (fromInt or fromUint):
let trimmer = unsignedTrimmer(dest.size)
r.res = "($1 $2)" % [r.res, trimmer]
elif dest.kind == tyBool:
r.res = "(!!($1))" % [r.res]
r.kind = resExpr
of tyInt:
elif toInt:
r.res = "(($1)|0)" % [r.res]
else:
# TODO: What types must we handle here?
Expand Down
32 changes: 32 additions & 0 deletions tests/misc/t8404.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
discard """
targets: "c cpp js"
"""
template main() =
block: # bug #8404
# can conv
template float2int(T) =
var a = -1.0
let b = T(a)
doAssert b < 0
let c = b + 1
doAssert c is T
doAssert c == 0

float2int(int8)
float2int(int16)
float2int(int32)
float2int(int64)

block:
# can handle middle conv
# `/` can trigger int to float
template float2int(T) =
let n = T(1 / 256)
doAssert n == 0

float2int(int8)
float2int(int16)
float2int(int32)
# float2int(int64)
static:
bung87 marked this conversation as resolved.
Show resolved Hide resolved
main()