7 ms·
Rust RFC 2094: non-lexical lifetimes
- solidsnack9000 9y agoThis is more or less the feasibility test for lifetimes/regions as a general purpose programming feature. It will probably be many years from now that another language tries to do anything like this with the same pervasiveness and breadth.
- tedunangst 9y agoWhy would that be? If rust proves the concept works, I'd expect more languages to try it.
- pcwalton 9y agoIt's very hard to retrofit the rules that the borrow check enforces onto an existing language. That's because the borrow check enforces "aliasing xor mutability", and basically all languages in wide use don't enforce that. Adding that to an existing language generally requires breaking backwards compatibility in fundamental ways. For example, all non-immutable objects in garbage-collected languages (or similar features such as shared_ptr in C++) are incompatible with these restrictions. As another example, mutable global variables are generally incompatible with the semantics that the borrow check enforces. That said, I could see analogous dynamic systems (rather than static ones as in the case of Rust) becoming more widespread. In fact, Transferables [1] in JavaScript are an example of one such system. [1]: https://developer.mozilla.org/en-US/docs/Web/API/Transferable https://developer.mozilla.org/en-US/docs/Web/API/Transferabl...
- sanderjd 9y agoI read the parent comment's "I'd expect more languages to try it" as meaning more new languages. Really interesting point about Transferables, I hadn't seen that.
- mmirate 9y agoHmm, what about wrapping everything in an Arc<RwLock<...>> and gradually removing the wrappers (and the extra method calls) from each variable, backtracking on compiler errors?
- solidsnack9000 9y agoSwift is likely to adopt it but it a much more measured way, since everything is `shared_ptr` by default in that language. Relatively few languages have the combination of requirements and goals that makes Rust's approach obligatory and necessary. So if it's successful we should see more of it but...we won't necessarily see many languages where it is as all-pervading as Rust.
- Manishearth 9y agoSwift's tentative plan seems to be to have a level between regular swift and super unsafe swift with UnsafePointer and stuff where you can write fast, low level, but safe code like Rust. In swift's case I'd think they would avoid too much complexity since the lifetime stuff won't be as pervasive as it is in Rust (so it can have more rough corners)
- pjmlp 9y agoGiven the current state of the borrow checker and how lifetimes are handled inside closures, essential for callbacks, I think in its current state anything close to Cocoa or Cocoa Touch would be quite hard to implement and use in Rust, unless it is full of Rc and RefCell everywhere.
- sanxiyn 9y agoLifetime handling inside closures is planned to be fixed. (It is a different fix from this one.)
- Retra 9y agoI don't think the current algorithm is actual a barrier to practical use. The worst I've had to suffer for it is a few extra lines of code here and there. This RFC might make things easier to use, but if it fails in that, it doesn't really prove the concept is infeasible.
- pjmlp 9y agoI kind of agree. In a way it is great the work being done by the Rust team, as many companies wouldn't bother with it. It is already influencing the design decisions of other languages. However it really needs to be more ergonomic, I for example have been bitten trying to call a method into self from inside a closure. From logical point of view it was clear, the closure wouldn't outlive the object that owned it, but the borrow checker thought otherwise. This is one of the use cases being solved by non-lexical lifetimes.
- sanxiyn 9y agoActually, as the text of RFC makes clear in "What this proposal will not fix" section, this does not fix "self in closure" issue. Actually, "self in closure" is explicitly listed as an example.
- deleted 9y ago[deleted]
- the_common_man 9y agoPrevious discussion - https://news.ycombinator.com/item?id=11611436 https://news.ycombinator.com/item?id=11611436 And an eli5 https://www.reddit.com/r/rust/comments/6brtsu/eli5_nonlexical_lifetimes/ https://www.reddit.com/r/rust/comments/6brtsu/eli5_nonlexica...
- nerdponx 9y agoIs there an ELI5 for lifetimes in general? There are so many features of Rust that I have no context for and no intuition about. I also do not have a CS degree.
- freditup 9y agoHa, don't worry about the CS degree - lifetimes are plenty confusing to people with a CS degree too! I think it'd take a combination of reading the theory and actually using Rust for most people to get a good grasp on them.
- staticassertion 9y agoNo CS degree here either. Variables exist in memory, right? But we only need them for some amount of time - then we want our memory back. There are many strategies for this - Java uses a garbage collector to search through memory and reclaim parts no longer in use, C programmers manually call 'free'. Java suffers from its strategy because of the performance impact of that scan/ freeing strategy. C suffers from the complexity of manually managing memory leading to many security vulnerabilities. Rust takes this concept of how long memory should live - when it should be freed - and encodes it into its type system. Similar to how you can say 'this thing is an int and it can be added to other ints' rust says 'this thing lives this long and it can reference other things that live this long, or shorter'. Then the compiler can tell you "hey, you're trying to access something that might have been freed" the same way it would say "you're trying to add an integer to a string". This means you don't have the performance impact of the GC but you also have a compiler watching your back, telling you when you may be doing something unsafe. The algorithm used to determine how long variables live and when they can / can't be accessed is the subject of the RFC.
- webkike 9y agoLiveness analysis is honestly pretty simple so I'm glad it's being introduced as a rule for determining lifetimes. I would be afraid of introducing rules that make for complicated specs, and this is almost one of these areas. However, it's fairly easy to describe liveness analysis with set theory so I think it works.
- sanxiyn 9y agoWell, analysis is simple, but it does make inference algorithm considerably more complex. (Analysis generates constraints, inference solves them.)
- zebraflask 9y agoThis sounds suspiciously similar to what JavaScript does.
- yorwba 9y agoJavaScript doesn't even have a static type system, let alone one that tracks lifetimes, those are left to the garbage collector. How is this similar to anything JavaScript does?
- ZenoArrow 9y agoIn which way(s)?
- zebraflask 9y agoScope? ES6+? It's pretty obvious both languages originated from Mozilla.
- richardwhiuk 9y agoBasically every language since Ada has scope - this is in no way similar to Javascript.
- deleted 9y ago[deleted]
- zebraflask 9y agoReally? Is that why Mozilla is trying to launch a browser written entirely in Rust? Or was.
- Ar-Curunir 9y agoJust because Mozilla worked on both languages doesn't mean they're related in any way. The goals, execution contexts, and safety guarantees of the two languages are completely different.
- Animats 9y agoThe big question with Rust lifetimes was "can you tighten the screws down that tight and still do anything"? Rust managed to get it to work reasonably well. This lightens it up a bit. It's not clear to me how far this goes. One annoying Rust idiom is having to allocate something, then pass it into a function so the function can return it. There's no way to give a return value the lifetime of the caller, not the callee. Will this change allow that? It doesn't look like it. This is more in the other direction. "In the new proposal, the lifetime of a reference lasts only for those portions of the function in which the reference may later be used (where the reference is live, in compiler speak)." In modern compilers, when a local variable has been used for the last time in its scope, it's dead, and its storage can be reused. This is mostly done to free up registers. It seems to be exposing liveness analysis at the source level. The use cases aren't that convincing. This may be more of a "because we can" feature.
- sanxiyn 9y agoBelieve me, all use cases there are based on actual user complaints and this is in no way "because we can" feature.
- richardwhiuk 9y agoIt's also a solid stepping stone to the next level of unergonomic problems with lifetimes.
- pcwalton 9y agoCan you provide specific examples?
- eddyb 9y ago> This may be more of a "because we can" feature. What I see is actually "it's wanted/needed enough that it's worth putting in the huge effort to make such a complex (and novel?) system reality". EDIT: to be more clear: what's new here isn't "exposing liveness at the source level" but "model liveness in region typing". If we were changing when the destructor runs (which we can't because backwards compatibility - also, doubtful we'd want that at all), that would be more "source level".