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

RFC for removing integer inference fallback #115

Merged
merged 2 commits into from
Jun 18, 2014

Conversation

brson
Copy link
Contributor

@brson brson commented Jun 11, 2014

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.

ranges of unsigned ints may need to be type-hinted:

```
for _ in range(0u, 10) { }

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.

@alexcrichton
Copy link
Member

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,
Copy link
Contributor

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?

Copy link
Contributor Author

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.

@bstrie
Copy link
Contributor

bstrie commented Jun 12, 2014

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?

@thestinger
Copy link

@bstrie: Yeah, adding a fallback would be backwards compatible. In the case of floats, I think f64 is actually a sane default. Floats are insane to start with, so it doesn't really matter :P.

@pcwalton
Copy link
Contributor

@alexcrichton I believe @nikomatsakis has already gathered these statistics.

@brendanzab
Copy link
Member

I'm in favor of this. I have been bitten by it before too.

@Kimundi
Copy link
Member

Kimundi commented Jun 12, 2014

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`?

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.

Copy link
Contributor Author

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(...)].

@bstrie
Copy link
Contributor

bstrie commented Jun 12, 2014

@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. :)

@nikomatsakis
Copy link
Contributor

@alexcrichton I did gather such statistics in the original issue.

@alexcrichton
Copy link
Member

@nikomatsakis, @pcwalton ah of course, I forgot!

For those wondering, the analysis is here: https://gist.github.com/nikomatsakis/11179747

@alexcrichton alexcrichton merged commit c809a95 into rust-lang:master Jun 18, 2014
@alexcrichton
Copy link
Member

Merging per the discussion that @brson has linked to.

@alexchandel
Copy link

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 a to be: signed because I haven't specified a signedness; and native-word sized because I haven't specified a size. And similarly for let a = 1.0. The very fact that Rust has type inference suggests all of this to me as a user.

@thestinger
Copy link

Integer literals are generic, the type of x in let x = 5; is determined based on the usage of x. For example, in let x = 5; x += 2u32;, x is a u32. You should only use a pointer-size integer if it's actually what you need. You can't use a fixed-size integer without thinking about the bounds, so a pointer-size integer is a bad fallback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-inference Type inference related proposals & ideas A-typesystem Type system related proposals & ideas
Projects
None yet
Development

Successfully merging this pull request may close these issues.