Skip to content

Commit 05ddf66

Browse files
authored
Merge pull request #883 from jrincayc/const_fn_additions
Rust 1.46 now allows more features in const fn
2 parents 66b4d58 + 6dceee1 commit 05ddf66

File tree

2 files changed

+21
-55
lines changed

2 files changed

+21
-55
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-54
Original file line numberDiff line numberDiff line change
@@ -183,58 +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 except for `&&` and `||` which are banned since
227-
they are short-circuiting.
228-
* Any kind of aggregate constructor (array, `struct`, `enum`, tuple, ...)
229-
* Calls to other *safe* const functions (whether by function call or method call)
230-
* Index expressions on arrays and slices
231-
* Field accesses on structs and tuples
232-
* Reading from constants (but not statics, not even taking a reference to a static)
233-
* `&` and `*` (only dereferencing of references, not raw pointers)
234-
* Casts except for raw pointer to integer casts
235-
* `unsafe` blocks and `const unsafe fn` are allowed, but the body/block may only do
236-
the following unsafe operations:
237-
* calls to const unsafe functions
188+
called from within [const context]s.
238189

239190
## Async functions
240191

@@ -396,6 +347,7 @@ fn foo_oof(#[some_inert_attribute] arg: u8) {
396347
[_WhereClause_]: generics.md#where-clauses
397348
[_OuterAttribute_]: ../attributes.md
398349
[const context]: ../const_eval.md#const-context
350+
[const functions]: ../const_eval.md#const-functions
399351
[tuple struct]: structs.md
400352
[tuple variant]: enumerations.md
401353
[external block]: external-blocks.md
@@ -416,10 +368,7 @@ fn foo_oof(#[some_inert_attribute] arg: u8) {
416368
[`doc`]: ../../rustdoc/the-doc-attribute.html
417369
[`must_use`]: ../attributes/diagnostics.md#the-must_use-attribute
418370
[patterns]: ../patterns.md
419-
[`?Sized`]: ../trait-bounds.md#sized
420-
[trait bounds]: ../trait-bounds.md
421371
[`export_name`]: ../abi.md#the-export_name-attribute
422372
[`link_section`]: ../abi.md#the-link_section-attribute
423373
[`no_mangle`]: ../abi.md#the-no_mangle-attribute
424-
[external_block_abi]: external-blocks.md#abi
425374
[built-in attributes]: ../attributes.html#built-in-attributes-index

0 commit comments

Comments
 (0)