From 20221fb914f652131c3d40e2bad46e61b1b0df3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20S=CC=B6c=CC=B6h=CC=B6n=CC=B6e=CC=B6i=CC=B6d=CC=B6?= =?UTF-8?q?e=CC=B6r=20Scherer?= Date: Fri, 5 Oct 2018 09:11:53 +0200 Subject: [PATCH 01/13] Document const fns --- src/items/functions.md | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/items/functions.md b/src/items/functions.md index 5fb071266e1fd..3c10a4972fa87 100644 --- a/src/items/functions.md +++ b/src/items/functions.md @@ -8,7 +8,7 @@ >       [_BlockExpression_] > > _FunctionFront_ :\ ->    `unsafe`? (`extern` _Abi_?)? +>    `const`? `unsafe`? (`extern` _Abi_?)? > > _Abi_ :\ >    [STRING_LITERAL] | [RAW_STRING_LITERAL] @@ -160,6 +160,38 @@ attributes], [`must_use`], [the procedural macro attributes], [the testing attributes], and [the optimization hint attributes]. +## Const functions + +Functions can be `const`, meaning they can be called from within array length expressions and the initializer of constants, statics and enum discriminants. When called from such a so-called "const context", the 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. + +If a `const fn` is called outside a "const context", it is indistinguishable from any other function. You can freely do anything with a `const fn` that you can do with a regular fn. + +`const fn`s have various restrictions to makes sure that you cannot define a `const fn` that can't be evaluated at compile-time. You will, for example, never be able to write a random number generator as a const fn. Calling a const fn at compile-time will always yield the same result as calling it at runtime, even if you call it multiple times. There's one exception to this rule: if you are doing complex floating point operations in extreme situations, then you might get (very slightly) different results. It is adviseable to not make array lengths and enum discriminants depend on floating point computations. + +Exhaustive list of permitted structures in `const fn`: + +1. type parameters where the parameters have any of the following as part of their bounds (either on `where` or directly on the parameters): + 1. lifetimes + 2. `Sized` + + This means that `` and `` + `` are all permitted. + Note that `?Sized` is the absence of a constraint when bounds have been fully elaborated + which includes adding implicit `Sized` bounds. + This entails that permitting `Sized` + lifetimes allows the above examples. + + This rule also applies to type parameters of items that contain `const fn`s. + +2. arithmetic operators on integers +3. boolean operators (except for `&&` and `||` which are banned since they are short-circuiting). +4. any kind of aggregate constructor (array, `struct`, `enum`, tuple, ...) +5. calls to other *safe* `const fn`s (methods and functions) +6. index operations on arrays and slices +7. field accesses on structs and tuples +8. reading from constants (but not statics, not even taking a reference to a static) +9. `&` and `*` (only dereferencing of references, not raw pointers) +10. casts except for raw pointer to `usize` casts +11. `const unsafe fn` is allowed, but the body must consist of safe operations only and you won't be able to call the `const unsafe fn` from within another `const fn` even if you use `unsafe` + [IDENTIFIER]: identifiers.html [RAW_STRING_LITERAL]: tokens.html#raw-string-literals [STRING_LITERAL]: tokens.html#string-literals From 41cbeb07695dfd9a718e787163787983a941e91d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20S=CC=B6c=CC=B6h=CC=B6n=CC=B6e=CC=B6i=CC=B6d=CC=B6?= =?UTF-8?q?e=CC=B6r=20Scherer?= Date: Mon, 8 Oct 2018 08:15:43 +0200 Subject: [PATCH 02/13] Address review comments --- src/items/functions.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/items/functions.md b/src/items/functions.md index 3c10a4972fa87..8d16748d670fc 100644 --- a/src/items/functions.md +++ b/src/items/functions.md @@ -170,26 +170,26 @@ If a `const fn` is called outside a "const context", it is indistinguishable fro Exhaustive list of permitted structures in `const fn`: -1. type parameters where the parameters have any of the following as part of their bounds (either on `where` or directly on the parameters): +1. type parameters where the parameters only have any of the following as part of their bounds (either on `where` or directly on the parameters): 1. lifetimes 2. `Sized` - This means that `` and `` + `` are all permitted. + This means that ``, `` and `` are all permitted. Note that `?Sized` is the absence of a constraint when bounds have been fully elaborated which includes adding implicit `Sized` bounds. This entails that permitting `Sized` + lifetimes allows the above examples. This rule also applies to type parameters of items that contain `const fn`s. -2. arithmetic operators on integers -3. boolean operators (except for `&&` and `||` which are banned since they are short-circuiting). +2. arithmetic and comparison operators on integers +3. all boolean operators except for `&&` and `||` which are banned since they are short-circuiting. 4. any kind of aggregate constructor (array, `struct`, `enum`, tuple, ...) 5. calls to other *safe* `const fn`s (methods and functions) 6. index operations on arrays and slices 7. field accesses on structs and tuples 8. reading from constants (but not statics, not even taking a reference to a static) 9. `&` and `*` (only dereferencing of references, not raw pointers) -10. casts except for raw pointer to `usize` casts +10. casts except for raw pointer to integer casts 11. `const unsafe fn` is allowed, but the body must consist of safe operations only and you won't be able to call the `const unsafe fn` from within another `const fn` even if you use `unsafe` [IDENTIFIER]: identifiers.html From 4e7bb217a7a970713b4b1cb9bb782e2830f17772 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20S=CC=B6c=CC=B6h=CC=B6n=CC=B6e=CC=B6i=CC=B6d=CC=B6?= =?UTF-8?q?e=CC=B6r=20Scherer?= Date: Mon, 8 Oct 2018 11:17:14 +0200 Subject: [PATCH 03/13] Address review comments --- src/items/functions.md | 76 +++++++++++++++++++++++++++--------------- 1 file changed, 50 insertions(+), 26 deletions(-) diff --git a/src/items/functions.md b/src/items/functions.md index 8d16748d670fc..a152f67e1af78 100644 --- a/src/items/functions.md +++ b/src/items/functions.md @@ -162,35 +162,57 @@ attributes]. ## Const functions -Functions can be `const`, meaning they can be called from within array length expressions and the initializer of constants, statics and enum discriminants. When called from such a so-called "const context", the 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. - -If a `const fn` is called outside a "const context", it is indistinguishable from any other function. You can freely do anything with a `const fn` that you can do with a regular fn. - -`const fn`s have various restrictions to makes sure that you cannot define a `const fn` that can't be evaluated at compile-time. You will, for example, never be able to write a random number generator as a const fn. Calling a const fn at compile-time will always yield the same result as calling it at runtime, even if you call it multiple times. There's one exception to this rule: if you are doing complex floating point operations in extreme situations, then you might get (very slightly) different results. It is adviseable to not make array lengths and enum discriminants depend on floating point computations. +Functions can be `const`, meaning they can be called from within array length +expressions and the initializer of constants, statics and enum discriminants. +When called from such a so-called "const context", the 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. + +If a `const fn` is called outside a "const context", it is indistinguishable +from any other function. You can freely do anything with a `const fn` that +you can do with a regular function. + +`const fn`s have various restrictions to makes sure that you cannot define a +`const fn` that can't be evaluated at compile-time. It is, for example, not +possible to write a random number generator as a const fn. Calling a +const fn at compile-time will always yield the same result as calling it at +runtime, even when called multiple times. There's one exception to this rule: +if you are doing complex floating point operations in extreme situations, +then you might get (very slightly) different results. +It is adviseable to not make array lengths and enum discriminants depend +on floating point computations. Exhaustive list of permitted structures in `const fn`: -1. type parameters where the parameters only have any of the following as part of their bounds (either on `where` or directly on the parameters): - 1. lifetimes - 2. `Sized` - - This means that ``, `` and `` are all permitted. - Note that `?Sized` is the absence of a constraint when bounds have been fully elaborated - which includes adding implicit `Sized` bounds. - This entails that permitting `Sized` + lifetimes allows the above examples. - - This rule also applies to type parameters of items that contain `const fn`s. - -2. arithmetic and comparison operators on integers -3. all boolean operators except for `&&` and `||` which are banned since they are short-circuiting. -4. any kind of aggregate constructor (array, `struct`, `enum`, tuple, ...) -5. calls to other *safe* `const fn`s (methods and functions) -6. index operations on arrays and slices -7. field accesses on structs and tuples -8. reading from constants (but not statics, not even taking a reference to a static) -9. `&` and `*` (only dereferencing of references, not raw pointers) -10. casts except for raw pointer to integer casts -11. `const unsafe fn` is allowed, but the body must consist of safe operations only and you won't be able to call the `const unsafe fn` from within another `const fn` even if you use `unsafe` +> **Note**: this list is more restrictive than what you can write in +regular constants + +* type parameters where the parameters only have any [trait bounds] + of the following kind: + * lifetimes + * `Sized` or [`?Sized`] + + This means that ``, `` and `` + are all permitted. + + This rule also applies to type parameters of impl blocks that + contain `const fn` methods + +* arithmetic and comparison operators on integers +* all boolean operators except for `&&` and `||` which are banned since + they are short-circuiting. +* any kind of aggregate constructor (array, `struct`, `enum`, tuple, ...) +* calls to other *safe* `const fn`s (whether by function call or method call) +* index expressions on arrays and slices +* field accesses on structs and tuples +* reading from constants (but not statics, not even taking a reference to a static) +* `&` and `*` (only dereferencing of references, not raw pointers) +* casts except for raw pointer to integer casts +* `const unsafe fn` is allowed, but the body must consist of safe operations + only and you won't be able to call the `const unsafe fn` from within another + `const fn` even if you use `unsafe` [IDENTIFIER]: identifiers.html [RAW_STRING_LITERAL]: tokens.html#raw-string-literals @@ -219,3 +241,5 @@ Exhaustive list of permitted structures in `const fn`: [`doc`]: attributes.html#documentation [`must_use`]: attributes.html#must_use [patterns]: patterns.html +[`?Sized`]: trait-bounds.html#sized +[trait bounds]: trait-bounds.html From 310812ae1e23f438fa10f68f15b68ae7e238eb90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20S=CC=B6c=CC=B6h=CC=B6n=CC=B6e=CC=B6i=CC=B6d=CC=B6?= =?UTF-8?q?e=CC=B6r=20Scherer?= Date: Mon, 8 Oct 2018 11:30:22 +0200 Subject: [PATCH 04/13] Create const_eval.md --- src/const_eval.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/const_eval.md diff --git a/src/const_eval.md b/src/const_eval.md new file mode 100644 index 0000000000000..bd9052824f126 --- /dev/null +++ b/src/const_eval.md @@ -0,0 +1,21 @@ +# Constant evaluation + +Constant evaluation is the process of computing the result of +[expressions] during compilation. + +## Const context + +A _const context_ is one of the following: + +* [array type length expressions] +* repeat expression length expessions +* the initializer of + * [constants] + * [statics] + * [enum discriminants] + +[array type length expressions]: types.html#array-and-slice-types +[enum discriminants]: items/enumerations.html#custom-discriminant-values-for-field-less-enumerations +[constants]: items/constant-items.html +[statics]: items/static-items.html +[expressions]: expressions.html#constant-expressions From eee560ccec35a137bebe124869cfae10bab86b15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20S=CC=B6c=CC=B6h=CC=B6n=CC=B6e=CC=B6i=CC=B6d=CC=B6?= =?UTF-8?q?e=CC=B6r=20Scherer?= Date: Mon, 8 Oct 2018 11:32:26 +0200 Subject: [PATCH 05/13] Update expressions.md --- src/expressions.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/expressions.md b/src/expressions.md index fd3d0e8838442..1f50f0c60528a 100644 --- a/src/expressions.md +++ b/src/expressions.md @@ -232,9 +232,7 @@ Implicit borrows may be taken in the following expressions: ## Constant expressions Certain types of expressions can be evaluated at compile time. These are called -_constant expressions_. Certain places, such as in -[constants](items/constant-items.html) and [statics](items/static-items.html), -require a constant expression, and are always evaluated at compile time. In +_constant expressions_ and are required in [const contexts]. In other places, such as in [`let` statements](statements.html#let-statements), constant expressions may be evaluated at compile time. If errors, such as out of bounds [array indexing] or [overflow] occurs, @@ -313,6 +311,7 @@ exist in `core::ops` and `core::cmp` with the same names. [let]: statements.html#let-statements [let statement]: statements.html#let-statements [Mutable `static` items]: items/static-items.html#mutable-statics +[const contexts]: const_eval.html [slice]: types.html#array-and-slice-types [static variables]: items/static-items.html [Temporary values]: #temporary-lifetimes From d9c33a422f879f6953447340024cf30306ab641e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20S=CC=B6c=CC=B6h=CC=B6n=CC=B6e=CC=B6i=CC=B6d=CC=B6?= =?UTF-8?q?e=CC=B6r=20Scherer?= Date: Mon, 8 Oct 2018 11:32:59 +0200 Subject: [PATCH 06/13] Fixup note --- src/items/functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/items/functions.md b/src/items/functions.md index a152f67e1af78..5204184ccc002 100644 --- a/src/items/functions.md +++ b/src/items/functions.md @@ -187,7 +187,7 @@ on floating point computations. Exhaustive list of permitted structures in `const fn`: > **Note**: this list is more restrictive than what you can write in -regular constants +> regular constants * type parameters where the parameters only have any [trait bounds] of the following kind: From 17485ac30601013f3f73671a7095c8f6dc024c92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20S=CC=B6c=CC=B6h=CC=B6n=CC=B6e=CC=B6i=CC=B6d=CC=B6?= =?UTF-8?q?e=CC=B6r=20Scherer?= Date: Mon, 8 Oct 2018 11:34:08 +0200 Subject: [PATCH 07/13] Crosslink const eval document --- src/items/functions.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/items/functions.md b/src/items/functions.md index 5204184ccc002..152b084d385b1 100644 --- a/src/items/functions.md +++ b/src/items/functions.md @@ -162,9 +162,8 @@ attributes]. ## Const functions -Functions can be `const`, meaning they can be called from within array length -expressions and the initializer of constants, statics and enum discriminants. -When called from such a so-called "const context", the function is interpreted +Functions can be `const`, meaning they can be called from within +[const contexts]. When called from a const context, the 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 @@ -224,6 +223,7 @@ Exhaustive list of permitted structures in `const fn`: [_Statement_]: statements.html [_Type_]: types.html [_WhereClause_]: items/generics.html#where-clauses +[const contexts]: const_eval.html [external blocks]: items/external-blocks.html [path]: paths.html [block]: expressions/block-expr.html From 9b2fb7ae548cfb6b801cda957a22c32050391a10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20S=CC=B6c=CC=B6h=CC=B6n=CC=B6e=CC=B6i=CC=B6d=CC=B6?= =?UTF-8?q?e=CC=B6r=20Scherer?= Date: Mon, 8 Oct 2018 14:04:09 +0200 Subject: [PATCH 08/13] Add const eval to summary --- src/SUMMARY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/SUMMARY.md b/src/SUMMARY.md index a15a5b17a5e6f..6259cfac95248 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -91,6 +91,8 @@ - [Behavior considered undefined](behavior-considered-undefined.md) - [Behavior not considered unsafe](behavior-not-considered-unsafe.md) +- [Constant Evalutation](const_eval.md) + [Appendix: Influences](influences.md) [Appendix: As-yet-undocumented Features](undocumented.md) From 6bc96f875e9a89450de809fb5cd934e1bacc156e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20S=CC=B6c=CC=B6h=CC=B6n=CC=B6e=CC=B6i=CC=B6d=CC=B6?= =?UTF-8?q?e=CC=B6r=20Scherer?= Date: Mon, 8 Oct 2018 14:13:56 +0200 Subject: [PATCH 09/13] Move const expressions to const_eval --- src/const_eval.md | 64 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/src/const_eval.md b/src/const_eval.md index bd9052824f126..a9ad1ca3d0c37 100644 --- a/src/const_eval.md +++ b/src/const_eval.md @@ -1,7 +1,43 @@ # Constant evaluation Constant evaluation is the process of computing the result of -[expressions] during compilation. +[expressions] during compilation. Only a subset of all expressions +can be evaluated at compile-time. + +## Constant expressions + +Certain types of expressions can be evaluated at compile time. These are called +_constant expressions_ and are required in const contexts. In +other places, such as in [`let` statements](statements.html#let-statements), +constant expressions may be evaluated at compile time. If errors, such as out +of bounds [array indexing] or [overflow] occurs, +then it is a compiler error if the value must be evaluated at compile time, +otherwise it is just a warning, but the code will most likely panic when run. + +The following expressions are constant expressions, so long as any operands are +also constant expressions and do not cause any [`Drop::drop`][destructors] calls +to be ran. + +* [Literals]. +* [Paths] to [functions](items/functions.html) and constants. + Recursively defining constants is not allowed. +* [Tuple expressions]. +* [Array expressions]. +* [Struct] expressions. +* [Enum variant] expressions. +* [Block expressions], including `unsafe` blocks, which only contain items and + possibly a constant tail expression. +* [Field] expressions. +* Index expressions, [array indexing] or [slice] with a `usize`. +* [Range expressions]. +* [Closure expressions] which don't capture variables from the environment. +* Built in [negation], [arithmetic, logical], [comparison] or [lazy boolean] + operators used on integer and floating point types, `bool` and `char`. +* Shared [borrow]s, except if applied to a type with [interior mutability]. +* The [dereference operator]. +* [Grouped] expressions. +* [Cast] expressions, except pointer to address and + function pointer to address casts. ## Const context @@ -18,4 +54,28 @@ A _const context_ is one of the following: [enum discriminants]: items/enumerations.html#custom-discriminant-values-for-field-less-enumerations [constants]: items/constant-items.html [statics]: items/static-items.html -[expressions]: expressions.html#constant-expressions +[expressions]: expressions.html +[array indexing]: expressions/array-expr.html#array-and-slice-indexing-expressions +[overflow]: expressions/operator-expr.html#overflow +[destructors]: destructors.html +[literals]: expressions/literal-expr.html +[paths]: expressions/path-expr.html +[tuple expressions]: expressions/tuple-expr.html +[array expressions]: expressions/array-expr.html +[struct]: expressions/struct-expr.html +[enum variant]: expressions/enum-variant-expr.html +[block expressions]: expressions/block-expr.html +[field]: expressions/field-expr.html +[array indexing]: expressions/array-expr.html#array-and-slice-indexing-expressions +[slice]: types.html#array-and-slice-types +[range expressions]: expressions/range-expr.html +[closure expressions]: expressions/closure-expr.html +[negation]: expressions/operator-expr.html#negation-operators +[arithmetic, logical]: expressions/operator-expr.html#arithmetic-and-logical-binary-operators +[comparison]: expressions/operator-expr.html#comparison-operators +[lazy boolean]: expressions/operator-expr.html#lazy-boolean-operators +[borrow]: expressions/operator-expr.html#borrow-operators +[interior mutability]: interior-mutability.html +[dereference operator]: expressions/operator-expr.html#the-dereference-operator +[grouped]: expressions/grouped-expr.html +[cast]: expressions/operator-expr.html#type-cast-expressions From 3e3be4250584bcf6ce3ef0413ae8efcb2b682a68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20S=CC=B6c=CC=B6h=CC=B6n=CC=B6e=CC=B6i=CC=B6d=CC=B6?= =?UTF-8?q?e=CC=B6r=20Scherer?= Date: Mon, 8 Oct 2018 14:14:09 +0200 Subject: [PATCH 10/13] Move const expressions to const_eval --- src/expressions.md | 35 ----------------------------------- 1 file changed, 35 deletions(-) diff --git a/src/expressions.md b/src/expressions.md index 1f50f0c60528a..a00a19643d495 100644 --- a/src/expressions.md +++ b/src/expressions.md @@ -229,41 +229,6 @@ Implicit borrows may be taken in the following expressions: * Operands of [comparison]. * Left operands of the [compound assignment]. -## Constant expressions - -Certain types of expressions can be evaluated at compile time. These are called -_constant expressions_ and are required in [const contexts]. In -other places, such as in [`let` statements](statements.html#let-statements), -constant expressions may be evaluated at compile time. If errors, such as out -of bounds [array indexing] or [overflow] occurs, -then it is a compiler error if the value must be evaluated at compile time, -otherwise it is just a warning, but the code will most likely panic when run. - -The following expressions are constant expressions, so long as any operands are -also constant expressions and do not cause any [`Drop::drop`][destructors] calls -to be ran. - -* [Literals]. -* [Paths] to [functions](items/functions.html) and constants. - Recursively defining constants is not allowed. -* [Tuple expressions]. -* [Array expressions]. -* [Struct] expressions. -* [Enum variant] expressions. -* [Block expressions], including `unsafe` blocks, which only contain items and - possibly a constant tail expression. -* [Field] expressions. -* Index expressions, [array indexing] or [slice] with a `usize`. -* [Range expressions]. -* [Closure expressions] which don't capture variables from the environment. -* Built in [negation], [arithmetic, logical], [comparison] or [lazy boolean] - operators used on integer and floating point types, `bool` and `char`. -* Shared [borrow]s, except if applied to a type with [interior mutability]. -* The [dereference operator]. -* [Grouped] expressions. -* [Cast] expressions, except pointer to address and - function pointer to address casts. - ## Overloading Traits Many of the following operators and expressions can also be overloaded for From 3a7b6d863183079d46d92313df7875e22801de8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20S=CC=B6c=CC=B6h=CC=B6n=CC=B6e=CC=B6i=CC=B6d=CC=B6?= =?UTF-8?q?e=CC=B6r=20Scherer?= Date: Tue, 9 Oct 2018 09:40:58 +0200 Subject: [PATCH 11/13] Mention const fn calls in list of valid const expressions --- src/const_eval.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/const_eval.md b/src/const_eval.md index a9ad1ca3d0c37..ab8112ba10838 100644 --- a/src/const_eval.md +++ b/src/const_eval.md @@ -37,7 +37,8 @@ to be ran. * The [dereference operator]. * [Grouped] expressions. * [Cast] expressions, except pointer to address and - function pointer to address casts. +* function pointer to address casts. +* function and method calls ## Const context From b89691f20ecdf28c25dfd8ee18db2662bd246c18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20S=CC=B6c=CC=B6h=CC=B6n=CC=B6e=CC=B6i=CC=B6d=CC=B6?= =?UTF-8?q?e=CC=B6r=20Scherer?= Date: Fri, 12 Oct 2018 18:42:09 +0200 Subject: [PATCH 12/13] Clarify function calls to "const function calls" --- src/const_eval.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/const_eval.md b/src/const_eval.md index ab8112ba10838..410e609e22a1b 100644 --- a/src/const_eval.md +++ b/src/const_eval.md @@ -37,8 +37,8 @@ to be ran. * The [dereference operator]. * [Grouped] expressions. * [Cast] expressions, except pointer to address and -* function pointer to address casts. -* function and method calls + function pointer to address casts. +* calls of const functions and const methods ## Const context From 259ac5f6923b4cc7d6a926373f4f441fc7ff8886 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20S=CC=B6c=CC=B6h=CC=B6n=CC=B6e=CC=B6i=CC=B6d=CC=B6?= =?UTF-8?q?e=CC=B6r=20Scherer?= Date: Fri, 12 Oct 2018 18:45:11 +0200 Subject: [PATCH 13/13] Prefer "const function" over `const fn` --- src/items/functions.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/items/functions.md b/src/items/functions.md index 152b084d385b1..9c44fc49d7c77 100644 --- a/src/items/functions.md +++ b/src/items/functions.md @@ -169,21 +169,21 @@ 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. -If a `const fn` is called outside a "const context", it is indistinguishable -from any other function. You can freely do anything with a `const fn` that +If a const function is called outside a "const context", it is indistinguishable +from any other function. You can freely do anything with a const function that you can do with a regular function. -`const fn`s have various restrictions to makes sure that you cannot define a -`const fn` that can't be evaluated at compile-time. It is, for example, not -possible to write a random number generator as a const fn. Calling a -const fn at compile-time will always yield the same result as calling it at +const functions have various restrictions to makes sure that you cannot define a +const function that can't be evaluated at compile-time. It is, for example, not +possible to write a random number generator as a const function. Calling a +const function at compile-time will always yield the same result as calling it at runtime, even when called multiple times. There's one exception to this rule: if you are doing complex floating point operations in extreme situations, then you might get (very slightly) different results. It is adviseable to not make array lengths and enum discriminants depend on floating point computations. -Exhaustive list of permitted structures in `const fn`: +Exhaustive list of permitted structures in const functions: > **Note**: this list is more restrictive than what you can write in > regular constants @@ -197,13 +197,13 @@ Exhaustive list of permitted structures in `const fn`: are all permitted. This rule also applies to type parameters of impl blocks that - contain `const fn` methods + contain const methods * arithmetic and comparison operators on integers * all boolean operators except for `&&` and `||` which are banned since they are short-circuiting. * any kind of aggregate constructor (array, `struct`, `enum`, tuple, ...) -* calls to other *safe* `const fn`s (whether by function call or method call) +* calls to other *safe* const functions (whether by function call or method call) * index expressions on arrays and slices * field accesses on structs and tuples * reading from constants (but not statics, not even taking a reference to a static) @@ -211,7 +211,7 @@ Exhaustive list of permitted structures in `const fn`: * casts except for raw pointer to integer casts * `const unsafe fn` is allowed, but the body must consist of safe operations only and you won't be able to call the `const unsafe fn` from within another - `const fn` even if you use `unsafe` + const function even if you use `unsafe` [IDENTIFIER]: identifiers.html [RAW_STRING_LITERAL]: tokens.html#raw-string-literals