@@ -48,12 +48,12 @@ The RFC has several major goals:
48
48
To that end the RFC proposes two mechanisms:
49
49
50
50
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
53
53
signaled. Specific intrinsics and library support are provided to
54
54
permit either explicit overflow checks or explicit wrapping.
55
55
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
57
57
assertion: an important safety check that is too expensive to
58
58
perform on all code.
59
59
@@ -99,42 +99,40 @@ a static/dynamic analysis tools as an error. This is largerly a
99
99
semantic distinction, though.
100
100
101
101
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
103
103
not describe a way to disable overflow checking completely). If
104
104
overflow checking is * enabled* , then an error condition always results
105
105
in a panic. For efficiency reasons, this panic may be delayed over
106
106
some number of pure operations, as described below.
107
107
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.
121
124
122
125
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.
138
136
139
137
## Enabling overflow checking
140
138
@@ -294,11 +292,10 @@ direct overhead of checking for overflow, there is some additional
294
292
overhead when checks are enabled because compilers may have to forego
295
293
other optimizations or code motion that might have been legal. This
296
294
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.
302
299
303
300
# Alternatives and possible future directions
304
301
0 commit comments