rust/compiler/rustc_borrowck/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
..
constraints Move rustc_mir::borrow_check to new crate rustc_borrowck. 2021-09-07 00:29:22 +02:00
diagnostics Suggest using a temporary variable to fix borrowck errors 2021-12-10 14:34:00 -08:00
region_infer Auto merge of #90491 - Mark-Simulacrum:push-pred-faster, r=matthewjasper 2021-11-24 15:51:46 +00:00
type_check Auto merge of #88439 - cynecx:unwind_asm, r=Amanieu 2021-12-04 05:59:16 +00:00
borrow_set.rs Add BorrowSet to public api 2021-10-25 22:16:07 +02:00
borrowck_errors.rs Move rustc_mir::borrow_check to new crate rustc_borrowck. 2021-09-07 00:29:22 +02:00
constraint_generation.rs Move rustc_mir::borrow_check to new crate rustc_borrowck. 2021-09-07 00:29:22 +02:00
consumers.rs Add InferCtxt::with_opaque_type_inference to get_body_with_borrowck_facts 2021-10-06 19:07:56 -07:00
dataflow.rs Add initial AST and MIR support for unwinding from inline assembly 2021-12-03 23:51:46 +01:00
def_use.rs Add initial AST and MIR support for unwinding from inline assembly 2021-12-03 23:51:46 +01:00
facts.rs Move the dataflow framework to its own crate. 2021-09-07 19:57:07 +02:00
invalidation.rs Add initial AST and MIR support for unwinding from inline assembly 2021-12-03 23:51:46 +01:00
lib.rs Add initial AST and MIR support for unwinding from inline assembly 2021-12-03 23:51:46 +01:00
location.rs Move rustc_mir::borrow_check to new crate rustc_borrowck. 2021-09-07 00:29:22 +02:00
member_constraints.rs Replace Fn impls with RPIT impls in rustc_index 2021-10-03 17:50:53 +02:00
nll.rs Rename functions reflect that inline const is also "typeck_child" 2021-11-07 04:00:34 +00:00
path_utils.rs Move rustc_mir::borrow_check to new crate rustc_borrowck. 2021-09-07 00:29:22 +02:00
place_ext.rs Move rustc_mir::borrow_check to new crate rustc_borrowck. 2021-09-07 00:29:22 +02:00
places_conflict.rs Move rustc_mir::borrow_check to new crate rustc_borrowck. 2021-09-07 00:29:22 +02:00
prefixes.rs Move rustc_mir::borrow_check to new crate rustc_borrowck. 2021-09-07 00:29:22 +02:00
renumber.rs Visit type in process_projection_elem. 2021-11-27 17:39:27 +01:00
universal_regions.rs Rename functions reflect that inline const is also "typeck_child" 2021-11-07 04:00:34 +00:00
used_muts.rs Move rustc_mir::borrow_check to new crate rustc_borrowck. 2021-09-07 00:29:22 +02:00