You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/expressions/literal-expr.md
+2
Original file line number
Diff line number
Diff line change
@@ -107,6 +107,7 @@ If the value does not fit in `u128`, the expression is rejected by the parser.
107
107
> `rustc` includes a [lint check] named `overflowing_literals`, defaulting to `deny`, which rejects expressions where this occurs.
108
108
109
109
> **Note**: `-1i8`, for example, is an application of the [negation operator] to the literal expression `1i8`, not a single integer literal expression.
110
+
> See [Overflow] for notes on representing the most negative value for a signed type.
110
111
111
112
## Floating-point literal expressions
112
113
@@ -159,6 +160,7 @@ A boolean literal expression consists of a single [BOOLEAN_LITERAL] token.
Copy file name to clipboardExpand all lines: src/expressions/operator-expr.md
+12-2
Original file line number
Diff line number
Diff line change
@@ -22,12 +22,20 @@ Integer operators will panic when they overflow when compiled in debug mode.
22
22
The `-C debug-assertions` and `-C overflow-checks` compiler flags can be used to control this more directly.
23
23
The following things are considered to be overflow:
24
24
25
-
* When `+`, `*` or `-` create a value greater than the maximum value, or less than the minimum value that can be stored.
26
-
This includes unary `-`on the smallest value of any signed integer type.
25
+
* When `+`, `*` or binary `-` create a value greater than the maximum value, or less than the minimum value that can be stored.
26
+
* Applying unary `-`to the most negative value of any signed integer type, unless the operand is a [literal expression] (or a literal expression standing alone inside one or more [grouped expressions][grouped expression]).
27
27
* Using `/` or `%`, where the left-hand argument is the smallest integer of a signed integer type and the right-hand argument is `-1`.
28
28
These checks occur even when `-C overflow-checks` is disabled, for legacy reasons.
29
29
* Using `<<` or `>>` where the right-hand argument is greater than or equal to the number of bits in the type of the left-hand argument, or is negative.
30
30
31
+
> **Note**: The exception for literal expressions behind unary `-` means that forms such as `-128_i8` or `let j: i8 = -(128)` never cause a panic and have the expected value of -128.
32
+
>
33
+
> In these cases, the literal expression already has the most negative value for its type (for example, `128_i8` has the value -128) because integer literals are truncated to their type per the description in [Integer literal expressions][literal expression].
34
+
>
35
+
> Negation of these most negative values leaves the value unchanged due to two's complement overflow conventions.
36
+
>
37
+
> In `rustc`, these most negative expressions are also ignored by the `overflowing_literals` lint check.
38
+
31
39
## Borrow operators
32
40
33
41
> **<sup>Syntax</sup>**\
@@ -580,6 +588,8 @@ See [this test] for an example of using this dependency.
580
588
581
589
[copies or moves]: ../expressions.md#moved-and-copied-types
0 commit comments