From 37cff85036e788fd527b66543b0ad81daedee31b Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sat, 21 Nov 2026 22:50:08 +0000 Subject: [PATCH 1/3] own: a[i] = v does not grow an array reached through a handle's payload (library TypedArray.set, workers) #982 made such a write a grows-only SetLengthOf, which own's passes read as a push into a Shared payload - a possible release of what callers borrow - and 8 DefaultLib tests stopped compiling under -mm=own. Co-Authored-By: Claude Opus 5.5 --- TSLANG_ISSUES.md | 6 ++- tslang/lib/TypeScript/MLIRGenImpl.h | 8 ++++ tslang/test/tester/CMakeLists.txt | 4 ++ .../test/tester/tests/00array_write_shared.ts | 37 +++++++++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 tslang/test/tester/tests/00array_write_shared.ts diff --git a/TSLANG_ISSUES.md b/TSLANG_ISSUES.md index d3ec52793..9517c01d0 100644 --- a/TSLANG_ISSUES.md +++ b/TSLANG_ISSUES.md @@ -2171,7 +2171,11 @@ Still open, looked at: the elements a borrow reads as dropped; in a branch between the value stored and its store, the store as a second reference. Not through a local over a parameter's array (`let src = order; src[i] = v`): changed in place through it - a push too - the local owns what it holds and the parameter is kept, so there the write is as before - (open). A parameter's array, a field's of a parameter, arrays of objects and of arrays: flat at 6 MB over 200000 + (open). Nor through a handle's payload (`s.value[i] = v`): a push there may move the elements another handle's + borrow reads (spec 23.3), so the library's TypedArray.set (and MessageQueue.close, the workers) were taken for + methods that release what their callers borrow, and 8 library tests stopped compiling under -mm=own ("a value that + borrows 'this.elements' is used here after it may be released"); the write there is as before (open). Test + `00array_write_shared.ts`. A parameter's array, a field's of a parameter, arrays of objects and of arrays: flat at 6 MB over 200000 rounds (gc, rc, own), clean on the wasm checked heap (own, rc). Not when the right side gave the variable another array (`a[1] = (a = [5], 9)`): the store goes into the one the left side read; a store of another through another place (`h.a[0] = (h.a = [5], 9)`, or in a call) is not seen, and grows the new one (open). diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index f911901b0..ff1543bdf 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -20295,6 +20295,14 @@ class MLIRGenImpl } } + // Not through a handle's payload (`s.value[i] = v`, the library's TypedArray.set): a growth there, as a + // push, may move the elements another handle's borrow reads (spec 23.3), so a method that writes one + // element would be taken for one that releases what its callers borrow. As before there. + if (own_facts::reachesSharedValue(arrayRef)) + { + return; + } + auto one = builder.create(location, i32Type, builder.getI32IntegerAttr(1)); auto newLength32 = builder.create( location, i32Type, builder.getI32IntegerAttr((int)SyntaxKind::PlusToken), index32, one); diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 725a8d780..0be4dd389 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -480,6 +480,7 @@ tslang_add_test(NAME test-compile-00-generator-symbol-iterator COMMAND test-runn tslang_add_test(NAME test-compile-00-generic-shadowed-type-param COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00generic_shadowed_type_param.ts") tslang_add_test(NAME test-compile-00-any-undefined-field COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_undefined_field.ts") tslang_add_test(NAME test-compile-00-array-write-grows COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00array_write_grows.ts") +tslang_add_test(NAME test-compile-00-array-write-shared COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00array_write_shared.ts") tslang_add_test(NAME test-compile-00-constructor-type-value COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts") tslang_add_test(NAME test-compile-00-string-concat-chain COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00string_concat_chain.ts") tslang_add_test(NAME test-compile-00-arrays COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00array.ts") @@ -1347,6 +1348,9 @@ tslang_add_test(NAME test-jit-own-00-any-undefined-field COMMAND test-runner -ji tslang_add_test(NAME test-jit-00-array-write-grows COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00array_write_grows.ts") tslang_add_test(NAME test-jit-rc-00-array-write-grows COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00array_write_grows.ts") tslang_add_test(NAME test-jit-own-00-array-write-grows COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00array_write_grows.ts") +tslang_add_test(NAME test-jit-00-array-write-shared COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00array_write_shared.ts") +tslang_add_test(NAME test-jit-rc-00-array-write-shared COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00array_write_shared.ts") +tslang_add_test(NAME test-jit-own-00-array-write-shared COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00array_write_shared.ts") tslang_add_test(NAME test-jit-00-constructor-type-value COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts") tslang_add_test(NAME test-jit-rc-00-constructor-type-value COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts") tslang_add_test(NAME test-jit-own-00-constructor-type-value COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts") diff --git a/tslang/test/tester/tests/00array_write_shared.ts b/tslang/test/tester/tests/00array_write_shared.ts new file mode 100644 index 000000000..764a9c0f4 --- /dev/null +++ b/tslang/test/tester/tests/00array_write_shared.ts @@ -0,0 +1,37 @@ +// A write of an element of an array held by a handle (`s.value[i] = v`, as the library's TypedArray.set +// does): under -mm=own it does not grow the array there, and so a method that writes one element is +// not taken for one that releases what its callers borrow ("a value that borrows 'this.sync' is used +// here after it may be released"). + +class Cells { + private store = new Shared([0, 0, 0]); + public set(index: int, value: int) { + this.store.value[index] = value; + } + + public get(index: int) { + return this.store.value[index]; + } +} + +class Queue { + sync = new Cells(); + close() { + this.sync.set(2, 1); + } +} + +class Worker1 { + q = new Shared(new Queue()); + end() { + this.q.value.close(); + this.q.value.close(); + } +} + +function main() { + const w = new Worker1(); + w.end(); + assert(w.q.value.sync.get(2) == 1, "written"); + print("done."); +} From 94dac974dc2124df48e5502237d7e6987eb7b635 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sat, 21 Nov 2026 23:33:03 +0000 Subject: [PATCH 2/3] own: a call of an interface's optional function-typed member lends the object (g.return(v) then g.next()) The bound method value was wrapped in an optional, given out of the If that tests the member (a branch's block argument by then), tested and unwrapped for the call; own read that as moving the object into the method value. Co-Authored-By: Claude Opus 5.5 --- TSLANG_ISSUES.md | 10 +++- .../TypeScript/OwnershipInferenceMoves.cpp | 57 ++++++++++++++++--- .../lib/TypeScript/OwnershipInferencePass.h | 1 + tslang/test/tester/CMakeLists.txt | 4 ++ .../tests/00interface_optional_member_call.ts | 33 +++++++++++ 5 files changed, 94 insertions(+), 11 deletions(-) create mode 100644 tslang/test/tester/tests/00interface_optional_member_call.ts diff --git a/TSLANG_ISSUES.md b/TSLANG_ISSUES.md index 9517c01d0..141a2a1f2 100644 --- a/TSLANG_ISSUES.md +++ b/TSLANG_ISSUES.md @@ -2214,9 +2214,13 @@ Tests: `00object_dictionary.ts`, `00own_param_exclusive_moves.ts` (compile, JIT, done: true }`, throw(e) throws e. The generator object holds a mode its yields check where they resume (addGeneratorResumeMethods, mlirGenGeneratorResumeChecks). Under -mm=own return() only for a T that owns nothing (a number, a boolean): its result would hold the value the caller lends. The library's `Generator` - declares them optional (`return?`, `throw?`: a generator declared otherwise has neither) once a compiler with - them is released. Under -mm=own a call through an optional one is read as moving the generator, so there a - program declaring them required calls them. + declares them optional (`return?`, `throw?`: a generator declared otherwise has neither; fields of a function type + as `next`, not methods - a method shifts the vtable, and a Generator then can't be cast to an Iterable, whose + `next` has another type). Under -mm=own a call of an optional function-typed member of an interface (`g.return(v)`) + was read as moving the object: the method value bound to it is wrapped in an optional, given out of the If that + tests the member (lowered by then: a branch's block argument), tested and unwrapped for the call - all of which + only calls it now lends the object (isBoundForCall, boundOnlyCalled), as a call of a member that is not optional + does. Test `00interface_optional_member_call.ts` (compile, JIT, rc, own). - -mm=own: an object literal's field of `undefined` or `null` (`{ t: undefined, n: 1 }`) was released twice: own_facts::isConstantData took null or undefined seen through its casts for data nothing owns, a box into `any` included - so the store of the box into the object was not checked as a move, and the diff --git a/tslang/lib/TypeScript/OwnershipInferenceMoves.cpp b/tslang/lib/TypeScript/OwnershipInferenceMoves.cpp index 1ee761cce..b775fa00a 100644 --- a/tslang/lib/TypeScript/OwnershipInferenceMoves.cpp +++ b/tslang/lib/TypeScript/OwnershipInferenceMoves.cpp @@ -899,15 +899,56 @@ namespace own_inference return false; } - return llvm::all_of(boundOp->getUsers(), [](mlir::Operation *boundUser) { - if (mlir::isa(boundUser)) - { - return true; - } + return boundOnlyCalled(boundOp.getResult(), 0); + }); + } - return mlir::isa(boundUser) && - llvm::all_of(boundUser->getUsers(), [](mlir::Operation *thisUser) { return isCall(thisUser); }); - }); + // A bound function only called: its method and object taken for a call, or - a call of an optional member, + // `g.return?.(v)`, `g.return(v)` of `return?: ...` - wrapped in an optional, given out of an If, tested and + // unwrapped for the call. Its object is lent to the call, not moved into a value that outlives it. + bool OwnershipInferencePass::boundOnlyCalled(mlir::Value bound, int depth) + { + if (depth > 6) + { + return false; + } + + return llvm::all_of(bound.getUses(), [&](mlir::OpOperand &use) { + auto *boundUser = use.getOwner(); + if (mlir::isa(boundUser)) + { + return true; + } + + if (mlir::isa(boundUser)) + { + return llvm::all_of(boundUser->getUsers(), [](mlir::Operation *thisUser) { return isCall(thisUser); }); + } + + if (auto castOp = mlir::dyn_cast(boundUser)) + { + return mlir::isa(castOp.getType()); + } + + if (mlir::isa(boundUser)) + { + return boundOnlyCalled(boundUser->getResult(0), depth + 1); + } + + if (auto resultOp = mlir::dyn_cast(boundUser)) + { + auto ifOp = mlir::dyn_cast_or_null(resultOp->getParentOp()); + return ifOp && use.getOperandNumber() < ifOp->getNumResults() && + boundOnlyCalled(ifOp->getResult(use.getOperandNumber()), depth + 1); + } + + // the If lowered: its result a block argument + if (auto brOp = mlir::dyn_cast(boundUser)) + { + return boundOnlyCalled(brOp.getDest()->getArgument(use.getOperandNumber()), depth + 1); + } + + return false; }); } diff --git a/tslang/lib/TypeScript/OwnershipInferencePass.h b/tslang/lib/TypeScript/OwnershipInferencePass.h index d5f28cb97..b8eb83e85 100644 --- a/tslang/lib/TypeScript/OwnershipInferencePass.h +++ b/tslang/lib/TypeScript/OwnershipInferencePass.h @@ -1132,6 +1132,7 @@ class OwnershipInferencePass // interface's `this` (`ts.ExtractInterfaceThis`) is bound so for a call of a function-typed // field (`i.toString()` where `toString: () => string`). static bool isBoundForCall(mlir::Operation *thisOp); + static bool boundOnlyCalled(mlir::Value bound, int depth); // Is `value` an argument the call's callee keeps? static bool isKeptArgument(mlir::Operation *call, mlir::Value value); diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 0be4dd389..296babe6a 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -481,6 +481,7 @@ tslang_add_test(NAME test-compile-00-generic-shadowed-type-param COMMAND test-ru tslang_add_test(NAME test-compile-00-any-undefined-field COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_undefined_field.ts") tslang_add_test(NAME test-compile-00-array-write-grows COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00array_write_grows.ts") tslang_add_test(NAME test-compile-00-array-write-shared COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00array_write_shared.ts") +tslang_add_test(NAME test-compile-00-interface-optional-member-call COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00interface_optional_member_call.ts") tslang_add_test(NAME test-compile-00-constructor-type-value COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts") tslang_add_test(NAME test-compile-00-string-concat-chain COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00string_concat_chain.ts") tslang_add_test(NAME test-compile-00-arrays COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00array.ts") @@ -1351,6 +1352,9 @@ tslang_add_test(NAME test-jit-own-00-array-write-grows COMMAND test-runner -jit tslang_add_test(NAME test-jit-00-array-write-shared COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00array_write_shared.ts") tslang_add_test(NAME test-jit-rc-00-array-write-shared COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00array_write_shared.ts") tslang_add_test(NAME test-jit-own-00-array-write-shared COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00array_write_shared.ts") +tslang_add_test(NAME test-jit-00-interface-optional-member-call COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00interface_optional_member_call.ts") +tslang_add_test(NAME test-jit-rc-00-interface-optional-member-call COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00interface_optional_member_call.ts") +tslang_add_test(NAME test-jit-own-00-interface-optional-member-call COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00interface_optional_member_call.ts") tslang_add_test(NAME test-jit-00-constructor-type-value COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts") tslang_add_test(NAME test-jit-rc-00-constructor-type-value COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts") tslang_add_test(NAME test-jit-own-00-constructor-type-value COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts") diff --git a/tslang/test/tester/tests/00interface_optional_member_call.ts b/tslang/test/tester/tests/00interface_optional_member_call.ts new file mode 100644 index 000000000..eb56d150b --- /dev/null +++ b/tslang/test/tester/tests/00interface_optional_member_call.ts @@ -0,0 +1,33 @@ +// A call of an interface's optional function-typed member (`undo?: ...`) lends the object to the call, as a +// call of a member that is not optional does: under -mm=own the object is used again after it (the library's +// Generator `return?` / `throw?`, `g.return(v)` and then `g.next()`), not moved into the method value. + +interface Counter { + n: number; + add: (x: number) => number; + undo?: (x: number) => number; +} + +function make(): Counter { + return { + n: 0, + add(x: number): number { + this.n += x; + return this.n; + }, + undo(x: number): number { + this.n -= x; + return this.n; + }, + }; +} + +function main() { + const c = make(); + c.add(5); + const r = c.undo(2); + assert(r == 3, "the optional member called"); + assert(c.add(1) == 4, "the object used again after it"); + assert(c.n == 4, "its field"); + print("done."); +} From 4c23b0e7e589f83d54ef9bcee6d940782a95ee4a Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sun, 22 Nov 2026 00:25:01 +0000 Subject: [PATCH 3/3] TSLANG 153: why the library's Generator.next takes no value (a Generator given to an Iterable as it is) Co-Authored-By: Claude Opus 5.5 --- TSLANG_ISSUES.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/TSLANG_ISSUES.md b/TSLANG_ISSUES.md index 141a2a1f2..2bf47dfe0 100644 --- a/TSLANG_ISSUES.md +++ b/TSLANG_ISSUES.md @@ -2214,9 +2214,12 @@ Tests: `00object_dictionary.ts`, `00own_param_exclusive_moves.ts` (compile, JIT, done: true }`, throw(e) throws e. The generator object holds a mode its yields check where they resume (addGeneratorResumeMethods, mlirGenGeneratorResumeChecks). Under -mm=own return() only for a T that owns nothing (a number, a boolean): its result would hold the value the caller lends. The library's `Generator` - declares them optional (`return?`, `throw?`: a generator declared otherwise has neither; fields of a function type - as `next`, not methods - a method shifts the vtable, and a Generator then can't be cast to an Iterable, whose - `next` has another type). Under -mm=own a call of an optional function-typed member of an interface (`g.return(v)`) + declares them optional (`return?`, `throw?`: a generator declared otherwise has neither), and its next() takes no + value: declared `next: (value?: TNext) =>`, a Generator given to an Iterable (`next: () =>`) was let through + as it is - castInterfaceToInterface reports a member of another type only when the vtable must be remapped, and + `next` is the first slot of both - so an Iterable's next() called a function of another signature: wasm's + "function signature mismatch" (the library's iteration_types.ts), natively a yield reading a value never given + (open: such a cast is not refused; next(value) through the library's Generator). Under -mm=own a call of an optional function-typed member of an interface (`g.return(v)`) was read as moving the object: the method value bound to it is wrapped in an optional, given out of the If that tests the member (lowered by then: a branch's block argument), tested and unwrapped for the call - all of which only calls it now lends the object (isBoundForCall, boundOnlyCalled), as a call of a member that is not optional