-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
RFC for removing integer inference fallback #115
Conversation
ranges of unsigned ints may need to be type-hinted: | ||
|
||
``` | ||
for _ in range(0u, 10) { } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps default type parameters could be dual-purposed to provide a fallback type for this case (see rust-lang/rust#13702). I.e. the signature of range would become:
pub fn range<A: Add<A, A> + PartialOrd + Clone + One = uint>(start: A, stop: A) -> Range<A>
although it wasn't clear to me if the original issue was motivated by this very function defaulting to int too often.
It would be nice to have statistics on the amount of the changes required (ascriptions necessary) to code after these tweaks were implemented. This is a difficult statistic to gather, however, and I would think that it would fall out of the implementation regardless. |
|
||
# Drawbacks | ||
|
||
This will force users to cast somewhat more often. In particular, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you really mean "cast" here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I meant 'provide type hints'. Will change.
I think I'm for this. In ancient Rust this was a useful hack, but nowadays it's probably too magical. And if we decide that we want the old behavior again later, adding the integral fallback back in would be a backwards-compatible change, yeah? |
@bstrie: Yeah, adding a fallback would be backwards compatible. In the case of floats, I think |
@alexcrichton I believe @nikomatsakis has already gathered these statistics. |
I'm in favor of this. I have been bitten by it before too. |
I'm also in favor of this. |
|
||
Currently these default to `int`, but we need to change the | ||
behavior. Niko suggests just making discriminants always `int`, but | ||
how does that interact with `repr`? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't enum discriminants be the smallest type that allows to represent them all? (with preference for either signed or unsigned, not sure)
Or maybe we could introduce integer range types, and then it could be the smallest range covering them all, with no ambiguity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that is going to be the rule. Enums are the smallest type (probably not specced as such), unless using #[repr(...)]
.
@thestinger, I'm fine with the f64 fallback for floats because at least it's falling back to a type whose size doesn't vary per-platform. In the past I've floated the idea of changing our integral fallback from int to i32, but that may very well be a worse idea than just removing the fallback altogether. :) |
@alexcrichton I did gather such statistics in the original issue. |
@nikomatsakis, @pcwalton ah of course, I forgot! For those wondering, the analysis is here: https://gist.github.com/nikomatsakis/11179747 |
Merging per the discussion that @brson has linked to. |
Too bad I arrived late to the party. Feel free to show me the light, but this feels like a significant backwards step in the elegance of Rust. When I type "let a = 4", I expect |
Integer literals are generic, the type of |
Rust infers primitive integer types, but when it can't find a solution it currently defaults to
int
. This is often seen as a potential subtle footgun. This RFC describes how to remove the feature, per rust-lang/rust#6023.