From e0b7e58da98d0e970d5b28ef9643a0aec6ba305e Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sat, 5 Dec 2026 14:27:07 +0000 Subject: [PATCH 1/2] -mm=own: a list walk may start from a const head and step through a call that lends its argument back; a closure may call another closure of the same frame (TSLANG 235, cabLimb) - A `let` that only borrows may be given a named const's value (`const a = new L(0); for (let q: L | null = a; q; q = q.next)`): each assignment borrows the const, bounded by its release and by anything that takes it (borrowedByLocalLets). A const moved, cut or cleared while the walk reads it stays refused. - A walk stepping through `nn(q).next` no longer counts rc's release of the call's temporary as a keep of what the `let` holds. - A closure reading a closure it captured out of its own box calls it; a captured closure that does not escape counts as a fresh fill, so `cap` running `frame` then `ring` (golden_line_cab) compiles. A closure that escapes still takes its captures. Co-Authored-By: Claude Opus 5.5 --- docs/own-memory-model.md | 7 +- .../OwnershipInferenceBorrowers.cpp | 100 +++++++++++++++++- .../TypeScript/OwnershipInferenceClosures.cpp | 35 +++++- .../TypeScript/OwnershipInferenceMoves.cpp | 59 ++++++++++- .../lib/TypeScript/OwnershipInferencePass.h | 18 ++++ .../OwnershipInferencePlaceReads.cpp | 28 ++++- tslang/test/tester/CMakeLists.txt | 12 ++- .../tester/own/own_closure_calls_closure.ts | 49 +++++++++ .../own_err_closure_calls_closure_returned.ts | 19 ++++ .../own_err_closure_calls_closure_stored.ts | 18 ++++ .../own/own_err_walk_const_head_cleared.ts | 13 +++ .../tester/own/own_err_walk_const_head_cut.ts | 11 ++ .../own/own_err_walk_const_head_moved.ts | 11 ++ .../tester/tests/00tslang_issue_235_own.ts | 57 ++++++++++ 14 files changed, 424 insertions(+), 13 deletions(-) create mode 100644 tslang/test/tester/own/own_closure_calls_closure.ts create mode 100644 tslang/test/tester/own/own_err_closure_calls_closure_returned.ts create mode 100644 tslang/test/tester/own/own_err_closure_calls_closure_stored.ts create mode 100644 tslang/test/tester/own/own_err_walk_const_head_cleared.ts create mode 100644 tslang/test/tester/own/own_err_walk_const_head_cut.ts create mode 100644 tslang/test/tester/own/own_err_walk_const_head_moved.ts create mode 100644 tslang/test/tester/tests/00tslang_issue_235_own.ts diff --git a/docs/own-memory-model.md b/docs/own-memory-model.md index b5563ee3b..8aa68b675 100644 --- a/docs/own-memory-model.md +++ b/docs/own-memory-model.md @@ -88,7 +88,9 @@ function main() { A `let` that is only given other locals' values, or fields', and is assigned again (`let cur = a; ... cur = b;`) borrows each value until its next assignment: none of them moves into it. Walking a -list from a local head is that case (`let node = head; while (node) { ...; node = node.next; }`). +list from a local head is that case (`let node = head; while (node) { ...; node = node.next; }`), +from a `const` head too (`const a = new L(0); for (let q: L | null = a; q; q = q.next)`), and so is a +step through a call that gives back the node it is given (`q = nn(q).next`). Giving the head up, or cutting the list, while the walk still reads it is an error. A local that borrows out of a container and is still used after a call that may free what it borrows @@ -351,7 +353,8 @@ fact: `new Person(p.name)` is an error. Pass a string you give up, or a fresh on A closure that does not escape borrows what it captures. A closure that escapes - returned, stored, or given to a function that keeps it - takes its captured variables with it, and the -frame that made it may not use them afterwards. +frame that made it may not use them afterwards. A closure that calls another closure of the same frame (`const step = () => +part(1)`) captures it as it captures anything else: borrowed while neither escapes. ```typescript function counter() { diff --git a/tslang/lib/TypeScript/OwnershipInferenceBorrowers.cpp b/tslang/lib/TypeScript/OwnershipInferenceBorrowers.cpp index 44db6d652..82a5532cb 100644 --- a/tslang/lib/TypeScript/OwnershipInferenceBorrowers.cpp +++ b/tslang/lib/TypeScript/OwnershipInferenceBorrowers.cpp @@ -421,6 +421,68 @@ namespace own_inference return slot; } + mlir::Value OwnershipInferencePass::namedConstValueOf(mlir::Value value) + { + // the mark is on the value, or on the view the declaration made (`const a: L | null = new L(0)`: the cast) + auto named = false; + auto root = value; + while (auto *def = root.getDefiningOp()) + { + named = named || def->hasAttr(OWNED_RESULT_NAMED_ATTR_NAME) || def->hasAttr(OWNED_RESULT_NAMED_CALL_ATTR_NAME); + if (!isView(def)) + { + break; + } + + root = def->getOperand(0); + } + + // (a lending call's result, a narrowed read - `nn(q)`, SafeCast - carry the mark too: not fresh) + auto *def = root.getDefiningOp(); + if (!named || !def || !isFresh(root) || isRecordResult(def) || isHandle(root) || + countedValues.contains(countedUnitOf(root))) + { + return {}; + } + + // ...and never moved away (`h.t = q`: a store into a place, a push, a call keeping it): a `let` borrowing a value + // that moves is decided as before, a borrow of the moved local (own_err_let_borrows_local_moved) + auto released = false; + auto moved = false; + forEachUse(root, [&](mlir::Operation *user, mlir::Value used) { + released = released || mlir::isa(user); + auto storeOp = mlir::dyn_cast(user); + moved = moved || + (storeOp && storeOp.getValue() == used && !storeOp.getReference().getDefiningOp()) || + mlir::isa(user) || + (isCall(user) && isKeptArgument(user, used)); + }); + return released && !moved ? root : mlir::Value(); + } + + mlir_ts::VariableOp OwnershipInferencePass::localBorrowerGiven(mlir::Operation *user, mlir::Value used, mlir::Operation *&from) + { + mlir_ts::VariableOp varOp; + if (auto declared = mlir::dyn_cast(user); declared && declared.getInitializer() == used) + { + varOp = declared; + } + else if (auto storeOp = mlir::dyn_cast(user); storeOp && storeOp.getValue() == used) + { + varOp = storeOp.getReference().getDefiningOp(); + } + + // (only a named `const`'s value: a read of a local's slot given to such a `let` is the slot's borrow, + // decided by its receivers - decideSlotReceivers, own_err_let_borrows_local_moved) + if (!varOp || !localBorrowingLets.contains(varOp) || !namedConstValueOf(used)) + { + return {}; + } + + from = user; + return varOp; + } + bool OwnershipInferencePass::readsOwningLocals(mlir::Value value, mlir_ts::VariableOp self) { if (auto owner = owningLocalReadOf(value)) @@ -428,6 +490,14 @@ namespace own_inference return owner != self; } + // ...or the value a named `const` owns (`const a = new L(0); for (let q: L | null = a; q; q = q.next)`, + // TSLANG 235): folded, the name is the value and there is no slot; the const keeps it and releases it where + // its scope ends (checkValueMoves bounds the borrow: borrowedByLocalLets) + if (namedConstValueOf(value)) + { + return true; + } + // `qq = r && q`: a merge of reads of owning locals (and of null) auto argument = mlir::dyn_cast(rootOf(value)); llvm::SmallVector loads; @@ -911,7 +981,14 @@ namespace own_inference auto firstOfRead = uses.size(); uses.push_back({user, {kills.begin(), kills.end()}}); - if (auto *kept = walkBorrowed(readOp.getResult(), kills, uses)) + // rc's release of the temporary a call returns that lends the read back (`const e = nn(c)`, `nn(c).next`) + // keeps nothing of it: the call's own check walks it and erases it with the call's bookkeeping + // (checkPlaceRead) - here it is no keep of what the `let` holds (TSLANG 235). Only for a `let` given only + // borrows and assigned (a walk: borrowsOnly): a local that borrows a place once keeps the call's release as a + // keep, and is refused as before (own_err_result_borrows_argument) + llvm::SmallVector lentBack; + auto keeping = onlyBorrows ? Keeping{&lentBack, false} : Keeping{nullptr, false}; + if (auto *kept = walkBorrowed(readOp.getResult(), kills, uses, nullptr, keeping)) { return kept; } @@ -1436,9 +1513,24 @@ namespace own_inference auto getThisOp = use.get().getDefiningOp(); auto getMethodOp = callee.getDefiningOp(); auto methodField = getThisOp ? getThisOp.getOperand().getDefiningOp() : mlir_ts::LoadOp(); - return use.getOperandNumber() == 1 && getMethodOp && methodField && - getThisOp.getOperand() == getMethodOp.getBoundFunc() && - mlir::isa(methodField.getReference().getType()); + if (use.getOperandNumber() != 1 || !getMethodOp || !methodField || getThisOp.getOperand() != getMethodOp.getBoundFunc()) + { + return false; + } + + if (mlir::isa(methodField.getReference().getType())) + { + return true; + } + + // ...or a closure this closure captured, read out of its own box (golden_line_cab's cabLimb: `cap` running + // `ring`): a box's fields are filled once, where the closure is made, and never written again + auto fieldOp = methodField.getReference().getDefiningOp(); + auto box = fieldOp ? mlir::dyn_cast(fieldOp.getObjectRef()) : mlir::BlockArgument(); + auto refType = box ? mlir::dyn_cast(box.getType()) : mlir_ts::RefType(); + return refType && box.getArgNumber() == 0 && box.getOwner()->isEntryBlock() && + mlir::isa(refType.getElementType()) && + mlir::isa(methodField.getType()); } } // namespace own_inference diff --git a/tslang/lib/TypeScript/OwnershipInferenceClosures.cpp b/tslang/lib/TypeScript/OwnershipInferenceClosures.cpp index cfe028539..f8542a967 100644 --- a/tslang/lib/TypeScript/OwnershipInferenceClosures.cpp +++ b/tslang/lib/TypeScript/OwnershipInferenceClosures.cpp @@ -116,12 +116,15 @@ namespace own_inference return def && isFresh(root) && (def->hasAttr(OWNED_RESULT_NAMED_ATTR_NAME) || def->hasAttr(OWNED_RESULT_NAMED_CALL_ATTR_NAME)); }; + // ...and another closure that does not escape (cabLimb's `cap` calling its `ring`): its own frees it auto noneFresh = llvm::none_of(closure->fills, [&](auto &fill) { auto value = fill.second.getValue(); - return !holdsNoBlock(value) && isFresh(rootOf(value)) && !ownedByNamedConst(value); + return !holdsNoBlock(value) && isFresh(rootOf(value)) && !ownedByNamedConst(value) && + !isNonEscapingClosureValue(value); }); auto holdsParam = llvm::any_of(closure->fills, [&](auto &fill) { - return isBorrowableCapture(fill.second.getValue()) || ownedByNamedConst(fill.second.getValue()); + return isBorrowableCapture(fill.second.getValue()) || ownedByNamedConst(fill.second.getValue()) || + isNonEscapingClosureValue(fill.second.getValue()); }); closure->borrowsCopies = !closure->escape && noneFresh && @@ -1983,6 +1986,27 @@ namespace own_inference } } + // captured by another closure that does not escape (`const cap = (t) => { ring(t); }` of a local + // closure `ring`, golden_line_cab's cabLimb): what may run it is what may run that closure + if (auto fieldOp = slot ? slot.getDefiningOp() : mlir_ts::PropertyRefOp(); + fieldOp && followClosureDepth < 8) + { + if (auto outer = closureOfBox(fieldOp.getObjectRef())) + { + llvm::SmallVector outerUses; + llvm::SmallVector outerEnds; + auto outerAliased = false; + ++followClosureDepth; + auto *outerEscape = followClosure(outer.getResult(), outerUses, outerEnds, outerAliased); + --followClosureDepth; + if (!outerEscape && !outerAliased) + { + uses.append(outerUses.begin(), outerUses.end()); + return; + } + } + } + // ...and a record read out of one: the closure's field read, not the others if (auto extractOp = mlir::dyn_cast(user)) { @@ -2006,6 +2030,13 @@ namespace own_inference return escape; } + bool OwnershipInferencePass::isNonEscapingClosureValue(mlir::Value value) + { + auto *def = rootOf(value).getDefiningOp(); + auto found = def && mlir::isa(def) ? closureOf.find(def) : closureOf.end(); + return found != closureOf.end() && found->second && !found->second->escape; + } + bool OwnershipInferencePass::holdsCaptures(mlir::Value value) { if (mlir::isa(value.getType())) diff --git a/tslang/lib/TypeScript/OwnershipInferenceMoves.cpp b/tslang/lib/TypeScript/OwnershipInferenceMoves.cpp index 80c8612fe..bf94609e8 100644 --- a/tslang/lib/TypeScript/OwnershipInferenceMoves.cpp +++ b/tslang/lib/TypeScript/OwnershipInferenceMoves.cpp @@ -1113,6 +1113,48 @@ namespace own_inference }); } + bool OwnershipInferencePass::borrowedByLocalLets(mlir::Value value, llvm::SetVector &toErase) + { + if (localBorrowingLets.empty()) + { + return true; + } + + llvm::SmallVector> lent; + llvm::SmallVector ends; + forEachUse(value, [&](mlir::Operation *user, mlir::Value used) { + mlir::Operation *from = nullptr; + if (auto borrower = localBorrowerGiven(user, used, from)) + { + lent.push_back({borrower, from}); + return; + } + + if (mlir::isa(user) || onlyDebugged(user)) + { + return; + } + + // the const's own release, and whatever takes the value away (a store, a push, a call that keeps it, a + // merge): the borrow ends there (one that takes rc's retain of it moves it: namedConstValueOf) + if (mlir::isa(user) || !isBorrow(user, used)) + { + ends.push_back(user); + } + }); + + auto owner = ownerName(value); + for (auto &[borrower, from] : lent) + { + if (!tryBorrow(borrower, ends, owner, toErase, from)) + { + return false; + } + } + + return true; + } + bool OwnershipInferencePass::checkValueMoves(mlir::Value value, llvm::SetVector &toErase) { // a counted value (--own-fallback=count) keeps rc's ops: no move of it is checked or erased @@ -1139,6 +1181,14 @@ namespace own_inference return false; } + // A `let` that only borrows (classifyLocalBorrowingLets) given this value, a named `const`'s, by its + // declaration or an assignment: a borrow of the const each time, never a move. The const's release ends it, + // and so does whatever takes the value (a const moved while the `let` still reads it is refused) (TSLANG 235) + if (!borrowedByLocalLets(value, toErase)) + { + return false; + } + if (quietly([&] { return movesOf(value, toErase); })) { return true; @@ -1149,7 +1199,8 @@ namespace own_inference llvm::SmallVector> notReads; llvm::SmallVector releases; forEachUse(value, [&](mlir::Operation *user, mlir::Value used) { - if (mlir::isa(user) || onlyDebugged(user)) + mlir::Operation *from = nullptr; + if (mlir::isa(user) || onlyDebugged(user) || localBorrowerGiven(user, used, from)) { return; } @@ -1452,10 +1503,16 @@ namespace own_inference } llvm::SmallVector fieldReads; + mlir::Operation *from = nullptr; if (mlir::isa(user)) { releases.push_back(user); } + // given to a `let` that only borrows it (borrowedByLocalLets bounded its uses): a read, not a taker + else if (localBorrowerGiven(user, used, from)) + { + reads.push_back(user); + } else if (isBorrow(user, used)) { reads.push_back(user); diff --git a/tslang/lib/TypeScript/OwnershipInferencePass.h b/tslang/lib/TypeScript/OwnershipInferencePass.h index 478f168d3..d26d88784 100644 --- a/tslang/lib/TypeScript/OwnershipInferencePass.h +++ b/tslang/lib/TypeScript/OwnershipInferencePass.h @@ -547,6 +547,18 @@ class OwnershipInferencePass void countUninitializedLets(); // The owning, uncaptured local whose slot `value` reads, other than a parameter's, or null. mlir_ts::VariableOp owningLocalReadOf(mlir::Value value); + // The value a named `const` owns that `value` is, seen through its views (`const a = new L(0)`, folded: the name + // is the value - OWNED_RESULT_NAMED_ATTR_NAME on it or on the view its declaration made, + // OWNED_RESULT_NAMED_CALL_ATTR_NAME of a call made direct) - fresh, and released where the const's scope ends; + // else null. + mlir::Value namedConstValueOf(mlir::Value value); + // The `let` of localBorrowingLets that `user` gives `used` to - its declaration, or the store of an assignment - + // and in `from`, that op; else null. + mlir_ts::VariableOp localBorrowerGiven(mlir::Operation *user, mlir::Value used, mlir::Operation *&from); + // The `let`s that only borrow (localBorrowingLets) given `value`, a named `const`'s: each borrows it up to its + // next assignment, bounded by the const's release and by whatever takes the value. False (reported) when one is + // used after that. + bool borrowedByLocalLets(mlir::Value value, llvm::SetVector &toErase); // Does `value` read owning locals other than `self` - one, or a merge of several (and of null)? bool readsOwningLocals(mlir::Value value, mlir_ts::VariableOp self); // The `let` of localBorrowingLets that an acquisition gives `value` to - its declaration's @@ -628,6 +640,12 @@ class OwnershipInferencePass // not know is such a use. mlir::Operation *followClosure(mlir::Value closure, llvm::SmallVectorImpl &uses, llvm::SmallVectorImpl &ends, bool &aliased); + // how many closures followClosure is following into, each captured by the next (bounded) + int followClosureDepth = 0; + + // A closure that captures this value borrows it rather than owning it: the value is another closure, one + // that does not escape (classifyClosures has decided it: closureOf), whatever captures it. + bool isNonEscapingClosureValue(mlir::Value value); // Does a call given this value give it a closure, which may run and touch what its box holds? static bool holdsCaptures(mlir::Value value); diff --git a/tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp b/tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp index 68f605d27..6f3ec2c94 100644 --- a/tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp +++ b/tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp @@ -875,6 +875,15 @@ namespace own_inference } forEachUse(owner, [&](mlir::Operation *use, mlir::Value used) { + // (a `let` that only borrows a named `const`'s value takes nothing, as one that borrows a + // local's below: its declaration, an assignment's store, rc's retain before the store) + mlir::Operation *from = nullptr; + if (localBorrowerGiven(use, used, from) || + (mlir::isa(use) && localBorrowerOf(use, from))) + { + return; + } + if (!mlir::isa(use) && !isBorrow(use, used)) { auto merged = mergedValuesOf(use, used); @@ -2109,8 +2118,25 @@ namespace own_inference return false; }; + // A closure this closure captured, read out of its own box (golden_line_cab's cabLimb: `cap` running `frame`, + // then `ring`): the box is filled once, where the closure is made, and lives while the closure runs - nothing + // the closure calls frees it. What keeps the read is still decided above. + auto ownBoxClosure = [&]() { + auto field = loadOp ? loadOp.getReference().getDefiningOp() : mlir_ts::PropertyRefOp(); + auto box = field ? mlir::dyn_cast(field.getObjectRef()) : mlir::BlockArgument(); + auto boxType = box ? mlir::dyn_cast(box.getType()) : mlir_ts::RefType(); + return boxType && box.getArgNumber() == 0 && box.getOwner()->isEntryBlock() && + mlir::isa(boxType.getElementType()) && mlir::isa(result.getType()); + }(); + if (ownBoxClosure) + { + toErase.insert(bookkeeping.begin(), bookkeeping.end()); + return true; + } + for (auto *end : rootEnds(chain)) - { if (outlives(end)) + { + if (outlives(end)) { return false; } diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index c55b8e0dc..99a5855a5 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -2817,8 +2817,9 @@ foreach(issue236_n 236 237 238 239 240 240_temps 240_fuzz) endforeach() endforeach() # TSLANG 245: a ternary of two borrowing calls' results borrows each; TSLANG 246: a closure in a loop borrows a const -# holding a call's result (238's loop form); every model runs them -foreach(issue245_n 245 246_named_call) +# holding a call's result (238's loop form); TSLANG 235 under own: list walks from a const head and through a +# call lending its argument back; every model runs them +foreach(issue245_n 245 246_named_call 235_own) foreach(issue245_mm gc rc none own) tslang_add_test(NAME test-jit-00tslang_issue_${issue245_n}-${issue245_mm} COMMAND test-runner -jit -mm=${issue245_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/00tslang_issue_${issue245_n}.ts") tslang_add_test(NAME test-compile-00tslang_issue_${issue245_n}-${issue245_mm} COMMAND test-runner -mm=${issue245_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/00tslang_issue_${issue245_n}.ts") @@ -6769,7 +6770,7 @@ foreach(own_test own_fresh_string own_fresh_array own_return_new own_try_local o own_field_let_borrow own_element_let_borrow own_borrow_merge_after_call own_call_no_drops own_getter_borrow own_param_kept own_any_box own_string_length_borrow - own_closure_borrow own_closure_escape own_generator own_generator_loop_local own_interface_view + own_closure_borrow own_closure_escape own_closure_calls_closure own_generator own_generator_loop_local own_interface_view own_async own_record_return own_generator_borrow own_global_script own_global_init own_constant_data own_record_local own_nullable_view own_declared_constant own_spread_build own_interface_field_call @@ -7938,6 +7939,11 @@ set(own_error_cases "own_err_const_alias_move|:12:5: error: '[^']*' borrows '[^']*' and cannot be assigned" "own_err_const_alias_field|:15:5: error: '[^']*' is moved here while '[^']*' borrows it" "own_err_field_alias|:9:11: error: a value that borrows 'n[.]v' is used here after it may be released or overwritten" + "own_err_walk_const_head_cut|:9:60: error: 'q' borrows 'q.next' but is used here after it may be released or overwritten" + "own_err_walk_const_head_cleared|:11:17: error: 'q' borrows 'q.next' but is used here after it may be released or overwritten" + "own_err_walk_const_head_moved|:9:5: error: 'q' is used here after its value was moved" + "own_err_closure_calls_closure_returned|:12:5: error: 'part' is captured by a closure that escapes, and is still used here" + "own_err_closure_calls_closure_stored|:17:5: error: 'part' is captured by a closure that escapes, and is still used here" "own_err_closure_escape_used|:9:11: error: '[^']*' is captured by a closure that escapes, and is still used here" "own_err_closure_escape_twice|:12:25: error: 'n' is captured by a closure that escapes, and is still used here" "own_err_closure_escape_param|:7:12: error: a closure that escapes owns what it captures, but -mm=own cannot move 'c' into it: its value is the caller's" diff --git a/tslang/test/tester/own/own_closure_calls_closure.ts b/tslang/test/tester/own/own_closure_calls_closure.ts new file mode 100644 index 000000000..308f7574c --- /dev/null +++ b/tslang/test/tester/own/own_closure_calls_closure.ts @@ -0,0 +1,49 @@ +// -mm=own: a closure that calls another local closure (golden_line_cab's cabLimb, whose `cap` runs its `frame`, +// `ring` and `radiusAt`). Neither escapes, so each borrows what it captures - the outer one the inner closure too - +// and the frame keeps them all: `part` is used after both, and freed once. +class Part { + p: number[] = []; + vert(x: number): void { this.p.push(x); } + count(): int { return this.p.length; } +} + +function churn() { + const junk: Part[] = []; + for (let i = 0; i < 64; i++) { + const c = new Part(); + c.vert(999.0); + junk.push(c); + } +} + +function limb(radii: number[], seg: int): Part { + const part = new Part(); + const rings: int[] = []; + const u: number[] = [0.0, 0.0]; + const radiusAt = (t: number): number => radii[0] + t; + const frame = (k: number): void => { u[0] = k; u[1] = -k; }; + const ring = (r: number): void => { + const start = part.count(); + for (let j = 0; j <= seg; j++) part.vert(u[0] * r); + rings.push(start); + }; + const cap = (t: number): void => { frame(t); ring(radiusAt(t)); }; + cap(0.0); + for (let i = 0; i <= 3; i++) { + frame(i); + ring(radiusAt(i)); + } + cap(1.0); + churn(); + assert(rings.length == 6); + assert(u[0] == 1.0); + return part; +} + +function main() { + const p = limb([1.0, 2.0], 4); + churn(); + assert(p.count() == 30); + assert(p.p[29] == 2.0); + print("done."); +} diff --git a/tslang/test/tester/own/own_err_closure_calls_closure_returned.ts b/tslang/test/tester/own/own_err_closure_calls_closure_returned.ts new file mode 100644 index 000000000..9cda93a8c --- /dev/null +++ b/tslang/test/tester/own/own_err_closure_calls_closure_returned.ts @@ -0,0 +1,19 @@ +// -mm=own: a closure captured by another closure that escapes escapes with it - the inner one owns what it +// captures, and the frame may not use that after it (the outer closure is returned). +class Part { + p: number[] = []; + vert(x: number): void { this.p.push(x); } +} + +function mk(seg: int): (t: number) => void { + const part = new Part(); + const ring = (r: number): void => { for (let j = 0; j <= seg; j++) part.vert(r); }; + const cap = (t: number): void => { ring(t); }; + part.vert(0.0); + return cap; +} + +function main() { + const f = mk(2); + f(1.0); +} diff --git a/tslang/test/tester/own/own_err_closure_calls_closure_stored.ts b/tslang/test/tester/own/own_err_closure_calls_closure_stored.ts new file mode 100644 index 000000000..eac38481e --- /dev/null +++ b/tslang/test/tester/own/own_err_closure_calls_closure_stored.ts @@ -0,0 +1,18 @@ +// -mm=own: a closure captured by another closure that is stored in a field escapes - what it captured is the +// closure's, and the frame may not use it after. +class Part { + p: number[] = []; + vert(x: number): void { this.p.push(x); } +} + +class Holder { + run: ((t: number) => void) | null = null; +} + +function main() { + const h = new Holder(); + const part = new Part(); + const ring = (r: number): void => { part.vert(r); }; + h.run = (t: number): void => { ring(t); }; + part.vert(0.0); +} diff --git a/tslang/test/tester/own/own_err_walk_const_head_cleared.ts b/tslang/test/tester/own/own_err_walk_const_head_cleared.ts new file mode 100644 index 000000000..4390d7af0 --- /dev/null +++ b/tslang/test/tester/own/own_err_walk_const_head_cleared.ts @@ -0,0 +1,13 @@ +// -mm=own (TSLANG 235): a cursor stepped from a `const` head, used after a call empties the head's link. +class L { i: int; next: L | null = null; constructor(i: int) { this.i = i; } } +function clear(n: L) { n.next = null; } + +function main() { + const a = new L(0); a.next = new L(1); a.next.next = new L(2); + let s: int = 0; + let q: L | null = a; + q = q.next; + clear(a); + if (q) s += q.i; + print(s); +} diff --git a/tslang/test/tester/own/own_err_walk_const_head_cut.ts b/tslang/test/tester/own/own_err_walk_const_head_cut.ts new file mode 100644 index 000000000..66878b0af --- /dev/null +++ b/tslang/test/tester/own/own_err_walk_const_head_cut.ts @@ -0,0 +1,11 @@ +// -mm=own (TSLANG 235): a cursor walking from a `const` that holds the head borrows the list; cutting the list +// through the head while the cursor still reads what was cut is refused. +class L { i: int; next: L | null = null; constructor(i: int) { this.i = i; } } + +function main() { + const a = new L(0); a.next = new L(1); a.next.next = new L(2); + let s: int = 0; + let q: L | null = a; + while (q) { if (q.i == 1) a.next = null; s += q.i; q = q.next; } + print(s); +} diff --git a/tslang/test/tester/own/own_err_walk_const_head_moved.ts b/tslang/test/tester/own/own_err_walk_const_head_moved.ts new file mode 100644 index 000000000..22c9e7651 --- /dev/null +++ b/tslang/test/tester/own/own_err_walk_const_head_moved.ts @@ -0,0 +1,11 @@ +// -mm=own (TSLANG 235): a `let` that borrows a `const`'s value is used after the const is moved away. +class L { i: int; next: L | null = null; constructor(i: int) { this.i = i; } } +let keepG: L | null = null; +function keep(n: L) { keepG = n; } + +function main() { + const a = new L(0); + let q: L | null = a; + keep(a); + print(q!.i); +} diff --git a/tslang/test/tester/tests/00tslang_issue_235_own.ts b/tslang/test/tester/tests/00tslang_issue_235_own.ts new file mode 100644 index 000000000..1efcd55a7 --- /dev/null +++ b/tslang/test/tester/tests/00tslang_issue_235_own.ts @@ -0,0 +1,57 @@ +// TSLANG 235 under -mm=own: walks down a linked list - a cursor that starts from a `const` holding the head, one +// that steps through a call lending its argument back (`const e = nn(p); p = e.next`), and a `let` given one of two +// consts. Each runs as under gc and rc; nodes are made at run time. +class L { + i: int; + next: L | null = null; + constructor(i: int) { this.i = i; } +} + +function nn(n: L | null): L { return n!; } + +function walkParam(a: L): int { + let p: L | null = a; + let s: int = 0; + while (p !== null) { + const e = nn(p); + s += e.i; + p = e.next; + } + return s; +} + +function main(argc: int) { + const a = new L(argc - 1); + const b = new L(argc); + b.next = new L(argc + 1); + a.next = b; + + let s: int = 0; + for (let q: L | null = a; q; q = q.next) s += q.i; + + let p: L | null = a; + let steps: int = 0; + while (p !== null) { + let n: int = 1; + while (n > 0 || p !== null) { + const e = nn(p); + p = e.next; + n--; + if (p === null) { + break; + } + } + steps++; + if (steps > 10) { + break; + } + } + + const c = new L(7); + let r: L = a; + if (argc > 5) r = c; + + print(s, steps, walkParam(a), r.i, c.i); + assert(s == 3 * argc && steps == 1 && walkParam(a) == s && r.i == argc - 1 && c.i == 7); + print("done."); +} From bea45dc3162040abd7961d813a9a2bed6dabdb9c Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sat, 5 Dec 2026 14:42:07 +0000 Subject: [PATCH 2/2] Test 235_own: main() without argc, so its exit code is defined on Linux Co-Authored-By: Claude Opus 5.5 --- tslang/test/tester/tests/00tslang_issue_235_own.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tslang/test/tester/tests/00tslang_issue_235_own.ts b/tslang/test/tester/tests/00tslang_issue_235_own.ts index 1efcd55a7..324493017 100644 --- a/tslang/test/tester/tests/00tslang_issue_235_own.ts +++ b/tslang/test/tester/tests/00tslang_issue_235_own.ts @@ -20,7 +20,10 @@ function walkParam(a: L): int { return s; } -function main(argc: int) { +let G: int = 1; + +function main() { + const argc: int = G; const a = new L(argc - 1); const b = new L(argc); b.next = new L(argc + 1);