1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2024-12-01 02:42:20 +02:00

Clarify what "undefined behavior" means in arithmetic.md (#2139)

Undefined behavior does not just mean that the behavior can be different
on different platforms. It is much worse than that: the runtime behavior
can be completely nonsensical since the whole program has undefined
behavior.

The compiler will optimizer as if there is no undefined behavior and
will can lead to strange situations as the compiler concludes that
things like `a > a + 1000` can never be reached if `a` is a signed 8-bit
value (since reaching it would trigger undefined behavior).
This commit is contained in:
Martin Geisler 2024-06-10 16:18:25 +02:00 committed by GitHub
parent 4c61cafda8
commit c45b291851
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -23,8 +23,8 @@ be covered in more detail later.
Arithmetic is very similar to other languages, with similar precedence.
What about integer overflow? In C and C++ overflow of _signed_ integers is
actually undefined, and might do different things on different platforms or
compilers. In Rust, it's defined.
actually undefined, and might do unknown things at runtime. In Rust, it's
defined.
Change the `i32`'s to `i16` to see an integer overflow, which panics (checked)
in a debug build and wraps in a release build. There are other options, such as