Narrowing casts are applied after every op if needed (#8918)

This way we make sure not to end up with weird values every now and
then.

Fixes #7300
Fixes #8909
This commit is contained in:
LemonBoy 2018-09-08 19:41:07 +02:00 • committed by Andreas Rumpf
commit 7107ec05de
2 changed files with 85 additions and 31 deletions

View file

@ -4,6 +4,9 @@ B0
B1
B2
B3
B4
B5
B6
'''
"""
@ -14,6 +17,14 @@ template crossCheck(ty: untyped, exp: untyped) =
echo "Got ", ct
echo "Expected ", rt
template add1(x: uint8): untyped = x + 1
template add1(x: uint16): untyped = x + 1
template add1(x: uint32): untyped = x + 1
template sub1(x: uint8): untyped = x - 1
template sub1(x: uint16): untyped = x - 1
template sub1(x: uint32): untyped = x - 1
block:
when true:
echo "B0"
@ -34,10 +45,34 @@ block:
crossCheck(uint64, (-1).uint64 + 5'u64)
echo "B3"
crossCheck(int8, 0'u8 - 5'u8)
crossCheck(int16, 0'u16 - 5'u16)
crossCheck(int32, 0'u32 - 5'u32)
crossCheck(int64, 0'u64 - 5'u64)
doAssert $sub1(0'u8) == "255"
doAssert $sub1(0'u16) == "65535"
doAssert $sub1(0'u32) == "4294967295"
echo "B4"
doAssert $add1(255'u8) == "0"
doAssert $add1(65535'u16) == "0"
doAssert $add1(4294967295'u32) == "0"
echo "B5"
crossCheck(int32, high(int32))
crossCheck(int32, high(int32).int32)
crossCheck(int32, low(int32))
crossCheck(int32, low(int32).int32)
crossCheck(int64, high(int8).int16.int32.int64)
crossCheck(int64, low(int8).int16.int32.int64)
echo "B6"
crossCheck(int64, 0xFFFFFFFFFFFFFFFF'u64)
crossCheck(int32, 0xFFFFFFFFFFFFFFFF'u64)
crossCheck(int16, 0xFFFFFFFFFFFFFFFF'u64)
crossCheck(int8 , 0xFFFFFFFFFFFFFFFF'u64)
# Out of range conversion, caught for `let`s only
# crossCheck(int8, 0'u8 - 5'u8)
# crossCheck(int16, 0'u16 - 5'u16)
# crossCheck(int32, 0'u32 - 5'u32)
# crossCheck(int64, 0'u64 - 5'u64)
# crossCheck(int8, 0'u16 - 129'u16)
# crossCheck(uint8, 0'i16 + 257'i16)