Skip to content

Using by-value self with an immediate, implicity-copyable struct should pass a copy as the self param. #10615

Closed
@luqmana

Description

@luqmana

This is from a code sample sent to the mailing list: https://mail.mozilla.org/pipermail/rust-dev/2013-November/006924.html

struct Value {
    n: int
}

impl Value {
    fn squared(mut self) -> Value {
        self.n *= self.n;
        self
    }
}

fn main() {
    let x = Value { n: 3 };
    let y = x.squared();
    println!("{} {}", x.n, y.n);
}

Prints out:

-> % rustc foo.rs && ./foo
9 9

The call to squared should be receiving a copy of x as the self param. This bug is only exhibited in the case of implicity copyable struct. If you add something like ~int to Value the compiler will rightly complain that x has been moved.

Also, if you add another field (like m: int) making Value an immediate no longer, we see the right behaviour:

-> % rustc foo.rs && ./foo
3 9

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: Code generation

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions