From 31f087e3888b483eb12b5a8db7ac6ff1be4b9802 Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Tue, 30 Jun 2020 17:24:08 +0200 Subject: [PATCH 1/5] Update const eval chapter to latest changes --- src/const_eval.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/const_eval.md b/src/const_eval.md index 5d6c367e6..0fd64a016 100644 --- a/src/const_eval.md +++ b/src/const_eval.md @@ -27,9 +27,7 @@ to be run. * [Struct] expressions. * [Enum variant] expressions. * [Block expressions], including `unsafe` blocks. - * [let statements] and thus irrefutable [patterns], with the caveat that until `if` and `match` - are implemented, one cannot use both short circuiting operators (`&&` and `||`) and let - statements within the same constant. + * [let statements] and thus irrefutable [patterns], including mutable bindings * [assignment expressions] * [compound assignment expressions] * [expression statements] @@ -45,6 +43,8 @@ to be run. * [Cast] expressions, except pointer to address and function pointer to address casts. * Calls of [const functions] and const methods. +* [loop], [while] and `while let` expressions. +* [if], [`if let`] and [match] expressions. ## Const context @@ -81,10 +81,14 @@ A _const context_ is one of the following: [functions]: items/functions.md [grouped]: expressions/grouped-expr.md [interior mutability]: interior-mutability.md +[if]: expressions/if-expr.md#if-expressions +[`if let`]: expressions/if-expr.md#if-let-expressions [lazy boolean]: expressions/operator-expr.md#lazy-boolean-operators [let statements]: statements.md#let-statements [literals]: expressions/literal-expr.md [logical]: expressions/operator-expr.md#arithmetic-and-logical-binary-operators +[loop]: expressions/loop-expr.md#infinite-loops +[match]: expressions/match-expr.md [negation]: expressions/operator-expr.md#negation-operators [overflow]: expressions/operator-expr.md#overflow [paths]: expressions/path-expr.md @@ -94,3 +98,5 @@ A _const context_ is one of the following: [statics]: items/static-items.md [struct]: expressions/struct-expr.md [tuple expressions]: expressions/tuple-expr.md +[while]: expressions/loop-expr.md#predicate-loops +[`while let`]: expressions/loop-expr.md#predicate-pattern-loops From 7cc2ee2d562df7d29a6118f2e838f756c9b704ab Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Wed, 1 Jul 2020 19:36:32 +0200 Subject: [PATCH 2/5] Address review comments --- src/const_eval.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/const_eval.md b/src/const_eval.md index 0fd64a016..d392a63a8 100644 --- a/src/const_eval.md +++ b/src/const_eval.md @@ -43,7 +43,7 @@ to be run. * [Cast] expressions, except pointer to address and function pointer to address casts. * Calls of [const functions] and const methods. -* [loop], [while] and `while let` expressions. +* [loop], [while] and [`while let`] expressions. * [if], [`if let`] and [match] expressions. ## Const context @@ -57,6 +57,12 @@ A _const context_ is one of the following: * [statics] * [enum discriminants] +## Const Functions + +A _const fn_ is a function that one is permitted to call from a const context. Declaring a function +`const` has no effect on any existing uses, it only restricts the types that arguments and the +return type may use, as well as prevent various expressions from being used within it. + [arithmetic]: expressions/operator-expr.md#arithmetic-and-logical-binary-operators [array expressions]: expressions/array-expr.md [array indexing]: expressions/array-expr.md#array-and-slice-indexing-expressions From 167d75106949fcd83eb6925ee91a0b6e15fcc7a4 Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Wed, 1 Jul 2020 19:42:47 +0200 Subject: [PATCH 3/5] Explain differences between const functions and const contexts --- src/const_eval.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/const_eval.md b/src/const_eval.md index d392a63a8..982d23bd0 100644 --- a/src/const_eval.md +++ b/src/const_eval.md @@ -63,6 +63,16 @@ A _const fn_ is a function that one is permitted to call from a const context. D `const` has no effect on any existing uses, it only restricts the types that arguments and the return type may use, as well as prevent various expressions from being used within it. +Notable features that const contexts have, but const fn haven't are: + +* floating point types +* `dyn Trait` types +* generic bounds on generic parameters beyond `Sized` +* dereferencing of raw pointers +* casting raw pointers to integers +* comparing raw pointers +* union field access + [arithmetic]: expressions/operator-expr.md#arithmetic-and-logical-binary-operators [array expressions]: expressions/array-expr.md [array indexing]: expressions/array-expr.md#array-and-slice-indexing-expressions From 8acf9072697096453ad415924d495022cdc7d19b Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Sun, 12 Jul 2020 18:57:29 +0200 Subject: [PATCH 4/5] Some more precise explanations of which features are allowed in const/const fn --- src/const_eval.md | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/const_eval.md b/src/const_eval.md index 982d23bd0..2447bacb3 100644 --- a/src/const_eval.md +++ b/src/const_eval.md @@ -38,10 +38,12 @@ to be run. * 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]. +* The [dereference operator] except for raw pointers. * [Grouped] expressions. -* [Cast] expressions, except pointer to address and - function pointer to address casts. +* [Cast] expressions, except + * pointer to address casts, + * function pointer to address casts, and + * unsizing casts to trait objects. * Calls of [const functions] and const methods. * [loop], [while] and [`while let`] expressions. * [if], [`if let`] and [match] expressions. @@ -65,13 +67,13 @@ return type may use, as well as prevent various expressions from being used with Notable features that const contexts have, but const fn haven't are: -* floating point types +* floating point operations + * floating point values are treated just like generic parameters without trait bounds beyond + `Copy`. So you cannot do anything with them but copy/move them around. * `dyn Trait` types * generic bounds on generic parameters beyond `Sized` -* dereferencing of raw pointers -* casting raw pointers to integers * comparing raw pointers -* union field access +* union field access or `transmute` invocations. [arithmetic]: expressions/operator-expr.md#arithmetic-and-logical-binary-operators [array expressions]: expressions/array-expr.md From e9d9b2e7db91a0f58b788604648f3aa41495dc3e Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Sun, 12 Jul 2020 19:50:51 +0200 Subject: [PATCH 5/5] Link to transmute and make it its own point --- src/const_eval.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/const_eval.md b/src/const_eval.md index 2447bacb3..974c4f92b 100644 --- a/src/const_eval.md +++ b/src/const_eval.md @@ -73,7 +73,8 @@ Notable features that const contexts have, but const fn haven't are: * `dyn Trait` types * generic bounds on generic parameters beyond `Sized` * comparing raw pointers -* union field access or `transmute` invocations. +* union field access +* [`transmute`] invocations. [arithmetic]: expressions/operator-expr.md#arithmetic-and-logical-binary-operators [array expressions]: expressions/array-expr.md @@ -116,5 +117,6 @@ Notable features that const contexts have, but const fn haven't are: [statics]: items/static-items.md [struct]: expressions/struct-expr.md [tuple expressions]: expressions/tuple-expr.md +[`transmute`]: ../std/mem/fn.transmute.html [while]: expressions/loop-expr.md#predicate-loops [`while let`]: expressions/loop-expr.md#predicate-pattern-loops