fix: Do not consider mutable usage of deref to *mut T
as deref_mut
#18252
+37
−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 #15799
We are doing some heuristics for deciding whether the given deref is deref or deref_mut here;
rust-analyzer/crates/hir-ty/src/infer/mutability.rs
Lines 182 to 200 in 5982d9c
But this heuristic is erroneous if we are dereferencing to a mut ptr and normally those cases are filtered out here as builtin;
rust-analyzer/crates/hir-ty/src/mir/lower/as_place.rs
Lines 165 to 177 in 5982d9c
Howerver, this works not so well if the given dereferencing is double dereferencings like the case in the #15799.
Here are two - outer and inner - dereferences here, and the outer dereference is marked as deref_mut because there is an assignment operation.
And this deref_mut marking is propagated into the inner dereferencing.
In the later MIR lowering, the outer dereference is filtered out as it's expr type is
*mut u32
, but the expr type in the inner dereference is an ADT, so this false-mutablility is not filtered out.This PR cuts propagation of this false mutablilty chain if the expr type is mut ptr.
Since this happens before the resolve_all, it may have some limitations when the expr type is determined as mut ptr at the very end of inferencing, but I couldn't find simple fix for it 🤔