Skip to content

Commit

Permalink
Clarify what "undefined behavior" means in arithmetic.md (#2139)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
mgeisler authored Jun 10, 2024
1 parent 4c61caf commit c45b291
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/types-and-values/arithmetic.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c45b291

Please sign in to comment.