2 ms·
There is no practical difference. Rust either checks for overflow or assumes wrapping, depending on the compilation option "overflow-checks = false/true". The
by adrian_b 5y ago
There is no practical difference.
Rust either checks for overflow or assumes wrapping, depending on the compilation option "overflow-checks = false/true".
The semantics of the 2 behaviors are incompatible so any Rust program written assuming one of the 2 behaviors will fail in certain cases when it is compiled with the contrary option.
There is absolutely no difference in the C++ case, because the behavior is also controlled by the compilation options.
It does not matter what the language specifications say, when they are overridden by compilation options both for Rust and for C++.
The consequence is the same for both languages. You must write the program assuming a desired behavior, but you must also ensure that the program is compiled with the corresponding option, otherwise failures are likely.
- Dylan16807 5y agoThe failure cases for mistaken wrapping and mistaken undefined are very different. And "most programs are unsafe" vs. "all programs are safe" is decidedly not "absolutely no difference" just because the former could be reconfigured.
- adrian_b 5y agoI cannot understand your argument. For release builds, the default compilation options result in unsafe programs, both for Rust and for C++. So with default options, all Rust programs are unsafe and all C++ programs are unsafe. I do not see any difference whatsoever. It is true that there is one difference in the options required for safe programs. For Rust you have just one option, to enable overflow checks. For C++, you must give 2 different options, one to enable overflow checks and one to forbid the optimizations that assume that integer overflow cannot happen. The extra flexibility of the C++ compilers is actually useful, because there are cases when you are certain that overflow cannot happen and in that cases you want the compiler to do all the applicable optimizations. The right way to use such options is to always enable the safe options, which will ensure correct behavior for the program in all cases and to disable the checks and/or enable the unsafe optimizations only for the cases when it can be predicted for sure that overflows will not happen. The failure cases for mistaken wrapping and "mistaken undefined" (the latter meaning that the compiler is allowed to assume that overflow cannot happen) are not different. If an unexpected overflow happens, that is a certain failure in both cases. When your program is written to work with modular numbers a.k.a. wrapping numbers, that is a different case and in that case enabling overflow checks will break that program. Interval numbers and modular numbers are distinct types that cannot be interchanged in a program whenever interval overflow or modular reduction may happen.
- Dylan16807 5y agoWith default options, Rust programs overflow in a limited way most people call 'safe', and C++ programs that would overflow have undefined behavior which is very unsafe. > The failure cases for mistaken wrapping and "mistaken undefined" (the latter meaning that the compiler is allowed to assume that overflow cannot happen) are not different. They are extremely different. You still get a number with overflowing, and most of the time it's the number you want. If you expect wrapping, it will always be the number you want. Undefined behavior can be much more broken, and it will actively defy expectations. No language will force you to write correct code. Safety is related but not a synonym for correct.