Skip to content

Commit 8dad941

Browse files
committed
RFC for removing integer inference fallback
1 parent 1ea0ce9 commit 8dad941

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

active/0000-rm-integer-fallback.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
- Start Date: 2014-06-11
2+
- RFC PR #: (leave this empty)
3+
- Rust Issue #: 6023
4+
5+
# Summary
6+
7+
Currently we use inference to find the current type of
8+
otherwise-unannotated integer literals, and when that fails the type
9+
defaults to `int`. This is often felt to be potentially error-prone
10+
behavior.
11+
12+
This proposal removes the integer inference fallback and strengthens
13+
the types required for several language features that interact with
14+
integer inference.
15+
16+
# Motivation
17+
18+
With the integer fallback, small changes to code can change the
19+
inferred type in unexpected ways. It's not clear how big a problem
20+
this is, but previous experiments[1] indicate that removing
21+
the fallback has a relatively small impact on existing code,
22+
so it's reasonable to back off of this feature in favor of more
23+
strict typing.
24+
25+
See also https://github.com/mozilla/rust/issues/6023.
26+
27+
[1]: https://gist.github.com/nikomatsakis/11179747
28+
29+
# Detailed design
30+
31+
The primary change here is that, when integer type inference fails,
32+
the compiler will emit an error instead of assigning the value the
33+
type `int`.
34+
35+
This change alone will cause a fair bit of existing code to be
36+
unable to type check because of lack of constraints. To add more
37+
constraints and increase likelihood of unification, we 'tighten'
38+
up what kinds of integers are required in some situations:
39+
40+
* Array repeat counts must be uint (`[expr, .. count]`)
41+
* << and >> require uint when shifting integral types
42+
43+
Finally, inference for `as` will be modified to track the types
44+
a value is being cast *to* for cases where the value being cast
45+
is unconstrained, like `0 as u8`.
46+
47+
# Drawbacks
48+
49+
This will force users to cast somewhat more often. In particular,
50+
ranges of unsigned ints may need to be type-hinted:
51+
52+
```
53+
for _ in range(0u, 10) { }
54+
```
55+
56+
# Alternatives
57+
58+
Do none of this.
59+
60+
# Unresolved questions
61+
62+
* If we're putting new restrictions on shift operators, should we
63+
change the traits, or just make the primitives special?
64+
65+
There is some question about how to treat enum discriminants:
66+
67+
```
68+
enum Color { Red = 0, Green = 1, Blue = 2 }
69+
```
70+
71+
Currently these default to `int`, but we need to change the
72+
behavior. Niko suggests just making discriminants always `int`, but
73+
how does that interact with `repr`?

0 commit comments

Comments
 (0)