Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameter borrows should happen before receiver borrows #17365

Closed
jethrogb opened this issue Sep 18, 2014 · 5 comments
Closed

Parameter borrows should happen before receiver borrows #17365

jethrogb opened this issue Sep 18, 2014 · 5 comments

Comments

@jethrogb
Copy link
Contributor

This works:

fn main() {
    let mut nums=vec![10i,11,12,13];
    let index=nums.len()-2;
    *nums.get_mut(index)=2;
}

I would like to be able to write this instead:

fn main() {
    let mut nums=vec![10i,11,12,13];
    *nums.get_mut(nums.len()-2)=2;
}

But:

test.rs:3:16: 3:20 error: cannot borrow `nums` as immutable because it is also borrowed as mutable
test.rs:3   *nums.get_mut(nums.len()-2)=2;
                          ^~~~
test.rs:3:3: 3:7 note: previous borrow of `nums` occurs here; the mutable borrow prevents subsequent moves, borrows, or modification of `nums` until the borrow ends
test.rs:3   *nums.get_mut(nums.len()-2)=2;
             ^~~~

Lexically, the receivers appears before the parameters, but semantically it appears later, which is when I'd expect the borrow to happen.

@thestinger
Copy link
Contributor

cc @nikomatsakis

I think this is likely a duplicate of an existing issue.

@jethrogb
Copy link
Contributor Author

I think #6393 is related but not quite the same.

@ftxqxd
Copy link
Contributor

ftxqxd commented Sep 18, 2014

Isn’t this #6268?

@jethrogb jethrogb changed the title Parameter borrows should happen before method object borrows Parameter borrows should happen before receiver borrows Sep 18, 2014
@jethrogb
Copy link
Contributor Author

"receivers" was the word I was looking for earlier, thank you!

#6268 isn't quite the same, that ticket is about mutable receivers, while here I'm using an immutable receiver as a parameter. Would love for #6268 to get fixed though :)

Edit: on closer inspection, the first example in #6268 is in fact the same as this, but the bug title seems to be more general.

@huonw
Copy link
Member

huonw commented Sep 18, 2014

Closing as a dupe of #6268. Thanks for filing! (That issue is more general: we don't necessary want to take the specific solution suggested in the title of this issue. Maybe we do, but in any case, #6268 is the underlying cause.)

@huonw huonw closed this as completed Sep 18, 2014
lnicola pushed a commit to lnicola/rust that referenced this issue Jun 23, 2024
…=Veykril

internal: Expose snippet capability to diagnostic quickfixes

Fixes rust-lang#16767
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants