Skip to content

Commit 7ad799d

Browse files
committed
Moving text to const_eval from functions.
1 parent a9549b0 commit 7ad799d

File tree

2 files changed

+21
-53
lines changed

2 files changed

+21
-53
lines changed

src/const_eval.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,24 @@ A _const context_ is one of the following:
6464

6565
A _const fn_ is a function that one is permitted to call from a const context. Declaring a function
6666
`const` has no effect on any existing uses, it only restricts the types that arguments and the
67-
return type may use, as well as prevent various expressions from being used within it.
67+
return type may use, as well as prevent various expressions from being used within it. You can freely do anything with a const function that
68+
you can do with a regular function.
69+
70+
When called from a const context, the function is interpreted by the
71+
compiler at compile time. The interpretation happens in the
72+
environment of the compilation target and not the host. So `usize` is
73+
`32` bits if you are compiling against a `32` bit system, irrelevant
74+
of whether you are building on a `64` bit or a `32` bit system.
75+
76+
Const functions have various restrictions to make sure that they can be
77+
evaluated at compile-time. It is, for example, not possible to write a random
78+
number generator as a const function. Calling a const function at compile-time
79+
will always yield the same result as calling it at runtime, even when called
80+
multiple times. There's one exception to this rule: if you are doing complex
81+
floating point operations in extreme situations, then you might get (very
82+
slightly) different results. It is advisable to not make array lengths and enum
83+
discriminants depend on floating point computations.
84+
6885

6986
Notable features that const contexts have, but const fn haven't are:
7087

src/items/functions.md

+3-52
Original file line numberDiff line numberDiff line change
@@ -183,59 +183,9 @@ aborts the process by executing an illegal instruction.
183183

184184
## Const functions
185185

186-
Functions qualified with the `const` keyword are const functions, as are
186+
Functions qualified with the `const` keyword are [const functions], as are
187187
[tuple struct] and [tuple variant] constructors. _Const functions_ can be
188-
called from within [const context]s. When called from a const context, the
189-
function is interpreted by the compiler at compile time. The interpretation
190-
happens in the environment of the compilation target and not the host. So
191-
`usize` is `32` bits if you are compiling against a `32` bit system, irrelevant
192-
of whether you are building on a `64` bit or a `32` bit system.
193-
194-
If a const function is called outside a [const context], it is indistinguishable
195-
from any other function. You can freely do anything with a const function that
196-
you can do with a regular function.
197-
198-
Const functions have various restrictions to make sure that they can be
199-
evaluated at compile-time. It is, for example, not possible to write a random
200-
number generator as a const function. Calling a const function at compile-time
201-
will always yield the same result as calling it at runtime, even when called
202-
multiple times. There's one exception to this rule: if you are doing complex
203-
floating point operations in extreme situations, then you might get (very
204-
slightly) different results. It is advisable to not make array lengths and enum
205-
discriminants depend on floating point computations.
206-
207-
Exhaustive list of permitted structures in const functions:
208-
209-
> **Note**: this list is more restrictive than what you can write in
210-
> regular constants
211-
212-
* Type parameters where the parameters only have any [trait bounds]
213-
of the following kind:
214-
* lifetimes
215-
* `Sized` or [`?Sized`]
216-
217-
This means that `<T: 'a + ?Sized>`, `<T: 'b + Sized>`, and `<T>`
218-
are all permitted.
219-
220-
This rule also applies to type parameters of impl blocks that
221-
contain const methods.
222-
223-
This does not apply to tuple struct and tuple variant constructors.
224-
225-
* Arithmetic and comparison operators on integers
226-
* All boolean operators including `&&` and `||`
227-
* Any kind of aggregate constructor (array, `struct`, `enum`, tuple, ...)
228-
* Calls to other *safe* const functions (whether by function call or method call)
229-
* Index expressions on arrays and slices
230-
* Field accesses on structs and tuples
231-
* Reading from constants (but not statics, not even taking a reference to a static)
232-
* `&` and `*` (only dereferencing of references, not raw pointers)
233-
* `if`, `if let`, and `match`
234-
* `while`, `while let`, and `loop`
235-
* Casts except for raw pointer to integer casts and cast to slice
236-
* `unsafe` blocks and `const unsafe fn` are allowed, but the body/block may only do
237-
the following unsafe operations:
238-
* calls to const unsafe functions
188+
called from within [const context]s.
239189

240190
## Async functions
241191

@@ -397,6 +347,7 @@ fn foo_oof(#[some_inert_attribute] arg: u8) {
397347
[_WhereClause_]: generics.md#where-clauses
398348
[_OuterAttribute_]: ../attributes.md
399349
[const context]: ../const_eval.md#const-context
350+
[const functions]: ../const_eval.md#const-functions
400351
[tuple struct]: structs.md
401352
[tuple variant]: enumerations.md
402353
[external block]: external-blocks.md

0 commit comments

Comments
 (0)