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 0fc894dff..8c6d947bf 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 @@ -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 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 63f46c019..9bd9b0aaa 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,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]`). diff --git a/tslang/include/TypeScript/Defines.h b/tslang/include/TypeScript/Defines.h index dc8aab927..b5765f319 100644 --- a/tslang/include/TypeScript/Defines.h +++ b/tslang/include/TypeScript/Defines.h @@ -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 diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index 907eab434..66c4e06dd 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -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; } diff --git a/tslang/lib/TypeScript/OwnershipSignatureResolve.cpp b/tslang/lib/TypeScript/OwnershipSignatureResolve.cpp index d4d50bc24..7949cf06b 100644 --- a/tslang/lib/TypeScript/OwnershipSignatureResolve.cpp +++ b/tslang/lib/TypeScript/OwnershipSignatureResolve.cpp @@ -262,7 +262,11 @@ namespace own_signature if (auto refOp = mlir::dyn_cast_or_null(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(def)) { diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 1b305b496..8fbc14861 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -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 diff --git a/tslang/test/tester/tests/export_own_record.ts b/tslang/test/tester/tests/export_own_record.ts index 035047165..5df0ecda6 100644 --- a/tslang/test/tester/tests/export_own_record.ts +++ b/tslang/test/tester/tests/export_own_record.ts @@ -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) {} } @@ -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; + } } diff --git a/tslang/test/tester/tests/import_own_record.ts b/tslang/test/tester/tests/import_own_record.ts index c72e98179..adfb00186 100644 --- a/tslang/test/tester/tests/import_own_record.ts +++ b/tslang/test/tester/tests/import_own_record.ts @@ -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."); } diff --git a/tslang/test/tester/tests/import_own_record_open_method.ts b/tslang/test/tester/tests/import_own_record_open_method.ts new file mode 100644 index 000000000..515262dd8 --- /dev/null +++ b/tslang/test/tester/tests/import_own_record_open_method.ts @@ -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); +}