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 @@ -5325,3 +5325,30 @@ are rejected too.

Not here: a `let` of a class type (a slot, not a folded name), and a push of the field into a global
array (spec 25's immortal keepers ask for a value made here).

## 65. A field taken out of the object an owning `let` holds, 2026-10-29

Spec 64 for a `let`: `let r = new Holder(); return r.value` (and `?:`, `??`) was still `'this value'
borrows a field and cannot be stored, returned or captured`. A `let` is a slot - an owning
`ts.Variable`, every mention a `Load` of it, its end a `ts.ReleaseSlot` - not a folded name.

- **The take** (`namedFieldTakeOf`, reached from `temporaryFieldTakeOf` for a read of a field of a
`Load` of an owning, uncaptured `ts.Variable`). The object is every read of the local. Its ends are
the local's releases and the stores into it (the next value: reads after one see another object), and
what makes it anew is the declaration and those stores. The rules of spec 64 hold over every read of
the local: nothing but its ends sees the object after the move, nothing made of it before (followed
through a local it is stored into, too) is used after, and the read is not made again without the
local made anew. Any other use of the local itself (its address) refuses.
- **Made here.** Every value the local is given - its initializer and each store - is `new`'s own or a
call's own result. A global is immortal under own (spec 25), so a local given one counts as owning:
without this, `let r = g; return r.value` emptied the global's field (the program died reading it).
The same rule now guards spec 64's `const`.
- **rc** (`consumeConditionalResults`): a branch yielding a field read out of a `Load` of an owning
local (`__owned`) retains in the branch as for a named object, when a receiver keeps the merge.

`own_let_field_take` returns, picks with a `?:` and a `??` (from a call's result), uses the read before
returning it and reads another field after, gives the local a new object after the take, takes in a loop
from a `let` declared in it and from one given anew in it, churning between steps, under own, rc, gc and
none; the ownership verifier is silent on it. `own_err_let_field_{global,read_again,loop,captured}` stay
borrows; so do a parameter or a mix of a parameter and a fresh object given to the local, and a `let h =
r` read after.
6 changes: 5 additions & 1 deletion tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1597,8 +1597,12 @@ class OwnedReturnConsumptionPass
auto loadOp = producerOf(resultOp->getOperand(0)).getDefiningOp<mlir_ts::LoadOp>();
auto propertyRefOp = loadOp ? loadOp.getReference().getDefiningOp<mlir_ts::PropertyRefOp>() : mlir_ts::PropertyRefOp();
auto *definingOp = propertyRefOp ? propertyRefOp.getObjectRef().getDefiningOp() : nullptr;
// (or an owning `let` holds, spec 65)
auto slotRead = mlir::dyn_cast_or_null<mlir_ts::LoadOp>(definingOp);
auto *slot = slotRead ? slotRead.getReference().getDefiningOp() : nullptr;
return compileOptions.memoryModel == MemoryModelOwn && mergeKept && definingOp &&
definingOp->hasAttr(OWNED_RESULT_NAMED_ATTR_NAME);
(definingOp->hasAttr(OWNED_RESULT_NAMED_ATTR_NAME) ||
(mlir::isa_and_nonnull<mlir_ts::VariableOp>(slot) && slot->hasAttr(OWNED_LOCAL_ATTR_NAME)));
};

if (llvm::none_of(yields, carriesOwned) && llvm::none_of(yields, borrowsBranchTemporary) &&
Expand Down
5 changes: 3 additions & 2 deletions tslang/lib/TypeScript/OwnershipInferencePass.h
Original file line number Diff line number Diff line change
Expand Up @@ -833,8 +833,9 @@ class OwnershipInferencePass
// Is `kept` rc's retain of `read`, a read of a field of a call's own result released as a
// temporary right after, with nothing else reading that field (spec 63)?
bool temporaryFieldTakeOf(mlir::Operation *read, mlir::Operation *kept, FieldTake &take);
// ...or of an object a `const` names, `new`'s or a call's own, that nothing sees after the move
// but its releases, and that is not taken from twice (spec 64)?
// ...or of an object a `const` names, `new`'s or a call's own, or an owning `let` given only such
// objects holds, that nothing sees after the move but its ends, and that is not taken from twice
// (specs 64, 65)?
bool namedFieldTakeOf(mlir::Operation *read, mlir::Operation *retain, mlir_ts::PropertyRefOp field, FieldTake &take);
// Can `call` be given a copy of `value`, a string read out of a place, in its place (callCopies)?
static bool copiesInto(mlir::Operation *call, mlir::Value value);
Expand Down
137 changes: 101 additions & 36 deletions tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,10 @@ namespace own_inference
// ...or out of an object a `const` names, `new`'s or a call's own (spec 64): `return r.value`.
// The read may be used before rc's retain of it, later in its block (`const v = r.value;
// print(v.n); return v`): the field is empty from the read on, and the read holds what it held.
if (call->hasAttr(OWNED_RESULT_NAMED_ATTR_NAME))
// An owning `let` the same (spec 65): `let r = new Holder(); return r.value`.
auto slotRead = mlir::dyn_cast<mlir_ts::LoadOp>(call);
auto slot = slotRead ? slotRead.getReference().getDefiningOp<mlir_ts::VariableOp>() : mlir_ts::VariableOp();
if (call->hasAttr(OWNED_RESULT_NAMED_ATTR_NAME) || (slot && isOwningVariable(slot)))
{
for (auto *user : loadOp->getUsers())
{
Expand Down Expand Up @@ -1231,70 +1234,132 @@ namespace own_inference
// The object a `const` names is `new`'s own, or a call's own result: nothing else holds it,
// and its releases end it. The field is left empty right at the read, so each release frees
// nothing of what the taker has, on a path that took it or not (`c ? init : r.value`).
// ...or the object an owning `let` holds (spec 65): every read of the local is the object, until
// the local ends or is given another - its releases and its stores are the object's ends, the
// store or the declaration in a loop makes it anew. Nothing else may use the local itself.
auto object = field.getObjectRef();
auto *producer = object.getDefiningOp();
if (!(mlir::isa<mlir_ts::NewOp>(producer) && producer->hasAttr(OWNED_RESULT_ATTR_NAME)) && !callResultIsOwn(producer))
// made here: `new`'s own, or a call's own result - not a global (immortal under own, spec 25,
// so a local given one is "owning" all the same) nor a parameter
auto madeHere = [&](mlir::Value value) {
auto *definingOp = value ? value.getDefiningOp() : nullptr;
return definingOp && ((mlir::isa<mlir_ts::NewOp>(definingOp) && definingOp->hasAttr(OWNED_RESULT_ATTR_NAME)) ||
callResultIsOwn(definingOp));
};
llvm::SmallVector<mlir::Value> objects;
llvm::SmallVector<mlir::Operation *> ends;
llvm::SmallVector<mlir::Operation *> renewals;
if (auto slotRead = mlir::dyn_cast<mlir_ts::LoadOp>(producer))
{
return false;
}
auto slot = slotRead.getReference().getDefiningOp<mlir_ts::VariableOp>();
if (!slot || slot.getCaptured().value_or(false) || !isOwningVariable(slot) ||
(slot.getInitializer() && !madeHere(slot.getInitializer())))
{
return false;
}

renewals.push_back(slot);
for (auto *user : slot->getUsers())
{
if (auto loadOp = mlir::dyn_cast<mlir_ts::LoadOp>(user))
{
objects.push_back(loadOp.getResult());
}
else if (auto storeOp = mlir::dyn_cast<mlir_ts::StoreOp>(user); storeOp && storeOp.getReference() == slot.getResult())
{
if (!madeHere(storeOp.getValue()))
{
return false;
}

llvm::SmallVector<mlir::Operation *> releases;
for (auto *user : object.getUsers())
ends.push_back(user);
renewals.push_back(user);
}
else if (mlir::isa<mlir_ts::ReleaseSlotOp>(user))
{
ends.push_back(user);
}
else if (!mlir::isa<mlir_ts::RetainSlotOp>(user))
{
return false;
}
}
}
else
{
if (mlir::isa<mlir_ts::ReleaseOp>(user))
if (!madeHere(object))
{
return false;
}

objects.push_back(object);
renewals.push_back(producer);
for (auto *user : object.getUsers())
{
releases.push_back(user);
if (mlir::isa<mlir_ts::ReleaseOp>(user))
{
ends.push_back(user);
}
}
}

if (releases.empty())
if (ends.empty())
{
return false;
}

// taken once for each object: not again around a loop that does not make it anew
auto *next = read->getNextNode();
if (!next || reachableAfter(next, read, producer))
if (!next || reachableAfter(next, read, renewals))
{
return false;
}

// Nothing sees the object after the move but its releases: no method, no move of it, no read
// of that field, nor anything made of the object before it that is used after (a borrow of it).
// Its other fields are its own still.
auto seenAfter = [&](mlir::Operation *op) { return op != read && op != retain && reachableAfter(read, op, releases); };
for (auto *user : object.getUsers())
// Nothing sees the object after the move but its ends: no method, no move of it, no read of that
// field, nor anything made of the object before it that is used after (a borrow of it, a local
// given it). Its other fields are its own still.
auto seenAfter = [&](mlir::Operation *op) { return op != read && op != retain && reachableAfter(read, op, ends); };
for (auto value : objects)
{
if (mlir::isa<mlir_ts::ReleaseOp>(user) || user == field.getOperation())
for (auto *user : value.getUsers())
{
continue;
}

if (auto propertyRefOp = mlir::dyn_cast<mlir_ts::PropertyRefOp>(user);
propertyRefOp && propertyRefOp.getPosition() != field.getPosition())
{
continue;
}

// the user, and what is made of its results in turn
llvm::SmallVector<mlir::Operation *> derived{user};
llvm::SmallPtrSet<mlir::Operation *, 8> visited;
while (!derived.empty())
{
auto *op = derived.pop_back_val();
if (!visited.insert(op).second)
if (mlir::isa<mlir_ts::ReleaseOp>(user) || user == field.getOperation())
{
continue;
}

if (seenAfter(op))
if (auto propertyRefOp = mlir::dyn_cast<mlir_ts::PropertyRefOp>(user);
propertyRefOp && propertyRefOp.getPosition() != field.getPosition())
{
return false;
continue;
}

for (auto result : op->getResults())
// the user, and what is made of its results in turn, through a local it is stored into
llvm::SmallVector<mlir::Operation *> derived{user};
llvm::SmallPtrSet<mlir::Operation *, 8> visited;
while (!derived.empty())
{
derived.append(result.getUsers().begin(), result.getUsers().end());
auto *op = derived.pop_back_val();
if (!visited.insert(op).second)
{
continue;
}

if (seenAfter(op))
{
return false;
}

for (auto result : op->getResults())
{
derived.append(result.getUsers().begin(), result.getUsers().end());
}

if (auto storeOp = mlir::dyn_cast<mlir_ts::StoreOp>(op))
{
auto reference = storeOp.getReference();
derived.append(reference.getUsers().begin(), reference.getUsers().end());
}
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3992,7 +3992,7 @@ foreach(own_test own_fresh_string own_fresh_array own_return_new own_try_local o
own_generator_this_param own_copy_within_returned_param own_swap_elements
own_callback_forwarded own_optional_record_read own_param_default_kept own_literal_into_array
own_generator_take own_rest_array_borrows own_string_copies
own_captured_string_callback own_callback_and_call own_callback_captured own_literal_closures own_literal_variable_closures own_temporary_field_take own_union_narrowed_calls own_named_field_take
own_captured_string_callback own_callback_and_call own_callback_captured own_literal_closures own_literal_variable_closures own_temporary_field_take own_union_narrowed_calls own_named_field_take own_let_field_take
own_callback_shapes own_callback_extended own_callback_interface
own_generic_specializations own_callback_cast own_callback_interface_slot)
tslang_add_test(NAME test-jit-own-${own_test} COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/own/${own_test}.ts")
Expand Down Expand Up @@ -4030,7 +4030,7 @@ foreach(keeps_mm rc gc none)
own_copy_within_returned_param own_swap_elements own_callback_forwarded
own_optional_record_read own_param_default_kept own_literal_into_array
own_generator_take own_rest_array_borrows own_string_copies
own_captured_string_callback own_callback_and_call own_callback_captured own_literal_closures own_literal_variable_closures own_temporary_field_take own_union_narrowed_calls own_named_field_take
own_captured_string_callback own_callback_and_call own_callback_captured own_literal_closures own_literal_variable_closures own_temporary_field_take own_union_narrowed_calls own_named_field_take own_let_field_take
own_callback_shapes own_callback_extended own_callback_interface
own_generic_specializations own_callback_cast own_callback_interface_slot)
tslang_add_test(NAME test-jit-${keeps_mm}-${keeps_test} COMMAND test-runner -jit -mm=${keeps_mm} "${PROJECT_SOURCE_DIR}/test/tester/own/${keeps_test}.ts")
Expand Down Expand Up @@ -4743,7 +4743,11 @@ set(own_error_cases
"own_err_named_field_read_again|:28:5: error: '[^']*' borrows a field and cannot be stored, returned or captured"
"own_err_named_field_method_after|:28:5: error: '[^']*' borrows a field and cannot be stored, returned or captured"
"own_err_named_field_object_moved|:28:5: error: '[^']*' borrows a field and cannot be stored, returned or captured"
"own_err_named_field_borrow_after|:29:5: error: '[^']*' borrows a field and cannot be stored, returned or captured")
"own_err_named_field_borrow_after|:29:5: error: '[^']*' borrows a field and cannot be stored, returned or captured"
"own_err_let_field_global|:15:5: error: '[^']*' borrows a field and cannot be stored, returned or captured"
"own_err_let_field_read_again|:17:5: error: '[^']*' borrows a field and cannot be stored, returned or captured"
"own_err_let_field_loop|:17:9: error: '[^']*' borrows a field and cannot be stored, returned or captured"
"own_err_let_field_captured|:18:5: error: '[^']*' borrows a field and cannot be stored, returned or captured")
foreach(own_error_case ${own_error_cases})
string(FIND "${own_error_case}" "|" own_error_sep)
string(SUBSTRING "${own_error_case}" 0 ${own_error_sep} own_error_name)
Expand Down
23 changes: 23 additions & 0 deletions tslang/test/tester/own/own_err_let_field_captured.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// -mm=own: a field moves out of the object an owning `let` holds only when the local was given `new`'s
// or a call's own result and nothing sees the object after the move (spec 65); a closure that reads the local, called after the take.
class Item {
constructor(public n: number) {}
}

class Holder {
value = new Item(3);
}

const shared = new Holder();

function take() {
let r = new Holder();
const read = () => r.value.n;
const v = r.value;
print(read());
return v;
}

function main() {
print(take().n);
}
20 changes: 20 additions & 0 deletions tslang/test/tester/own/own_err_let_field_global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// -mm=own: a field moves out of the object an owning `let` holds only when the local was given `new`'s
// or a call's own result and nothing sees the object after the move (spec 65); given a global, which it does not own: the global would be left with an empty field.
class Item {
constructor(public n: number) {}
}

class Holder {
value = new Item(3);
}

const shared = new Holder();

function take() {
let r = shared;
return r.value;
}

function main() {
print(take().n, shared.value.n);
}
25 changes: 25 additions & 0 deletions tslang/test/tester/own/own_err_let_field_loop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// -mm=own: a field moves out of the object an owning `let` holds only when the local was given `new`'s
// or a call's own result and nothing sees the object after the move (spec 65); taken in a loop that does not give the local anew: the second turn finds the field empty.
class Item {
constructor(public n: number) {}
}

class Holder {
value = new Item(3);
}

const shared = new Holder();

function take() {
const out: Item[] = [];
let r = new Holder();
for (let i = 0; i < 3; i++) {
out.push(r.value);
}

return out;
}

function main() {
print(take().length);
}
22 changes: 22 additions & 0 deletions tslang/test/tester/own/own_err_let_field_read_again.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// -mm=own: a field moves out of the object an owning `let` holds only when the local was given `new`'s
// or a call's own result and nothing sees the object after the move (spec 65); the field read again after it is taken.
class Item {
constructor(public n: number) {}
}

class Holder {
value = new Item(3);
}

const shared = new Holder();

function take() {
let r = new Holder();
const v = r.value;
print(r.value.n);
return v;
}

function main() {
print(take().n);
}
Loading
Loading