3 ms·
Right. A local reference is escaping the loop scope at the "return". This is safe but weird, and the current borrow checker isn't smart enough to confirm that i
by Animats 3y ago
Right. A local reference is escaping the loop scope at the "return". This is safe but weird, and the current borrow checker isn't smart enough to confirm that it's safe. The fancier "Polonius" borrow checker in development is supposed to be less restrictive.
The code example is Rust written as if it were classic C. In Rust, that example would usually be written like this:
/// Find leading zeros in an array of bytes.
fn find_leading_0s(bytes: &mut [u8]) -> Option<&mut [u8]> {
if let Some(pos) = bytes.iter().position(|v| *v != 0 ) { // search
Some(&mut bytes[..pos]) // find
} else {
None // no find
}
}
Rust isn't a fully functional language, but the functional features play well with the lifetime system and are optimized well.
(Rust playground example: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=8013c9e08e14db8c7b0d7764c01d1674 https://play.rust-lang.org/?version=stable&mode=debug&editio...)
- angiosperm 3y agoA simple "--make-borrow-violations-into-warnings" flag (that for the sake of the panicky would work only on debug builds) would let you get on with your work, and come back before pushing to a shared repository to settle up. Probably need to release it as a patch so as not to need approval of the purity gatekeepers. People using it get an immediate boost in productivity as it enables exploration with no ultimate reduction in safety.
- returningfory2 3y agoDo you think there should also be a “--make-type-violations-into-warnings’ flag?
- estebank 3y agoI think there would be value of such an option, as long as it would be completely forbidden to publish to crates.io with code that only works with those flags. It could work by replacing any function with type errors (including those caused by type definitions) to panic on use, but otherwise still generate a binary. For type errors it is less useful than for lifetime errors because usually the problems of the former are very local while for the later it they are not.
- cornholio 3y agoSounds like a great way to paint yourself into a corner, and explore solutions that are impossible to be made viable in the general case, even if they happen to work for certain inputs during debug.
- estebank 3y agoI would like that mode exclusively for the test suite to use. That way you start writing your tests, and if there's a borrowck error You delay that until the test runs. If it runs, and then it fails when accessing data, you can give more information to the programmer about the failing case. If it runs successfully, you just fail that one test with the borrowck error. I would love it if we could have something akin to prop test for borrowck so that even the test isn't needed.
- angiosperm 3y agoYou would not ever be obliged to use the flag, so its presence would not affect you. It doesn't matter whether it "works for certain inputs", or for any inputs. The point is to be able to direct attention to more immediately important algorithmic matters now, and then address borrow checker pedentism once actually difficult questions are resolved. The point is not to be able to ship code with borrow violations. It is hard for me to understand what is so difficult about this distinction.
- estebank 3y agoIf that's the workflow you want during development, you can always replace references with a RwLock. That of course is not made very fun by neither the language (you need to touch too many places in your code to switch back and forth) nor the tooling (we need more advanced refactoring tools, if the language won't change, I'd love it if we had an "add a lifetime to this type everywhere" action in my editor).
- cornholio 3y agoThe distinction is quite clear, the reason to ever use such a flag - less so. Toolchain features are very much a popularity contest, if there is no real reason to add a feature (and, in fact substantial reason not to), it shouldn't be implemented. The idea to use it during rapid prototyping is especially problematic, since that's the point where pernicious data access patterns will infect the entire design. Once the application "works, save from some double free bugs in weird testcases we don't expect in production, and which require a one month refactoring", the temptation to ship the flag into production will become overwhelming.
- voxl 3y agoYou should just not use Rust. A language should hold strong to its philosophical principles. These kind of changes are fundamentally design by committee.
- dmurray 3y agoI agree in principle - what's the point in deciding to use Rust but turning off the borrow checker? But on the other hand - Rust already has unsafe mode, which is exactly for allowing you to avoid some safety checks when you've decided you are smarter than the compiler - this really is an example where the compiler isn't smart enough, as evidenced by the fact there are plans to make it work So another "unsafe" type of marker that watered down the borrow checker would not be totally inconsistent with the language philosophy. It should work at the block or the file level, though - surely you wouldn't want to turn that off for all your code.
- apendleton 3y agoThe whole point of the current `unsafe` mechanism is that you have to explicitly opt one specific chunk of the code out of stricter checking, and it's obvious which chunk of code it is that needs that behavior (the bit inside the block) and which isn't (everything else) -- that bit can be subjected to extra scrutiny upon review, and there's a community-wide shared understanding of what invariants that chunk needs to uphold for the program as a whole to be sound. I'd argue that the alternative mechanism you propose (a compiler flag), which just opts the whole program out of strict checking, _is_ fundamentally philosophically different. Having the opt-in/opt-out mechanism be block-level is a language-design/philosophical decision.
- dmurray 3y agoRight, I didn't propose a compiler-level flag (though the original suggester did), I specifically said it needs to work at the block or file level.
- duped 3y agoA program with borrow checking violations is incorrect the vast majority of the time. Not something to be "settled up" but something that is wrong.
- angiosperm 3y agoMost of the code you write is similarly incorrect at first. Then you fix it, and then it's right. Unless you ship the buggy code, the bugs harm nobody. Likewise, borrow failures. It is hard to understand what is so difficult about this concept.
- orf 3y agoIdiomatic: https://godbolt.org/z/6ndYYxhx3 https://godbolt.org/z/6ndYYxhx3 Original: https://godbolt.org/z/rPfEWPGc1 https://godbolt.org/z/rPfEWPGc1 It’s cool to see the same code be generated. You can use “vec!” To stop it specializing the function in the length
- Animats 3y agoIt's surprisingly difficult to write something where the compiler doesn't detect that it's working on a constant. I had to code up a loop that pushed values onto the vector before the compiler generated the general case. "black_box" is not, apparently, opaque to the optimizer. Ideomatic form, inner loop: [1] LBB4_3: cmp byte ptr [rax + rcx], 0 jne .LBB4_4 inc rcx cmp rdx, rcx jne .LBB4_3 C-like form, inner loop: [2] .LBB4_3: cmp byte ptr [rax + rcx], 0 jne .LBB4_4 inc rcx cmp rdx, rcx jne .LBB4_3 Both ways, the compiler found the optimal solution. In the C-like loop, it was able to hoist and then elide the subscript check. In the ideomatic form, it's implicit in the iterator that you won't go off the end of the array. The lambda function was expanded inline and optimized. There's zero call overhead from using a lambda function there. Very nice work by the Rust compiler people and LLVM. [1] https://godbolt.org/z/5hn7Y9Mf9 https://godbolt.org/z/5hn7Y9Mf9 [2] https://godbolt.org/z/rW4MG4v3c https://godbolt.org/z/rW4MG4v3c