Skip to content

Commit 6bc96f8

Browse files
authored
Move const expressions to const_eval
1 parent 9b2fb7a commit 6bc96f8

File tree

1 file changed

+62
-2
lines changed

1 file changed

+62
-2
lines changed

src/const_eval.md

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,43 @@
11
# Constant evaluation
22

33
Constant evaluation is the process of computing the result of
4-
[expressions] during compilation.
4+
[expressions] during compilation. Only a subset of all expressions
5+
can be evaluated at compile-time.
6+
7+
## Constant expressions
8+
9+
Certain types of expressions can be evaluated at compile time. These are called
10+
_constant expressions_ and are required in const contexts. In
11+
other places, such as in [`let` statements](statements.html#let-statements),
12+
constant expressions may be evaluated at compile time. If errors, such as out
13+
of bounds [array indexing] or [overflow] occurs,
14+
then it is a compiler error if the value must be evaluated at compile time,
15+
otherwise it is just a warning, but the code will most likely panic when run.
16+
17+
The following expressions are constant expressions, so long as any operands are
18+
also constant expressions and do not cause any [`Drop::drop`][destructors] calls
19+
to be ran.
20+
21+
* [Literals].
22+
* [Paths] to [functions](items/functions.html) and constants.
23+
Recursively defining constants is not allowed.
24+
* [Tuple expressions].
25+
* [Array expressions].
26+
* [Struct] expressions.
27+
* [Enum variant] expressions.
28+
* [Block expressions], including `unsafe` blocks, which only contain items and
29+
possibly a constant tail expression.
30+
* [Field] expressions.
31+
* Index expressions, [array indexing] or [slice] with a `usize`.
32+
* [Range expressions].
33+
* [Closure expressions] which don't capture variables from the environment.
34+
* Built in [negation], [arithmetic, logical], [comparison] or [lazy boolean]
35+
operators used on integer and floating point types, `bool` and `char`.
36+
* Shared [borrow]s, except if applied to a type with [interior mutability].
37+
* The [dereference operator].
38+
* [Grouped] expressions.
39+
* [Cast] expressions, except pointer to address and
40+
function pointer to address casts.
541

642
## Const context
743

@@ -18,4 +54,28 @@ A _const context_ is one of the following:
1854
[enum discriminants]: items/enumerations.html#custom-discriminant-values-for-field-less-enumerations
1955
[constants]: items/constant-items.html
2056
[statics]: items/static-items.html
21-
[expressions]: expressions.html#constant-expressions
57+
[expressions]: expressions.html
58+
[array indexing]: expressions/array-expr.html#array-and-slice-indexing-expressions
59+
[overflow]: expressions/operator-expr.html#overflow
60+
[destructors]: destructors.html
61+
[literals]: expressions/literal-expr.html
62+
[paths]: expressions/path-expr.html
63+
[tuple expressions]: expressions/tuple-expr.html
64+
[array expressions]: expressions/array-expr.html
65+
[struct]: expressions/struct-expr.html
66+
[enum variant]: expressions/enum-variant-expr.html
67+
[block expressions]: expressions/block-expr.html
68+
[field]: expressions/field-expr.html
69+
[array indexing]: expressions/array-expr.html#array-and-slice-indexing-expressions
70+
[slice]: types.html#array-and-slice-types
71+
[range expressions]: expressions/range-expr.html
72+
[closure expressions]: expressions/closure-expr.html
73+
[negation]: expressions/operator-expr.html#negation-operators
74+
[arithmetic, logical]: expressions/operator-expr.html#arithmetic-and-logical-binary-operators
75+
[comparison]: expressions/operator-expr.html#comparison-operators
76+
[lazy boolean]: expressions/operator-expr.html#lazy-boolean-operators
77+
[borrow]: expressions/operator-expr.html#borrow-operators
78+
[interior mutability]: interior-mutability.html
79+
[dereference operator]: expressions/operator-expr.html#the-dereference-operator
80+
[grouped]: expressions/grouped-expr.html
81+
[cast]: expressions/operator-expr.html#type-cast-expressions

0 commit comments

Comments
 (0)