granite-rust/crates
bors[bot] dbb1c1b4b1
Merge #11184
11184: Correctly pass through mutable parameter references when extracting a function r=Veykril a=Vannevelj

Fixes https://github.com/rust-analyzer/rust-analyzer/issues/10277

I have based this investigation based on my understanding of [the Borrowing chapter](https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html) but I wasn't able to debug the test runs or see it in action in an IDE. I'll try to figure out how to do that for future PRs but for now, the tests seem to confirm my understanding. I'll lay out my hypothesis below.

Here we define the parameters for the to-be-generated function: 

7409880a07/crates/ide_assists/src/handlers/extract_function.rs (L882)

Three values in particular are important here: `requires_mut`, `is_copy` and `move_local`. These will in turn be used here to determine the kind of parameter:

7409880a07/crates/ide_assists/src/handlers/extract_function.rs (L374-L381)

and then here to determine what transformation is needed for the calling argument:

7409880a07/crates/ide_assists/src/handlers/extract_function.rs (L383-L390)

which then gets transformed here:

7409880a07/crates/syntax/src/ast/make.rs (L381-L383)

What I believe is happening is that 
* `requires_mut` is `false` (it already is marked as mutable), 
* `is_copy` is `false` (`Foo` does not implement `Copy`), and 
* `move_local` is `false` (it has further usages)

According to the pattern matching in `fn kind()`, that would lead to `ParamKind::SharedRef` which in turn applies a transformation that prepends `&`.

However if I look at the chapter on borrowing then we only need to mark an argument as a reference if we actually own it. In this case the value is passed through as a reference parameter into the current function which means we never had ownership in the first place. By including the additional check for a reference parameter, `move_local` now becomes `true` and the resulting parameter is now `ParamKind::Value` which will avoid applying any transformations. This was further obscured by the fact that you need further usages of the variable or `move_local` would be considered `true` after all.

I didn't follow it in depth but it appears this idea applies for both the generated argument and the generated parameter.
There are existing tests that account for `&mut` values but they refer to local variables for which we do have ownership and as such they didn't expose this issue.

Co-authored-by: Jeroen Vannevel <jer_vannevel@outlook.com>
2022-01-04 10:19:37 +00:00
..
base_db Merge #11069 2021-12-20 16:50:35 +00:00
cfg Bump MSRV (1.57) 2021-12-16 01:56:12 +05:00
flycheck Bump MSRV (1.57) 2021-12-16 01:56:12 +05:00
hir Fix tool module classification not working correctly 2022-01-02 23:14:18 +01:00
hir_def minor: add missing test 2022-01-03 17:21:43 +03:00
hir_expand minor: drop dead code 2022-01-02 19:03:38 +03:00
hir_ty Merge #11115 2022-01-03 16:00:05 +00:00
ide Merge #11177 2022-01-03 02:03:03 +00:00
ide_assists reduced the tests to their bare essence 2022-01-04 10:11:04 +00:00
ide_completion minor: Simplify 2022-01-04 10:56:28 +01:00
ide_db minor: Cleanup syntax highlighting 2022-01-02 22:23:05 +01:00
ide_diagnostics Merge #11028 2021-12-20 13:45:35 +00:00
ide_ssr compress 2021-12-28 19:17:34 +03:00
limit Bump MSRV (1.57) 2021-12-16 01:56:12 +05:00
mbe simplify 2022-01-03 16:27:16 +03:00
parser add empty input tests 2022-01-02 18:50:32 +03:00
paths Bump MSRV (1.57) 2021-12-16 01:56:12 +05:00
proc_macro_api Merge #11028 2021-12-20 13:45:35 +00:00
proc_macro_srv Merge #11028 2021-12-20 13:45:35 +00:00
proc_macro_test Bump MSRV (1.57) 2021-12-16 01:56:12 +05:00
profile Bump MSRV (1.57) 2021-12-16 01:56:12 +05:00
project_model Bump MSRV (1.57) 2021-12-16 01:56:12 +05:00
rust-analyzer internal: Handle macro calls better in highlighting 2022-01-02 19:10:10 +01:00
sourcegen Bump MSRV (1.57) 2021-12-16 01:56:12 +05:00
stdx minor: replace panics with types 2022-01-02 19:05:37 +03:00
syntax Migrate to SyntaxNodePtr in new rowan 2021-12-29 23:48:53 -08:00
test_utils fix: Do not complete Drop::drop, complete std::mem::drop instead 2021-12-22 02:25:38 +01:00
text_edit Bump MSRV (1.57) 2021-12-16 01:56:12 +05:00
toolchain Bump MSRV (1.57) 2021-12-16 01:56:12 +05:00
tt Bump MSRV (1.57) 2021-12-16 01:56:12 +05:00
vfs Bump MSRV (1.57) 2021-12-16 01:56:12 +05:00
vfs-notify Bump MSRV (1.57) 2021-12-16 01:56:12 +05:00