rust/src/doc/nomicon/drop-flags.md

96 lines
3.9 KiB
Markdown
Raw Normal View History

% Drop Flags
The examples in the previous section introduce an interesting problem for Rust.
We have seen that it's possible to conditionally initialize, deinitialize, and
2015-07-28 15:13:54 -07:00
reinitialize locations of memory totally safely. For Copy types, this isn't
2015-07-14 09:56:10 -07:00
particularly notable since they're just a random pile of bits. However types
with destructors are a different story: Rust needs to know whether to call a
destructor whenever a variable is assigned to, or a variable goes out of scope.
How can it do this with conditional initialization?
2015-07-28 15:13:54 -07:00
Note that this is not a problem that all assignments need worry about. In
particular, assigning through a dereference unconditionally drops, and assigning
2015-07-30 18:47:02 -07:00
in a `let` unconditionally doesn't drop:
2015-07-28 15:13:54 -07:00
```
let mut x = Box::new(0); // let makes a fresh variable, so never need to drop
let y = &mut x;
*y = Box::new(1); // Deref assumes the referent is initialized, so always drops
```
This is only a problem when overwriting a previously initialized variable or
one of its subfields.
2015-07-14 09:56:10 -07:00
It turns out that Rust actually tracks whether a type should be dropped or not
*at runtime*. As a variable becomes initialized and uninitialized, a *drop flag*
2015-07-30 18:47:02 -07:00
for that variable is toggled. When a variable might need to be dropped, this
flag is evaluated to determine if it should be dropped.
2015-07-30 18:47:02 -07:00
Of course, it is often the case that a value's initialization state can be
statically known at every point in the program. If this is the case, then the
2015-07-28 15:13:54 -07:00
compiler can theoretically generate more efficient code! For instance, straight-
2015-07-14 09:56:10 -07:00
line code has such *static drop semantics*:
```rust
2015-07-14 09:56:10 -07:00
let mut x = Box::new(0); // x was uninit; just overwrite.
let mut y = x; // y was uninit; just overwrite and make x uninit.
x = Box::new(0); // x was uninit; just overwrite.
y = x; // y was init; Drop y, overwrite it, and make x uninit!
2015-07-28 15:13:54 -07:00
// y goes out of scope; y was init; Drop y!
// x goes out of scope; x was uninit; do nothing.
```
2015-07-30 18:47:02 -07:00
Similarly, branched code where all branches have the same behaviour with respect
to initialization has static drop semantics:
```rust
2015-07-14 11:07:00 -07:00
# let condition = true;
2015-07-14 09:56:10 -07:00
let mut x = Box::new(0); // x was uninit; just overwrite.
if condition {
2015-07-14 09:56:10 -07:00
drop(x) // x gets moved out; make x uninit.
} else {
2015-07-14 09:56:10 -07:00
println!("{}", x);
drop(x) // x gets moved out; make x uninit.
}
2015-07-14 09:56:10 -07:00
x = Box::new(0); // x was uninit; just overwrite.
2015-07-28 15:13:54 -07:00
// x goes out of scope; x was init; Drop x!
```
However code like this *requires* runtime information to correctly Drop:
```rust
2015-07-14 11:07:00 -07:00
# let condition = true;
let x;
if condition {
2015-07-14 09:56:10 -07:00
x = Box::new(0); // x was uninit; just overwrite.
println!("{}", x);
}
2015-07-30 18:47:02 -07:00
// x goes out of scope; x might be uninit;
2015-07-28 15:13:54 -07:00
// check the flag!
```
Of course, in this case it's trivial to retrieve static drop semantics:
```rust
2015-07-14 11:07:00 -07:00
# let condition = true;
if condition {
2015-07-14 09:56:10 -07:00
let x = Box::new(0);
println!("{}", x);
}
```
As of Rust 1.0, the drop flags are actually not-so-secretly stashed in a hidden
2015-07-28 15:13:54 -07:00
field of any type that implements Drop. Rust sets the drop flag by overwriting
2015-07-30 18:47:02 -07:00
the entire value with a particular bit pattern. This is pretty obviously Not
2015-07-28 15:13:54 -07:00
The Fastest and causes a bunch of trouble with optimizing code. It's legacy from
a time when you could do much more complex conditional initialization.
As such work is currently under way to move the flags out onto the stack frame
where they more reasonably belong. Unfortunately, this work will take some time
as it requires fairly substantial changes to the compiler.
Regardless, Rust programs don't need to worry about uninitialized values on
the stack for correctness. Although they might care for performance. Thankfully,
Rust makes it easy to take control here! Uninitialized values are there, and
2015-07-30 18:47:02 -07:00
you can work with them in Safe Rust, but you're never in danger.