11# Constant evaluation
2+ r[ const-eval]
23
4+ r[ const-eval.general]
35Constant evaluation is the process of computing the result of
46[ expressions] during compilation. Only a subset of all expressions
57can be evaluated at compile-time.
68
79## Constant expressions
810
11+ r[ const-eval.const-expr]
12+
13+ r[ const-eval.const-expr.general]
914Certain 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
1526must be evaluated at compile time (i.e. in const contexts). Otherwise, these
1627behaviors are warnings, but will likely panic at run-time.
1728
29+
30+ r[ const-eval.const-expr.list]
1831The following expressions are constant expressions, so long as any operands are
1932also constant expressions and do not cause any [ ` Drop::drop ` ] [ destructors ] calls
2033to be run.
2134
35+ r[ const-eval.const-expr.literal]
2236* [ Literals] .
37+
38+ r[ const-eval.const-expr.parameter]
2339* [ Const parameters] .
40+
41+ r[ const-eval.const-expr.path-item]
2442* [ Paths] to [ functions] and [ constants] .
2543 Recursively defining constants is not allowed.
44+
45+ r[ const-eval.const-expr.path-static]
2646* Paths to [ statics] . These are only allowed within the initializer of a static.
47+
48+ r[ const-eval.const-expr.tuple]
2749* [ Tuple expressions] .
50+
51+ r[ const-eval.const-expr.array]
2852* [ Array expressions] .
53+
54+ r[ const-eval.const-expr.constructor]
2955* [ Struct] expressions.
56+
57+ r[ const-eval.const-expr.block]
3058* [ Block expressions] , including ` unsafe ` and ` const ` blocks.
3159 * [ let statements] and thus irrefutable [ patterns] , including mutable bindings
3260 * [ assignment expressions]
3361 * [ compound assignment expressions]
3462 * [ expression statements]
63+
64+ r[ const-eval.const-expr.field]
3565* [ Field] expressions.
66+
67+ r[ const-eval.const-expr.index]
3668* Index expressions, [ array indexing] or [ slice] with a ` usize ` .
69+
70+ r[ const-eval.const-expr.range]
3771* [ Range expressions] .
72+
73+ r[ const-eval.const-expr.closure]
3874* [ Closure expressions] which don't capture variables from the environment.
75+
76+ r[ const-eval.const-expr.builtin-arith-logic]
3977* Built-in [ negation] , [ arithmetic] , [ logical] , [ comparison] or [ lazy boolean]
4078 operators used on integer and floating point types, ` bool ` , and ` char ` .
79+
80+ r[ const-eval.const-expr.borrows]
4181* All forms of [ borrow] s, including raw borrows, with one limitation:
4282 mutable borrows and shared borrows to values with interior mutability
4383 are only allowed to refer to * transient* places. A place is * transient*
4484 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+
4691* [ Grouped] expressions.
92+
93+ r[ const-eval.const-expr.cast]
4794* [ Cast] expressions, except
4895 * pointer to address casts and
4996 * function pointer to address casts.
97+
98+ r[ const-eval.const-expr.const-fn]
5099* Calls of [ const functions] and const methods.
100+
101+ r[ const-eval.const-expr.loop]
51102* [ loop] , [ while] and [ ` while let ` ] expressions.
103+
104+ r[ const-eval.const-expr.if-match]
52105* [ if] , [ ` if let ` ] and [ match] expressions.
53106
54107## Const context
55108[ const context ] : #const-context
56109
110+ r[ const-eval.const-context]
111+
112+
113+ r[ const-eval.const-context.general]
57114A _ const context_ is one of the following:
58115
116+ r[ const-eval.const-context.array-length]
59117* [ Array type length expressions]
118+
119+ r[ const-eval.const-context.repeat-length]
60120* [ Array repeat length expressions] [ array expressions ]
121+
122+ r[ const-eval.const-context.init]
61123* The initializer of
62124 * [ constants]
63125 * [ statics]
64126 * [ enum discriminants]
127+
128+ r[ const-eval.const-context.generic]
65129* A [ const generic argument]
130+
131+ r[ const-eval.const-context.block]
66132* A [ const block]
67133
68134Const contexts that are used as parts of types (array type and repeat length
@@ -73,10 +139,17 @@ generics.
73139
74140## Const Functions
75141
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
77149` const ` has no effect on any existing uses, it only restricts the types that arguments and the
78150return type may use, and restricts the function body to constant expressions.
79151
152+ r[ const-eval.const-fn.const-context]
80153When called from a const context, the function is interpreted by the
81154compiler at compile time. The interpretation happens in the
82155environment of the compilation target and not the host. So ` usize ` is
0 commit comments