From 2c1f1f21bf93293940624898cfc37f2c3d42711b Mon Sep 17 00:00:00 2001 From: Araq Date: Sat, 10 Feb 2018 21:20:48 +0100 Subject: [PATCH] fixes #3985 --- compiler/semfold.nim | 7 ++++++- compiler/types.nim | 7 +++++-- tests/misc/tunsignedcmp.nim | 10 ++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/compiler/semfold.nim b/compiler/semfold.nim index 59c55010f..62bab4edb 100644 --- a/compiler/semfold.nim +++ b/compiler/semfold.nim @@ -375,7 +375,12 @@ proc getAppType(n: PNode): PNode = result = newStrNodeT("console", n) proc rangeCheck(n: PNode, value: BiggestInt) = - if value < firstOrd(n.typ) or value > lastOrd(n.typ): + var err = false + if n.typ.skipTypes({tyRange}).kind in {tyUInt..tyUInt64}: + err = value <% firstOrd(n.typ) or value >% lastOrd(n.typ, fixedUnsigned=true) + else: + err = value < firstOrd(n.typ) or value > lastOrd(n.typ) + if err: localError(n.info, errGenerated, "cannot convert " & $value & " to " & typeToString(n.typ)) diff --git a/compiler/types.nim b/compiler/types.nim index 10827f830..7878f87d7 100644 --- a/compiler/types.nim +++ b/compiler/types.nim @@ -621,7 +621,7 @@ proc firstOrd*(t: PType): BiggestInt = internalError("invalid kind for first(" & $t.kind & ')') result = 0 -proc lastOrd*(t: PType): BiggestInt = +proc lastOrd*(t: PType; fixedUnsigned = false): BiggestInt = case t.kind of tyBool: result = 1 of tyChar: result = 255 @@ -640,11 +640,14 @@ proc lastOrd*(t: PType): BiggestInt = of tyInt64: result = 0x7FFFFFFFFFFFFFFF'i64 of tyUInt: if platform.intSize == 4: result = 0xFFFFFFFF + elif fixedUnsigned: result = 0xFFFFFFFFFFFFFFFF'i64 else: result = 0x7FFFFFFFFFFFFFFF'i64 of tyUInt8: result = 0xFF of tyUInt16: result = 0xFFFF of tyUInt32: result = 0xFFFFFFFF - of tyUInt64: result = 0x7FFFFFFFFFFFFFFF'i64 + of tyUInt64: + if fixedUnsigned: result = 0xFFFFFFFFFFFFFFFF'i64 + else: result = 0x7FFFFFFFFFFFFFFF'i64 of tyEnum: assert(t.n.sons[sonsLen(t.n) - 1].kind == nkSym) result = t.n.sons[sonsLen(t.n) - 1].sym.position diff --git a/tests/misc/tunsignedcmp.nim b/tests/misc/tunsignedcmp.nim index 4fa4a7705..11b67ac5f 100644 --- a/tests/misc/tunsignedcmp.nim +++ b/tests/misc/tunsignedcmp.nim @@ -9,6 +9,8 @@ true 1 0 it should stop now +18446744073709551615 +4294967295 ''' """ @@ -31,3 +33,11 @@ for i in countdown(count, 0): if i == 0: echo "it should stop now" stop_me = true + +# bug #3985 +const + HIGHEST_64BIT_UINT = 0xFFFFFFFFFFFFFFFF'u + HIGHEST_32BIT_UINT = 0xFFFFFFFF'u + +echo($HIGHEST_64BIT_UINT) +echo($HIGHEST_32BIT_UINT)