rust/compiler/rustc_middle/src
Noah Lev e27315268b 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.
2021-12-10 14:34:00 -08:00
..
dep_graph Address review. 2021-10-20 18:51:15 +02:00
hir Revert "Auto merge of #91354 - fee1-dead:const_env, r=spastorino" 2021-12-03 10:11:21 -03:00
infer Miscellaneous inlining improvements 2021-06-02 08:49:58 +02:00
middle Remove unused root_parent. 2021-11-28 21:48:28 +01:00
mir Suggest using a temporary variable to fix borrowck errors 2021-12-10 14:34:00 -08:00
query address review 2021-12-01 12:12:40 +01:00
thir add a CastKind to Node::Cast 2021-09-09 01:32:03 +01:00
traits Revert "Auto merge of #91354 - fee1-dead:const_env, r=spastorino" 2021-12-03 10:11:21 -03:00
ty Rollup merge of #91551 - b-naber:const-eval-normalization-ice, r=oli-obk 2021-12-08 16:08:07 +01:00
util Use AddAssign impl 2021-11-09 23:47:36 +01:00
arena.rs Add some comments. 2021-11-19 07:52:59 +11:00
lib.rs std: Stabilize the thread_local_const_init feature 2021-11-29 07:23:46 -08:00
lint.rs Simplify for loop desugar 2021-11-21 08:15:21 -06:00
macros.rs Rename TypeFolderFallible to FallibleTypeFolder 2021-12-02 16:14:18 +00:00
tests.rs mv compiler to compiler/ 2020-08-30 18:45:07 +03:00
thir.rs rename mir -> thir around abstract consts 2021-09-09 01:32:03 +01:00