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

Inconsistent unsigned -> signed RangeDefect usage across integer sizes #15210

Closed
tersec opened this issue Aug 21, 2020 · 0 comments
Closed

Inconsistent unsigned -> signed RangeDefect usage across integer sizes #15210

tersec opened this issue Aug 21, 2020 · 0 comments

Comments

@tersec
Copy link
Contributor

tersec commented Aug 21, 2020

Converting uint8, uint16, and uint32 to, respectively, int8, int16, and int32 values out of range of the target type results in a RangeError, but converting a uint64 outside of signed 64-bit range to int64 behaves differently, apparently reinterpreting the bit pattern in place, with no error. This seems a less-than-optimal level of internal consistency.

This comes up in status-im/nimbus-eth2#1544.

Example

For uint8/int8:

let a = not 0'u8
echo a
echo a.int8

For uint16/int16:

let a = not 0'u16
echo a
echo a.int16

For uint32/int32:

let a = not 0'u32
echo a
echo a.int32

For uint64/int64:

let a = not 0'u64
echo a
echo a.int64

Current Output

$ ./nim --version
Nim Compiler Version 1.3.5 [Linux: amd64]
Compiled at 2020-08-21
Copyright (c) 2006-2020 by Andreas Rumpf

git hash: 8a004e2fc07c87b8308c3c03e4448372a2094383
active boot switches: -d:release
$ ./test_u8 
255
Nim/bin/test_u8.nim(3) test_u8
Nim/lib/system/fatal.nim(49) sysFatal
Error: unhandled exception: value out of range: 255 notin -128 .. 127 [RangeDefect]
$ ./test_u16
65535
Nim/bin/test_u16.nim(3) test_u16
Nim/lib/system/fatal.nim(49) sysFatal
Error: unhandled exception: value out of range: 65535 notin -32768 .. 32767 [RangeDefect]
$ ./test_u32
4294967295
Nim/bin/test_u32.nim(3) test_u32
Nim/lib/system/fatal.nim(49) sysFatal
Error: unhandled exception: value out of range: 4294967295 notin -2147483648 .. 2147483647 [RangeDefect]
$ ./test_u64
18446744073709551615
-1

Expected Output

One path to consistency would probably be:

$ ./test_u64
18446744073709551615
Nim/bin/test_u64.nim(3) test_u64
Nim/lib/system/fatal.nim(49) sysFatal
Error: unhandled exception: value out of range: 18446744073709551615 notin -9223372036854775808 .. 9223372036854775807 [RangeDefect]
-1

Additional Information

I tested this on Nim 1.2.6:

Nim Compiler Version 1.2.6 [Linux: amd64]
Compiled at 2020-09-07
Copyright (c) 2006-2020 by Andreas Rumpf

active boot switches: -d:release

With identical results except that RangeDefect was spelled RangeError.

For Nim 1.0.4:

Nim Compiler Version 1.0.4 [Linux: amd64]
Compiled at 2020-02-04
Copyright (c) 2006-2019 by Andreas Rumpf

active boot switches: -d:release

It was the same as 1.2.6, except that the RangeError messages did not display the allowed range of values for each type.

Nim 0.19.4:

$ nim --version
Nim Compiler Version 0.19.4 [Linux: amd64]
Compiled at 2019-03-05
Copyright (c) 2006-2018 by Andreas Rumpf

active boot switches: -d:release

Behaves exactly the same for these examples as Nim 1.0.4.

#12554 is related, but different because it focuses on narrowing unsigned -> unsigned integer conversions. There are still non-overlapping ranges here, but neither type's range is a superset of the other, and no one behavior exists across all the supported integer sizes. Whatever the desired behavior, it should probably be the same across, at least, 32 and 64-bit integers.

Araq added a commit that referenced this issue Aug 28, 2020
@Araq Araq closed this as completed in ff13f8c Aug 31, 2020
narimiran pushed a commit that referenced this issue Sep 11, 2020
* fixes #15210 [backport:1.2]

* use patched version of bigints library

(cherry picked from commit ff13f8c)
mildred pushed a commit to mildred/Nim that referenced this issue Jan 11, 2021
* fixes nim-lang#15210 [backport:1.2]

* use patched version of bigints library
Araq added a commit that referenced this issue Feb 24, 2021
* fixes #15210 [backport:1.2]

* make tests green
* make ordinal work
* makes Swapable test compile
* make Indexable example work
* concepts: 'self' is now 'Self'
* concepts: make Dictionary example compile
* document the new concept implementation
* concepts: make typeDesc work properly
* concepts: allow documentation comments (d'oh)
ringabout pushed a commit to ringabout/Nim that referenced this issue Mar 22, 2021
* fixes nim-lang#15210 [backport:1.2]

* make tests green
* make ordinal work
* makes Swapable test compile
* make Indexable example work
* concepts: 'self' is now 'Self'
* concepts: make Dictionary example compile
* document the new concept implementation
* concepts: make typeDesc work properly
* concepts: allow documentation comments (d'oh)
narimiran added a commit that referenced this issue Mar 22, 2021
ardek66 pushed a commit to ardek66/Nim that referenced this issue Mar 26, 2021
* fixes nim-lang#15210 [backport:1.2]

* make tests green
* make ordinal work
* makes Swapable test compile
* make Indexable example work
* concepts: 'self' is now 'Self'
* concepts: make Dictionary example compile
* document the new concept implementation
* concepts: make typeDesc work properly
* concepts: allow documentation comments (d'oh)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants