@@ -10,20 +10,27 @@ or erroneous.
10
10
##### Integer overflow
11
11
12
12
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.
14
16
15
17
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.
19
26
20
27
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.
24
30
25
31
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.
27
34
28
35
See [ RFC 560] for error conditions, rationale, and more details about
29
36
integer overflow.
0 commit comments