Skip to content

Commit 1fc5924

Browse files
committed
Rephrase and clarify integer overflow semantics
1 parent 25e919b commit 1fc5924

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/behavior-not-considered-unsafe.md

+15-8
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,27 @@ or erroneous.
1010
##### Integer overflow
1111

1212
If a program contains arithmetic overflow, the programmer has made an
13-
error.
13+
error. In the following discussion, we maintain a distinction between
14+
arithmetic overflow and wrapping arithmetic. The first is erroneous,
15+
while the second is intentional.
1416

1517
When the programmer has enabled `debug_assert!` assertions (for
16-
example, by enabling a non-optimized build), the compiler will insert
17-
dynamic checks that `panic` on overflow. Other kinds of builds may
18-
result in silently wrapped values on overflow.
18+
example, by enabling a non-optimized build), implementations must
19+
insert dynamic checks that `panic` on overflow. Other kinds of builds
20+
may result in `panics` or silently wrapped values on overflow, at the
21+
implementation's discretion.
22+
23+
In the case of implicitly-wrapped overflow, implementations must
24+
provide well-defined (even if still considered erroneous) results by
25+
using two's complement overflow conventions.
1926

2027
The integral types provide inherent methods to allow programmers
21-
explicitly to perform wrapping arithmetic. For example, (using UFCS)
22-
`i32::wrapping_add` provides two's complement, wrapping addition, as
23-
in `a + b` in the C programming language.
28+
explicitly to perform wrapping arithmetic. For example,
29+
`i32::wrapping_add` provides two's complement, wrapping addition.
2430

2531
The standard library also provides a `Wrapping<T>` newtype which
26-
overloads arithmetic operators by way of the `WrappingOps` trait.
32+
ensures all standard arithmetic operations for `T` have wrapping
33+
semantics.
2734

2835
See [RFC 560] for error conditions, rationale, and more details about
2936
integer overflow.

0 commit comments

Comments
 (0)