Skip to content

unused var causes copying of resource type #2408

Closed

Description

@rntz

The following program prints "0\n0\n", when it should print "0\n":

resource echo(x: {mut a: int}) {
    io::println(#fmt("%d", x.a));
    x.a += 1;
}

fn main() {
    let x = [echo({mut a: 0})];
    let y = x;
}

The same effect occurs if you rename y to _y to eliminate the unused variable warning. The same effect does not occur if you use y, eg the following program has correct behavior:

resource echo(x: {mut a: int}) {
    io::println(#fmt("%d", x.a));
    x.a += 1;
}

fn main() {
    let x = [echo({mut a: 0})];
    let y = x;
    y;
}

The use of a record with a mutable field is also not essential to the bug; I used it to demonstrate that the bug is actually due to copying, and not running the destructor twice on the same value.

Activity

  1. pcwalton commented on May 18, 2012

    @pcwalton
    Contributor

    I suspect the no-implicit-copies proposal fixes this.

  2. nikomatsakis commented on May 18, 2012

    @nikomatsakis
    Contributor

    in theory, resources ought not to be copyable in the first place.

  3. msullivan commented on May 29, 2012

    @msullivan
    Contributor

    y is a last-use of x, though, so it isn't a copy, it's a move, right?

  4. nikomatsakis commented on May 30, 2012

    @nikomatsakis
    Contributor

    indeed. I presume then that this is a bug in trans, where the last_use information is not being used and so a copy is induced.

  5. nikomatsakis commented on May 30, 2012

    @nikomatsakis
    Contributor

    hmm, my theory may not hold water. it's not clear to me why using y would prevent the error, then.

  6. msullivan commented on Jun 5, 2012

    @msullivan
    Contributor

    It looks like the bug is in the code for initializing locals when it has been determined that they can be kept off of the stack. As it turns out, basically the only way to keep liveness from saying something needs to be kept on the stack is for it to be unused.

    I'm just going to fix this by getting rid of the stuff where we avoid spilling variables. It never seems to get used and llvm can do it anyways.

  7. nikomatsakis commented on Jun 6, 2012

    @nikomatsakis
    Contributor

    @msullivan is this bug specifically in the code that you will be deleting? or is it some more general thing in the treatment of immediates?

  8. msullivan commented on Jun 6, 2012

    @msullivan
    Contributor

    I believe the bug is specifically in the code for immediate local variable initialization, but similar bugs might lurk elsewhere.

  9. added a commit that references this issue on Sep 22, 2022
  10. added a commit that references this issue on Jun 2, 2025
  11. added a commit that references this issue on Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions