Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,13 @@ checks that the program is rejected against the rc library.

- An exported function, or a method of an exported class, gets no owned or borrowed facts (§15.8
says why they cannot be exported), and a virtual call on one gets no `__own_no_drops`. That is
sound and restrictive for `-shared` modules.
sound and restrictive for `-shared` modules. The exception is a call on an object of another
module's `@final` class. It still goes through the vtable, but no class can extend the class, so
the slot holds the method the call names. MLIRGen marks its `ts.ThisVirtualSymbolRef`
`__final_receiver`, and the signature pass resolves the call as one to that imported function,
so its `__own_no_drops` applies. A record (§32) can then be read across such a call
(`import_own_record.ts`); across a method call on a class that is not `@final` it cannot
(`import_own_record_open_method.ts`).
- A value an importer under own receives from a library built under rc (a call's result, now an
object it builds with `new` too) is destroyed at the end of its owner's scope. Own's release
skips only immortal blocks. So if the library kept a reference of its own, that is a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ already resolves (`addDefined`, or `addImported` for an own library over a DLL i
canonicalizes to the `SymbolCallInternal` that `isFresh` accepts. `noDrops` does not depend on a
function being open, so the public `RegExp.exec` called directly gets its body's facts.

**Amended 2026-10-24.** A call on an object of a `@final` class another module defines stays a vtable call (see "Direct calls only into this module"). MLIRGen marks its `ts.ThisVirtualSymbolRef` `__final_receiver`, and `resolveValue` resolves it as a call to the method it names, not to the method's family. So `addImported` applies, and an own library's `__own_no_drops` covers the call.

The builder checks this first, on the three lib.ts shapes (`re.exec(s)` returning
`MatchResults | null`, `this.toString()` returned as `string | number`, and
`this.value.match(expr)`, whose callee destroys its own local `MatchResults` after `[...result]`).
Expand Down
4 changes: 4 additions & 0 deletions tslang/include/TypeScript/Defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@
// `@final class C`: no class may extend C, so a call on a value of type C is a direct call to the
// method C has (docs/superpowers/specs/2026-10-19-final-classes-design.md)
#define FINAL_DECORATOR "final"
// On a `ts.ThisVirtualSymbolRef`: the receiver's class is @final, but defined in another module, so
// the call still goes through the vtable. The slot holds the method the reference names, which
// -mm=own calls the way it calls that module's function: its `__own_no_drops` applies.
#define FINAL_RECEIVER_ATTR_NAME "__final_receiver"
// the old name of @cpp, an error now: unknown, it would leave the class an ordinary one
#define CPP_VTABLE_OLD_NAME "cppvtable"
// A @cpp method's C++ return type has a destructor or a copy constructor (see
Expand Down
5 changes: 5 additions & 0 deletions tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8408,6 +8408,11 @@ class MLIRGenImpl
location, getBoundFunctionType(effectiveFuncType), effectiveThisValue, vtableAccess,
builder.getI32IntegerAttr(methodInfo.virtualIndex),
mlir::FlatSymbolRefAttr::get(builder.getContext(), funcName));
if (finalReceiver && !methodInfo.isAbstract)
{
thisVirtualSymbOp->setAttr(FINAL_RECEIVER_ATTR_NAME, builder.getUnitAttr());
}

return thisVirtualSymbOp;
}

Expand Down
6 changes: 5 additions & 1 deletion tslang/lib/TypeScript/OwnershipSignatureResolve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,11 @@ namespace own_signature
if (auto refOp = mlir::dyn_cast_or_null<mlir_ts::ThisVirtualSymbolRefOp>(def))
{
identifier = refOp.getIdentifierAttr().getAttr();
index = refOp.getIndex();
// a @final receiver's slot holds the method named: no family, one function
if (!refOp->hasAttr(FINAL_RECEIVER_ATTR_NAME))
{
index = refOp.getIndex();
}
}
else if (auto refOp = mlir::dyn_cast_or_null<mlir_ts::VirtualSymbolRefOp>(def))
{
Expand Down
5 changes: 5 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1619,6 +1619,11 @@ add_test(NAME test-compile-shared-import-own-record-outlives
COMMAND test-runner -shared -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/import_own_record_outlives.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_own_record.ts")
set_tests_properties(test-compile-shared-import-own-record-outlives PROPERTIES PASS_REGULAR_EXPRESSION "borrows a call's result but is used here after it may be released or overwritten"
FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed")
# ...and after a call of a method of a class that is not @final: an override may destroy what it borrows
add_test(NAME test-compile-shared-import-own-record-open-method
COMMAND test-runner -shared -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/import_own_record_open_method.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_own_record.ts")
set_tests_properties(test-compile-shared-import-own-record-open-method PROPERTIES PASS_REGULAR_EXPRESSION "borrows a call's result but is used here after it may be released or overwritten"
FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed")
# ...and the importer releases each box, which no run can see (own-shared-record.cmake)
if (WIN32)
add_test(NAME test-own-shared-record-released
Expand Down
13 changes: 12 additions & 1 deletion tslang/test/tester/tests/export_own_record.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// The library side of @record across a module boundary (own spec 32): `pairOf` and `Maker.pairWith`
// return a borrowing record - a box their caller owns, holding a borrow of each argument `@lends`
// names. The importer sees only these declarations; `@record` tells it the box is its to release.
// See import_own_record.ts.
// `Maker.weigh` and `Open.look` destroy nothing, and the library says so (`__own_no_drops`); only
// `Maker` is @final. See import_own_record.ts and import_own_record_open_method.ts.
export class Node {
constructor(public v: number) {}
}
Expand All @@ -26,4 +27,14 @@ export class Maker {
pairWith(n: Node): Pair {
return { a: this.base, b: n };
}

weigh(n: Node) {
return n.v + this.base.v;
}
}

export class Open {
look(n: Node) {
return n.v;
}
}
11 changes: 7 additions & 4 deletions tslang/test/tester/tests/import_own_record.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
// A borrowing record from another module (export_own_record.ts): the importer owns each box
// `pairOf` and `pairWith` return, and releases it at the end of each iteration, leaving the borrowed
// nodes to their owners. Each loop calls one of them: a call to another module's method may destroy
// what it can reach, so a record cannot be read across one. Under gc and rc the markers mean nothing.
// nodes to their owners. Each record is read after a call to `Maker.weigh`, which the library lists
// as destroying nothing: `Maker` is @final, so the call through its vtable is to that method and no
// other. Under gc and rc the markers mean nothing.
import './export_own_record'

function main() {
const a = new Node(1);
const b = new Node(2);
const m = new Maker();
let sum = 0;
for (let i = 0; i < 1000; i++) {
const p = pairOf(a, b);
sum += m.weigh(a);
sum += p.a.v + p.b.v;
}

const m = new Maker();
for (let i = 0; i < 1000; i++) {
const q = m.pairWith(a);
sum += m.weigh(b);
sum += q.a.v + q.b.v;
}

assert(sum == 9000, "records across the boundary");
assert(sum == 22000, "records across the boundary");
assert(a.v == 1 && b.v == 2 && m.base.v == 5, "the borrowed nodes outlive the records");
print("done.");
}
13 changes: 13 additions & 0 deletions tslang/test/tester/tests/import_own_record_open_method.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// -mm=own: `Open` (export_own_record.ts) is not @final, so a call of `look` may run an override the
// importer cannot see, one that destroys what the record borrows: although the library lists
// `Open.look` as destroying nothing, a read of the record after the call is refused.
import './export_own_record'

function main() {
const a = new Node(1);
const b = new Node(2);
const o = new Open();
const p = pairOf(a, b);
print(o.look(a));
print(p.a.v);
}
Loading