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

Warn on implicit range downsizing #24706

Open
arnetheduck opened this issue Feb 21, 2025 · 0 comments
Open

Warn on implicit range downsizing #24706

arnetheduck opened this issue Feb 21, 2025 · 0 comments

Comments

@arnetheduck
Copy link
Contributor

arnetheduck commented Feb 21, 2025

Description

range is a type that currently is best avoided due to the large number of bugs, semantic gaps and inconsistencies it has with respect to the rest of the language.

Removing it is hard, but we can at least start plugging some of the gaps with warnings, starting with the most obvious one, where "non-overlapping" range conversion implicitly triggers runtime panics, a significant footgun compared to other similar conversions that have to be made explicit (ie assigning an int to an int8):

type FakeUint8 = range[0..255]
type OffByOneRange = range[1..256]
var v: FakeUint8
var x = 256
var y = OffByOneRange(256)

v = x # panics, should trigger warning
v = FakeUint8(x) # panics, should not trigger warning
v = y # panics should trigger warning

proc xxx(v: FakeUint8)= discard

xxx(x) # panics, should trigger warning
xxx(FakeUint8(x)) # panics, should not trigger warning

The above strategy would model range in line with other "integer size" conversions where converting to a "bigger" range is allowed implicitly while converting to a smaller range requires explicit action.

In this world, an int is obviously a special case of range[int.low..int.high], int16 is naturally a range[int16.low..int16.high] and so on, which would help guide a sound strategy for all integer sizes, now and in the future..

Nim Version

2.2

Current Output


Expected Output


Known Workarounds

No response

Additional Information

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant