From 49fd50de4f827b000073bb73bf5c06e0c37908ff Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Thu, 29 Oct 2026 09:16:06 +0000 Subject: [PATCH] -mm=own: a field taken out of the object an owning `let` holds (spec 65) `let r = new Holder(); return r.value` (and `?:`, `??`) was still "'this value' borrows a field and cannot be stored, returned or captured". namedFieldTakeOf now takes a read of a field of a load of an owning, uncaptured local: the object is every read of the local, its ends the local's releases and the stores into it, and the declaration and those stores make it anew. Every value the local is given must be `new`'s or a call's own result: a global is immortal under own, so a local given one is owning all the same, and `let r = g; return r.value` emptied the global's field. The same rule now guards spec 64's `const`. The derived-use walk follows a value stored into a local. consumeConditionalResults retains a branch yielding a field read out of an owning local's load, as for a named object. Co-Authored-By: Claude Opus 5.5 --- .../2026-09-24-own-memory-model-design.md | 27 ++++ .../TypeScript/OwnedReturnConsumptionPass.cpp | 6 +- .../lib/TypeScript/OwnershipInferencePass.h | 5 +- .../OwnershipInferencePlaceReads.cpp | 137 +++++++++++++----- tslang/test/tester/CMakeLists.txt | 10 +- .../tester/own/own_err_let_field_captured.ts | 23 +++ .../tester/own/own_err_let_field_global.ts | 20 +++ .../test/tester/own/own_err_let_field_loop.ts | 25 ++++ .../own/own_err_let_field_read_again.ts | 22 +++ tslang/test/tester/own/own_let_field_take.ts | 114 +++++++++++++++ 10 files changed, 347 insertions(+), 42 deletions(-) create mode 100644 tslang/test/tester/own/own_err_let_field_captured.ts create mode 100644 tslang/test/tester/own/own_err_let_field_global.ts create mode 100644 tslang/test/tester/own/own_err_let_field_loop.ts create mode 100644 tslang/test/tester/own/own_err_let_field_read_again.ts create mode 100644 tslang/test/tester/own/own_let_field_take.ts 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 0714d930e..90cc65b7a 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 @@ -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. diff --git a/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp b/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp index a582cc3da..a7436e083 100644 --- a/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp +++ b/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp @@ -1597,8 +1597,12 @@ class OwnedReturnConsumptionPass auto loadOp = producerOf(resultOp->getOperand(0)).getDefiningOp(); auto propertyRefOp = loadOp ? loadOp.getReference().getDefiningOp() : mlir_ts::PropertyRefOp(); auto *definingOp = propertyRefOp ? propertyRefOp.getObjectRef().getDefiningOp() : nullptr; + // (or an owning `let` holds, spec 65) + auto slotRead = mlir::dyn_cast_or_null(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(slot) && slot->hasAttr(OWNED_LOCAL_ATTR_NAME))); }; if (llvm::none_of(yields, carriesOwned) && llvm::none_of(yields, borrowsBranchTemporary) && diff --git a/tslang/lib/TypeScript/OwnershipInferencePass.h b/tslang/lib/TypeScript/OwnershipInferencePass.h index 0fb26d473..cd8e2fa50 100644 --- a/tslang/lib/TypeScript/OwnershipInferencePass.h +++ b/tslang/lib/TypeScript/OwnershipInferencePass.h @@ -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); diff --git a/tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp b/tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp index 30bc66e5c..fae1da5a5 100644 --- a/tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp +++ b/tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp @@ -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(call); + auto slot = slotRead ? slotRead.getReference().getDefiningOp() : mlir_ts::VariableOp(); + if (call->hasAttr(OWNED_RESULT_NAMED_ATTR_NAME) || (slot && isOwningVariable(slot))) { for (auto *user : loadOp->getUsers()) { @@ -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(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(definingOp) && definingOp->hasAttr(OWNED_RESULT_ATTR_NAME)) || + callResultIsOwn(definingOp)); + }; + llvm::SmallVector objects; + llvm::SmallVector ends; + llvm::SmallVector renewals; + if (auto slotRead = mlir::dyn_cast(producer)) { - return false; - } + auto slot = slotRead.getReference().getDefiningOp(); + 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(user)) + { + objects.push_back(loadOp.getResult()); + } + else if (auto storeOp = mlir::dyn_cast(user); storeOp && storeOp.getReference() == slot.getResult()) + { + if (!madeHere(storeOp.getValue())) + { + return false; + } - llvm::SmallVector releases; - for (auto *user : object.getUsers()) + ends.push_back(user); + renewals.push_back(user); + } + else if (mlir::isa(user)) + { + ends.push_back(user); + } + else if (!mlir::isa(user)) + { + return false; + } + } + } + else { - if (mlir::isa(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(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(user) || user == field.getOperation()) + for (auto *user : value.getUsers()) { - continue; - } - - if (auto propertyRefOp = mlir::dyn_cast(user); - propertyRefOp && propertyRefOp.getPosition() != field.getPosition()) - { - continue; - } - - // the user, and what is made of its results in turn - llvm::SmallVector derived{user}; - llvm::SmallPtrSet visited; - while (!derived.empty()) - { - auto *op = derived.pop_back_val(); - if (!visited.insert(op).second) + if (mlir::isa(user) || user == field.getOperation()) { continue; } - if (seenAfter(op)) + if (auto propertyRefOp = mlir::dyn_cast(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 derived{user}; + llvm::SmallPtrSet 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(op)) + { + auto reference = storeOp.getReference(); + derived.append(reference.getUsers().begin(), reference.getUsers().end()); + } } } } diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index bafe08564..d8f25309f 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -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") @@ -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") @@ -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) diff --git a/tslang/test/tester/own/own_err_let_field_captured.ts b/tslang/test/tester/own/own_err_let_field_captured.ts new file mode 100644 index 000000000..a402c1af0 --- /dev/null +++ b/tslang/test/tester/own/own_err_let_field_captured.ts @@ -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); +} diff --git a/tslang/test/tester/own/own_err_let_field_global.ts b/tslang/test/tester/own/own_err_let_field_global.ts new file mode 100644 index 000000000..6698e3177 --- /dev/null +++ b/tslang/test/tester/own/own_err_let_field_global.ts @@ -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); +} diff --git a/tslang/test/tester/own/own_err_let_field_loop.ts b/tslang/test/tester/own/own_err_let_field_loop.ts new file mode 100644 index 000000000..b254f9377 --- /dev/null +++ b/tslang/test/tester/own/own_err_let_field_loop.ts @@ -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); +} diff --git a/tslang/test/tester/own/own_err_let_field_read_again.ts b/tslang/test/tester/own/own_err_let_field_read_again.ts new file mode 100644 index 000000000..5bb420a67 --- /dev/null +++ b/tslang/test/tester/own/own_err_let_field_read_again.ts @@ -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); +} diff --git a/tslang/test/tester/own/own_let_field_take.ts b/tslang/test/tester/own/own_let_field_take.ts new file mode 100644 index 000000000..a22de0149 --- /dev/null +++ b/tslang/test/tester/own/own_let_field_take.ts @@ -0,0 +1,114 @@ +// A field taken out of the object an owning `let` holds, given `new`'s or a call's own result, that +// nothing sees after the move but the local's end or its next value (spec 65): `return r.value`, `?:`, +// `??`, the read used before it is returned and another field read after, the local given a new object +// after the take, a `let` in a loop, and one given anew in the loop. The field is left empty at the read, +// so the local's release frees nothing of what was taken. Each step churns, so a value freed with its +// object reads garbage; every model runs it. +class Item { + constructor(public n: number) {} +} + +class Holder { + value = new Item(3); + other = new Item(9); +} + +function churn() { + const junk: Item[] = []; + for (let i = 0; i < 64; i++) junk.push(new Item(999)); +} + +function make() { + return new Holder(); +} + +function returned() { + let r = new Holder(); + return r.value; +} + +function chosen(c: boolean, initial: Item) { + let r = new Holder(); + return c ? initial : r.value; +} + +function orDefault(initial?: Item) { + let r = make(); + return initial ?? r.value; +} + +function usedFirst() { + let r = new Holder(); + const v = r.value; + churn(); + print(v.n + r.other.n); + return v; +} + +function givenAnew() { + let r = new Holder(); + const v = r.value; + r = new Holder(); + churn(); + print(r.value.n); + return v; +} + +function eachNew() { + const out: Item[] = []; + for (let i = 0; i < 3; i++) { + let r = new Holder(); + out.push(r.value); + } + + return out; +} + +function renewedInLoop() { + const out: Item[] = []; + let r = new Holder(); + for (let i = 0; i < 3; i++) { + out.push(r.value); + r = make(); + } + + return out; +} + +function test() { + let s: number = 0; + const a = returned(); + churn(); + s += a.n; + const b = chosen(false, new Item(4)); + const c = chosen(true, new Item(4)); + churn(); + s += b.n + c.n; + const d = orDefault(); + const e = orDefault(new Item(5)); + churn(); + s += d.n + e.n; + const f = usedFirst(); + churn(); + s += f.n; + const g = givenAnew(); + churn(); + s += g.n; + const items = eachNew(); + const more = renewedInLoop(); + churn(); + for (const item of items) s += item.n; + for (const item of more) s += item.n; + return s; +} + +function main() { + let ok = 0; + for (let i = 0; i < 300; i++) { + if (test() == 3 + 7 + 8 + 3 + 3 + 9 + 9) ok++; + churn(); + } + + assert(ok == 300, "ok"); + print("done."); +}