@@ -152,8 +152,9 @@ an unspecified result, or task panic, depending on whether the
152
152
overflow is checked.
153
153
154
154
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.
157
158
158
159
The implication is that overflow is considered to be an abnormal circumstance,
159
160
a program error, and the programmer expects it not to happen, resp. it is her
@@ -172,20 +173,34 @@ Notes:
172
173
however, this will most likely be the same as the wraparound result.
173
174
Implementations should avoid needlessly exacerbating program errors with
174
175
additional unpredictability or surprising behavior.
175
-
176
176
* Most importantly: this is ** not** undefined behavior in the C sense. Only the
177
177
result of the operation is left unspecified, as opposed to the entire
178
178
program's meaning, as in C. The programmer would not be allowed to rely on a
179
179
specific, or any, result being returned on overflow, but the compiler would
180
180
also not be allowed to assume that overflow won't happen and optimize based
181
181
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).
182
197
183
198
To state it plainly: This is for the programmer's benefit, and not the
184
199
optimizer's.
185
200
186
201
See also Appendix A.
187
202
188
- ### The default
203
+ ## The default
189
204
190
205
In general, implementations are always free to insert dynamic checks
191
206
for overflow if they so choose. However, when running in debug mode
0 commit comments