Skip to content

Mutable trait object cast counts as move, not as temporary borrow #11163

New issue

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

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

Already on GitHub? Sign in to your account

Closed
Kimundi opened this issue Dec 27, 2013 · 3 comments
Closed

Mutable trait object cast counts as move, not as temporary borrow #11163

Kimundi opened this issue Dec 27, 2013 · 3 comments

Comments

@Kimundi
Copy link
Member

Kimundi commented Dec 27, 2013

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.

@Kimundi
Copy link
Member Author

Kimundi commented Dec 27, 2013

cc @nikomatsakis

@alexcrichton
Copy link
Member

Possible dupe of #8813, but I don't know enough about this to close one way or the other.

@nikomatsakis
Copy link
Contributor

Dup of #8813

flip1995 pushed a commit to flip1995/rust that referenced this issue Jul 31, 2023
changelog: [`min_ident_chars`]: don't lint const generics

Fixes: rust-lang#11163

changelog: [`min_ident_chars`]: don't lint const generics
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

No branches or pull requests

3 participants