nimskull icon indicating copy to clipboard operation
nimskull copied to clipboard

range checked integer types

Open saem opened this issue 1 year ago • 2 comments

Summary/Goal

Revisit the old behaviour and make things consistent again wrt range checked integer types, along with necessary supports (conversion/coercion and types).

Background

Previously, range checks were applied when converting between integer types, eg:

var a = -1
let b = cast[uint8](a) # reinterprets -1 as 255
let c = uint8(a)  # would fail the range check, as -1 is outside of 0-255

Currently, c would also be treated as 255. The distinction was that cast[uint8] would reinterpt the bits, and uint8 would apply a range checked type conversion from int -> uint8. The previous behaviour was more consistent, as we check ranges everywhere when doing type conversions. Getting this old behaviour was put behind the --legacy:checkUnsignedConversions command line switch.

The legacy flag and associated code is being removed (https://github.com/nim-works/nimskull/pull/575). The "legacy" approach is likely better, although simply enabling the behaviour isn't the correct thing to do. There are various operations missing to make this smooth (clamping conversions, etc) and possibly even types (unsafe/uninterpreted integer types).

References

For reference sake only this change was introduced in the following Nim commit, with linked Nim RFC. Again, this is for reference, we have different design goals.

saem avatar Mar 10 '23 22:03 saem