-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
consider removing the fallback to int for integer inference #6023
Comments
I think this might be pretty annoying; e.g. println(1.to_str()); |
Nominated for backwards-compatible milestone, but I suggest WONTFIX |
I agree it would be annoying if you actually want Especially if we end up with generic integers like Haskell, this would make overflow checked integers and big integers as first-class as the built-ins (excluding constant expressions). |
accepted for backwards-compatible milestone |
Visiting for triage. |
cc #4704 |
I personally favor not guessing, or at least trying it |
Using unsigned values for such purposes can cause bugs, e.g. when a loop counts down past 0. This is why the Google C++ Style Guide http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml?showone=Integer_Types#Integer_Types says:
|
@1fish2 note that our for i in range(0u, 10).invert() { print!("{} ", i) } prints
|
Agreed (esp. about writing your own loops). Perhaps a better way to put it is doing unsigned math doesn't save you from thinking about negative values, it just underflows them to large values. Does range() always iterate downwards? I don't see any doc on that, e.g. http://static.rust-lang.org/doc/master/std/iter/fn.range.html |
@1fish2: he was missing the |
@1fish2: well, I think we can agree that a pointer-sized integer with wrapping on overflow is a bad default for high-level application code whether or not we agree on the value of unsigned integers at API boundaries :) |
I do agree The solution of adding an explicit type suffix 'u' or 'i' (with or without precision of size) is also good and acceptable in my view. Then, seems an assignment without type name nor suffix should be a compile error? |
@denispir we already have said type suffixes. I really want generic numeric literals. Neither int or uint are the right choice, usually, and when they are, type inference can take care of it (only need to specify the type once, somewhere). |
Nominating for closing. I don't see a lot of momentum to change this and it's a huge change. Does anybody think we can or will? |
I still think this is a language misfeature and will lead to real bugs. I expect that the process of adding In my opinion having generic integer literals is a mistake if they can unexpectedly fall back to |
I'd like to try Niko -------- Original message -------- Nominating for closing. I don't see a lot of momentum to change this and it's a huge change. Does anybody think we can or will? — |
Leaving on 1.0 milestone for now. Niko has been tasked with gathering statistics so that we can better understand the impact of making this change (or not doing so). |
Did some investigation: https://gist.github.com/nikomatsakis/11179747 It doesn't look like this would cause much trouble. |
As for range I think it's fine to have to write 0u or whatever. I usually On Tue, Apr 22, 2014 at 5:55 PM, Niko Matsakis notifications@github.comwrote:
|
The corresponding RFC for this issue has been approved: rust-lang/rfcs#115 |
This breaks a fair amount of code. The typical patterns are: * `for _ in range(0, 10)`: change to `for _ in range(0u, 10)`; * `println!("{}", 3)`: change to `println!("{}", 3i)`; * `[1, 2, 3].len()`: change to `[1i, 2, 3].len()`. RFC rust-lang#30. Closes rust-lang#6023. [breaking-change]
link to the Box docs in related lint documentation. changelog: link to the box docs in lint docs
I find it really easy to accidentally end up using
int
. It can be inferred asuint
based on a function call, but if you delete a line of code you're back toint
and possibly a bug. I don't think it's that much pain to tag one of them withi
.If #4169 is implemented, different strategies for overflow (expand to big integer, wrap, trap with a condition) can just be implemented as first-class library types. Haskell has a fallback to a big integer, but at least that's much less likely to cause a bug.
The text was updated successfully, but these errors were encountered: