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
28 changes: 14 additions & 14 deletions tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -920,8 +920,8 @@ drop-free `.instanceOf` call, the payload is borrowed for as long as the box liv
forwarding another's borrow, a field-or-`null` result, a returned parameter), `own_param_kept`
(field, forwarding, method into `this`, push, constructor parameter property), `own_any_box`.
- **Negatives:**
- calls that drop: `own_err_call_drops_indirectly`, `own_err_call_given_borrow`,
`own_err_call_this_borrow`, `own_err_exported_virtual_call`;
- calls that drop: `own_err_call_drops_indirectly`, `own_counted_call_given_borrow` (counted since TSLANG 240),
`own_counted_call_this_borrow` (counted since TSLANG 240), `own_err_exported_virtual_call`;
- borrowed results: `own_err_borrowed_result_stored`, `own_err_borrowed_result_outlives`,
`own_err_borrowed_result_let_outlives`, `own_err_borrowed_result_escapes`,
`own_err_borrowed_result_family`;
Expand Down Expand Up @@ -3560,7 +3560,7 @@ rc, gc and none, AOT and JIT, and on Linux; own emits no counting. `own_interfac
function field through a global and a local `Shared<S>` of a literal, through a parameter with no
handle, and through a handle given as a parameter, each after churn, under the four models (counted,
so not in `own_test`), and `--verify-ownership` is quiet under own and rc. The five §27 negatives
now export their interface (the open form, same texts). New negatives: `own_err_interface_exported_impl`
now export their interface (the open form, same texts). New negatives: `own_counted_interface_exported_impl` (counted since TSLANG 240)
(the plan's program with `export class B`), `own_err_interface_family_disagrees`,
`own_err_interface_member_drops`, `own_err_interface_from_interface`,
`own_err_interface_result_outlives`, `own_err_interface_fn_field_drops`,
Expand Down Expand Up @@ -3636,7 +3636,7 @@ a block freed too early is reused before it is read.
function); the second remains.
`own_declared_scalar_no_drops` holds a field's
borrow across `sqrt`, across a function that calls `sqrt` and `floor`, and across a call through a
closed interface whose member does; `own_err_declared_drops` (`strlen(s: string)`) is unchanged.
closed interface whose member does; `own_counted_declared_drops` (counted since TSLANG 238-240) (`strlen(s: string)`) is unchanged.
- **A `public` method of a class no other module can name** (controller ruling) is not callable from
another module: the class is not exported (its vtable is private), not a word of `__decls`, and not
in the type of an exported or declared function or of an exported or imported global (a derived
Expand Down Expand Up @@ -3665,11 +3665,11 @@ a block freed too early is reused before it is read.
Teeth: with function reinterpretations not opening anything, `own_err_interface_fn_any_param` and
`_result` compile and their AOT runs fail with `0xC0000374` (with and without `--opt`), and `_drops`
prints 77777 instead of 5 (gc 5). With every declaration without `export` taken as destroying
nothing, `own_err_declared_drops` compiles, and so does a program that lends a field and passes the
nothing, `own_counted_declared_drops` (counted since TSLANG 238-240) compiles, and so does a program that lends a field and passes the
field's object to a declared `free(p: V)`; its AOT run fails with `0xC0000374`. With a `public`
method's class never nameable, `own_err_interface_public_reachable` compiles; with every module taken
for an executable, `own-exe-exports.cmake` fails (the object file and the DLL compile), and so do
`own_err_interface_exported_impl` and `own_err_interface_keeps_param`. The last two rules protect
`own_counted_interface_exported_impl` (counted since TSLANG 240) and `own_err_interface_keeps_param`. The last two rules protect
callers in another module, which no single-module run can show.

Corpus (`--emit=obj`, not an executable): 528 of 693 before this section and after, the same files.
Expand Down Expand Up @@ -4121,7 +4121,7 @@ start value, a named callback `add` that only borrows its accumulator beside `pi
under own, rc, gc and none, AOT and JIT, and on Linux; own emits no counting, and
`--verify-ownership` is quiet on it under own and rc. Negatives: `own_err_mixed_return`
(rewritten), `own_err_mixed_return_join_use` (a field read out of the kept parameter before the join
and used after it: `is moved here on some paths only`), `own_err_callback_escapes` (`pick` also
and used after it: `is moved here on some paths only`), `own_counted_callback_escapes` (counted since TSLANG 238-240) (`pick` also
called through a `let`: `cannot make the callers of 'pick' give it up`), `own_err_callback_disagrees`,
`own_err_callback_promoted_used`. `own_err_borrowed_result_escapes` passed `firstOf` straight to
`apply`; the call through `g` now reaches only `firstOf` and gets its borrowed result, so the program
Expand Down Expand Up @@ -4787,7 +4787,7 @@ function was not a function type.
`own_callback_forwarded` sorts an `Item[]` through an optional comparison given on (and with none
given), gives a callback on twice (`eachTwice` to `each`) and to the function itself, and calls
an optional comparison once tested, churning between steps, under own, rc, gc and none; own emits no counting. Negatives:
`own_err_callback_forwarded_drops` (the function passed to `sortIt` replaces a global array, so
`own_counted_callback_forwarded_drops` (counted since TSLANG 240) (the function passed to `sortIt` replaces a global array, so
`selectionSort`'s call with two elements is a use of them after they may be released) and
`own_err_callback_forwarded_keeps` (it keeps its argument in a global array, for good, which a call
through a value cannot make immortal).
Expand Down Expand Up @@ -5025,7 +5025,7 @@ that may destroy anything, and so was every call that gave the closure on.
`own_callback_captured` sums, finds the largest and walks through callbacks given to a closure that
calls them and to a local that names them, from a class's methods over the array a field holds,
churning between steps, under own, rc, gc and none; own emits no counting. Negative:
`own_err_callback_captured_drops`, whose callback replaces a global array that the walk's element
`own_counted_callback_captured_drops` (counted since TSLANG 238-240), whose callback replaces a global array that the walk's element
may be part of.

Not here, then: a closure held in a local and called, and a callback stored into a field or
Expand Down Expand Up @@ -5074,8 +5074,8 @@ local is a view of the closure, called by name with its box.
`own_callback_shapes` walks an array a class holds through a closure in a local, a callback stored
into a field by a constructor and by a method, and callbacks a function returns, called through a
local and given on, reading each element again after the call and churning between steps, under
own, rc, gc and none; own emits no counting. Negatives: `own_err_callback_field_drops` and
`own_err_callback_returned_drops`, whose callbacks replace a global array the walk's element may be
own, rc, gc and none; own emits no counting. Negatives: `own_counted_callback_field_drops` (counted since TSLANG 238-240) and
`own_counted_callback_returned_drops` (counted since TSLANG 238-240), whose callbacks replace a global array the walk's element may be
part of.

Not here: a class whose object becomes an interface or `any` keeps its fields' families open; so
Expand All @@ -5098,7 +5098,7 @@ a `!ts.ref<!ts.class_storage<@Walker>>`), where the family saw no access of its
interface, `any`, `object`, `unknown` or a union. A class-to-class cast (`Sub` to `Walker`) keeps it.

`own_callback_extended` stores and calls the base's field from a derived class and one two levels
down, under own, rc, gc and none; own emits no counting. Negative: `own_err_callback_extended_drops`,
down, under own, rc, gc and none; own emits no counting. Negative: `own_counted_callback_extended_drops` (counted since TSLANG 238-240),
whose derived class stores a function that replaces a global array the walk's element may be part of.

Not here: a class whose object, or a derived class's, becomes `any` keeps its fields' families open;
Expand Down Expand Up @@ -5166,7 +5166,7 @@ vtable holds (§58).
its callback field stored by the constructor and by a method, under own, rc, gc and none; own emits no
counting. `own_callback_interface_slot` calls two classes' methods through an interface, directly, and
with a caller's callback parameter given on through the interface. Negatives:
`own_err_callback_cast_field_drops` (a draining callback stored into a field of a class cast to `any`
`own_counted_callback_cast_field_drops` (counted since TSLANG 240) (a draining callback stored into a field of a class cast to `any`
and back), `own_err_callback_interface_slot_drops` (a callback given only through the interface still
counts for a direct call that lends the global array it replaces) and
`own_err_callback_interface_slot_targets` (a callback parameter given on through the slot reaches
Expand Down Expand Up @@ -6041,7 +6041,7 @@ the way back from a `return`) would leave one unreleased - the ownership verifie
own a clause local is decided as any local: `own_err_borrow_catch_local` / `_finally_local` now report
it where it is given what another only borrows.

Tests: `own_err_capture_block_closure`, `own_moved_some_paths_break`, `00catch_finally_locals_own` (rc,
Tests: `own_counted_capture_block_closure` (counted since TSLANG 238-240), `own_moved_some_paths_break`, `00catch_finally_locals_own` (rc,
gc, none; refused under own), `00catch_finally_owned_locals_eh` (every model). Fuzz seeds 920000 and
921000 x500: no failure.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ kinds:
that may free it is copied for the call, and the copy released after it, when the call is a
callback (a function value nothing can mark `@nodrops`); a call that keeps it takes the copy. A
direct call that may free it stays an error, so `@nodrops` keeps its meaning
(`own_err_declared_drops`). A string given twice is copied twice (it was
(`own_counted_declared_drops` (counted since TSLANG 238-240)). A string given twice is copied twice (it was
`own_err_string_call_keeps_twice`, an error for want of a retain to turn into a copy).
5. **A parameter kept on some paths only** is released on the others (spec 33's exit releases and
spec 30's edge releases): `takenParams` is the parameters a move reaches every return from, not
Expand Down Expand Up @@ -101,7 +101,7 @@ kinds:
Positive, under own (AOT and JIT): `own_generator_method_this`, `own_string_copy_call`,
`own_param_kept_some_paths`, `own_method_callbacks`, `own_loop_read_again`,
`own_length_drops_elements`, `own_tuple_param_kept`; each also prints the same under gc, rc and none.
Errors: `own_err_generator_method_reassigned`, `own_err_callback_method_overridden`.
Errors: `own_err_generator_method_reassigned`, `own_counted_callback_method_overridden` (counted since TSLANG 240).
Retired: `own_err_param_kept_some_paths` (rule 5), `own_err_string_call_keeps_twice` (rule 4).

## 6. Objects in `Map` and `Set`: `Shared<T>`
Expand Down
167 changes: 164 additions & 3 deletions tslang/lib/TypeScript/OwnershipFacts.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ namespace mlir_ts = mlir::typescript;
// every path (`return this`). Given a temporary the caller owns no more after the call, the result is
// the caller's own (spec 78).
#define OWN_RESULT_ITSELF_ATTR_NAME "__own_result_itself"
// Beside OWN_RESULT_BORROWS_ATTR_NAME naming one argument (a plain borrow, no record): the places every path reads the
// result through, from that argument down - [position, slot type] pairs as a chain has them (a field's position,
// OWN_INTERFACE_FIELD_POSITION through an interface, -1 for an element). The caller's chain of the result has these
// places, as if it had read them itself, not "anywhere under the argument": `this.near(n)` returning
// `this.lists[slot]` is not dropped by a call that only writes `this.bag` (TSLANG 236).
#define OWN_RESULT_PLACES_ATTR_NAME "__own_result_places"
// On the local an object literal of tuple type is built in, when it is an unboxed borrowing record
// (OWN_RESULT_RECORD_UNBOXED_ATTR_NAME): the fields it borrows.
#define OWN_RECORD_UNBOXED_FIELDS_ATTR_NAME "__own_record_unboxed_fields"
Expand Down Expand Up @@ -2545,6 +2551,15 @@ inline bool typeMayHold(mlir::ModuleOp module, mlir::Type holder, mlir::Type tar
return typeMayHold(module, holder, optionalType.getElementType(), seen);
}

// a value that holds no block (a number, a string's text is its own) frees nothing else, whatever the target -
// not a subclass's object either (TSLANG 237: an `int[]`'s elements)
if (mlir::isa<mlir_ts::NumberType, mlir_ts::BooleanType, mlir_ts::StringType, mlir_ts::CharType, mlir_ts::LiteralType,
mlir_ts::NullType, mlir_ts::UndefinedType, mlir_ts::EnumType, mlir::IntegerType,
mlir::FloatType, mlir::IndexType>(holder))
{
return false;
}

// the target an object of its type only: an array, or a @final class's (not a base class's, whose
// object may be a subclass's, held in a field of that type)
if (auto targetClass = mlir::dyn_cast<mlir_ts::ClassType>(target))
Expand All @@ -2561,9 +2576,7 @@ inline bool typeMayHold(mlir::ModuleOp module, mlir::Type holder, mlir::Type tar
return true;
}

if (mlir::isa<mlir_ts::NumberType, mlir_ts::BooleanType, mlir_ts::StringType, mlir_ts::CharType, mlir_ts::LiteralType,
mlir_ts::NullType, mlir_ts::UndefinedType, mlir_ts::EnumType, mlir_ts::OpaqueType, mlir::IntegerType,
mlir::FloatType, mlir::IndexType>(holder))
if (mlir::isa<mlir_ts::OpaqueType>(holder))
{
return false;
}
Expand Down Expand Up @@ -2798,6 +2811,25 @@ inline mlir::Value boundThis(mlir::Value value)
return {};
}

// The places a call's result is read through, from the argument it borrows (OWN_RESULT_PLACES_ATTR_NAME); none when
// the call does not say.
inline std::optional<llvm::SmallVector<std::pair<int64_t, mlir::Type>>> resultPlaces(mlir::Operation *op)
{
auto attr = op ? op->getAttrOfType<mlir::ArrayAttr>(OWN_RESULT_PLACES_ATTR_NAME) : mlir::ArrayAttr();
if (!attr)
{
return std::nullopt;
}

llvm::SmallVector<std::pair<int64_t, mlir::Type>> places;
for (auto entry : attr.getAsRange<mlir::ArrayAttr>())
{
places.push_back({mlir::cast<mlir::IntegerAttr>(entry[0]).getInt(), mlir::cast<mlir::TypeAttr>(entry[1]).getValue()});
}

return places;
}

// The parameter whose argument `value` is a borrow of, or -1 (spec 2.4, borrowed-from-argument).
// Seen through views, casts out of `!ts.opaque`, `ts.Unbox` (the payload an `any` box holds), the
// object a method is called on,
Expand Down Expand Up @@ -2958,6 +2990,135 @@ inline int borrowedParam(mlir::Value value, int depth = 0)
return -1;
}

// The places `value`, a borrow of parameter `param` (borrowedParam), is read through from the parameter down: each
// field (its position), field through an interface (OWN_INTERFACE_FIELD_POSITION) and element (-1), with its slot
// type, added to `places`. Merged values add each one's; a value that holds no block adds none. False where the way
// is not one of those: a handle's payload, a call's result that does not say its own places
// (OWN_RESULT_PLACES_ATTR_NAME), a union member's tuple - the result may then be anywhere under the parameter.
inline bool borrowedPlaces(mlir::Value value, int param, llvm::SmallVectorImpl<std::pair<int64_t, mlir::Type>> &places,
int depth = 0)
{
if (depth > 32)
{
return false;
}

auto add = [&](std::pair<int64_t, mlir::Type> place) {
if (!llvm::is_contained(places, place))
{
places.push_back(place);
}
};

value = rootOf(value);
if (auto argument = mlir::dyn_cast<mlir::BlockArgument>(value))
{
if (argument.getOwner()->isEntryBlock())
{
return static_cast<int>(argument.getArgNumber()) == param;
}

llvm::SmallVector<mlir::Value> merged;
if (!mergedInto(argument, merged))
{
return false;
}

return llvm::all_of(merged, [&](mlir::Value passed) {
return holdsNoBlock(passed) || borrowedPlaces(passed, param, places, depth + 1);
});
}

if (auto object = boundThis(value))
{
return borrowedPlaces(object, param, places, depth + 1);
}

auto *def = value.getDefiningOp();
if (auto castOp = mlir::dyn_cast_or_null<mlir_ts::CastOp>(def);
castOp && (mlir::isa<mlir_ts::OpaqueType, mlir_ts::InterfaceType>(castOp.getIn().getType()) ||
(mlir::isa<mlir_ts::AnyType>(castOp.getIn().getType()) &&
mlir::isa<mlir_ts::InterfaceType>(castOp.getType()))))
{
return borrowedPlaces(castOp.getIn(), param, places, depth + 1);
}

if (auto extractOp = mlir::dyn_cast_or_null<mlir_ts::ExtractInterfaceThisOp>(def))
{
return borrowedPlaces(extractOp.getOperand(), param, places, depth + 1);
}

if (auto unboxOp = mlir::dyn_cast_or_null<mlir_ts::UnboxOp>(def))
{
return borrowedPlaces(unboxOp.getIn(), param, places, depth + 1);
}

if (auto unionValueOp = mlir::dyn_cast_or_null<mlir_ts::GetValueFromUnionOp>(def))
{
return borrowedPlaces(unionValueOp->getOperand(0), param, places, depth + 1);
}

if (auto place = placeBase(value))
{
switch (place.kind)
{
case PlaceKind::Field:
add({value.getDefiningOp<mlir_ts::PropertyRefOp>().getPosition(), value.getType()});
break;
case PlaceKind::Element:
add({-1, value.getType()});
break;
case PlaceKind::InterfaceField:
add({OWN_INTERFACE_FIELD_POSITION, value.getType()});
break;
default:
return false;
}

return borrowedPlaces(place.base, param, places, depth + 1);
}

if (auto loadOp = mlir::dyn_cast_or_null<mlir_ts::LoadOp>(def))
{
auto ref = loadOp.getReference();
if (isPlace(ref))
{
return borrowedPlaces(ref, param, places, depth + 1);
}

auto index = -1;
auto varOp = ref.getDefiningOp<mlir_ts::VariableOp>();
return varOp && isParameterSlot(varOp, index) && index == param;
}

if (auto varOp = mlir::dyn_cast_or_null<mlir_ts::VariableOp>(def))
{
auto index = -1;
return isParameterSlot(varOp, index) && index == param;
}

// a plain borrow of one argument that says its places: those, then the argument's
if (def && isCall(def) && resultBorrows(def) >= 0 && !isRecordResult(def))
{
auto indices = resultBorrowsAll(def);
auto calleePlaces = resultPlaces(def);
auto args = callArgs(def);
if (indices.size() != 1 || !calleePlaces || static_cast<size_t>(indices.front()) >= args.size())
{
return false;
}

for (auto &calleePlace : *calleePlaces)
{
add(calleePlace);
}

return borrowedPlaces(args[indices.front()], param, places, depth + 1);
}

return false;
}

// ---- Borrowing records (spec 32) ----
//
// An object literal boxed as an interface whose fields hold values it does not own - `this`, a
Expand Down
Loading
Loading