1
1
# Constant evaluation
2
+ r[ const-eval]
2
3
4
+ r[ const-eval.general]
3
5
Constant evaluation is the process of computing the result of
4
6
[ expressions] during compilation. Only a subset of all expressions
5
7
can be evaluated at compile-time.
6
8
7
9
## Constant expressions
8
10
11
+ r[ const-eval.const-expr]
12
+
13
+ r[ const-eval.const-expr.general]
9
14
Certain forms of expressions, called constant expressions, can be evaluated at
10
- compile time. In [ const contexts] ( #const-context ) , these are the only allowed
11
- expressions, and are always evaluated at compile time. In other places, such as
12
- [ let statements] , constant expressions * may*
13
- be, but are not guaranteed to be, evaluated at compile time. Behaviors such as
14
- out of bounds [ array indexing] or [ overflow] are compiler errors if the value
15
+ compile time.
16
+
17
+ r[ const-eval.const-expr.const-context]
18
+ In [ const contexts] ( #const-context ) , these are the only allowed
19
+ expressions, and are always evaluated at compile time.
20
+
21
+ r[ const-eval.const-expr.runtime-context]
22
+ In other places, such as [ let statements] , constant expressions * may* be, but are not guaranteed to be, evaluated at compile time.
23
+
24
+ r[ const-eval.const-expr.error]
25
+ Behaviors such as out of bounds [ array indexing] or [ overflow] are compiler errors if the value
15
26
must be evaluated at compile time (i.e. in const contexts). Otherwise, these
16
27
behaviors are warnings, but will likely panic at run-time.
17
28
29
+
30
+ r[ const-eval.const-expr.list]
18
31
The following expressions are constant expressions, so long as any operands are
19
32
also constant expressions and do not cause any [ ` Drop::drop ` ] [ destructors ] calls
20
33
to be run.
21
34
35
+ r[ const-eval.const-expr.literal]
22
36
* [ Literals] .
37
+
38
+ r[ const-eval.const-expr.parameter]
23
39
* [ Const parameters] .
40
+
41
+ r[ const-eval.const-expr.path-item]
24
42
* [ Paths] to [ functions] and [ constants] .
25
43
Recursively defining constants is not allowed.
44
+
45
+ r[ const-eval.const-expr.path-static]
26
46
* Paths to [ statics] . These are only allowed within the initializer of a static.
47
+
48
+ r[ const-eval.const-expr.tuple]
27
49
* [ Tuple expressions] .
50
+
51
+ r[ const-eval.const-expr.array]
28
52
* [ Array expressions] .
53
+
54
+ r[ const-eval.const-expr.constructor]
29
55
* [ Struct] expressions.
56
+
57
+ r[ const-eval.const-expr.block]
30
58
* [ Block expressions] , including ` unsafe ` and ` const ` blocks.
31
59
* [ let statements] and thus irrefutable [ patterns] , including mutable bindings
32
60
* [ assignment expressions]
33
61
* [ compound assignment expressions]
34
62
* [ expression statements]
63
+
64
+ r[ const-eval.const-expr.field]
35
65
* [ Field] expressions.
66
+
67
+ r[ const-eval.const-expr.index]
36
68
* Index expressions, [ array indexing] or [ slice] with a ` usize ` .
69
+
70
+ r[ const-eval.const-expr.range]
37
71
* [ Range expressions] .
72
+
73
+ r[ const-eval.const-expr.closure]
38
74
* [ Closure expressions] which don't capture variables from the environment.
75
+
76
+ r[ const-eval.const-expr.builtin-arith-logic]
39
77
* Built-in [ negation] , [ arithmetic] , [ logical] , [ comparison] or [ lazy boolean]
40
78
operators used on integer and floating point types, ` bool ` , and ` char ` .
79
+
80
+ r[ const-eval.const-expr.borrows]
41
81
* All forms of [ borrow] s, including raw borrows, with one limitation:
42
82
mutable borrows and shared borrows to values with interior mutability
43
83
are only allowed to refer to * transient* places. A place is * transient*
44
84
if its lifetime is strictly contained inside the current [ const context] .
45
- * The [ dereference operator] .
85
+
86
+ r[ const-eval.const-expr.deref]
87
+ * The [ dereference operator] except for raw pointers.
88
+
89
+ r[ const-eval.const-expr.group]
90
+
46
91
* [ Grouped] expressions.
92
+
93
+ r[ const-eval.const-expr.cast]
47
94
* [ Cast] expressions, except
48
95
* pointer to address casts and
49
96
* function pointer to address casts.
97
+
98
+ r[ const-eval.const-expr.const-fn]
50
99
* Calls of [ const functions] and const methods.
100
+
101
+ r[ const-eval.const-expr.loop]
51
102
* [ loop] , [ while] and [ ` while let ` ] expressions.
103
+
104
+ r[ const-eval.const-expr.if-match]
52
105
* [ if] , [ ` if let ` ] and [ match] expressions.
53
106
54
107
## Const context
55
108
[ const context ] : #const-context
56
109
110
+ r[ const-eval.const-context]
111
+
112
+
113
+ r[ const-eval.const-context.general]
57
114
A _ const context_ is one of the following:
58
115
116
+ r[ const-eval.const-context.array-length]
59
117
* [ Array type length expressions]
118
+
119
+ r[ const-eval.const-context.repeat-length]
60
120
* [ Array repeat length expressions] [ array expressions ]
121
+
122
+ r[ const-eval.const-context.init]
61
123
* The initializer of
62
124
* [ constants]
63
125
* [ statics]
64
126
* [ enum discriminants]
127
+
128
+ r[ const-eval.const-context.generic]
65
129
* A [ const generic argument]
130
+
131
+ r[ const-eval.const-context.block]
66
132
* A [ const block]
67
133
68
134
Const contexts that are used as parts of types (array type and repeat length
@@ -73,10 +139,17 @@ generics.
73
139
74
140
## Const Functions
75
141
76
- A _ const fn_ is a function that one is permitted to call from a const context. Declaring a function
142
+ r[ const-eval.const-fn]
143
+
144
+ r[ const-eval.const-fn.general]
145
+ A _ const fn_ is a function that one is permitted to call from a const context.
146
+
147
+ r[ const-eval.const-fn.usage]
148
+ Declaring a function
77
149
` const ` has no effect on any existing uses, it only restricts the types that arguments and the
78
150
return type may use, and restricts the function body to constant expressions.
79
151
152
+ r[ const-eval.const-fn.const-context]
80
153
When called from a const context, the function is interpreted by the
81
154
compiler at compile time. The interpretation happens in the
82
155
environment of the compilation target and not the host. So ` usize ` is
0 commit comments