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
16 changes: 16 additions & 0 deletions TSLANG_ISSUES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
1 change: 1 addition & 0 deletions docs/whats-new.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
56 changes: 56 additions & 0 deletions tslang/lib/TypeScript/MLIRGenStatements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<PropertyAccessExpression>()->expression);
}

auto iteratedType = iterated == SyntaxKind::PropertyAccessExpression && root == SyntaxKind::ThisKeyword
? evaluate(iterated, genContext)
: mlir::Type();
if (iteratedType && isa<mlir_ts::ArrayType>(iteratedType))
{
NodeFactory nf(NodeFactoryFlags::None);
auto _i = nf.createIdentifier(S(".i"));
NodeArray<VariableDeclaration> 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<ts::Statement> statements;
auto initializer = forOfStatementAST->initializer;
if (initializer == SyntaxKind::VariableDeclarationList)
{
auto declList = initializer.as<VariableDeclarationList>();
auto declared = declList->declarations.front();
NodeArray<VariableDeclaration> 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<Expression>(), 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;
Expand Down
4 changes: 4 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
49 changes: 49 additions & 0 deletions tslang/test/tester/tests/00own_generator_for_of_this_field.ts
Original file line number Diff line number Diff line change
@@ -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.");
}
Loading