Skip to content

-mm=own: a let assigned a new value after its value moved is still treated as moved #528

Closed

Description

@ASDAlexander77

Under -mm=own, once a let has given its value away, assigning it a new object does not make it an owner again. A later read is reported as a use after the move, or as a borrow outliving an overwrite. Rust accepts both shapes: assigning to a moved-from binding gives it a fresh value. Both programs print 1 2 under -mm=rc and -mm=gc.

Moved into a field

class Node { constructor(public value: number) {} }
class Holder { node: Node; }

function main() {
    const h = new Holder();
    let a = new Node(1);
    h.node = a;          // the first Node moves into h
    a = new Node(2);     // a owns a new Node
    print(h.node.value, a.value);
}
reassign.ts:9:25: error: 'this value' is used here after its value was moved
    print(h.node.value, a.value);
                        ^
reassign.ts:7:5: note: value moved here
    h.node = a;          // the first Node moves into h
    ^

Moved into another local

class Node { constructor(public value: number) {} }

function main() {
    let a = new Node(1);
    let b = a;           // the first Node moves into b
    a = new Node(2);     // a owns a new Node
    print(b.value, a.value);
}
reassign2.ts:7:11: error: 'this value' borrows 'this value' but is used here after 'this value' is released or overwritten
    print(b.value, a.value);
          ^
reassign2.ts:6:5: note: 'this value' is released or overwritten here

Here b borrows a instead of taking it over, because a is read again later. The overwrite then ends the borrow, even though the later read is of the new value.

Expected

Both programs compile and print 1 2. The assignment a = new Node(2) ends the old value's story for a. The moved-from slot holds nothing to release, and reads after the assignment see the new owner.

Notes

🤖 Generated with Claude Code

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions