Skip to content

-mm=rc: { s: p.child } takes no reference; overwriting p.child frees what the literal holds #460

Closed

Description

@ASDAlexander77

Under -mm=rc, an object literal whose field is read out of another object's field takes no reference. Overwriting the source field frees the value the literal still holds.

class Node {
    name = "";
}

class P {
    child: Node = new Node();
}

function main() {
    const p = new P();
    p.child.name = "first" + 1;
    const o = { s: p.child };
    p.child = new Node();
    let keep: string[] = [];
    for (let i = 0; i < 1000; i++) keep.push("k" + i);
    print(o.s.name);
}

Expected: first1.

Actual, tslang --emit=jit --no-default-lib, main 8b9d631:

  • gc and none: first1.
  • rc: k15. The node and its string were freed, and the keep loop reused the memory.

Under -mm=own the program is rejected (borrows a field and cannot be stored), which is correct. Found by the review of Shared<T> (own phase 8). A Shared<T> field read into an object literal crashes under rc the same way.

🤖 Generated with Claude Code

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