5 ms·
This is because the borrow checker is based on lexical scope at the moment. It can (and almost assuredly will) be based on non-lexical scope in the future, but
by steveklabnik 10y ago
This is because the borrow checker is based on lexical scope at the moment. It can (and almost assuredly will) be based on non-lexical scope in the future, but enabling that has taken years of work to refactor the compiler in a way to let it happen. We're close, but not there yet.
See also threads like this: https://internals.rust-lang.org/t/accepting-nested-method-calls-with-an-mut-self-receiver/4588 https://internals.rust-lang.org/t/accepting-nested-method-ca...
- xxr 10y agoCould you please elaborate a little on the difference here between lexical and non-lexical scope, Steve?
- Manishearth 10y ago{ let mut x = 1; let y = &x; do_stuff_with(y); x += 1 // not allowed right now because y is still active // no use of y here. possible uses of x } The lexical scope of y is the entire block (well, starting at the declaration of y). The actual scope of liveness ends after do_stuff_with -- y is no longer used at this point and x could be mutated. Right now you have to introduce an explicit scope block to make this work, but it's not a fundamental restriction in the language design.
- braveo 10y agoThe borrow checker drawbacks was actually one of my biggest pet peeves with Rust. If you guys can solve that problem in a meaningful way it'll go a long way towards making me a lot more positive about using Rust. As it was, I felt as though I were manipulating a child in order to get it to do what I wanted.
- Manishearth 10y agoFWIW a lot of the issues that newcomers come across are just due to lack of familiarity. The feeling of manipulating a child is common initially, but soon you'll learn how Rust expects you to design your code, and you'll be able to write code that passes the borrow checker without much issue. This is true when learning most languages; it takes some time to write code idiomatically, and till then you'll be stumbling over many things. But yeah, we should improve this.
- madez 10y agoThe learning curve of a program is relevant. Even if it just takes a weekend of fiddling around with Rust to learn how to satisfy it, it will hinder adoption.
- Manishearth 10y agoOf course. And we're always looking to improve that. Just pointing out that this isn't as major a problem when actually using Rust once you've picked it up.
- vvanders 10y agoI'd concur, in fact I don't know if I'd like to see the borrow checker get more complex because then it becomes harder to keep a mental model in your head of what it's doing. I found once I grasped the model that the borrow checker uses that I rarely fight it and instead find it catching many things that I miss.
- steveklabnik 10y agoYes, I've said this as well. Non-lexical lifetimes will make the borrow checker more complex; but if it tends to work closer to the way that you intuit things, as a developer, it will _feel_ simpler. Right now, the rules are very simple to explain. But that means your code gets a little more complex at times.
- braveo 10y ago> Right now, the rules are very simple to explain. But that means your code gets a little more complex at times. And more awkward in places due to convincing the checker that what you're doing is ok.
- Manishearth 10y agoI think a good example of this is how value categories work in C++ (even pre-move-semantics). It's initially quite intuitive and makes sense. But as you use the language more you start questioning why it doesn't make sense and you eventually have to learn how rvalues work.
- madez 10y agoI also think this is an issue for Rust. The borrow checker needs to understand more, safe programs. Some are trivially safe but the borrow checker can't verify that. That is a problem. I don't even think the borrow checker needs to become super clever, but it is too rudimentary right now.
- steveklabnik 10y agoSpecifics here would be very useful; sometimes you _think_ something is safe, but it's not, because you forgot something the compiler didn't. Take http://manishearth.github.io/blog/2015/05/17/the-problem-with-shared-mutability/ http://manishearth.github.io/blog/2015/05/17/the-problem-wit... for example...
- madez 10y agoHow about the example given below by Manish? The following program will never fail. fn main() { let mut x = 0; let y = &x; println!("{}", y); x += 1; println!("{}", x); } It still doesn't pass the borrow checker.
- steveklabnik 10y agoI mean, yes, I'm not saying that there aren't safe programs that the borrow checker can't know about. This is an inherent aspect of how safety works; for soundness reasons, you want to be conservative. What I do mean is, many times we have people drop by IRC and say "the borrow checker is dumb, I want to do X, and it won't let me" and the reply is something like "have you considered threading" and they go "ohhhhh I see." There was a good quote of the week about this at some point, but I can't find it right now. I don't happen to run into patterns like the above code in real Rust code, personally. That doesn't mean that they don't happen, but I'm interested in _real world_ cases where this becomes an issue. We've got a bunch of them, which is why we're pursuing non-lexical lifetimes, but it seems like for many, this is a _constant_ struggle, and I run into a NLL situation maybe once every two months. Figuring out that disconnect is very interesting to me. It matters because I'm going to be responsible for teaching it, so real examples are way better than contrived ones. Moving that focus is the point of my current re-write of these docs :)
- lemming 10y agoIf only manipulating my child were as easy as solving borrow checker problems. At least the borrow checker tells you what you're doing wrong.