From 04f22b6df726b3505ff8a61acb3e5ad3d6ac9a0b Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Tue, 24 Nov 2026 13:05:59 +0000 Subject: [PATCH] -mm=own: for-of over a field of this in a generator reads the field at each step (TSLANG 182) Co-Authored-By: Claude Opus 5.5 --- TSLANG_ISSUES.md | 16 ++++++ docs/whats-new.md | 1 + tslang/lib/TypeScript/MLIRGenStatements.cpp | 56 +++++++++++++++++++ tslang/test/tester/CMakeLists.txt | 4 ++ .../00own_generator_for_of_this_field.ts | 49 ++++++++++++++++ 5 files changed, 126 insertions(+) create mode 100644 tslang/test/tester/tests/00own_generator_for_of_this_field.ts diff --git a/TSLANG_ISSUES.md b/TSLANG_ISSUES.md index 34dad40b2..b044c1bfc 100644 --- a/TSLANG_ISSUES.md +++ b/TSLANG_ISSUES.md @@ -3025,3 +3025,19 @@ Looked at, still open: Tests `00own_union_shape_field_borrow.ts` (gc, rc, own), diagnostic-location `generator_method_throwing_object_through_yielding_finally_own.ts`. + +182 (158, USER RULING 2026-11-24). Fixed: +- -mm=own: `for (const s of this.items) yield s` in a generator method (158): "a value that borrows a field cannot be + stored, returned or captured". The loop kept the field's array in the generator's state across its yields. Under + -mm=own, a for-of in a generator over an array field reached from `this` (`this.items`, `this.inner.words`) reads + the field again at each step: `for (let .i = 0; .i < this.items.length; .i++) { const s = this.items[.i]; ... }`, + with `break`, `continue`, a destructuring element and a loop variable declared before the loop as before. By the + ruling, a field assigned during the loop is followed (`this.items = [...]` in the body ends a loop over the new + array), which JavaScript's for-of would not do; under the other memory models the loop is as before. + +Looked at, still open: +- -mm=own: an element that is an object (`for (const o of this.objs) yield o.n`), and the same loop written with an + index, is "a value that borrows an element cannot be stored": a generator's local is a field of its state, and a + borrow of an element cannot be one. + +Tests `00own_generator_for_of_this_field.ts` (gc, rc, own). diff --git a/docs/whats-new.md b/docs/whats-new.md index 81de88886..3dd9eee91 100644 --- a/docs/whats-new.md +++ b/docs/whats-new.md @@ -1,5 +1,6 @@ # What's new +- -mm=own: `for (const s of this.items) yield s` works in a generator method: the field is read again at each step (a field assigned during the loop is followed, unlike JavaScript's for-of) - -mm=own: a field read through a union of object shapes can be returned as a borrow of the argument (`getBox(s: Sq | Ci) { return s.box; }`); a method that throws an object through a `finally` that yields is a compile error at the call - -mm=own: a value moved in some branches of an `if ... else if` with no `else` is released on the others; `typeof a === "string"` narrows a `const` holding an `any`; a call that throws an object through a `finally` that yields is a compile error, not a stop at run time - A closure in a closure of a parameter's default value works (`m(f = () => () => this)`), and under -mm=rc a closure made by a default value lives through the call; an exported arrow const and a const set from a later function's call work beside top-level code; a namespace declared in several blocks can have a non-exported class, function or variable of one name in each diff --git a/tslang/lib/TypeScript/MLIRGenStatements.cpp b/tslang/lib/TypeScript/MLIRGenStatements.cpp index aa3398f7c..5f3f3e160 100644 --- a/tslang/lib/TypeScript/MLIRGenStatements.cpp +++ b/tslang/lib/TypeScript/MLIRGenStatements.cpp @@ -1911,6 +1911,62 @@ namespace mlirgen { auto location = loc(forOfStatementAST); + // -mm=own: `for (const s of this.items) yield s` in a generator kept the field's array in the generator's + // state across its yields, a borrow of a field stored (TSLANG 158). The field is read again at each step, + // `for (let .i = 0; .i < this.items.length; .i++) { const s = this.items[.i]; ... }` (USER RULING 2026-11-24: + // own only; a field assigned during the loop is followed, which JavaScript's for-of would not). + if (compileOptions.memoryModel == MemoryModelOwn && genContext.allocateVarsInContextThis && + !forOfStatementAST->awaitModifier) + { + auto iterated = stripParentheses(forOfStatementAST->expression); + auto root = iterated; + while (root == SyntaxKind::PropertyAccessExpression) + { + root = stripParentheses(root.as()->expression); + } + + auto iteratedType = iterated == SyntaxKind::PropertyAccessExpression && root == SyntaxKind::ThisKeyword + ? evaluate(iterated, genContext) + : mlir::Type(); + if (iteratedType && isa(iteratedType)) + { + NodeFactory nf(NodeFactoryFlags::None); + auto _i = nf.createIdentifier(S(".i")); + NodeArray declarations; + declarations.push_back(nf.createVariableDeclaration(_i, undefined, undefined, nf.createNumericLiteral(S("0")))); + auto initVars = nf.createVariableDeclarationList(declarations, NodeFlags::Let); + auto cond = nf.createBinaryExpression( + _i, nf.createToken(SyntaxKind::LessThanToken), + nf.createPropertyAccessExpression(iterated, nf.createIdentifier(S(LENGTH_FIELD_NAME)))); + auto incr = nf.createPrefixUnaryExpression(nf.createToken(SyntaxKind::PlusPlusToken), _i); + auto element = nf.createElementAccessExpression(iterated, _i); + + NodeArray statements; + auto initializer = forOfStatementAST->initializer; + if (initializer == SyntaxKind::VariableDeclarationList) + { + auto declList = initializer.as(); + auto declared = declList->declarations.front(); + NodeArray elementDeclarations; + elementDeclarations.push_back( + nf.createVariableDeclaration(declared->name, undefined, declared->type, element)); + statements.push_back( + nf.createVariableStatement(undefined, nf.createVariableDeclarationList(elementDeclarations, declList->flags))); + } + else + { + statements.push_back(nf.createExpressionStatement( + nf.createBinaryExpression(initializer.as(), nf.createToken(SyntaxKind::EqualsToken), element))); + } + + statements.push_back(forOfStatementAST->statement); + auto forStatement = nf.createForStatement(initVars, cond, incr, nf.createBlock(statements)); + forStatement->pos = forOfStatementAST->pos; + forStatement->_end = forOfStatementAST->_end; + return mlirGen(forStatement, genContext); + } + } + // the loop's label, handed back to the `for` the loop is made into: generating the iterated value or its // iterator can generate functions whose own loops would take it auto loopLabel = label; diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 3564edbff..0dec1cb4e 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -1408,6 +1408,10 @@ tslang_add_test(NAME test-jit-own-00-generator-throw-through-yielding-finally CO tslang_add_test(NAME test-jit-00-own-partial-moves-any-narrowing COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00own_partial_moves_any_narrowing.ts") tslang_add_test(NAME test-jit-rc-00-own-partial-moves-any-narrowing COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00own_partial_moves_any_narrowing.ts") tslang_add_test(NAME test-jit-own-own-moved-on-two-of-three-paths COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/own/own_moved_on_two_of_three_paths.ts") +tslang_add_test(NAME test-jit-00-own-generator-for-of-this-field COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00own_generator_for_of_this_field.ts") +tslang_add_test(NAME test-jit-rc-00-own-generator-for-of-this-field COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00own_generator_for_of_this_field.ts") +tslang_add_test(NAME test-jit-own-00-own-generator-for-of-this-field COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00own_generator_for_of_this_field.ts") +tslang_add_test(NAME test-compile-own-00-own-generator-for-of-this-field COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00own_generator_for_of_this_field.ts") tslang_add_test(NAME test-jit-00-own-union-shape-field-borrow COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00own_union_shape_field_borrow.ts") tslang_add_test(NAME test-jit-rc-00-own-union-shape-field-borrow COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00own_union_shape_field_borrow.ts") tslang_add_test(NAME test-jit-own-00-own-union-shape-field-borrow COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00own_union_shape_field_borrow.ts") diff --git a/tslang/test/tester/tests/00own_generator_for_of_this_field.ts b/tslang/test/tester/tests/00own_generator_for_of_this_field.ts new file mode 100644 index 000000000..cd33be83c --- /dev/null +++ b/tslang/test/tester/tests/00own_generator_for_of_this_field.ts @@ -0,0 +1,49 @@ +// TSLANG 182: `for (const s of this.items) yield s` in a generator method (158). Under -mm=own the field is read again +// at each step (USER RULING 2026-11-24), so the loop keeps no borrow of it in the generator's state +class Inner { + words: string[] = ["x" + 1, "y" + 2, "z" + 3]; +} + +class B { + items: string[] = ["a" + 1, "b" + 2, "c" + 3]; + pairs: number[][] = [[1, 2], [3, 4]]; + inner = new Inner(); + + *each() { + for (const s of this.items) { + if (s == "b2") continue; + yield s; + } + } + + *firstTwo() { + let k = 0; + for (const s of this.inner.words) { + if (k++ == 2) break; + yield s; + } + } + + *sums() { + for (const [a, b] of this.pairs) yield a + b; + } + + *assigned() { + let s: string = ""; + for (s of this.items) yield s.length; + } +} + +function main() { + let out = ""; + for (const s of new B().each()) out += s; + for (const s of new B().firstTwo()) out += s; + assert(out == "a1c3x1y2"); + + let t: number = 0; + for (const n of new B().sums()) t += n; + for (const n of new B().assigned()) t += n; + assert(t == 16); + + print("done."); +}