Repository navigation
Document universal &mut reborrows - #2373
Jules-Bertholet wants to merge 5 commits into
Conversation
| 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. |
There was a problem hiding this comment.
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);There was a problem hiding this comment.
The x in generic(x) is a "place expression in value expression context" with type &mut.
There was a problem hiding this comment.
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?)
There was a problem hiding this comment.
It should be "mutable or movable". Fixed
There was a problem hiding this comment.
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;
}There was a problem hiding this comment.
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
Re-use phrasing from expr.deref.mut
Documents rust-lang/rust#163494.