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

0xFFFFFFFF convert to int32 result in 4294967295? #5251

Closed
khchen opened this issue Jan 19, 2017 · 10 comments
Closed

0xFFFFFFFF convert to int32 result in 4294967295? #5251

khchen opened this issue Jan 19, 2017 · 10 comments
Labels

Comments

@khchen
Copy link
Contributor

khchen commented Jan 19, 2017

import typetraits
proc `$`*[T](some:typedesc[T]): string = name(T)

proc test(x: int64) = echo x
const
  A = 0xFFFFFFFF'i32 #'
  B = 0xFFFFFFFF.int32

test(A)
test(B)

echo "type of B is ", B.type
echo "B > int32.high ? ", B > int32.high

Nim Compiler Version 0.16.0
output is the same on both 32/64 bit mode:
-1
4294967295
type of B is int32
B > int32.high ? true

@krux02
Copy link
Contributor

krux02 commented Jan 19, 2017

I think that A should raise a compile error, or at least a warning. 0xFFffFFff is in my opinion not a valid int32 literal. uint32 would be ok though.

B should behave like A currently does.

@Araq
Copy link
Member

Araq commented Jan 19, 2017

A should produce -1, that's what was asked for. B should produce a compiletime error.

@khchen
Copy link
Contributor Author

khchen commented Jan 19, 2017

I think A is ok, it use a hex format to represent a -1 value. The type must be int32 becasue I specify it.
B has the problem. It do a "int64 to int32" convert. However, the convert should raise a error.
(0xFFFFFFFF without suffix always generate a int64 type value on both 32/64 bit)

@khchen
Copy link
Contributor Author

khchen commented Jan 19, 2017

We answer the question at the same time, lol.

@krux02
Copy link
Contributor

krux02 commented Jan 20, 2017

I don't think B should produce a compile time error. When I call int32 on an int64 at runtime, I would also not expect a runtime error.

@khchen I recently learned how this is possible to happen. In the compiler all integers are stored as value of BiggestInt. This value is tagged with the explicit type, in this case int32, but that does not limit it to be out of the range of an int32. Operations are still on BiggestInt range. int32 does not wrap around at compile time in the 32bit range.

@khchen
Copy link
Contributor Author

khchen commented Jan 20, 2017

Here is a similar problem:

import typetraits

proc `$`*[T](some:typedesc[T]): string = name(T)

const
  A = 0xFFFFFFFE'u32 #'
  B = not A

echo A.type
echo B.type
echo B.int64
# echo B # Compile Error: cannot convert -4294967295 to uint64
echo cast[uint32](B)

output:
uint32
uint32
-4294967295
1

B is already uint32, however, I need use cast[uint32] to get the correct value.

@khchen
Copy link
Contributor Author

khchen commented Jan 20, 2017

Another type problem:

import typetraits
proc `$`*[T](some:typedesc[T]): string = name(T)
const
  A = -2147483648
  B = 2147483647
  C = -2147483648.int

echo A.type # int64, why not just int?
echo B.type # int, correct
echo C.type # int, correct

A literal should be a int type if it is possible.

@krux02
Copy link
Contributor

krux02 commented Jan 20, 2017

I think this bug should get a tag integer-literals-are-special. Integer literals are treated specially in Nim, and there are known problems with it.

@Araq Araq added the unsigned label Feb 2, 2017
@markus-oberhumer
Copy link
Contributor

See also issue #6829.

@Araq
Copy link
Member

Araq commented May 28, 2019

Now it fails to compile with "temp.nim(8, 17) Error: 4294967295 can't be converted to int32" which is good.

And the other example works as expected now.

@Araq Araq closed this as completed May 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants