Skip to content

Commit 8637d7a

Browse files
committed
clarify extreme operator behaviour
1 parent 6e537fe commit 8637d7a

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

text/0560-integer-overflow.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,14 @@ The error conditions that can arise, and their defined results, are as
125125
follows. The intention is that the defined results are the same as the
126126
defined results today. The only change is that now a panic may result.
127127

128-
- The operations `+`, `-`, `*`, `/`, `%` can underflow and
129-
overflow.
128+
- The operations `+`, `-`, `*`, can underflow and overflow. When checking is
129+
enabled this will panic. When checking is disabled this will two's complement
130+
wrap.
131+
- The operations `/`, `%` are nonsensical for the arguments `INT_MIN` and `-1`.
132+
When this occurs there is an unconditional panic.
130133
- Shift operations (`<<`, `>>`) can shift a value of width `N` by more
131-
than `N` bits.
134+
than `N` bits. This is prevented by unconditionally masking the bits
135+
of the right-hand-side to wrap modulo `N`.
132136

133137
## Enabling overflow checking
134138

@@ -145,7 +149,7 @@ potential overflow (and, in particular, for code where overflow is
145149
expected and normal, they will be immediately guided to use the
146150
wrapping methods introduced below). However, because these checks will
147151
be compiled out whenever an optimized build is produced, final code
148-
wilil not pay a performance penalty.
152+
will not pay a performance penalty.
149153

150154
In the future, we may add additional means to control when overflow is
151155
checked, such as scoped attributes or a global, independent
@@ -451,17 +455,7 @@ were:
451455

452456
# Unresolved questions
453457

454-
The C semantics of wrapping operations in some cases are undefined:
455-
456-
- `INT_MIN / -1`, `INT_MIN % -1`
457-
- Shifts by an excessive number of bits
458-
459-
This RFC takes no position on the correct semantics of these
460-
operations, simply preserving the existing semantics. However, it may
461-
be worth trying to define the wrapping semantics of these operations
462-
in a portable way, even if that implies some runtime cost. Since these
463-
are all error conditions, this is an orthogonal topic to the matter of
464-
overflow.
458+
None today (see Updates section below).
465459

466460
# Future work
467461

@@ -491,6 +485,10 @@ Since it was accepted, the RFC has been updated as follows:
491485
2. `as` was changed to restore the behavior before the RFC (that is,
492486
it truncates to the target bitwidth and reinterprets the highest
493487
order bit, a.k.a. sign-bit, as necessary, as a C cast would).
488+
3. Shifts were specified to mask off the bits of over-long shifts.
489+
4. Overflow was specified to be two's complement wrapping (this was mostly
490+
a clarification).
491+
5. `INT_MIN / -1` and `INT_MIN % -1` panics.
494492

495493
# Acknowledgements and further reading
496494

0 commit comments

Comments
 (0)