Open mppf opened 6 years ago
I've just filed a few other cases that might be considered part of this issue's list:
I added another related issue:
lifetime checker issues with combination of owned and borrowed #11666
I just learned that Swift has a rule for exclusive write access but only within a thread. https://github.com/apple/swift-evolution/blob/master/proposals/0176-enforce-exclusive-access-to-memory.md https://docs.swift.org/swift-book/LanguageGuide/MemorySafety.html
I wonder if some of these same ideas could apply. I'm just not sure if it could be practical.
This issue collects interesting lifetime checking cases that the current effort does not detect.
array-resize-with-ref
assign-owned
clear-owned
reset-owned
assign-owned-array
Going further
It might be possible to detect these cases by assuming that any function accepting an
Owned(T)
by reference has the ability to clear it; and therefore any borrows from it will be invalid if they span a call to such a function.How would such an analysis handle records or arrays containing
Owned
? One strategy would be to apply the same rule to these. But, that might be so conservative that an example such as the following would be excluded, even though it doesn't contain a use-after-free:Here is a ref-focused variant of the same program:
It seems that doing an acceptable job with these might require compiler analysis to say which functions actually have the capability to invalidate a borrow (based upon their bodies). But even that would rule out something like this, unless complex techniques like symbolic execution or iteration space analysis are applied:
Besides these issues, race conditional can be involved in use-after-free errors, and it's hard to see how to detect such issues at compile-time without pretty drastic measures (such as Rust's only-one-mutable-ref rule).