5 ms·
Borrow checking without lifetimes
- JonChesterfield 3y agoThis is about moving rust's borrow checker into the type system. I don't follow the details (not a rust dev) but the top level plan of doing static checks in the static type system sounds right. Has interesting links to related projects.
- eslaught 3y agoThere's more going on here. They are adding a notion of "places" (what I would call "field paths") and an explicit syntax for "modes" (what I would call "privileges"). So you can now say: shared(x.a.b.c) or: mut(y.d.e.f) That makes it possible to pass around a reference with the promise that the callee will only access a subset of the fields of whatever referenced type you're talking about, as well as other things. Superficially it looks very much like what I did in Regent [1] though I am sure the details differ widely. [1]: https://regent-lang.org/tutorial/06_privileges/ https://regent-lang.org/tutorial/06_privileges/
- cwzwarich 3y agoThe Rust borrow checker has had this model for over a decade at this point; it's just not exposed as part of the language. One strong principle in Rust language design is to avoid proliferation of too many distinct reference types. In this case, I personally wish the language had support for it, since it would allow you to seamlessly refactor a piece of code into its own function.
- pornel 3y agoThe article mentions expanding capabilities to support self-referential structs. Currently structs are allowed to borrow from any data outside of them, but not from any of their own fields. Rust doesn't want to have move constructors, and borrowing from inline fields can't be safe, but borrowing from heap-allocated data owned by the same struct should be possible, and currently there's no non-hacky way to express that.
- cwzwarich 3y agoI was specifically referring to the ability to borrow a set of fields/paths. Enabling more useful self-referential structs would be something more novel.
- fanf2 3y ago“Place” has long been Rust’s term for lvalue https://doc.rust-lang.org/stable/reference/expressions.html#place-expressions-and-value-expressions https://doc.rust-lang.org/stable/reference/expressions.html#...
- nu11ptr 3y agoI only skimmed this article, but is it fair to say if this makes it into Rust proper that we would gain self referential structs?
- orlp 3y agoNo, self-referential structs are disallowed by the nature of Rust having move semantics by default, without a concept of a 'move constructor'.
- slashdev 3y agoIt’s not clear to me that the compiler couldn’t detect when you try to move a self referential string and prevent it. It basically does if you use pin.
- orlp 3y agoI'm not saying a theoretical language couldn't be made that allows this, but in Rust it would be breaking backwards compatibility. For example [T]::sort (obviously) needs to be able to move the elements in the passed array, but there is no Move bound on T or something similar. > It basically does if you use pin. And we have Pin today, but I assumed the OP meant 'self-referential struct' as in, without using Pin.
- nu11ptr 3y ago> And we have Pin today, but I assumed the OP meant 'self-referential struct' as in, without using Pin. I did, but with Pin would be fine as well
- devit 3y agoIt would have to be introduced by making it default, so all existing generic items would have Move bound on all generic parameters. Only in a new edition the default can then be swapped.
- comex 3y ago
- olliej 3y agoI'm curious to see how this turns out, but one thing I'm not sure I agree with is that lifetimes are confusing? I want to be clear here, it's been a few years since my job was coding in rust, but I found lifetimes to probably be among the easiest parts of rust to understand. Is the issue actually not understanding what lifetimes are, or just that people need to annotate them explicitly (which obviously other languages don't do[1])? The former seems surprising to me, the latter I could understand more readily due to the combination of the newness/novelty of explicitly annotating the lifetime, and also the ' prefix to lifetime names which I can imagine triggering a "that's weird looking" response when people first need them - I think I may have had that response at first, but it was fairly brief as it's just an initially new/novel syntax, and even now having the sigil prefix seems like a reasonable way to distinguish lifetimes from anything else, it's similar to why there's a * in generators in JS despite it not being strictly necessary for parsing or type checking. One thing that did just occur to me though is how many adopters of rust were coming from languages with entirely automatic lifetime management (GCs, etc) so having to think about object lifetimes at all is new? I realize I've mostly assumed people adopting rust have been C, C++, etc devs where thinking about such things is mandatory and continuous, but if there are lots of adopters from managed languages then maybe that's new. In that case though I don't think the issue is rust's lifetimes specifically in as much as it's just object lifetimes at all?[3] [1] Because they don't need to because of implicit lifetime management (GC, automatic refcounting[2], etc) or because their programming model is "what memory safety?" [2] Yes I know refcounting is technically GC, but this is comparison to scanning GCs [3] [this is an addition] I want to be clear, I am not critiquing devs who have come from managed languages for not being aware of lifetimes, this is literally just saying "if you're from a language where it's not a thing you have to think about, the entire concept may be new"
- ComputerGuru 3y ago> In that case though I don't think the issue is rust's lifetimes specifically in as much as it's just object lifetimes at all? A comment [0] in this very thread lends credence to this. [0]: https://news.ycombinator.com/item?id=39597296 https://news.ycombinator.com/item?id=39597296
- cogman10 3y ago
- pimlottc 3y agoFor someone unfamiliar with rust, the title is a real crash blossom.
- oersted 3y agoThank you for teaching me a new phrase :)
- logicprog 3y agoThe push to eliminate lifetimes in favor of loans (with this or polonius) in the pursuit of allowing a larger subset of correct programs to be expressed in Rust makes sense at first blush — of course we want to be able to prove more correct programs correct! Who wants to fight the borrow checker all the time over things we know are fine! — but I'm concerned in the long run it will actually turn out to be a bad thing. I vehemently disagree with the article that lifetimes are at all nebulous or hard to grasp, IMO they're a pretty straightforward concept, and they map really nicely onto single ownership, move, RAII based memory management, and underlying C style memory management concepts, whereas OTOH it feels like loans are perhaps less nebulous, but also map more poorly onto the best comprehensible, and specific ways to think about low level memory management (and also less well to concepts in other low level languages lile C++). Instead of seeing your program as a mostly 1D collection of mostly contiguous scopes that the program counter jumps around in, now you have to view it as a gigantic thicket of constraints. So in essence, by making Rust's static analysis more powerful and less annoying at first, we're actually making it harder to fully grasp in the long run. It's sort of like the Haskell monad problem — the more powerful you make your compiler/language abstractions to allow proving more code, the less comprehensible everything gets. And I think in both cases, trading some power, past a certain point, for long term comprehensability with more straightforward concepts is better.
- logicprog 3y agoI like Rust a lot right now, but with polonius and some of the unnecessary and weird syntactic sugar that's being added, plus the fact that instead of full coroutines to encompass both async and generators (like Kotlin has) we're getting neutered coroutines to do generators and async is a separate but similar concept, I think the Rust designers are making a lot of missteps lately. I know nothing's perfect, but it kind of sucks.
- armchairhacker 3y ago“You have become the very thing you swore to destroy!”
- 3y ago
- adamwk 3y agoI may be out of my depth here as I've only casually used Rust, but this seems similar to Swift's proposed lifetime dependencies[1]. They're not in the type system formally so maybe they're closer to poloneius work [1]: https://github.com/apple/swift-evolution/blob/3055becc53a3c3c2551dfceb8de04316902e4c5d/proposals/NNNN-lifetime-dependency.md https://github.com/apple/swift-evolution/blob/3055becc53a3c3...