Skip to content

Commit b71f831

Browse files
committed
Combine the two const function sections
There were two sections defining const functions, which was a little confusing to me and made it a little difficult to maintain them. This combines them so that there is only one section. It could have gone in either chapter, but I arbitrarily picked const-eval. This removes `items.fn.const.extern`. The reference generally assumes that if something can be defined, it can be used unless explicitly restricted. This used to be a restriction, but it morphed over time as `extern` support was slowly added. I noticed that `const-eval.const-fn.type-restrictions` is not clear to me and needs further work to explain the type restrictions.
1 parent 431465a commit b71f831

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

src/const_eval.md

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -258,28 +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 `const` has no effect on any existing uses.
266-
267-
r[const-eval.const-fn.restrictions]
268-
Declaring a function `const` restricts the types that arguments and the 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+
> ```
269270
270271
r[const-eval.const-fn.const-context]
271-
When called from a const context, the function is interpreted by the
272-
compiler at compile time. The interpretation happens in the
273-
environment of the compilation target and not the host. So `usize` is
274-
`32` bits if you are compiling against a `32` bit system, irrelevant
275-
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. -->
276286
277287
[arithmetic]: expressions/operator-expr.md#arithmetic-and-logical-binary-operators
278288
[array expressions]: expressions/array-expr.md
279289
[array indexing]: expressions/array-expr.md#array-and-slice-indexing-expressions
280290
[array indexing]: expressions/array-expr.md#array-and-slice-indexing-expressions
281291
[array type length expressions]: types/array.md
282292
[assignment expressions]: expressions/operator-expr.md#assignment-expressions
293+
[async]: items/functions.md#async-functions
283294
[compound assignment expressions]: expressions/operator-expr.md#compound-assignment-expressions
284295
[block expressions]: expressions/block-expr.md
285296
[borrow]: expressions/operator-expr.md#borrow-operators
@@ -290,6 +301,7 @@ of whether you are building on a `64` bit or a `32` bit system.
290301
[const functions]: items/functions.md#const-functions
291302
[const generic argument]: items/generics.md#const-generics
292303
[const generic parameters]: items/generics.md#const-generics
304+
[constant expressions]: #constant-expressions
293305
[constants]: items/constant-items.md
294306
[Const parameters]: items/generics.md
295307
[dereference expression]: expressions/operator-expr.md#the-dereference-operator
@@ -322,5 +334,7 @@ of whether you are building on a `64` bit or a `32` bit system.
322334
[statics]: items/static-items.md
323335
[struct]: expressions/struct-expr.md
324336
[temporary lifetime extension]: destructors.scope.lifetime-extension
337+
[tuple enum variant]: items/enumerations.md
325338
[tuple expressions]: expressions/tuple-expr.md
339+
[tuple struct]: items/structs.md
326340
[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)