Skip to content

unused var causes copying of resource type #2408

Closed
@rntz

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.

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