7 ms·
Fighting the Borrow Checker
- weberc2 10y agoI've often wondered why the extra scope is needed to borrow the same variable twice in a function. It seems like the borrow checker is being overly pedantic in this case by preventing users from writing a valid program. Rather than training new users to pacify the borrow checker, perhaps the borrow checker could be made to permit valid programs so as to reduce the learning curve?
- steveklabnik 10y agoThis 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 ago
- vertex-four 10y agoThis is something that will be done as soon as MIR - a new internal layer in the Rust compiler which is easier to operate on - is stable. Right now, it's difficult to make the borrow checker much smarter and the work would have to be thrown away and reimplemented once MIR is stable.
- steveklabnik 10y agoMIR is already in the compiler and used for everything; it's not that MIR needs to become stable (it's unclear if it ever will be), the next step is porting borrowck to be a MIR pass rather than (I believe currently) a HIR pass.
- Lerc 10y agoIt certainly seems incongruous to require explicit scoping on usage when identifiers are reused inline. Conceptually they have the same principle where it shouldn't matter if you don't cross the streams.
- steveklabnik 10y agoThis is important for soundness; it's shadowing, not replacing let s = String::from("hello"); let s = &s[..]; Here, the second s refers to the first, and if it were go to out of scope right away, would be dangling. let s = String::from("hello"); { let s = &s[..3]; // here, s is "hell" } // here, it's the full string again This falls out of the usual scoping rules, basically. You can think of any variable introducing a new scope where it's valid; if you don't close it yourself, it's closed at the end of the enclosing scope.
- alyandon 10y agoWhy can't the rust compiler introduce a hidden temporary variable for the let name = jill.name().first_name() example instead of requiring the developer to explicitly declare and use one? Edit: To clarify, I am a Rust neophyte and have only begun to scratch the surface of the language. Edit2: Also, "There is no reason a person should know which class they are in, nor should a class know which people are enrolled in it. " I know what the author is trying to say but when you read that at face value it comes across as a little humorous.
- steveklabnik 10y agoPlease see the link to Rust's internals forum I liked below, it goes over this in great detail. (Along with ways we might fix this in the future.)
- barrkel 10y agoIndeed, this is the approach taken in many languages with reference counted types. One problem is that the user would have no way of naming the thing that the lifetime is attached to. Diagnostic messages when trying to hand off 'name' to other code could be perplexing, as the delta between broken code and working code would be larger.
- Groxx 10y agoGiven their current error messages, that doesn't seem like a problem. If you must name it, call it `name'` or something, but the output could easily be something like 28 | let name = jill.name().first_name(); | ----------- value returned here 28 | let name = jill.name().first_name(); | ^^^^^^^^^^^^^ leaves scope before use, | and is unavailable here
- Manishearth 10y ago> Why can't the rust compiler introduce a hidden temporary variable for the let name = jill.name().first_name() example instead of requiring the developer to explicitly declare and use one? It could, but it doesn't yet. This kind of analysis needs porting borrowck to MIR, which still has to happen. It's too complicated given the current way it works. Though it is a matter of explicit over implicit too. If `name()` had a destructor you may want it to be explicitly tied to a scope by dint of being declared in one instead of floating around in a magical hidden variable that's tied to the scope of a different one. So there are reasons for why it may never be made to work that way.
- kaosjester 10y agoI really hate the phrase "fighting". Calling it a fight doesn't do justice to the conversations you have with the borrow checker when you use Rust every day. You don't fight with the borrow checker, because there isn't a fight to win. It's far more elegant, more precise. It's fencing; you fence with the borrow checker, with ripostes and parries and well-aimed thrusts. And sometimes, you get to the end and you realize you lose anyway because the thing you were trying to do was fundamentally wrong. And it's okay, because it's just fencing, and you're a little wiser, a little better-honed, a little more practiced for your next bout.
- echelon 10y agoFor a Rust practitioner, there is no fight against the borrow checker. It's like saying a Java developer fights the type system. Is the borrow checker painful to grasp at first? A little. But the safety and assurance it gives you is immeasurable. Write enough Rust, and before long, you'll know when your code won't compile as written.
- madez 10y agoJust because something is worth the hazzle doesn't mean people do it. Of course, some people are willing to study and learn, but I think you also need to cater to those who are not if you want widespread adoption.
- AnimalMuppet 10y agoIf I understand correctly, the borrow checker is part of how Rust prevents dangerous code. Rust is not in the business of catering to those who want to write dangerous code, even if that means Rust will not achieve widespread adoption. There are already languages is wide use that allow writing dangerous code; for Rust to become another such language would not be a step forward.
- steveklabnik 10y agoRust does let you write dangerous code; it just makes you explicitly annotate it. As a systems language, Rust _must_ give you total control when you need it. It's just not the default.
- nialv7 10y agoI really feel like I'm going lengths in order to satisfy the compiler. The compiler should work for me, not the other way around. Why do I need to manually create a scope when the compiler can see that variable is never used again afterwards? In Jill.name().first_name(). Why can't the compiler extend the lifetime of the return value until the end of the statement? Having a borrow check to ensure memory safety is good. But those are just unnecessary and pointless. These are not pitfalls in user code, but pitfalls in language design
- steveklabnik 10y ago> when the compiler can see If you look at the rest of this thread, you'll see that the compiler _can't_ currently see that; this is the whole issue.
- nialv7 10y agoI wasn't talking about this particular compiler. I meant "in theory, a compiler would be able to see".
- steveklabnik 10y agoThat's fair; as I said elsewhere, we are very interested in making your theory a reality :)
- deleted 10y ago[deleted]
- lukewink 10y agoIn the example given here, how would one answer the following questions: 1. For a given school, return all the students 2. For a given school, return all the classes In the last data model given (the correct one), the above 2 things can't be done (I think) without doing something funky like enumerating all the Enrollments and finding unique students/classes. In order to modify the data model to provide the needed information, wouldn't you run into the same borrow checker problems?