Smarter borrow checking for function calls

The issue:

This compiles fine

array[10] = array[11]

This raises borrow checker error

set(array, 10, get(array, 11))
// or
array.set(10, array.get(11))

The reason for this behavior is the immutable borrow in get interferes with mutable borrow in set, because rust evaluates from left to right.

For example this compiles without a problem.

set(10, get(11, array), array)

I think the obvious way to solve it, is to make the compiler understand, that the values are borrowed after the function body is entered (arguments are already evaluated and put on stack by that time).

I believe this is already implemented on nightly: https://github.com/rust-lang/rust/issues/46037

[blog post] Nested method calls via two-phase borrowing contains a proper explanation and discussion of the proposed solution that I believe got implemented.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.