-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Borrow compilation error with vec.swap(0, vec.len() - 1)
, while vec.push(vec.len())
works.
#74319
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
two-phase borrows don't seem to work with autoderef at all, but I'm not sure if there's a canonical issue for that |
Is autoderef happening in both of them or just in the vec.swap(..) case? |
|
edit: actually, no, the method on |
No, that is in "Methods from |
That line is actually <[_]>::swap(<Vec<_> as DerefMut>::deref_mut(&mut vec), 0, Vec::len(&vec) - 1); And currently the |
I ran into the same issue again. This did not compiled: merge_sort(&mut v[..v.len()]);
merge_sort(&mut v[v.len()..]); But this did: let temp = v.len() / 2;
merge_sort(&mut v[..temp]);
merge_sort(&mut v[temp..]); The signature: fn merge_sort(v: &mut [i32]) { ... } I'm out of context, are there any other other opened issues referencing this problem? Will Polonius be able to fix it? |
No. As I mentioned earlier, the desugared code will use a mutable borrow before an immutable one, so this code should not compile, otherwise it is unsound in general. To make NLL work across autoderef would likely be a langauge-level major change because the compiler'll need to move the location of autoderef. |
This doesn't compile: vec[vec.len() - 1] = 0; But this does: (playground) use std::ops::IndexMut;
*vec.index_mut(vec.len() - 1) = 0; The doc of
No, clearly not, there is magic between them. But this doesn't compile *vec.get_mut(vec.len() - 1).unwrap() = 0; Nor this: *std::ops::IndexMut::index_mut(&mut vec, vec.len() - 1) = 0; What's the difference? |
rust/compiler/rustc_typeck/src/check/place_op.rs Lines 376 to 380 in 96859db
Related: #49434 |
(I had put a big comment here but then I realized that this issue isn't specifically about |
I tried compiling this code (here is the playgound link):
I expected a Successful compilation, but this happened:
Note 1: that the line above the error also contains
vec.len()
, but it compiles.Note 2:
vec.swap(0, vec.len());
produces the same error (nothing to do with- 1
).Meta
rustc --version --verbose
:Also confirmed in the playground against Nightly and Beta.
The text was updated successfully, but these errors were encountered: