3 ms·
What's interesting to me about SEME regions/non-lexical borrows is that it's a great example of a tradeoff: Right now, the borrow checker's rules are very stra
by steveklabnik 11y ago
What's interesting to me about SEME regions/non-lexical borrows is that it's a great example of a tradeoff:
Right now, the borrow checker's rules are very straightforward: references live for the entire lexical scope that they're alive. However, the downside of this is that sometimes you want the scope to end early, and so you have to code around it.
With non-lexical borrows, the rules get much more complex: borrows are determined by the control flow graph, not lexical scope. However, you no longer need to code around those edge cases.
So the question of which is better really comes down to a question: do programmers find reasoning about lexical scope or the CFG more intuitive? It would appear the latter is the case, which is kind of surprising for me, but maybe it shouldn't be.
- markus2012 11y agoCan't you think of it as still being scoped - just more fine grained? Pieces of structs can be borrow checked (what you are using) instead of the whole struct?
- steveklabnik 11y agoYes, it's a scope, but not a lexical scope, hence the name :) I believe that 'borrowing part of a struct' is a different extension to the system, actually, though maybe they'll be put together. I haven't been involved in the pre-RFC myself.
- pcwalton 11y agoWe already do that. (See LoanPath in the compiler.) It's not a problem of reasoning about the structure of data--it's a problem of defining a notion of "overlapping control flow regions" that is simultaneously intuitive to the programmer, easy to compute, sound, and satisfies the ordering constraints we need (well-defined GLB, LUB, and partial order).
- Manishearth 11y agoRemember that Rust CFGs are much simpler than C++ CFGs (no goto, for one), and in general CFG analysis is simpler due to ownership. It's easier to reason about Rust control flow than it is about C++.