fix: Closure capturing for let exprs #20039
Merged
+43
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #18201
We have been handling closure captures for let expressions in a wrong way:
Here,
rust-analyzer/crates/hir-ty/src/infer/closure.rs
Lines 1237 to 1241 in 6807f60
which ends up as a capture by value if the captured target is not
Copy
rust-analyzer/crates/hir-ty/src/infer/closure.rs
Lines 1034 to 1051 in 6807f60
But rustc handles this as a capture by ref(immutable, in most cases) in the following lines:
https://github.com/rust-lang/rust/blob/6f935a044d1ddeb6160494a6320d008d7c311aef/compiler/rustc_hir_typeck/src/expr_use_visitor.rs#L452-L454
https://github.com/rust-lang/rust/blob/6f935a044d1ddeb6160494a6320d008d7c311aef/compiler/rustc_hir_typeck/src/upvar.rs#L2093-L2114
Of course, rustc's capturing logic is more sophiscated than my modification here and we lack many details of them but at least, this one is at least more precise than the present, I think 😅
I think that we should move our capturing logic closer to that of rustc, but after next-gen solver migration