diff --git a/TSLANG_ISSUES.md b/TSLANG_ISSUES.md index 2f60d69d0..6ce8c978c 100644 --- a/TSLANG_ISSUES.md +++ b/TSLANG_ISSUES.md @@ -5,10 +5,15 @@ should be a complete file that was run (`tslang file.ts`, JIT, Windows x64), wit ## Open -Items 1-26 (collected 2026-10-12 to pre-alpha115) are fixed or answered; their write-ups are in this -file's git history. The last three, fixed by #775 (regression tests +Items 1-27 (collected 2026-10-12 to pre-alpha116) are fixed or answered; their write-ups are in this +file's git history. 24-26 were fixed by #775 (regression tests `tslang/test/tester/tests/00tslang_issues_24_26.ts` and `ref_element_layout_error.ts`): 24 (an array literal holding `const X = 1 << n` widened to 64-bit elements: shifts and bitwise operators now give an `s32`, as `int` is); 25 (`cond ? [1] : []` crashed when iterated: an empty array literal now takes the other branch's array type); 26 (`Ref(x)` of a `number` was accepted for a `Ref` parameter: a -reference to a number laid out otherwise is now an error). +reference to a number laid out otherwise is now an error). 27 (`-mm=rc`: a template literal passed +straight to a call reached the callee with no reference behind it, so `console.log(text)` freed it +before `this.message = text` stored it): a template, and an object or literal array printed into a +string, now carry a reference of their own as `"a" + b` does, which the caller gives back after the +call. Under `-mm=own` and rc the same strings also leaked (3M template arguments: ~570 MB, now ~50 MB). +Regression test `tslang/test/tester/tests/00tslang_issue_27.ts`. diff --git a/tslang/lib/TypeScript/MLIRGenCast.cpp b/tslang/lib/TypeScript/MLIRGenCast.cpp index 696a26256..b27b35f85 100644 --- a/tslang/lib/TypeScript/MLIRGenCast.cpp +++ b/tslang/lib/TypeScript/MLIRGenCast.cpp @@ -489,8 +489,11 @@ namespace mlirgen auto concatValues = builder.create(location, stringType, mlir::ArrayRef{strs}); - return V(concatValues); - } + // a new string, owned as a template's is (TSLANG 27) + markFreshBlockOwned(location, concatValues); + + return V(concatValues); + } return mlir::failure(); } @@ -587,7 +590,11 @@ namespace mlirgen auto concatValues = builder.create(location, stringType, mlir::ArrayRef{strs}); - return V(concatValues); + // `{ a: 1 }` builds a new string, owned as a template's is: unmarked, passed + // straight to a call it reached the callee with no reference behind it (TSLANG 27) + markFreshBlockOwned(location, concatValues); + + return V(concatValues); } // An interface printed: its `toString` when it has one, as a class's is; else its fields, read diff --git a/tslang/lib/TypeScript/MLIRGenExpressions.cpp b/tslang/lib/TypeScript/MLIRGenExpressions.cpp index 40b9535fd..82191a919 100644 --- a/tslang/lib/TypeScript/MLIRGenExpressions.cpp +++ b/tslang/lib/TypeScript/MLIRGenExpressions.cpp @@ -2180,6 +2180,13 @@ namespace mlirgen auto concatValues = builder.create(location, stringType, mlir::ArrayRef{strs}); + // a template builds a new string, as `"a" + b` does: it carries a reference of its own, + // which a receiver takes over and which is given back at the end of the block otherwise. + // Unmarked, `f(\`a${b}\`)` passed a string nothing held: under rc a callee that handed its + // parameter to a call (`console.log(text)`) dropped the count to zero and freed it before + // `this.message = text` stored it (TSLANG 27). + markFreshBlockOwned(location, concatValues); + return V(concatValues); } diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 889d0a24e..bd0f92810 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -1780,6 +1780,11 @@ endforeach() foreach(issues_mm gc rc none own) tslang_add_test(NAME test-jit-00-tslang-issues-24-26-${issues_mm} COMMAND test-runner -jit -mm=${issues_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/00tslang_issues_24_26.ts") endforeach() +# TSLANG_ISSUES 27: a template literal argument is held by the caller until the call returns +foreach(issues_mm gc rc none own) + tslang_add_test(NAME test-jit-00-tslang-issue-27-${issues_mm} COMMAND test-runner -jit -mm=${issues_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/00tslang_issue_27.ts") + tslang_add_test(NAME test-compile-00-tslang-issue-27-${issues_mm} COMMAND test-runner -mm=${issues_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/00tslang_issue_27.ts") +endforeach() # ...a reference to a number is no reference to an f32 add_test(NAME test-compile-ref-element-layout-error COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/ref_element_layout_error.ts") diff --git a/tslang/test/tester/tests/00tslang_issue_27.ts b/tslang/test/tester/tests/00tslang_issue_27.ts new file mode 100644 index 000000000..1e3c11b42 --- /dev/null +++ b/tslang/test/tester/tests/00tslang_issue_27.ts @@ -0,0 +1,54 @@ +// TSLANG_ISSUES 27: a template literal passed straight to a call is a new string the caller holds +// until the call returns. Under rc it arrived with no reference behind it, so a callee handing its +// parameter to a call first (console.log, whose rest array retains and releases it) dropped the +// count to zero and freed the string before it stored it. + +const NAMES = ["down", "off", "point", "tilt"]; + +// stands for console.log: the rest array holds what it is given and lets go of it on return +function log(...parts: string[]) { + print(parts[0]); +} + +class Hud { + message: string = ""; + show(text: string) { + log(text); + this.message = text; + } +} + +let kept = ""; +function keep(text: string) { + log(text); + kept = text; +} + +function main() { + const hud = new Hud(); + for (let i = 0; i < 8; i++) { + hud.show(`Gravity: ${NAMES[i % 4]}`); + // a fresh string made in the block reuses what was freed + const other = `XXXXXXXX: ${NAMES[(i + 1) % 4]}`; + assert(hud.message == "Gravity: " + NAMES[i % 4], "the method kept its template argument"); + assert(other == "XXXXXXXX: " + NAMES[(i + 1) % 4], "the next template"); + + keep(`Keep ${i}: ${NAMES[i % 4]}`); + const again = `YYYYYYYY: ${NAMES[(i + 2) % 4]}`; + assert(kept == "Keep " + i + ": " + NAMES[i % 4], "the function kept its template argument"); + assert(again.length == 10 + NAMES[(i + 2) % 4].length, "the template after it"); + + // an object or a literal array printed into a string is a new string too + keep({ a: i }); + const third = `ZZZZZZZZ: ${NAMES[(i + 3) % 4]}`; + assert(kept == "{ a: " + i + " }", "the function kept an object printed into a string"); + assert(third.length == 10 + NAMES[(i + 3) % 4].length, "the template after the object"); + + hud.show([1, 2]); + const fourth = `WWWWWWWW: ${NAMES[i % 4]}`; + assert(hud.message == "[ 1, 2 ]", "the method kept a literal array printed into a string"); + assert(fourth.length == 10 + NAMES[i % 4].length, "the template after the array"); + } + + print("done."); +}