Skip to content

Document universal &mut reborrows - #2373

Draft
Jules-Bertholet wants to merge 5 commits into
rust-lang:masterfrom
Jules-Bertholet:reborrow-everywhere
Draft

Jules-Bertholet wants to merge 5 commits into
rust-lang:masterfrom
Jules-Bertholet:reborrow-everywhere

Conversation

@Jules-Bertholet

Copy link
Copy Markdown
Contributor

Comment thread src/expressions.md Outdated
If the type of that value implements [`Copy`], then the value will be copied.

r[expr.move.mut-ref]
If the type of that value is `&mut T`, and the place expression is mutable, then the value will be reborrowed. This is equivalent to applying `&mut *` (a [dereference][deref] and then a [mutable borrow][borrow]) to the place.

@traviscross traviscross Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What do you make of this example (the first example in the stabilization report) under the rule? How would we justify the reborrow here?

fn generic(_: impl Sized) {}
let x = &mut ();
generic(x);
generic(x);

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The x in generic(x) is a "place expression in value expression context" with type &mut.

@traviscross traviscross Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The rule as written requires that the place expression be mutable, and x is not a mutable place expression.

(Perhaps it means to say, e.g., that a dereference of the place expression be mutable?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It should be "mutable or movable". Fixed

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What do you make of this, which is accepted by your PR but rejected by the rule?

struct S<'a>(&'a mut u8);
impl Drop for S<'_> { fn drop(&mut self) {} }

fn f(x: S<'_>) {
    let y = x.0; // ERROR or OK?
    *y = 1;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point. The current wording of the Reference concerning Drop isn't even grammatically correct lol (matches a singular with a plural). I've changed this again to just re-use the wording from expr.deref.mut

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants