Skip to content

Commit 406dd2a

Browse files
committed
Merge remote-tracking branch 'pnkfelix/no-array-elem-moves'
2 parents cbd1e62 + 58ce407 commit 406dd2a

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

text/0000-no-array-elem-moves.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
- Start Date: 2014-12-19
2+
- RFC PR: (leave this empty)
3+
- Rust Issue: (leave this empty)
4+
5+
# Summary
6+
7+
In order to prepare for an expected future implementation of
8+
[non-zeroing dynamic drop], remove support for:
9+
10+
* moving individual elements into an *uninitialized* fixed-sized array, and
11+
12+
* moving individual elements out of fixed-sized arrays `[T; n]`,
13+
(copying and borrowing such elements is still permitted).
14+
15+
[non-zeroing dynamic drop]: https://github.com/rust-lang/rfcs/pull/320
16+
17+
# Motivation
18+
19+
If we want to continue supporting dynamic drop while also removing
20+
automatic memory zeroing and drop-flags, then we need to either (1.)
21+
adopt potential complex code generation strategies to support arrays
22+
with only *some* elements initialized (as discussed in the [unresolved
23+
questions for RFC PR 320], or we need to (2.) remove support for
24+
constructing such arrays in safe code.
25+
26+
[unresolved questions for RFC PR 320]: https://github.com/pnkfelix/rfcs/blob/6288739c584ee6830aa0f79f983c5e762269c562/active/0000-nonzeroing-dynamic-drop.md#how-to-handle-moves-out-of-arrayindex_expr
27+
28+
This RFC is proposing the second tack.
29+
30+
The expectation is that relatively few libraries need to support
31+
moving out of fixed-sized arrays (and even fewer take advantage of
32+
being able to initialize individual elements of an uninitialized
33+
array, as supporting this was almost certainly not intentional in the
34+
language design). Therefore removing the feature from the language
35+
will present relatively little burden.
36+
37+
# Detailed design
38+
39+
If an expression `e` has type `[T; n]` and `T` does not implement
40+
`Copy`, then it will be illegal to use `e[i]` in an r-value position.
41+
42+
If an expression `e` has type `[T; n]` expression `e[i] = <expr>`
43+
will be made illegal at points in the control flow where `e` has not
44+
yet been initialized.
45+
46+
Note that it *remains* legal to overwrite an element in an initialized
47+
array: `e[i] = <expr>`, as today. This will continue to drop the
48+
overwritten element before moving the result of `<expr>` into place.
49+
50+
Note also that the proposed change has no effect on the semantics of
51+
destructuring bind; i.e. `fn([a, b, c]: [Elem; 3]) { ... }` will
52+
continue to work as much as it does today.
53+
54+
A prototype implementation has been posted at [Rust PR 21930].
55+
56+
[Rust PR 21930]: https://github.com/rust-lang/rust/pull/21930
57+
58+
# Drawbacks
59+
60+
* Adopting this RFC is introducing a limitation on the language based
61+
on a hypothetical optimization that has not yet been implemented
62+
(though much of the ground work for its supporting analyses are
63+
done).
64+
65+
Also, as noted in the [comment thread from RFC PR 320]
66+
67+
[comment thread from RFC PR 320]: https://github.com/rust-lang/rfcs/pull/320#issuecomment-59533551
68+
69+
* We support moving a single element out of an n-tuple, and "by
70+
analogy" we should support moving out of `[T; n]`
71+
(Note that one can still move out of `[T; n]` in some cases
72+
via destructuring bind.)
73+
74+
* It is "nice" to be able to write
75+
```rust
76+
fn grab_random_from(actions: [Action; 5]) -> Action { actions[rand_index()] }
77+
```
78+
to express this now, one would be forced to instead use clone() (or
79+
pass in a `Vec` and do some element swapping).
80+
81+
82+
# Alternatives
83+
84+
We can just leave things as they are; there are hypothetical
85+
code-generation strategies for supporting non-zeroing drop even with
86+
this feature, as discussed in the [comment thread from RFC PR 320].
87+
88+
# Unresolved questions
89+
90+
None
91+

0 commit comments

Comments
 (0)