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

Borrowing &mut multiple times in fn argument list fails, although the borrows do not overlap. #7448

Closed
michaelwoerister opened this issue Jun 28, 2013 · 2 comments

Comments

@michaelwoerister
Copy link
Member

I recently had a case where a function needed an argument prepared by another function. Both functions take the same &mut (they both update a cache reachable through that reference). However, I had to pull the argument preparation call out of the argument list to avoid a cannot borrow ... as mutable more than once at a time error. Here is a minimal test case:

fn g(x: &mut int) -> int {
    *x
}

fn f(x: &mut int, y: int) {
    *x += y;
}

fn main() {
    let mut a = 1;

    // This works:
    let tmp = g(&mut a);
    f(&mut a, tmp);

    // This is essentially the same, but fails the borrow check
    f(&mut a, g(&mut a));
}

Maybe I'm overlooking something here, but I think the second call should work too, as the ref borrowed by g() is returned before it is borrowed by f().

@jdm
Copy link
Contributor

jdm commented Jun 28, 2013

Nominating for well-defined milestone.

@nikomatsakis
Copy link
Contributor

Dup of #6268

flip1995 pushed a commit to flip1995/rust that referenced this issue Jul 15, 2021
Rename run_lints -> lints_enabled

Just a quick rename of a utilities function. `run_lints` kinda suggested that the lints were run by this function. But the only thing this function does is to check if the lints are enabled in the context of the `hir_id`

changelog: none
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

3 participants