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 @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions tslang/include/TypeScript/Defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion tslang/lib/TypeScript/MLIRGenAccessCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -708,10 +708,17 @@ namespace mlirgen

assert(genContext.allowPartialResolve || methodInfo->virtualIndex >= 0);

return builder.create<mlir_ts::VirtualSymbolRefOp>(
auto virtualSymbOp = builder.create<mlir_ts::VirtualSymbolRefOp>(
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);
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 @@ -270,8 +270,12 @@ namespace own_signature
}
else if (auto refOp = mlir::dyn_cast_or_null<mlir_ts::VirtualSymbolRefOp>(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<mlir_ts::ThisSymbolRefOp>(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 @@ -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
Expand Down
19 changes: 17 additions & 2 deletions tslang/test/tester/tests/export_own_record.ts
Original file line number Diff line number Diff line change
@@ -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) {}
}
Expand All @@ -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
Expand All @@ -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 {
Expand Down
13 changes: 8 additions & 5 deletions tslang/test/tester/tests/import_own_record.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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.");
}
12 changes: 12 additions & 0 deletions tslang/test/tester/tests/import_own_record_final_drops.ts
Original file line number Diff line number Diff line change
@@ -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);
}
Loading