4 ms·
This seems very hard for a programmer to reason about. It’s odd to do this sort of flow-sensitive analysis on the CFG for a language feature that affects whethe
by rayiner 3y ago
This seems very hard for a programmer to reason about. It’s odd to do this sort of flow-sensitive analysis on the CFG for a language feature that affects whether code is accepted or rejected, instead of some optimization that’s transparent to the programmer.
- mibsl 3y agoThis is done so that the programmer doesn't have to reason about it. Polonius makes the compiler accept code that's obviously valid, but the current borrow checker isn't sophisticated enough to declare it as safe. I've bumped into this kind of problem when writing rust, at first it was hard to understand why the compiler doesn't accept my code.
- FpUser 3y agoThis kind of control would drive me up the wall. If I write code and my logic is wrong I appreciate compiler being able to tell me to sod off. Doing it when the logic is perfectly valid is a turn off.
- vacuity 3y agoThat's the draw of having a borrow checker, which is arguably the contributor to the biggest pain points of Rust. Everything is a tradeoff. Rust's "unsafe" blocks are also a tradeoff. To my understanding, Rice's theorem ensures that, for checking borrows, either all invalid programs and some valid programs are rejected, or all valid programs and some invalid programs are accepted. Given Rust's goal of overall safety, the conservative route (which rejects some valid programs) is favored.
- FpUser 3y ago>" the conservative route (which rejects some valid programs) is favored." I understand it and consider it very reasonable choice given the goals. Still no fun ;)
- hollerith 3y agoSome languages require you to declare your variables, and if you use an undeclared variable, the compiler complains and refuses to emit any code. Is that a turn off for you, too?
- FpUser 3y ago>"Is that a turn off for you, too?" Quite the opposite
- phoe-krk 3y agoIf I understand the article correctly (and I'm not a Rust programmer), the above is an optimization that permits the programmer to write code that is correct with regard to object lifetimes, but currently rejected by the compiler. This is because the current borrowing model is too simple to account for that particular case, and hence, a new model is being developed that makes compiling this code possible without sacrificing correctness. But, still, the whole selling point of Rust is "flow-sensitive analysis (...) for a language feature that affects whether code is accepted or rejected". Accepting or rejecting some code based on whether it fits the memory model of that language is the main advantage that Rust has over other strongly typed statically compiled languages.
- rayiner 3y agoI wouldn’t say that’s the “whole selling point of Rust.” The borrow checker was based on lexical scopes, rather than the CFG, until last year.
- phoe-krk 3y agoThanks - I quoted too much of the parent, I've amended my comment.
- steveklabnik 3y ago“Until last year” is technically correct but not really accurate. Lifetimes were made non-lexical in 2018, but only for Rust 2018 code and newer. Last year is when they were made non-lexical for 2015 code as well. For the vast majority of Rust developers, lifetimes have been non-lexical for five years now.
- lolinder 3y ago> It’s odd to do this sort of flow-sensitive analysis on the CFG for a language feature that affects whether code is accepted or rejected Is this something new here with this rendition of the borrow checker? I thought the whole premise of the borrow checker was to do a data flow analysis, decide whether to accept or reject the program, and if necessary to provide information about why the program was rejected. It looks like what this project is trying to accomplish is to use better control flow mapping to reduce the number of unnecessary rejections that programmers see.
- rayiner 3y agoUntil last year, the borrow checker operated on lexical scopes, which are apparent in the source code and the programmer can reason about. Last year, the borrow checker was changed to support non-lexical lifetimes by operating on the control flow graph—something the programmer never sees and is hard to reason about. This project continues running further in that direction, making it harder for the programmer to understand why the borrow checker does what it does.
- ForkMeOnTinder 3y agoThe new implementations are more permissive than the old ones*. So if your program is rejected, it would have been rejected under the old, simpler implementation as well, and you can use your intuition of the simpler rules to understand why. *excluding bugfixes, of course What's actually hard to reason about is problem cases like in TFA which seem like they should be valid, but the compiler rejects anyway. These are cases where people's intuition doesn't match the current implementation, so I think moving the implementation closer to people's mental model of borrowing will make the language easier.
- steveklabnik 3y agoSo originally, lifetimes were lexical only. And when the move towards non-lexical lifetimes was being considered, this aspect was discussed heavily. It turns out that programmers seem to think about scope in a cfg style, and don’t really think in lexical scope, even though it’s “easier.” Tons of people, even new ones, would look at some code Rust rejects and be like “but obviously this is safe because:” and then state the non-lexical reasoning. People generally find the non-lexical borrow checker to be far easier to use. I think it is actually one of the larger things Rust has discovered, that doesn’t get talked about very much. And it’s also a great demonstration that simpler doesn’t always mean easier.
- sfink 3y agoThis is rather fascinating to me, as someone who maintains a flow-sensitive checker. No relation to the Rust borrow checker, since mine is an unsound linter for C++ code. Though there are similarities, as mine is also about memory safety (it looks for pointers that are live across a function call that might GC, and therefore could invalidate them by moving the data they're pointing to.) I spend a lot of effort trying to make the errors comprehensible, so I have to think about the mental models people are using. The entry point for people is almost always a reported error. The notion "live across a GC call" isn't natural to most people, so I dump out the entire live range (which might be from the previous iteration of a loop, for example) in the hopes that it'll sort of tell a story: "see, you set the variable's value to a pointer here, then you run this code, and this function call within it might GC, and then you use the variable's value here". But that's just an example path through the function, and potentially confusing if that particular path is not possible in practice (but another unmentioned path is). That's the risk of having a more precise analysis: there are more ways that the specific error can be wrong even though the report is a true positive. Earlier, the analysis was simpler to mentally model: is there a path fragment that mentions variable X, then calls something that can GC, and finally mentions X again? But since then, I've added much more precise data flow to check whether the final use of X is an actual use and not clobbering the previous value (in which case, its live range ended sometime earlier). As with the borrow checker, that just provides reasons to not report an error that the simpler analysis would have, but those do matter to the user. Especially when they're struggling to restructure code, and having to mentally do the same analysis to figure out what changes will fix the problem. In my experience, there are usually simple code fixes that eliminate a problem entirely and would have worked with the simpler analysis. For the more complicated cases, users are generally pretty appreciative when the analysis is smart enough to allow fixes to be more targeted and natural. (They aren't appreciative of the false positives that the more sophisticated analysis never showed them, because they pretty much only think of this stuff when they get an error report. But that's ok!)