Re:Linux: Using goto In Kernel Code (2003)
by jiggawatts on Saturday July 17, 2021 @07:20AM
(#27865042)
Read the articles I linked. This is essentially impossible, because Rust has gained features that C doesn't have by giving up goto. Adding goto back in would require those features to be removed, or to be watered down dramatically.
You wouldn't be able to goto into or out of most (all?) blocks that would change ownership, for example.
Re:Linux: Using goto In Kernel Code (2003)
by chrismorgan on Saturday July 17, 2021 @09:03AM
(#27865487)
No, not impossible at all; it will just reject any that would be memory-unsafe due to problems with borrow lengths, potentially undefined variables, and things like that. I never said you could goto absolutely anywhere; plenty of gotos are patently unsafe, and Rust would duly reject them.
Rust currently checks borrows using control-flow graphs (though I believe the plan is for that to change, and the replacement might make life harder for implementing goto, I don’t know). Rust’s mid-level intermediate representation (MIR) actually has goto. In a control-flow graph, goto is just adding new edges, and it’s comparatively simple to make it work.
Re:Linux: Using goto In Kernel Code (2003)
by ByteJockey on Saturday July 17, 2021 @08:22AM
(#27865298)
That's not that much different than C. C's goto is already limited compared to the actual jump instruction because you can't cross function boundaries because of the restrictions C puts on you.
Re:Linux: Using goto In Kernel Code (2003)
by varajelle on Saturday July 17, 2021 @08:21AM
(#27865294)
Why not?
C++ still has goto despite also having RTTI. One just need to restrict a bit where we can jump.
Do you mean RAII?
I would also like continue to work that way, so that every (labelled) block can be freely looped and exited (with a value)
Re:Linux: Using goto In Kernel Code (2003)
by chrismorgan on Saturday July 17, 2021 @07:16AM
(#27865019)
I don’t think I’d want continue to work on a block. Repeating the block seems different from leaving the block early (which can already be done with return or with panicking). I think it’s better to need to write it as a loop before you can continue.
I completely agree, In rust looping has important semantics with regards to lifetimes and ownership so the continue part would be a bad idea. I think it would be cool in something like javascript (if blocks could return values...)
Rust could comparatively easily implement goto, and it’d be unconditionally safe (as regards memory safety). I’d even honestly like it to, mostly just because it’s possible rather than because it’d be useful—most prominent goto patterns can be expressed better in other ways in Rust, e.g. RAII (destructors) instead of goto cleanup, and break/continue let you emulate any structured uses of goto. (I live in hope that https://github.com/rust-lang/rust/issues/48594 will one day land, so you can use break to skip the remainder of a labelled block, no loop required.)