Nim icon indicating copy to clipboard operation
Nim copied to clipboard

range of uint64 shows signed upper bound

Open rotu opened this issue 3 years ago • 3 comments

What happened?

Nim reports a negative upper bound for a range with a uint64 outside the range of int64. This does not appear to happen for other integral types.

echo range[low(uint64) .. high(uint64)]

https://play.nim-lang.org/#ix=48Cp

Nim Version

playground 1.6.6

Current Standard Output Logs

range 0..-1(uint64)

Expected Standard Output Logs

range0..18446744073709551615(uint64)

Possible Solution

No response

Additional Information

No response

rotu avatar Aug 25 '22 17:08 rotu

I've done some tests, it only happen when print within range. I traced a while in compiler , no clue where cause it.

import std/parseutils

var a: BiggestUInt
discard parseBiggestUInt("18446744073709551615", a)
doAssert a == 0xFFFF_FFFF_FFFF_FFFF'u64
doAssert $(low(uint64) .. high(uint64)) == "0 .. 18446744073709551615"

echo $range[0'u64 .. 0xFFFF_FFFF_FFFF_FFFF'u64] # same
echo $range[low(uint64) .. high(uint64)] 

bung87 avatar Sep 14 '22 05:09 bung87

I also seem to get a similar issue when trying to convert between unsigned integer types.

uint32(uint64.high)

Note the "-1" in the below error message

stdin(5, 7) Error: conversion from uint64 to uint32 is invalid stdin(5, 7) Error: conversion from uint64 to uint32 is invalid stack trace: (most recent call last) stdin(5, 7) stdin(5, 7) Error: illegal conversion from '-1' to '[0..4294967295]' lineinfos.nim(297) raiseRecoverableError Error: unhandled exception: illegal conversion from '-1' to '[0..4294967295]' [ERecoverableError]

rotu avatar Sep 14 '22 06:09 rotu

yeah, I also notice the "-1", at first I thought it might be number parsing error in lexer.nim , so I do parseBiggestUInt for comfirmation. somewhere maybe do 0 - 1 operation cause this.

bung87 avatar Sep 14 '22 06:09 bung87

the PNode intVal is BiggestInt which defined as

type
  BiggestInt* = int64
  # simulate intVal -> ast.getInt
  # echo cast[uint64](cast[int64](18446744073709551615'u64))
  # Error: conversion from uint64 to BiggestInt is invalid
  # echo cast[uint64](BiggestInt(high(uint64)))

the number stored as int64, when use it should cast back to uint64

bung87 avatar Oct 29 '22 19:10 bung87