|
| 1 | +% Drop Flags |
| 2 | + |
| 3 | +The examples in the previous section introduce an interesting problem for Rust. |
| 4 | +We have seen that's possible to conditionally initialize, deinitialize, and |
| 5 | +*reinitialize* locations of memory totally safely. For Copy types, this isn't |
| 6 | +particularly notable since they're just a random pile of bits. However types with |
| 7 | +destructors are a different story: Rust needs to know whether to call a destructor |
| 8 | +whenever a variable is assigned to, or a variable goes out of scope. How can it |
| 9 | +do this with conditional initialization? |
| 10 | + |
| 11 | +It turns out that Rust actually tracks whether a type should be dropped or not *at |
| 12 | +runtime*. As a variable becomes initialized and uninitialized, a *drop flag* for |
| 13 | +that variable is toggled. When a variable *might* need to be dropped, this flag |
| 14 | +is evaluated to determine if it *should* be dropped. |
| 15 | + |
| 16 | +Of course, it is *often* the case that a value's initialization state can be |
| 17 | +*statically* known at every point in the program. If this is the case, then the |
| 18 | +compiler can theoretically generate more effecient code! For instance, |
| 19 | +straight-line code has such *static drop semantics*: |
| 20 | + |
| 21 | +```rust |
| 22 | +let mut x = Box::new(0); // x was uninit |
| 23 | +let mut y = x; // y was uninit |
| 24 | +x = Box::new(0); // x was uninit |
| 25 | +y = x; // y was init; Drop y! |
| 26 | + // y was init; Drop y! |
| 27 | + // x was uninit |
| 28 | +``` |
| 29 | + |
| 30 | +And even branched code where all branches have the same behaviour with respect |
| 31 | +to initialization: |
| 32 | + |
| 33 | +```rust |
| 34 | +let mut x = Box::new(0); // x was uninit |
| 35 | +if condition { |
| 36 | + drop(x) // x gets moved out |
| 37 | +} else { |
| 38 | + println!("{}", x); |
| 39 | + drop(x) // x gets moved out |
| 40 | +} |
| 41 | +x = Box::new(0); // x was uninit |
| 42 | + // x was init; Drop x! |
| 43 | +``` |
| 44 | + |
| 45 | +However code like this *requires* runtime information to correctly Drop: |
| 46 | + |
| 47 | +```rust |
| 48 | +let x; |
| 49 | +if condition { |
| 50 | + x = Box::new(0); // x was uninit |
| 51 | + println!("{}", x); |
| 52 | +} |
| 53 | + // x might be uninit; check the flag! |
| 54 | +``` |
| 55 | + |
| 56 | +Of course, in this case it's trivial to retrieve static drop semantics: |
| 57 | + |
| 58 | +```rust |
| 59 | +if condition { |
| 60 | + let x = Box::new(0); |
| 61 | + println!("{}", x); |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +As of Rust 1.0, the drop flags are actually not-so-secretly stashed in a hidden |
| 66 | +field of any type that implements Drop. Rust sets the drop flag by |
| 67 | +overwriting the *entire* value with a particular byte. This is pretty obviously |
| 68 | +Not The Fastest and causes a bunch of trouble with optimizing code. It's legacy |
| 69 | +from a time when you could do much more complex conditional initialization. |
| 70 | + |
| 71 | +As such work is currently under way to move the flags out onto the stack frame |
| 72 | +where they more reasonably belong. Unfortunately, this work will take some time |
| 73 | +as it requires fairly substantial changes to the compiler. |
| 74 | + |
| 75 | +Regardless, Rust programs don't need to worry about uninitialized values on |
| 76 | +the stack for correctness. Although they might care for performance. Thankfully, |
| 77 | +Rust makes it easy to take control here! Uninitialized values are there, and |
| 78 | +you can work with them in Safe Rust, but you're *never* in danger. |
0 commit comments