Overloaded operators can copy self when self is noncopyable. #2548

Closed
eholk opened this Issue Jun 8, 2012 · 9 comments

Comments

Projects
None yet
4 participants
@eholk
Contributor

eholk commented Jun 8, 2012

resource r<T> (_x: T) { io::println("Goodbye, World!") }

fn main() {
    let mut res = r(5);

    let mut v = [mut];
    v <- [mut res] + v;
}

This outputs Goodbye, World! twice, when it should only do it once.

@eholk

This comment has been minimized.

Show comment
Hide comment
@eholk

eholk Jun 8, 2012

Contributor

This small variant has the correct behavior.

resource r<T> (_x: T) { io::println("Goodbye, World!") }

fn main() {
    let mut res = r(5);

    let mut v = [mut];
    v <- [mut res];
}
Contributor

eholk commented Jun 8, 2012

This small variant has the correct behavior.

resource r<T> (_x: T) { io::println("Goodbye, World!") }

fn main() {
    let mut res = r(5);

    let mut v = [mut];
    v <- [mut res];
}
@eholk

This comment has been minimized.

Show comment
Hide comment
@eholk

eholk Jun 8, 2012

Contributor

This happens because trans does not know to move in tvec::trans_add. I'm attempting to fix this by moving vector addition to libcore.

Contributor

eholk commented Jun 8, 2012

This happens because trans does not know to move in tvec::trans_add. I'm attempting to fix this by moving vector addition to libcore.

@ghost ghost assigned eholk Jun 11, 2012

@eholk

This comment has been minimized.

Show comment
Hide comment
@eholk

eholk Jun 21, 2012

Contributor

I just landed the "move vector-append out of trans" code, so this should be mostly fixed. Unfortunately, there's a bug in checking self types that means it's still not too hard to copy an uncopyable.

Contributor

eholk commented Jun 21, 2012

I just landed the "move vector-append out of trans" code, so this should be mostly fixed. Unfortunately, there's a bug in checking self types that means it's still not too hard to copy an uncopyable.

@hatahet

This comment has been minimized.

Show comment
Hide comment
@hatahet

hatahet Jul 18, 2012

Contributor

Are there 2 instances being created?

class r {
  let mut x: int;
  new() {
    self.x = 1;
  }
  drop {
    self.x += 1;
    io::println("self.x: " + int::str(self.x));
  }
}

fn main() {
  let mut res = r();

  let mut _v = ~[mut];
  _v <- ~[mut res] + _v;
}

Output:

self.x: 2
self.x: 2
Contributor

hatahet commented Jul 18, 2012

Are there 2 instances being created?

class r {
  let mut x: int;
  new() {
    self.x = 1;
  }
  drop {
    self.x += 1;
    io::println("self.x: " + int::str(self.x));
  }
}

fn main() {
  let mut res = r();

  let mut _v = ~[mut];
  _v <- ~[mut res] + _v;
}

Output:

self.x: 2
self.x: 2
@eholk

This comment has been minimized.

Show comment
Hide comment
@eholk

eholk Jul 18, 2012

Contributor

Yeah, that's the problem with this issue. Classes with a destructor are supposed to be non-copyable, but this shows how to trick the type system into copying one for you.

Contributor

eholk commented Jul 18, 2012

Yeah, that's the problem with this issue. Classes with a destructor are supposed to be non-copyable, but this shows how to trick the type system into copying one for you.

@nikomatsakis

This comment has been minimized.

Show comment
Hide comment
@nikomatsakis

nikomatsakis Aug 3, 2012

Contributor

I don't understand, what leads to the copy of self? @eholk can you update the title and maybe add a comment on what precisely is still wrong?

Contributor

nikomatsakis commented Aug 3, 2012

I don't understand, what leads to the copy of self? @eholk can you update the title and maybe add a comment on what precisely is still wrong?

@eholk

This comment has been minimized.

Show comment
Hide comment
@eholk

eholk Aug 3, 2012

Contributor

@nikomatsakis I'll add an updated test case on this. After thinking about it for a bit, I think this might be a dup of #2587.

Contributor

eholk commented Aug 3, 2012

@nikomatsakis I'll add an updated test case on this. After thinking about it for a bit, I think this might be a dup of #2587.

eholk added a commit that referenced this issue Aug 3, 2012

@eholk

This comment has been minimized.

Show comment
Hide comment
@eholk

eholk Aug 3, 2012

Contributor

Hmm, that test case should actually be a compile-fail test (unless + took ownership of self), since + is defined on vectors of copyable things.

Contributor

eholk commented Aug 3, 2012

Hmm, that test case should actually be a compile-fail test (unless + took ownership of self), since + is defined on vectors of copyable things.

@catamorphism

This comment has been minimized.

Show comment
Hide comment
@catamorphism

catamorphism Sep 6, 2012

Contributor

Should be fixed as of 46990ad

Contributor

catamorphism commented Sep 6, 2012

Should be fixed as of 46990ad

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