diff --git a/TSLANG_ISSUES.md b/TSLANG_ISSUES.md index 69c4f1a8c..34dad40b2 100644 --- a/TSLANG_ISSUES.md +++ b/TSLANG_ISSUES.md @@ -3004,3 +3004,24 @@ Looked at, still open: Tests `00own_partial_moves_any_narrowing.ts` (gc, rc, own), diagnostic-location `generator_call_throwing_object_through_yielding_finally_own.ts`. + +181 (-mm=own items 180 left: 164 and methods through a yielding finally, 2026-11-24). Fixed: +- A field read through a union of object shapes, returned (`function getBox(s: Sq | Ci): Box { return s.box; }`, + 164): "a value is merged from several branches". The field is read in a branch for each shape, out of the member the + union holds (ts.GetValueFromUnionOp, ts.ExtractProperty of it), and that read was not seen as a part of the + argument: borrowedParam follows both now, so the function returns a borrow of its argument, as with one shape. Its + callers read the result and may not store it, as for one shape. +- A method, in a try whose finally yields, that throws an object out of itself (`try { yield 1; w.boom(); } finally + { yield 2; }`): it stopped at run time. Methods are followed as functions are (180): the method a call names + (ts.ThisSymbolRef, a static's ts.SymbolRef, a virtual one by the class the call goes through). + +Looked at, still open: +- -mm=own: a method overridden in a subclass that throws an object, called through the base class in such a try, is + not seen (the base's method is the one followed); a call of a function value either. +- -mm=own: `for (const s of this.items) yield s` in a generator method (158): the loop keeps the field's array in the + generator's state across its yields, a borrow of a field stored. Reading the field again at each step + (`for (let i ...) yield this.items[i]`, which works) would not be JavaScript's for-of when the field is assigned + during the loop: a ruling is needed. + +Tests `00own_union_shape_field_borrow.ts` (gc, rc, own), diagnostic-location +`generator_method_throwing_object_through_yielding_finally_own.ts`. diff --git a/docs/whats-new.md b/docs/whats-new.md index 8ebead7e1..81de88886 100644 --- a/docs/whats-new.md +++ b/docs/whats-new.md @@ -1,5 +1,6 @@ # What's new +- -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 - A destructuring `var` in a block is the function's (`{ var [a, b] = t; } a + b`); `Object.keys(o).push("z")` and other changes to an array a call made; a class method and a function of its namespace can share a name; a generator can be typed by a program's own iterator interface; a module arrow const can call a later one beside top-level code diff --git a/tslang/lib/TypeScript/MLIRGenAccessCall.cpp b/tslang/lib/TypeScript/MLIRGenAccessCall.cpp index d9afb1ddd..57ac736ac 100644 --- a/tslang/lib/TypeScript/MLIRGenAccessCall.cpp +++ b/tslang/lib/TypeScript/MLIRGenAccessCall.cpp @@ -3144,23 +3144,29 @@ namespace mlirgen // -mm=own: a call of a function that throws an object out of itself is a throw of it (mlirGen(ThrowStatement)): // through a finally block that yields it is the clear error that throw is (it stopped at run time), and the // caller throws it out of itself too where no catch clause of its own is around the call - if (compileOptions.memoryModel == MemoryModelOwn) + // (a function by its name, `f()`, or a method of a class by its own, `w.boom()` - ts.ThisSymbolRef - and + // through its virtual table: the method of the class the call names) + auto *calleeOp = funcRefValue.getDefiningOp(); + auto calleeName = calleeOp ? calleeOp->getAttrOfType("identifier") : mlir::FlatSymbolRefAttr(); + if (compileOptions.memoryModel == MemoryModelOwn && calleeName && + mlir::isa(calleeOp)) { - if (auto symbolRefOp = funcRefValue.getDefiningOp()) + auto callee = calleeName.getValue(); { // (a call of a function the callee is generated before: what it throws is known later, so the calls // out of a function are remembered, and walked when a call is in such a try) if (!genContext.throwCaughtInFunction && genContext.funcOp) { - uncaughtCalls[mlir_ts::FuncOp(genContext.funcOp).getName()].insert(symbolRefOp.getIdentifier()); + uncaughtCalls[mlir_ts::FuncOp(genContext.funcOp).getName()].insert(callee); } if (!genContext.throwCaughtBeforeYieldingFinally && yieldingFinallyOf(genContext)) { - auto thrownType = ownThrownObjectType(symbolRefOp.getIdentifier()); + auto thrownType = ownThrownObjectType(callee); if (!thrownType.empty()) { - emitError(location) << ownThrownThroughYieldingFinallyMessage(symbolRefOp.getIdentifier(), thrownType); + emitError(location) << ownThrownThroughYieldingFinallyMessage(callee, thrownType); return mlir::failure(); } @@ -3168,7 +3174,7 @@ namespace mlirgen // (checkOwnCallsThroughYieldingFinally) if (!genContext.dummyRun) { - pendingOwnYieldingFinallyCalls.push_back({symbolRefOp.getIdentifier().str(), location}); + pendingOwnYieldingFinallyCalls.push_back({callee.str(), location}); } } } diff --git a/tslang/lib/TypeScript/OwnershipFacts.h b/tslang/lib/TypeScript/OwnershipFacts.h index 1ff0ba24b..6adabaccb 100644 --- a/tslang/lib/TypeScript/OwnershipFacts.h +++ b/tslang/lib/TypeScript/OwnershipFacts.h @@ -2809,6 +2809,19 @@ inline int borrowedParam(mlir::Value value, int depth = 0) return borrowedParam(unboxOp.getIn(), depth + 1); } + // the member a union of object shapes holds, read out of it, and a field of that member: the union's (`s.box` of + // a `Sq | Ci`, read in a branch for each shape, TSLANG 164) + if (auto unionValueOp = mlir::dyn_cast_or_null(def)) + { + return borrowedParam(unionValueOp->getOperand(0), depth + 1); + } + + if (auto extractOp = mlir::dyn_cast_or_null(def); + extractOp && extractOp->getOperand(0).getDefiningOp()) + { + return borrowedParam(extractOp->getOperand(0), depth + 1); + } + // a field, an element, a field through an interface: the argument's block holds them. A // handle's payload is any handle's, no parameter's. if (auto place = placeBase(value); place && place.kind != PlaceKind::SharedValue) diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index ad8b9ac68..3564edbff 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -1408,6 +1408,9 @@ 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-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") tslang_add_test(NAME test-jit-own-00-own-partial-moves-any-narrowing COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00own_partial_moves_any_narrowing.ts") tslang_add_test(NAME test-compile-own-00-own-partial-moves-any-narrowing COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00own_partial_moves_any_narrowing.ts") tslang_add_test(NAME test-jit-00-iterable-string-for-of COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00iterable_string_for_of.ts") @@ -4403,6 +4406,15 @@ set_tests_properties(test-compile-diagnostic-location-generator-call-throwing-ob PROPERTIES PASS_REGULAR_EXPRESSION "generator_call_throwing_object_through_yielding_finally_own\\.ts:10:9: error: -mm=own: 'viaMid' throws an object of type 'Failure', and thrown through a finally block that yields it is not supported" FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed") +# -mm=own: a method so called is checked too (TSLANG 181) +add_test(NAME test-compile-diagnostic-location-generator-method-throwing-object-through-yielding-finally-own + COMMAND $ --emit=obj --no-default-lib -mm=own + "${PROJECT_SOURCE_DIR}/test/tester/diagnostic-location/generator_method_throwing_object_through_yielding_finally_own.ts" + -o "${CMAKE_CURRENT_BINARY_DIR}/diagnostic-location-generator-method-throwing-object-through-yielding-finally-own.obj") +set_tests_properties(test-compile-diagnostic-location-generator-method-throwing-object-through-yielding-finally-own + PROPERTIES PASS_REGULAR_EXPRESSION "generator_method_throwing_object_through_yielding_finally_own\\.ts:16:9: error: -mm=own: 'W[.]boom' throws an object of type 'Failure'" + FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed") + # -mm=own: an object sent by next() is an error at the yield that reads it, pointing at Shared. add_test(NAME test-compile-diagnostic-location-generator-next-object-own COMMAND $ --emit=obj --no-default-lib -mm=own diff --git a/tslang/test/tester/diagnostic-location/generator_method_throwing_object_through_yielding_finally_own.ts b/tslang/test/tester/diagnostic-location/generator_method_throwing_object_through_yielding_finally_own.ts new file mode 100644 index 000000000..f5137503a --- /dev/null +++ b/tslang/test/tester/diagnostic-location/generator_method_throwing_object_through_yielding_finally_own.ts @@ -0,0 +1,28 @@ +// -mm=own: a call, in a try whose finally yields, of a method that throws an object out of itself is an error at the +// call, as such a throw written there is +class Failure { + msg = "x" + 1; +} + +class W { + boom() { + throw new Failure(); + } +} + +function* g(w: W) { + try { + yield 1; + w.boom(); + } finally { + yield 2; + } +} + +function main() { + try { + for (const v of g(new W())) print(v); + } catch (e) { + print("caught"); + } +} diff --git a/tslang/test/tester/tests/00own_union_shape_field_borrow.ts b/tslang/test/tester/tests/00own_union_shape_field_borrow.ts new file mode 100644 index 000000000..5516484e3 --- /dev/null +++ b/tslang/test/tester/tests/00own_union_shape_field_borrow.ts @@ -0,0 +1,31 @@ +// TSLANG 181: a field read through a union of object shapes, returned, borrows the union (164); a getter of it is +// read and called as the getter of one shape is +class Box { + n = 1; + t = "b" + 1; +} + +type Sq = { kind: "sq"; size: number; box: Box }; +type Ci = { r: number; kind: "ci"; box: Box }; + +function getBox(s: Sq | Ci): Box { + return s.box; +} + +function sumOf(i: number) { + if (i % 2 == 0) { + const s: Sq | Ci = { kind: "sq", size: 2, box: new Box() }; + return getBox(s).n + getBox(s).t.length; + } + + const c: Sq | Ci = { r: 1.5, kind: "ci", box: new Box() }; + const b = getBox(c); + return b.n * 10; +} + +function main() { + let t: number = 0; + for (let i = 0; i < 1000; i++) t += sumOf(i); + assert(t == 6500); + print("done."); +}