5 ms·
Ah yes, that RFC is roughly what I had in mind, thanks. I believe that it's safe to promote through an if, although obviously not through a general loop.
by asuffield 12y ago
Ah yes, that RFC is roughly what I had in mind, thanks.
I believe that it's safe to promote through an if, although obviously not through a general loop.
- dbaupp 12y agoYes, I agree that it should be safe, I've softened my original text. However, it would require dynamically tracking if the destructor needs to be run, and there's currently discussion[1] about Rust possibly moving to a static model, for the highest performance. [1]: https://github.com/rust-lang/rfcs/pull/210 https://github.com/rust-lang/rfcs/pull/210
- asuffield 12y agoConsider this: On a two-way if statement, then a given storage location is either set on zero, one or both branches. If it is set on neither branch then the if statement is irrelevant and can be ignored. If it is set on one branch, then either it had an original value and hence can be treated as being set on both branches, or it must be destroyed within the branch of the if (no null pointers - think about it until it is clear that the type system guarantees this). Hence we are only interested in cases which are isomorphic with the location being set on both branches. We can treat this as a phi node following the if: there is one output value, which has been created in one of two different ways. In this case we don't know statically which value has been constructed, but we do know statically how and when to destroy it regardless of which one we get, because both branches have the same type and storage location. We don't actually need to know where it came from. Any obvious problems? I think it works...
- dbaupp 12y agoIt doesn't work, the &str could come from completely different types in the two branches. I.e. one branch could be created by .as_slice() on a String, the other could be created by referencing a global, e.g. let s = if cond { let some_string = create_it(); some_string.as_slice() } else { "literals are always-valid &str's" }; `some_string`s destructor should only be run if the first branch was taken.
- TheLoneWolfling 12y agoCouldn't the compiler get around that by introducing a boolean variable that is set depending on the branch of the if statement taken, that it checks before running the destructor? Although this starts getting really messy.
- dbaupp 12y agoYes, that's exactly how it would be handled, but it's then dynamic destruction, not static.
- TheLoneWolfling 12y agoCan you elaborate? I fail to see why this is dynamic - it still determines at compile time if/when to run destructors. I was under the impression that dynamic destruction was when you determine when to run destructors at runtime. Garbage collectors or reference counting, in other words.
- dbaupp 12y agoIt is dynamic because it is not known if the destructor call is executed at compile time. I'm using the terminology from RFC PR #210 that I linked above.
- TheLoneWolfling 12y agoOh, ok. On a related note, the approach I mentioned is actually mentioned in that RFC PR: > Store drop-flags for fragments of state on stack out-of-band
- dbaupp 12y agoYes, correct, that's what "dynamic destruction" is referring to.
- asuffield 12y ago