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

Integral literals larger than i64::MAX are not detected as overflow #14269

Closed
lilyball opened this issue May 18, 2014 · 7 comments
Closed

Integral literals larger than i64::MAX are not detected as overflow #14269

lilyball opened this issue May 18, 2014 · 7 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.

Comments

@lilyball
Copy link
Contributor

The type_overflow lint does not catch integral literals larger than i64::MAX. I have to assume the problem is the overflow happens when generating the AST, and the lint operates on the AST, so it can't tell that it overflowed.

Example:

println!("{}", 9223372036854775808i64);
// prints -9223372036854775808
println!("{}", 18446744073709551615i64);
// prints -1

It does manage to catch 18446744073709551616, which is a parse error ("int literal is too large"). This is u64::MAX + 1, so the parser can definitively tell it's too large even though it doesn't know which integral type is being used.

@huonw huonw added the A-lint label May 18, 2014
@hirschenberger
Copy link
Contributor

Duplicate of #2252 ?

@lilyball
Copy link
Contributor Author

@hirschenberger #2252 appears to be a specific issue about float literals.

@hirschenberger
Copy link
Contributor

Right, but there #4220 was mentioned, which is closed but handeled a similar problem for ints.

@lilyball
Copy link
Contributor Author

@hirschenberger Look closer. #4220 was fixed by the type_overflow lint. This issue here is describing an issue that the type_overflow lint doesn't catch.

@hirschenberger
Copy link
Contributor

You're right, sorry.

@hirschenberger
Copy link
Contributor

I have patch which solves this issue, but a runtime test is failing now which tests i64 overflowing.

https://github.com/rust-lang/rust/blob/master/src/test/run-pass/big-literals.rs#L18-L19

whereas the i32 type works, which is inconsistent. What's the required behaviour, shall we fail with a parse error on all overflowing literals?

hirschenberger added a commit to hirschenberger/rust that referenced this issue Jul 24, 2014
The `type_overflow` lint, doesn't catch the overflow for `i64` because
the overflow happens earlier in the parse phase when the `u64` as biggest
possible int gets casted to `i64` , without checking the for overflows.
We can't lint in the parse phase, so a refactoring of the `LitInt` type
was necessary.

The types `LitInt`, `LitUint` and `LitIntUnsuffixed` where merged to one
type `LitInt` which stores it's value as `u64`. An additional parameter was
added which indicate the signedness of the type and the sign of the value.
hirschenberger added a commit to hirschenberger/rust that referenced this issue Aug 5, 2014
The `type_overflow` lint, doesn't catch the overflow for `i64` because
the overflow happens earlier in the parse phase when the `u64` as biggest
possible int gets casted to `i64` , without checking the for overflows.
We can't lint in the parse phase, so a refactoring of the `LitInt` type
was necessary.

The types `LitInt`, `LitUint` and `LitIntUnsuffixed` where merged to one
type `LitInt` which stores it's value as `u64`. An additional parameter was
added which indicate the signedness of the type and the sign of the value.
bors added a commit that referenced this issue Aug 5, 2014
Fixes missing overflow lint for i64 #14269

The `type_overflow` lint, doesn't catch the overflow for `i64` because the overflow happens earlier in the parse phase when the `u64` as biggest possible int gets casted to `i64` , without checking the for
overflows.
We can't lint in the parse phase, so we emit a compiler error, as we do for overflowing `u64`

Perhaps a consistent behaviour would be to emit a parse error for *all*  overflowing integer types.

See #14269
@hirschenberger
Copy link
Contributor

I think this can be closed, fixed with #15709 @kballard

@sfackler sfackler closed this as completed Aug 5, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.
Projects
None yet
Development

No branches or pull requests

4 participants