Skip to content

Commit 865eae6

Browse files
committed
Rephrase the *optional* mode so it is deterministic; but make it clear
that not having checks enabled is not the same as having them explicitly *disabled*.
1 parent 9681e60 commit 865eae6

File tree

1 file changed

+34
-37
lines changed

1 file changed

+34
-37
lines changed

text/0000-integer-overflow.md

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ The RFC has several major goals:
4848
To that end the RFC proposes two mechanisms:
4949

5050
1. Optional, dynamic overflow checking. Ordinary arithmetic operations
51-
(e.g., `a+b`) would *optionally* check for overflow. If checking is
52-
enabled, and an overflow occurs, a thread panic will be
51+
(e.g., `a+b`) would conditionally check for overflow. If an
52+
overflow occurs when checking is enabled, a thread panic will be
5353
signaled. Specific intrinsics and library support are provided to
5454
permit either explicit overflow checks or explicit wrapping.
5555
2. Overflow checking would be, by default, tied to debug assertions
56-
(`debug_assert!`). It can be seen as analogous to a debug
56+
(`debug_assert!`). It can be seen as analogous to a debug
5757
assertion: an important safety check that is too expensive to
5858
perform on all code.
5959

@@ -99,42 +99,40 @@ a static/dynamic analysis tools as an error. This is largerly a
9999
semantic distinction, though.
100100

101101
The result of an error condition depends upon the state of overflow
102-
checking, which can be either *enabled* or *optional* (this RFC does
102+
checking, which can be either *enabled* or *default* (this RFC does
103103
not describe a way to disable overflow checking completely). If
104104
overflow checking is *enabled*, then an error condition always results
105105
in a panic. For efficiency reasons, this panic may be delayed over
106106
some number of pure operations, as described below.
107107

108-
If overflow checking is *optional*, then an error condition may result
109-
in a panic, but the erroneous operation may also simply produce a
110-
value. The permitted results for each error condition are specified
111-
below. When checking is optional, panics are not required to be
112-
consistent across executions. This means that, for example, the
113-
compiler could insert code which results in an overflow only if the
114-
value that was produced is actually consumed (which may occur on only
115-
one arm of a branch).
116-
117-
In the future, we may add some way to explicit *disable* overflow
118-
checking (probably in a scoped fashion). In that case, the result of
119-
each error condition would simply be the same as the optional state
120-
when no panic occurs.
108+
If overflow checking is *default*, that means that erroneous
109+
operations will produce a value as specified below. Note though that
110+
code which encounters an error condition is still considered buggy.
111+
In particular, Rust source code (in particular library code) cannot
112+
rely on wrapping semantics, and should always be written with the
113+
assumption that overflow checking *may* be enabled. This is because
114+
overflow checking may be enabled by a downstream consumer of the
115+
library.
116+
117+
In the future, we could add some way to explicitly *disable* overflow
118+
checking in a scoped fashion. In that case, the result of each error
119+
condition would simply be the same as the optional state when no panic
120+
occurs, and this would requests for override checking specified
121+
elsewhere. However, no mechanism for disabling overflow checks is
122+
provided by this RFC: instead, it is recommended that authors use the
123+
wrapped primitives.
121124

122125
The error conditions that can arise, and their defined results, are as
123-
follows. In all cases, the defined results are the same as the defined
124-
results today. The only change is that now a panic may result.
125-
126-
- The operations `+`, `-`, `*`, `/`, and `%` can underflow and overflow. If no panic occurs,
127-
the result of such an operation is defined to be the same as wrapping.
128-
- When truncating, the casting operation `as` can overflow if the truncated bits contain
129-
non-zero values. If no panic occurs, the result of such an operation is defined to
130-
be the same as wrapping.
131-
- Shift operations (`<<`, `>>`) can shift a value of width `N` by more
132-
than `N` bits. If no panic occurs, the result of such an operation
133-
is an *undefined value* (note that this is not the same as
134-
*undefined behavior* in C).
135-
- The reason that there is no defined result here is because
136-
different CPUs have different behavior, and we wish to be able to translate shifting
137-
operations directly to the corresponding machine instructions.
126+
follows. The intention is that the defined results are the same as the
127+
defined results today. The only change is that now a panic may result.
128+
129+
- The operations `+`, `-`, `*`, `/`, `%` can underflow and
130+
overflow. Shift operations (`<<`, `>>`) can shift a value of width `N` by more
131+
than `N` bits. In these cases, the result is the same as the pre-existing,
132+
wrapping semantics.
133+
- When truncating, the casting operation `as` can overflow if the
134+
truncated bits contain non-zero values. If no panic occurs, the
135+
result of such an operation is defined to be the same as wrapping.
138136

139137
## Enabling overflow checking
140138

@@ -294,11 +292,10 @@ direct overhead of checking for overflow, there is some additional
294292
overhead when checks are enabled because compilers may have to forego
295293
other optimizations or code motion that might have been legal. This
296294
concern seems minimal since, in optimized builds, overflow checking
297-
will be considered *optional* and hence the compiler is free to ignore
298-
it. Certainly if we ever decided to change the default for overflow
299-
checking from *optional* to *enabled* in optimized builds, we would
300-
want to measure carefully and likely include some means of disabling
301-
checks in particularly hot paths.
295+
will not be enabled. Certainly if we ever decided to change the
296+
default for overflow checking to *enabled* in optimized builds, we
297+
would want to measure carefully and likely include some means of
298+
disabling checks in particularly hot paths.
302299

303300
# Alternatives and possible future directions
304301

0 commit comments

Comments
 (0)