|
| 1 | +- Feature Name: const_locals |
| 2 | +- Start Date: 2018-01-11 |
| 3 | +- RFC PR: (leave this empty) |
| 4 | +- Rust Issue: (leave this empty) |
| 5 | + |
| 6 | +# Summary |
| 7 | +[summary]: #summary |
| 8 | + |
| 9 | +Allow `let` bindings in the body of constants and const fns. Additionally enable |
| 10 | +destructuring in `let` bindings and const fn arguments. |
| 11 | + |
| 12 | +# Motivation |
| 13 | +[motivation]: #motivation |
| 14 | + |
| 15 | +It makes writing const fns much more like writing regular functions and is |
| 16 | +not possible right now because the old constant evaluator was a constant folder |
| 17 | +that could only process expressions. With the miri const evaluator this feature |
| 18 | +exists but is still disallowed. |
| 19 | + |
| 20 | +# Guide-level explanation |
| 21 | +[guide-level-explanation]: #guide-level-explanation |
| 22 | + |
| 23 | +`let` bindings in constants and const fn work just like `let` bindings |
| 24 | +everywhere else. Historically these did not exist in constants and const fn |
| 25 | +because it would have been very hard to support them in the old const evaluator. |
| 26 | + |
| 27 | +# Reference-level explanation |
| 28 | +[reference-level-explanation]: #reference-level-explanation |
| 29 | + |
| 30 | +Expressions like `a + b + c` are already transformed to |
| 31 | + |
| 32 | +```rust |
| 33 | +let tmp = a + b; |
| 34 | +tmp + c |
| 35 | +``` |
| 36 | + |
| 37 | +With this RFC we can create bindings ourselves instead of only allowing compiler |
| 38 | +generated bindings. |
| 39 | + |
| 40 | +# Drawbacks |
| 41 | +[drawbacks]: #drawbacks |
| 42 | + |
| 43 | +You can create mutable locals in constants and then actually modify them. This |
| 44 | +has no real impact on the constness, as the mutation happens entirely at compile |
| 45 | +time and results in an immutable value. |
| 46 | + |
| 47 | +# Rationale and alternatives |
| 48 | +[alternatives]: #alternatives |
| 49 | + |
| 50 | +The backend already supports this 100%. This is essentially just disabling a |
| 51 | +check |
| 52 | + |
| 53 | +## Why is this design the best in the space of possible designs? |
| 54 | + |
| 55 | +Being the only design makes it the best design by definition |
| 56 | + |
| 57 | +## What is the impact of not doing this? |
| 58 | + |
| 59 | +Not having locals and destructuring severely limits the functions that can be |
| 60 | +turned into const fn and generally leads to unreadable const fns. |
| 61 | + |
| 62 | +# Unresolved questions |
| 63 | +[unresolved]: #unresolved-questions |
0 commit comments