You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Because good works, I would expect bad to work as well.
$ rustc test.rs
test.rs:17:8: 17:13 error: use of moved value: `self`
test.rs:17 self.foo();
^~~~~
test.rs:16:12: 16:19 note: `self` moved here because it has type `&mut B`,
which is non-copyable (perhaps you meant to use clone()?)
test.rs:16 foo(self as &mut A);
^~~~~~~
error: aborting due to previous error
Yes this is a bug. I've noticed it too. The compiler should reborrow automatically as we do almost everywhere else; you can workaround it temporarily by making the reborrow explicit (&mut *self as &mut A).
I noticed that this is also an issue with references to generics. The following example should compile and run just fine:
traitTraitA{}structA;implTraitAforA{}fnfoo1<T:TraitA>(something:&T){object_bar(something);object_bar(something);}fnfoo2<T:TraitA>(something:&mutT){generic_bar(something);generic_bar(something);}fnfoo3<T:TraitA>(something:&mutT){mut_object_bar(something);mut_object_bar(something);}fnfoo4<T:TraitA>(something:&mutT){object_bar(something);object_bar(something);}fnobject_bar(_something:&TraitA){}fnmut_object_bar(_something:&mutTraitA){}fngeneric_bar<T:TraitA>(_something:&mutT){}fnmain(){letmut a = A;foo1(&a);foo2(&mut a);foo3(&mut a);foo4(&mut a);}
but rustc complains with
<anon>:19:20: 19:29 error: use of moved value: `something`
<anon>:19 mut_object_bar(something);
^~~~~~~~~
<anon>:18:20: 18:29 note: `something` moved here because it has type `&mut TraitA`, which is non-copyable (perhaps you meant to use clone()?)
<anon>:18 mut_object_bar(something);
^~~~~~~~~
<anon>:24:16: 24:25 error: use of moved value: `something`
<anon>:24 object_bar(something);
^~~~~~~~~
<anon>:23:16: 23:25 note: `something` moved here because it has type `&TraitA`, which is moved by default (use `copy` to override)
<anon>:23 object_bar(something);
^~~~~~~~~
error: aborting due to 2 previous errors
playpen: application terminated with error code 101
I think this is still as true as it ever was. In general, we aggressively "reborrow" all &mut values so as to hide the fact that they are affine (that is, they move by default). But in this particular case of doing x as &mut Foo, we do not reborrow, so the value x gets consumed. This is not exactly a bug but it is suboptimal. I consider it an enhancement.
I'm pulling a massive triage effort to get us ready for 1.0. As part of this, I'm moving stuff that's wishlist-like to the RFCs repo, as that's where major new things should get discussed/prioritized.
Activity
nikomatsakis commentedon Aug 28, 2013
Yes this is a bug. I've noticed it too. The compiler should reborrow automatically as we do almost everywhere else; you can workaround it temporarily by making the reborrow explicit (
&mut *self as &mut A).Ogeon commentedon Jul 26, 2014
I noticed that this is also an issue with references to generics. The following example should compile and run just fine:
but
rustccomplains withPlaypen link
steveklabnik commentedon Jan 23, 2015
We no longer auto-borrow in this situation, so I think that this is invalid. @nikomatsakis @alexcrichton ?
nikomatsakis commentedon Feb 2, 2015
I think this is still as true as it ever was. In general, we aggressively "reborrow" all
&mutvalues so as to hide the fact that they are affine (that is, they move by default). But in this particular case of doingx as &mut Foo, we do not reborrow, so the valuexgets consumed. This is not exactly a bug but it is suboptimal. I consider it an enhancement.steveklabnik commentedon Feb 2, 2015
I'm pulling a massive triage effort to get us ready for 1.0. As part of this, I'm moving stuff that's wishlist-like to the RFCs repo, as that's where major new things should get discussed/prioritized.
This issue has been moved to the RFCs repo: rust-lang/rfcs#800
Auto merge of rust-lang#8813 - evantypanski:redundant_alloc_fat_ptr, …
Merge rust-lang#8813