about summary refs log tree commit diff
path: root/compiler/rustc_codegen_llvm/src
diff options
context:
space:
mode:
authorNoah Lev <camelidcamel@gmail.com>2021-03-15 15:09:06 -0700
committerNoah Lev <camelidcamel@gmail.com>2021-12-10 14:34:00 -0800
commite27315268b10c9ef2f4c3d815dfc79f513327def (patch)
tree0ef1a2372a5a5cc6abf9efcd91f439faa09124b2 /compiler/rustc_codegen_llvm/src
parent0b42deaccc2cbe17a68067aa5fdb76104369e1fd (diff)
downloadrust-e27315268b10c9ef2f4c3d815dfc79f513327def.tar.gz
rust-e27315268b10c9ef2f4c3d815dfc79f513327def.zip
Suggest using a temporary variable to fix borrowck errors
In Rust, nesting method calls with both require `&mut` access to `self`
produces a borrow-check error:

    error[E0499]: cannot borrow `*self` as mutable more than once at a time
     --> src/lib.rs:7:14
      |
    7 |     self.foo(self.bar());
      |     ---------^^^^^^^^^^-
      |     |    |   |
      |     |    |   second mutable borrow occurs here
      |     |    first borrow later used by call
      |     first mutable borrow occurs here

That's because Rust has a left-to-right evaluation order, and the method
receiver is passed first. Thus, the argument to the method cannot then
mutate `self`.

There's an easy solution to this error: just extract a local variable
for the inner argument:

    let tmp = self.bar();
    self.foo(tmp);

However, the error doesn't give any suggestion of how to solve the
problem. As a result, new users may assume that it's impossible to
express their code correctly and get stuck.

This commit adds a (non-structured) suggestion to extract a local
variable for the inner argument to solve the error. The suggestion uses
heuristics that eliminate most false positives, though there are a few
false negatives (cases where the suggestion should be emitted but is
not). Those other cases can be implemented in a future change.
Diffstat (limited to 'compiler/rustc_codegen_llvm/src')
0 files changed, 0 insertions, 0 deletions