Skip to content

Commit 6166408

Browse files
authored
Merge pull request #2341 from oli-obk/const_locals
Allow locals and destructuring in const fn
2 parents b00660e + f9d79d6 commit 6166408

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

text/2341-const-locals.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
- Feature Name: `const_locals`
2+
- Start Date: 2018-01-11
3+
- RFC PR: [rust-lang/rfcs#2341](https://github.com/rust-lang/rfcs/pull/2341)
4+
- Rust Issue: [rust-lang/rust#48821](https://github.com/rust-lang/rust/issues/48821)
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+
This means that you can only move out of any let binding once, even though in a
28+
const environment obtaining a copy of the object could be done by executing the
29+
code twice, side effect free. All invariants held by runtime code are also
30+
upheld by constant evaluation.
31+
32+
# Reference-level explanation
33+
[reference-level-explanation]: #reference-level-explanation
34+
35+
Expressions like `a + b + c` are already transformed to
36+
37+
```rust
38+
let tmp = a + b;
39+
tmp + c
40+
```
41+
42+
With this RFC we can create bindings ourselves instead of only allowing compiler
43+
generated bindings.
44+
45+
# Drawbacks
46+
[drawbacks]: #drawbacks
47+
48+
You can create mutable locals in constants and then actually modify them. This
49+
has no real impact on the constness, as the mutation happens entirely at compile
50+
time and results in an immutable value.
51+
52+
# Rationale and alternatives
53+
[alternatives]: #alternatives
54+
55+
The backend already supports this 100%. This is essentially just disabling a
56+
check
57+
58+
## Why is this design the best in the space of possible designs?
59+
60+
Being the only design makes it the best design by definition
61+
62+
## What is the impact of not doing this?
63+
64+
Not having locals and destructuring severely limits the functions that can be
65+
turned into const fn and generally leads to unreadable const fns.
66+
67+
# Unresolved questions
68+
[unresolved]: #unresolved-questions

0 commit comments

Comments
 (0)