Skip to content

Commit b2fd0f2

Browse files
authored
Merge pull request #1569 from chorman0773/spec-add-identifiers-const_eval
Add spec identifiers to const_eval.md
2 parents bab007c + e58f65e commit b2fd0f2

File tree

1 file changed

+78
-7
lines changed

1 file changed

+78
-7
lines changed

src/const_eval.md

+78-7
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,132 @@
11
# Constant evaluation
2+
r[const-eval]
23

4+
r[const-eval.general]
35
Constant evaluation is the process of computing the result of
46
[expressions] during compilation. Only a subset of all expressions
57
can be evaluated at compile-time.
68

79
## Constant expressions
810

11+
r[const-eval.const-expr]
12+
13+
r[const-eval.const-expr.general]
914
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
1526
must be evaluated at compile time (i.e. in const contexts). Otherwise, these
1627
behaviors are warnings, but will likely panic at run-time.
1728

29+
r[const-eval.const-expr.list]
1830
The following expressions are constant expressions, so long as any operands are
1931
also constant expressions and do not cause any [`Drop::drop`][destructors] calls
2032
to be run.
2133

34+
r[const-eval.const-expr.literal]
2235
* [Literals].
36+
37+
r[const-eval.const-expr.parameter]
2338
* [Const parameters].
39+
40+
r[const-eval.const-expr.path-item]
2441
* [Paths] to [functions] and [constants].
2542
Recursively defining constants is not allowed.
43+
44+
r[const-eval.const-expr.path-static]
2645
* Paths to [statics]. These are only allowed within the initializer of a static.
46+
47+
r[const-eval.const-expr.tuple]
2748
* [Tuple expressions].
49+
50+
r[const-eval.const-expr.array]
2851
* [Array expressions].
52+
53+
r[const-eval.const-expr.constructor]
2954
* [Struct] expressions.
55+
56+
r[const-eval.const-expr.block]
3057
* [Block expressions], including `unsafe` and `const` blocks.
3158
* [let statements] and thus irrefutable [patterns], including mutable bindings
3259
* [assignment expressions]
3360
* [compound assignment expressions]
3461
* [expression statements]
62+
63+
r[const-eval.const-expr.field]
3564
* [Field] expressions.
65+
66+
r[const-eval.const-expr.index]
3667
* Index expressions, [array indexing] or [slice] with a `usize`.
68+
69+
r[const-eval.const-expr.range]
3770
* [Range expressions].
71+
72+
r[const-eval.const-expr.closure]
3873
* [Closure expressions] which don't capture variables from the environment.
74+
75+
r[const-eval.const-expr.builtin-arith-logic]
3976
* Built-in [negation], [arithmetic], [logical], [comparison] or [lazy boolean]
4077
operators used on integer and floating point types, `bool`, and `char`.
78+
79+
r[const-eval.const-expr.borrows]
4180
* All forms of [borrow]s, including raw borrows, with one limitation:
4281
mutable borrows and shared borrows to values with interior mutability
4382
are only allowed to refer to *transient* places. A place is *transient*
4483
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+
4690
* [Grouped] expressions.
91+
92+
r[const-eval.const-expr.cast]
4793
* [Cast] expressions, except
4894
* pointer to address casts and
4995
* function pointer to address casts.
96+
97+
r[const-eval.const-expr.const-fn]
5098
* Calls of [const functions] and const methods.
99+
100+
r[const-eval.const-expr.loop]
51101
* [loop], [while] and [`while let`] expressions.
102+
103+
r[const-eval.const-expr.if-match]
52104
* [if], [`if let`] and [match] expressions.
53105

54106
## Const context
55107
[const context]: #const-context
56108

109+
r[const-eval.const-context]
110+
111+
r[const-eval.const-context.general]
57112
A _const context_ is one of the following:
58113

114+
r[const-eval.const-context.array-length]
59115
* [Array type length expressions]
116+
117+
r[const-eval.const-context.repeat-length]
60118
* [Array repeat length expressions][array expressions]
119+
120+
r[const-eval.const-context.init]
61121
* The initializer of
62122
* [constants]
63123
* [statics]
64124
* [enum discriminants]
125+
126+
r[const-eval.const-context.generic]
65127
* A [const generic argument]
128+
129+
r[const-eval.const-context.block]
66130
* A [const block]
67131

68132
Const contexts that are used as parts of types (array type and repeat length
@@ -73,10 +137,17 @@ generics.
73137

74138
## Const Functions
75139

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
77147
`const` has no effect on any existing uses, it only restricts the types that arguments and the
78148
return type may use, and restricts the function body to constant expressions.
79149

150+
r[const-eval.const-fn.const-context]
80151
When called from a const context, the function is interpreted by the
81152
compiler at compile time. The interpretation happens in the
82153
environment of the compilation target and not the host. So `usize` is

0 commit comments

Comments
 (0)