Closed
Description
struct MovingType { data: ~() }
trait Trait {}
impl Trait for MovingType {}
fn main() {
let mut x: MovingType = MovingType { data: ~() };
a(&mut x);
}
fn a(x: &mut MovingType) {
b(x);
{
c(x as &mut Trait);
//c_cast_wrapper(x);
}
b(x);
}
fn b(_: &mut MovingType) {}
fn c(_: &mut Trait) {}
fn c_cast_wrapper(x: &mut MovingType) {
c(x as &mut Trait);
}
The compiler currently rejects the code above, because the as &mut Trait
cast counts as moving the &mut MovingType
reference:
moved_mutable_ref_trait_cast.rs:16:6: 16:7 error: use of moved value: `x`
moved_mutable_ref_trait_cast.rs:16 b(x);
^
moved_mutable_ref_trait_cast.rs:13:10: 13:11 note: `x` moved here because it has type `&mut MovingType`, which is non-copyable (perhaps you meant to use clone()?)
moved_mutable_ref_trait_cast.rs:13 c(x as &mut Trait);
^
error: aborting due to previous error
task 'rustc' failed at 'explicit failure', /build/rust-git/src/rust/src/libsyntax/diagnostic.rs:102
task '<main>' failed at 'explicit failure', /build/rust-git/src/rust/src/librustc/lib.rs:440
However, if you introduce indirection by having the cast be performed by a wrapper that takes &mut MovingType
, then the program compiles.
This can be demonstrated by swapping the the commented and uncommented lines in the code block.
As far as I can see, this is a bug, as either the compiler should allow both or forbid both.
Metadata
Metadata
Assignees
Labels
No labels