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

Integer overflow checking #6012

Closed
catamorphism opened this issue Apr 22, 2013 · 8 comments
Closed

Integer overflow checking #6012

catamorphism opened this issue Apr 22, 2013 · 8 comments
Labels
A-codegen Area: Code generation E-hard Call for participation: Hard difficulty. Experience needed to fix: A lot.

Comments

@catamorphism
Copy link
Contributor

At various times, the issue of whether we should enable integer overflow checking (meaning that an integer overflow would be a hard failure at runtime rather than silently wrapping around) has come up; most recently, in this mailing list thread. A more specific example (which I'm about to close in favor of this more general bug) is #2470.

We should decide whether we want to address this form of safety in Rust's semantics.

@sanxiyn
Copy link
Member

sanxiyn commented Apr 23, 2013

I propose that we follow a prior work here, namely C# Language Specification, 4th edition, section 14.5.13.

14.5.13 The checked and unchecked operators

The checked and unchecked operators are used to control the overflow-checking context for integral-type arithmetic operations and conversions.

@sanxiyn
Copy link
Member

sanxiyn commented Apr 23, 2013

Note that LLVM has overflow intrinsics which compiles to overflow flag and carry flag (if things are aligned). Mono compiles C# checked operators to LLVM overflow intrinsics when it is using the LLVM backend (as a matter of fact, Mono added these to LLVM for their own use).

@bblum
Copy link
Contributor

bblum commented Jun 19, 2013

This came up at lunch today. I'm led to understand that LLVM has a "check if the last operation overflowed" operation, so we should have a corresponding way (primitive or library) to check for it. Sometimes programmers want their operations to overflow (hashing), and sometimes not (pointer arithmetic) --we should leave that decision to them.

@bstrie
Copy link
Contributor

bstrie commented Jul 12, 2013

Nominating for Well-Defined.

@emberian
Copy link
Member

emberian commented Aug 1, 2013

I don't think it should be part of the language, but a #[check_wrapping] attribute to enable runtime checks could be useful, at least for debugging.

@thestinger
Copy link
Contributor

I've implemented intrinsics + traits for overflow checked add, sub and mul operations in #8408.

For div, checking x / 0 and INT_MIN / -1 are already required to make the non-checked division avoid undefined behaviour, so the checked equivalent would just return an Option instead of failing. I've left that out for now.

@thestinger
Copy link
Contributor

There's now a CheckedDiv trait, so I consider the implementation of the overflow checking itself to be complete.

The built-in integer types are defined as wrapping, but it's now easy to implement an alternate type expanding to a big integer on overflow like the integers in Python. I don't think it makes sense to take any other path, because as long as the type is restricted to fixed-size you need to consider behaviour on overflow.

If there is a specific proposal, that can be opened as an issue. There have been many discussions about this with a consistent consensus that overflow checking is too high a cost to pay. The only full solution is an unbounded integer type, and that comes with the baggage of out-of-memory if you aren't checking inputs.

@thestinger
Copy link
Contributor

I opened #8635 as a replacement for this issue.

flip1995 pushed a commit to flip1995/rust that referenced this issue Oct 9, 2020
Downgrade interior_mutable_const lints to warn by default

This change updates the two lints in the file non_copy_const.rs to be warn by default rather than deny by default. It also updates the known problems for declare_interior_mutable_const to mention some issues that are affected by the lints.

This is a repeat pull request since I botched the first one (rust-lang#6012). Apart from my messing up the commits of that one, I also had a problem where the stderr of the tests didn't change despite me changing both lints to warn by default. Is this normal behaviour for some lints or do I need to adjust the tests to accommodate the change?

fixes rust-lang#5863
changelog: none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation E-hard Call for participation: Hard difficulty. Experience needed to fix: A lot.
Projects
None yet
Development

No branches or pull requests

6 participants