Skip to content

Commit 630dd70

Browse files
committed
Clarify that overflow panics are not required to be precise,
and that panics may occur only on some branches.
1 parent e1af498 commit 630dd70

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

text/0000-integer-overflow.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,9 @@ an unspecified result, or task panic, depending on whether the
152152
overflow is checked.
153153

154154
The semantics of bitshifts are also changed. The number of bits to
155-
shift by must be within the interval
156-
`[0..N)` where `N` is the number of bits in the value being shifted, or else the result is panic/undefined, as above.
155+
shift by must be within the interval `0..N` (using Rust semantics)
156+
where `N` is the number of bits in the value being shifted, or else
157+
the result is panic/undefined, as above.
157158

158159
The implication is that overflow is considered to be an abnormal circumstance,
159160
a program error, and the programmer expects it not to happen, resp. it is her
@@ -172,20 +173,34 @@ Notes:
172173
however, this will most likely be the same as the wraparound result.
173174
Implementations should avoid needlessly exacerbating program errors with
174175
additional unpredictability or surprising behavior.
175-
176176
* Most importantly: this is **not** undefined behavior in the C sense. Only the
177177
result of the operation is left unspecified, as opposed to the entire
178178
program's meaning, as in C. The programmer would not be allowed to rely on a
179179
specific, or any, result being returned on overflow, but the compiler would
180180
also not be allowed to assume that overflow won't happen and optimize based
181181
on this assumption.
182+
* Even when checking for an overflow, compilers are not required to
183+
panic at the precise point of overflow. They are free to coalesce
184+
checks from adjacent operations, for example, or otherwise move the
185+
point of panic around. However, when checks are enabled (e.g.,
186+
during a debug build, by default), then any operation which
187+
overflows must result in a panic some finite number of steps after
188+
the operation that caused the overflow. (For example, the panic
189+
cannot be delayed until after a loop, unless the number of
190+
iterations in that loop can be statically bounded.)
191+
* When overflow checking is not explicitly enabled, the matter of
192+
whether an overflow results in panic or an undefined value is not
193+
required to be consistent across executions. This means that, for
194+
example, the compiler could insert code which results in an
195+
overflow only if the value that was produced is actually consumed
196+
(which may occur on only one arm of a branch).
182197

183198
To state it plainly: This is for the programmer's benefit, and not the
184199
optimizer's.
185200

186201
See also Appendix A.
187202

188-
### The default
203+
## The default
189204

190205
In general, implementations are always free to insert dynamic checks
191206
for overflow if they so choose. However, when running in debug mode

0 commit comments

Comments
 (0)