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