Skip to content

Copying Primitive Types #8

Description

@phase

With 0b6cf37, I introduced a very primitive ownership implementation into the Semantic Analysis pass. The biggest caveat of this is that it will error when moving primitive types, such as ints.

In Rust, you can't use a struct once you've moved it.

struct P {
    x: i32,
}

fn main() {
    let a = P { x: 7 };
    let b = a;
    let c = a.x + b.x;
    println!("{}", c);
}

This code will result in the following error:

rustc 1.16.0 (30cf806ef 2017-03-10)
error[E0382]: use of moved value: `a.x`
 --> <anon>:8:13
  |
7 |     let b = a;
  |         - value moved here
8 |     let c = a.x + b.x;
  |             ^^^ value used here after move
  |
  = note: move occurs because `a` has type `P`, which does not implement the `Copy` trait

error: aborting due to previous error

This behavior has been implemented:

class P
    let x : Int

    init (v : Int)
        x = v
;

main () : Int
    let a = new P(7),
    let b = a,
    let c = a.x + b.x,
    0
11             let b = a,
12             let c = a.x + b.x,
   ~~~~~~~~~~~~^
13             0
Reference to 'a' has already been used.

However, Rust handles primitives differently...

fn main() {
    let a = 7;
    let b = a;
    let c = a + b;
    println!("{}", c);
}

This code is perfectly allowed in Rust, since a is being copied to b when it is moved.

main ()
  let a = 7,
  let b = a,
  let c = a + b,
  0

This will currently error with the following:

4             let b = a,
5             let c = a + b,
  ~~~~~~~~~~~~^
6             0
Reference to 'a' has already been used.

If we look at the above Rust error, we can see that our struct didn't implement the Copy trait. Integers in Rust implement this trait, and thus can be copied. It would be easy to create a special case for our defined primitive types, but having something like "traits" would be beneficial to users who want their classes to be copyable. Maybe an annotation?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions