From a048a3e9a124eaa03cd02f3c9d52a6e3a8366a8e Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Tue, 20 Feb 2018 14:26:07 +0100 Subject: [PATCH 1/3] Allow locals and destructuring in const fn --- text/0000-const-locals.md | 63 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 text/0000-const-locals.md diff --git a/text/0000-const-locals.md b/text/0000-const-locals.md new file mode 100644 index 00000000000..23fa9e14e5b --- /dev/null +++ b/text/0000-const-locals.md @@ -0,0 +1,63 @@ +- Feature Name: const_locals +- Start Date: 2018-01-11 +- RFC PR: (leave this empty) +- Rust Issue: (leave this empty) + +# Summary +[summary]: #summary + +Allow `let` bindings in the body of constants and const fns. Additionally enable +destructuring in `let` bindings and const fn arguments. + +# Motivation +[motivation]: #motivation + +It makes writing const fns much more like writing regular functions and is +not possible right now because the old constant evaluator was a constant folder +that could only process expressions. With the miri const evaluator this feature +exists but is still disallowed. + +# Guide-level explanation +[guide-level-explanation]: #guide-level-explanation + +`let` bindings in constants and const fn work just like `let` bindings +everywhere else. Historically these did not exist in constants and const fn +because it would have been very hard to support them in the old const evaluator. + +# Reference-level explanation +[reference-level-explanation]: #reference-level-explanation + +Expressions like `a + b + c` are already transformed to + +```rust +let tmp = a + b; +tmp + c +``` + +With this RFC we can create bindings ourselves instead of only allowing compiler +generated bindings. + +# Drawbacks +[drawbacks]: #drawbacks + +You can create mutable locals in constants and then actually modify them. This +has no real impact on the constness, as the mutation happens entirely at compile +time and results in an immutable value. + +# Rationale and alternatives +[alternatives]: #alternatives + +The backend already supports this 100%. This is essentially just disabling a +check + +## Why is this design the best in the space of possible designs? + +Being the only design makes it the best design by definition + +## What is the impact of not doing this? + +Not having locals and destructuring severely limits the functions that can be +turned into const fn and generally leads to unreadable const fns. + +# Unresolved questions +[unresolved]: #unresolved-questions From 0f24c33097dfc34a5dbbbdf6a955de21948234c0 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Tue, 20 Feb 2018 16:42:00 +0100 Subject: [PATCH 2/3] Add a comment about `!Copy` types in let bindings in constants --- text/0000-const-locals.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/text/0000-const-locals.md b/text/0000-const-locals.md index 23fa9e14e5b..f2c186c259a 100644 --- a/text/0000-const-locals.md +++ b/text/0000-const-locals.md @@ -24,6 +24,11 @@ exists but is still disallowed. everywhere else. Historically these did not exist in constants and const fn because it would have been very hard to support them in the old const evaluator. +This means that you can only move out of any let binding once, even though in a +const environment obtaining a copy of the object could be done by executing the +code twice, side effect free. All invariants held by runtime code are also +upheld by constant evaluation. + # Reference-level explanation [reference-level-explanation]: #reference-level-explanation From f9d79d648f7834e8fb1e337f5bddffef0fc9f1d2 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Wed, 7 Mar 2018 17:31:11 +0100 Subject: [PATCH 3/3] RFC 2341 --- text/{0000-const-locals.md => 2341-const-locals.md} | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename text/{0000-const-locals.md => 2341-const-locals.md} (91%) diff --git a/text/0000-const-locals.md b/text/2341-const-locals.md similarity index 91% rename from text/0000-const-locals.md rename to text/2341-const-locals.md index f2c186c259a..f85496a2a91 100644 --- a/text/0000-const-locals.md +++ b/text/2341-const-locals.md @@ -1,7 +1,7 @@ -- Feature Name: const_locals +- Feature Name: `const_locals` - Start Date: 2018-01-11 -- RFC PR: (leave this empty) -- Rust Issue: (leave this empty) +- RFC PR: [rust-lang/rfcs#2341](https://github.com/rust-lang/rfcs/pull/2341) +- Rust Issue: [rust-lang/rust#48821](https://github.com/rust-lang/rust/issues/48821) # Summary [summary]: #summary