Skip to content

Commit e11adf6

Browse files
authored
Merge pull request #2013 from tshepang/add-a-paragraph
const functions: separate rule about users and rule about what is allowed in such functions
2 parents cd00008 + b71f831 commit e11adf6

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

src/const_eval.md

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -258,27 +258,39 @@ generics.
258258
r[const-eval.const-fn]
259259
## Const functions
260260
261-
r[const-eval.const-fn.general]
262-
A _const fn_ is a function that one is permitted to call from a const context.
261+
r[const-eval.const-fn.intro]
262+
A _const function_ is a function that can be called from a const context. It is defined with the `const` qualifier, and also includes [tuple struct] and [tuple enum variant] constructors.
263263
264-
r[const-eval.const-fn.usage]
265-
Declaring a function
266-
`const` has no effect on any existing uses, it only restricts the types that arguments and the
267-
return type may use, and restricts the function body to constant expressions.
264+
> [!EXAMPLE]
265+
> ```rust
266+
> const fn square(x: i32) -> i32 { x * x }
267+
>
268+
> const VALUE: i32 = square(12);
269+
> ```
268270
269271
r[const-eval.const-fn.const-context]
270-
When called from a const context, the function is interpreted by the
271-
compiler at compile time. The interpretation happens in the
272-
environment of the compilation target and not the host. So `usize` is
273-
`32` bits if you are compiling against a `32` bit system, irrelevant
274-
of whether you are building on a `64` bit or a `32` bit system.
272+
When called from a const context, a const function is interpreted by the compiler at compile time. The interpretation happens in the environment of the compilation target and not the host. So `usize` is `32` bits if you are compiling against a `32` bit system, irrelevant of whether you are building on a `64` bit or a `32` bit system.
273+
274+
r[const-eval.const-fn.outside-context]
275+
When a const function is called from outside a const context, it behaves the same as if it did not have the `const` qualifier.
276+
277+
r[const-eval.const-fn.body-restriction]
278+
The body of a const function may only use [constant expressions].
279+
280+
r[const-eval.const-fn.async]
281+
Const functions are not allowed to be [async].
282+
283+
r[const-eval.const-fn.type-restrictions]
284+
The types of a const function's parameters and return type are restricted to those that are compatible with a const context.
285+
<!-- TODO: Define the type restrictions. -->
275286
276287
[arithmetic]: expressions/operator-expr.md#arithmetic-and-logical-binary-operators
277288
[array expressions]: expressions/array-expr.md
278289
[array indexing]: expressions/array-expr.md#array-and-slice-indexing-expressions
279290
[array indexing]: expressions/array-expr.md#array-and-slice-indexing-expressions
280291
[array type length expressions]: types/array.md
281292
[assignment expressions]: expressions/operator-expr.md#assignment-expressions
293+
[async]: items/functions.md#async-functions
282294
[compound assignment expressions]: expressions/operator-expr.md#compound-assignment-expressions
283295
[block expressions]: expressions/block-expr.md
284296
[borrow]: expressions/operator-expr.md#borrow-operators
@@ -289,6 +301,7 @@ of whether you are building on a `64` bit or a `32` bit system.
289301
[const functions]: items/functions.md#const-functions
290302
[const generic argument]: items/generics.md#const-generics
291303
[const generic parameters]: items/generics.md#const-generics
304+
[constant expressions]: #constant-expressions
292305
[constants]: items/constant-items.md
293306
[Const parameters]: items/generics.md
294307
[dereference expression]: expressions/operator-expr.md#the-dereference-operator
@@ -321,5 +334,7 @@ of whether you are building on a `64` bit or a `32` bit system.
321334
[statics]: items/static-items.md
322335
[struct]: expressions/struct-expr.md
323336
[temporary lifetime extension]: destructors.scope.lifetime-extension
337+
[tuple enum variant]: items/enumerations.md
324338
[tuple expressions]: expressions/tuple-expr.md
339+
[tuple struct]: items/structs.md
325340
[while]: expressions/loop-expr.md#predicate-loops

src/items/functions.md

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -279,14 +279,7 @@ For other considerations and limitations regarding unwinding across FFI boundari
279279
r[items.fn.const]
280280
## Const functions
281281

282-
r[items.fn.const.intro]
283-
Functions qualified with the `const` keyword are [const functions], as are [tuple struct] and [tuple enum variant] constructors. _Const functions_ can be called from within [const contexts].
284-
285-
r[items.fn.const.extern]
286-
Const functions may use the [`extern`] function qualifier.
287-
288-
r[items.fn.const.exclusivity]
289-
Const functions are not allowed to be [async](#async-functions).
282+
See [const functions] for the definition of const functions.
290283

291284
r[items.fn.async]
292285
## Async functions
@@ -467,9 +460,6 @@ fn foo_oof(#[some_inert_attribute] arg: u8) {
467460

468461
[const contexts]: ../const_eval.md#const-context
469462
[const functions]: ../const_eval.md#const-functions
470-
[tuple struct]: structs.md
471-
[tuple enum variant]: enumerations.md
472-
[`extern`]: #extern-function-qualifier
473463
[external block]: external-blocks.md
474464
[path]: ../paths.md
475465
[block]: ../expressions/block-expr.md

0 commit comments

Comments
 (0)