diff --git a/tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md b/tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md index 8c6d947bf..3bf6cecc5 100644 --- a/tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md +++ b/tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md @@ -996,10 +996,11 @@ checks that the program is rejected against the rc library. 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. 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 + the slot holds the method the call names. MLIRGen marks its `ts.ThisVirtualSymbolRef` (an + accessor's `ts.VirtualSymbolRef`) `__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`), but not across one the library does not list + (`import_own_record_final_drops.ts`), nor across a method call on a class that is not `@final` (`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 diff --git a/tslang/docs/superpowers/specs/2026-10-19-final-classes-design.md b/tslang/docs/superpowers/specs/2026-10-19-final-classes-design.md index 9bd9b0aaa..43219080b 100644 --- a/tslang/docs/superpowers/specs/2026-10-19-final-classes-design.md +++ b/tslang/docs/superpowers/specs/2026-10-19-final-classes-design.md @@ -123,7 +123,7 @@ 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. +**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. An accessor's `ts.VirtualSymbolRef` is marked and resolved the same way. 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 diff --git a/tslang/include/TypeScript/Defines.h b/tslang/include/TypeScript/Defines.h index b5765f319..4089e5100 100644 --- a/tslang/include/TypeScript/Defines.h +++ b/tslang/include/TypeScript/Defines.h @@ -325,8 +325,9 @@ // `@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 +// On a `ts.ThisVirtualSymbolRef` (a method's) or a `ts.VirtualSymbolRef` (an accessor's): 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 diff --git a/tslang/lib/TypeScript/MLIRGenAccessCall.cpp b/tslang/lib/TypeScript/MLIRGenAccessCall.cpp index d92ff016e..ce1460f38 100644 --- a/tslang/lib/TypeScript/MLIRGenAccessCall.cpp +++ b/tslang/lib/TypeScript/MLIRGenAccessCall.cpp @@ -708,10 +708,17 @@ namespace mlirgen assert(genContext.allowPartialResolve || methodInfo->virtualIndex >= 0); - return builder.create( + auto virtualSymbOp = builder.create( location, funcEntry.funcType, vtableAccess, builder.getI32IntegerAttr(methodInfo->virtualIndex), mlir::FlatSymbolRefAttr::get(builder.getContext(), funcEntry.name)); + // a @final receiver of another module's class, as ClassMethodAccess marks it + if (finalReceiver && !methodInfo->isAbstract) + { + virtualSymbOp->setAttr(FINAL_RECEIVER_ATTR_NAME, builder.getUnitAttr()); + } + + return virtualSymbOp; }; auto getterValue = resolveVirtualAccessorFunc(getFunc, getMethodInfo); diff --git a/tslang/lib/TypeScript/OwnershipSignatureResolve.cpp b/tslang/lib/TypeScript/OwnershipSignatureResolve.cpp index 7949cf06b..77d7be2cf 100644 --- a/tslang/lib/TypeScript/OwnershipSignatureResolve.cpp +++ b/tslang/lib/TypeScript/OwnershipSignatureResolve.cpp @@ -270,8 +270,12 @@ namespace own_signature } else if (auto refOp = mlir::dyn_cast_or_null(def)) { + // an accessor's, read the same way identifier = refOp.getIdentifierAttr().getAttr(); - index = refOp.getIndex(); + if (!refOp->hasAttr(FINAL_RECEIVER_ATTR_NAME)) + { + index = refOp.getIndex(); + } } else if (auto refOp = mlir::dyn_cast_or_null(def)) { diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 8fbc14861..dee036729 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -1624,6 +1624,11 @@ 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 after a call of a @final class's accessor its library does not list as destroying nothing +add_test(NAME test-compile-shared-import-own-record-final-drops + COMMAND test-runner -shared -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/import_own_record_final_drops.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_own_record.ts") +set_tests_properties(test-compile-shared-import-own-record-final-drops PROPERTIES PASS_REGULAR_EXPRESSION "borrows the result of 'Maker.pairWith' 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 diff --git a/tslang/test/tester/tests/export_own_record.ts b/tslang/test/tester/tests/export_own_record.ts index 5df0ecda6..689170287 100644 --- a/tslang/test/tester/tests/export_own_record.ts +++ b/tslang/test/tester/tests/export_own_record.ts @@ -1,8 +1,9 @@ // 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. -// `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. +// `Maker.weigh`, the `Maker.weight` accessors and `Open.look` destroy nothing, and the library says +// so (`__own_no_drops`); `Maker.rebase` destroys the node it replaces. Only `Maker` is @final. See +// import_own_record.ts, import_own_record_open_method.ts and import_own_record_final_drops.ts. export class Node { constructor(public v: number) {} } @@ -21,6 +22,7 @@ export function pairOf(a: Node, b: Node): Pair { @final export class Maker { base = new Node(5); + w = 0; @lends("this", "n") @record @@ -31,6 +33,19 @@ export class Maker { weigh(n: Node) { return n.v + this.base.v; } + + get weight() { + return this.base.v; + } + + set weight(n: number) { + this.w = n; + } + + @keeps("n") + set rebase(n: Node) { + this.base = n; + } } export class Open { diff --git a/tslang/test/tester/tests/import_own_record.ts b/tslang/test/tester/tests/import_own_record.ts index adfb00186..e0681b789 100644 --- a/tslang/test/tester/tests/import_own_record.ts +++ b/tslang/test/tester/tests/import_own_record.ts @@ -1,8 +1,9 @@ // 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 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. +// nodes to their owners. Each record is read after a call to `Maker.weigh`, and the second after +// `Maker.weight` is set and read too, which the library lists as destroying nothing: `Maker` is +// @final, so a call through its vtable is to that method or accessor and no other. Under gc and rc +// the markers mean nothing. import './export_own_record' function main() { @@ -19,10 +20,12 @@ function main() { for (let i = 0; i < 1000; i++) { const q = m.pairWith(a); sum += m.weigh(b); + m.weight = i; + sum += m.weight; sum += q.a.v + q.b.v; } - assert(sum == 22000, "records across the boundary"); - assert(a.v == 1 && b.v == 2 && m.base.v == 5, "the borrowed nodes outlive the records"); + assert(sum == 27000, "records across the boundary"); + assert(a.v == 1 && b.v == 2 && m.base.v == 5 && m.w == 999, "the borrowed nodes outlive the records"); print("done."); } diff --git a/tslang/test/tester/tests/import_own_record_final_drops.ts b/tslang/test/tester/tests/import_own_record_final_drops.ts new file mode 100644 index 000000000..1f16b18d2 --- /dev/null +++ b/tslang/test/tester/tests/import_own_record_final_drops.ts @@ -0,0 +1,12 @@ +// -mm=own: `Maker` (export_own_record.ts) is @final, so `m.rebase = ...` is a call of the setter the +// library defines, and the library does not list it as destroying nothing: it replaces the node the +// record borrows. A read of the record after it is refused. +import './export_own_record' + +function main() { + const a = new Node(1); + const m = new Maker(); + const q = m.pairWith(a); + m.rebase = new Node(9); + print(q.a.v); +}