Skip to content

Commit f0f6155

Browse files
authored
Merge pull request #1566 from RalfJung/const-fn-float
const_eval: update for const-fn float stabilization
2 parents d2b3d80 + 76bd77a commit f0f6155

File tree

1 file changed

+7
-23
lines changed

1 file changed

+7
-23
lines changed

src/const_eval.md

+7-23
Original file line numberDiff line numberDiff line change
@@ -61,40 +61,24 @@ A _const context_ is one of the following:
6161
* A [const generic argument]
6262
* A [const block]
6363

64+
Const contexts that are used as parts of types (array type and repeat length
65+
expressions as well as const generic arguments) can only make restricted use of
66+
surrounding generic parameters: such an expression must either be a single bare
67+
const generic parameter, or an arbitrary expression not making use of any
68+
generics.
69+
6470
## Const Functions
6571

6672
A _const fn_ is a function that one is permitted to call from a const context. Declaring a function
6773
`const` has no effect on any existing uses, it only restricts the types that arguments and the
68-
return type may use, as well as prevent various expressions from being used within it. You can freely
69-
do anything with a const function that you can do with a regular function.
74+
return type may use, and restricts the function body to constant expressions.
7075

7176
When called from a const context, the function is interpreted by the
7277
compiler at compile time. The interpretation happens in the
7378
environment of the compilation target and not the host. So `usize` is
7479
`32` bits if you are compiling against a `32` bit system, irrelevant
7580
of whether you are building on a `64` bit or a `32` bit system.
7681

77-
Const functions have various restrictions to make sure that they can be
78-
evaluated at compile-time. It is, for example, not possible to write a random
79-
number generator as a const function. Calling a const function at compile-time
80-
will always yield the same result as calling it at runtime, even when called
81-
multiple times. There's one exception to this rule: if you are doing complex
82-
floating point operations in extreme situations, then you might get (very
83-
slightly) different results. It is advisable to not make array lengths and enum
84-
discriminants depend on floating point computations.
85-
86-
87-
Notable features that are allowed in const contexts but not in const functions include:
88-
89-
* floating point operations
90-
* floating point values are treated just like generic parameters without trait bounds beyond
91-
`Copy`. So you cannot do anything with them but copy/move them around.
92-
93-
Conversely, the following are possible in a const function, but not in a const context:
94-
95-
* Use of generic type and lifetime parameters.
96-
* Const contexts do allow limited use of [const generic parameters].
97-
9882
[arithmetic]: expressions/operator-expr.md#arithmetic-and-logical-binary-operators
9983
[array expressions]: expressions/array-expr.md
10084
[array indexing]: expressions/array-expr.md#array-and-slice-indexing-expressions

0 commit comments

Comments
 (0)