Skip to content

Conversion to unsigned integers shall not produce range checks #175

@Araq

Description

@Araq

This is a followup of:

nim-lang/Nim#12554

There are two conflicting designs at play here:

  1. Nim's distinction between T(x) and cast[T](x) implies that runtime checks for T(x) are always anticipated and desired. This is what the spec for version 1 says and no special casing for unsigned is mentioned.

  2. Unsigned numbers are heavily used in low level contexts and diverging too much from C's behaviour is confusing and dangerous. Arithmetic operations on unsigned wrap around because of this requirement.

In the past Nim followed (2) but starting with version 1 Nim followed its own spec. This RFC proposes to change the spec to reflect what was true in previous Nim versions.

Note that either (1) or (2) is fine from a design standpoint IMO but the legacy code relies on (2) (including Nim's own library) and now the compiler inserts time ticking bombs in the form of secret runtime checks that can result in exceptions being raised without any warnings. This is bad enough to revert it to the old behavior and change the spec so that it matches Nim's historical behavior.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions