Skip to content
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

No errors/warning for usage of moved value with structs #55653

Closed
synepis opened this issue Nov 3, 2018 · 1 comment
Closed

No errors/warning for usage of moved value with structs #55653

synepis opened this issue Nov 3, 2018 · 1 comment

Comments

@synepis
Copy link

synepis commented Nov 3, 2018

The Rust compiler doesn't complain upon trying to use a moved value.
Note that there seems to be no side effects, but it should still be disallowed or at least warned against.

Example (try it on Playgound):

#[derive(Debug)]
struct Point { x: i32, y: i32 }

fn main() {
    let mut p1 = Point { x: 1, y: 2 };
    println!("p1: {:?}", p1);    // p1: Point { x: 1, y: 2 }
    p1.x = -1;
    println!("p1: {:?}", p1);    //  p1: Point { x: -1, y: 2 }

    let mut p2 = p1;
    println!("p2: {:?}", p2);    // p2: Point { x: -1, y: 2 }
    p2.x = 3;
    // println!("p1: {:?}", p1); // Compiler error: "p1 was already moved!"
    p1.x = -11;                  // Compiles but what does it do?
    println!("p2: {:?}", p2);    // p2: Point { x: 3, y: 2
}

I would expect p1.x = -11; not to compile.

$ rustc --version --verbose
rustc 1.28.0
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.28.0
@sfackler
Copy link
Member

sfackler commented Nov 3, 2018

Duplicate of #21232.

@sfackler sfackler closed this as completed Nov 4, 2018
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

2 participants