-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Use the size of numeric literals to inform the inferred numeric type #8337
Comments
Related to #4220, which is just about enforcing overflow on unsuffixed literals whose type is known rather than actually using the literal value to inform the type inference. |
There are going to be many overflow bugs in Rust code resulting from generic literals falling back to It's very odd to assume a fixed-size signed integer wrapping on overflow is desired as the default in most applications. I think 90% of use cases are going to want |
Triage: no progress, although #4220 is will be fixed by #10018. (FWIW, I mostly agree with @thestinger, we just need a fast bigint in the stdlib and real generic literals.) |
Now that Rust no longer infers literals, is this still relevant? |
I think it can be closed since the decision was to remove the fallback altogether. |
I've annotated the following program with its current output:
As shown by the last (commented-out) line, Rust is already smart enough to reject numeric literals whose values are larger than the maximum value that can be represented by their type, but only when their type is specified by a suffix:
foo
, it's perfectly willing to infer the default type ofint
, even when the value is too large to be contained in anint
.foo
, it's perfectly willing to infer a concrete type from a previously-given numeric suffix, even when that type is too small to hold the value of the later, un-suffixed literal.This might be naive of me, but it would be wonderful if these issues could be addressed somehow. The first issue would be complicated by the fact that
int
is a different size on different architectures, and that inferring the type toi64
would require assigning the same type to the prior literal. The second issue would require recognizing that1i8
and8_000_000_000
cannot be the same type without overflow, and would throw an error just as the8_000_000_000i8
line does today.The text was updated successfully, but these errors were encountered: