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

Call to FnMut closure borrows closure too early #26193

Closed
jimblandy opened this issue Jun 10, 2015 · 3 comments
Closed

Call to FnMut closure borrows closure too early #26193

jimblandy opened this issue Jun 10, 2015 · 3 comments

Comments

@jimblandy
Copy link
Contributor

The following definition compiles fine:

fn two<V>(f: &mut FnMut(V) -> V, v: V) -> V {
    let temp = f(v);
    f(temp)
}

whereas the more direct version does not:

fn two<V>(f: &mut FnMut(V) -> V, v: V) -> V {
    f(f(v))
}

The error we get here is:

error: cannot borrow `*f` as mutable more than once at a time
         f(f(v))
           ^
note: previous borrow of `*f` occurs here; the mutable borrow prevents subsequent moves, borrows, or modification of `*f` until the borrow ends
         f(f(v))
         ^
note: previous borrow ends here
         f(f(v))
               ^
error: cannot borrow `*f` as mutable more than once at a time
         f(f(v))
           ^
note: previous borrow of `*f` occurs here; the mutable borrow prevents subsequent moves, borrows, or modification of `*f` until the borrow ends
         f(f(v))
         ^
note: previous borrow ends here
         f(f(v))
               ^

(The repetition is in the original.)

If I'm reading the notes here correctly, it seems that a call like closure(args...) borrows closure before evaluating args..., so that args... itself is not allowed to refer to closure. Is it really necessary to borrow closure so early? Can't we give the borrow a scope that starts after the arguments have been evaluated?

Using: rustc 1.2.0-nightly (d6c8028 2015-06-09)

@bluss
Copy link
Member

bluss commented Jun 11, 2015

Maybe the repetition is from cargo test — it usually kicks off two builds at once.

This is basically the issue borrow scopes should not always be lexical #6393 and rfc issue rust-lang/rfcs#811

@bluss
Copy link
Member

bluss commented Jun 11, 2015

Closing since it's a duplicate. Feel free to report other issues you find!

@bluss bluss closed this as completed Jun 11, 2015
@jimblandy
Copy link
Contributor Author

Yes, that's it exactly. Obviously [cough] this has nothing to do with closures; any nested method call that takes a &mut self will exhibit the same problem.

Thanks!

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

2 participants