Skip to content

Modify mutable deref rule for &mut T #1758

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

chorman0773
Copy link
Contributor

This indicates that any movable place expression of type &mut T can be dereferenced to yield a mutable place.
Presently the rule only lists variables and fields of variables. However, if a &mut T is contained within a movable Box, it is also valid to deref the &mut T and use the resulting place mutably, even if the Box was immutable. See the following playgrounds:

@rustbot rustbot added the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Mar 13, 2025
@@ -241,7 +241,7 @@ The following expressions can be mutable place expression contexts:
* [Temporary values].
* [Fields][field]: this evaluates the subexpression in a mutable place expression context.
* [Dereferences][deref] of a `*mut T` pointer.
* Dereference of a variable, or field of a variable, with type `&mut T`.
* Dereference of a movable place, with type `&mut T`. This includes variables and their fields, as well as temporaries.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Dereference of a movable place, with type `&mut T`. This includes variables and their fields, as well as temporaries.
* Dereference of a movable place with type `&mut T`. This includes variables and their fields, as well as temporaries.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add an internal link on "movable place" also.

@chorman0773
Copy link
Contributor Author

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024

I checked something and this might be a bit trickerier. According to [expr.move.movable-place], x.0 is not a movable place (which is correct). However, it can be dereferenced mutably (which the previous wording reflects).
I'm not sure how to reconcile both cases without building a completely new inductive list.

@chorman0773
Copy link
Contributor Author

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author. and removed S-waiting-on-review Status: The marked PR is awaiting review from a maintainer labels Mar 20, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants