From 6afb370c973ce97fc30a6a41060682ed5c4fa600 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Mon, 9 Nov 2026 20:35:44 +0000 Subject: [PATCH] An indexer's set: an imported class's gives up what it keeps, and the call has set's result (TSLANG 43) - -mm=own: `obj[key] = value` on a class imported from a library calls `set` through `Cast(SearchForAddressOfSymbol("C.set"))`; the signature pass now resolves that callee to the declared function, so its @keeps/@lends apply and the caller no longer frees what `set` kept (double free, hang at exit). - Every model: the call lowered for an indexer's `set` had no result even when `set` returns one (`JsonValue.set(...): this`), so it disagreed with its callee and LLVM lowering crashed now and then. It now has the setter's results, unused. - -mm=own: a method returning `this` that declares `@keeps` keeps the `@lends("this")` its result type implies, instead of being told to say `@lends`. Tests: import_class_keeps_indexer.ts/export_class_keeps_indexer.ts (gc, rc, own; compiled and JIT), 00class_indexer_set_result.ts. Spec 78 note, spec 79. Co-Authored-By: Claude Opus 5.5 --- TSLANG_ISSUES.md | 12 +++ .../2026-09-24-own-memory-model-design.md | 16 +++- tslang/lib/TypeScript/LowerToAffineLoops.cpp | 32 +++++++- .../lib/TypeScript/OwnershipSignaturePass.cpp | 6 +- .../lib/TypeScript/OwnershipSignaturePass.h | 4 + .../TypeScript/OwnershipSignatureResolve.cpp | 24 ++++++ tslang/test/tester/CMakeLists.txt | 7 ++ .../tests/00class_indexer_set_result.ts | 78 +++++++++++++++++++ .../tests/export_class_keeps_indexer.ts | 38 +++++++++ .../tests/import_class_keeps_indexer.ts | 17 ++++ 10 files changed, 228 insertions(+), 6 deletions(-) create mode 100644 tslang/test/tester/tests/00class_indexer_set_result.ts create mode 100644 tslang/test/tester/tests/export_class_keeps_indexer.ts create mode 100644 tslang/test/tester/tests/import_class_keeps_indexer.ts diff --git a/TSLANG_ISSUES.md b/TSLANG_ISSUES.md index 6e1b72555..b6aa95a89 100644 --- a/TSLANG_ISSUES.md +++ b/TSLANG_ISSUES.md @@ -95,3 +95,15 @@ the variable. 42 (`[data, this.data] = [this.data, data]` did not move a class i every model, and `data` of such a local is a view of it. Regression tests `tslang/test/tester/own/own_chain_returns_this.ts`, `own_closure_keeps_param.ts`, `own_swap_local_field.ts`, `own_err_this_result_other.ts` and `own_err_closure_kept_param_used.ts`. + +Item 43, found on 2026-11-09 while giving the default library's `JsonValue` an indexer +(TypeScriptCompilerDefaultLib PR #86), is fixed. 43 (`bag[key] = value` on a class imported from a +library, whose indexer's `set` is `@keeps("value")`: the caller freed the value `set` kept, so it was +freed twice and the program hung at exit under `-mm=own`): the indexer's accessors of such a class are +looked up by name, and the ownership passes now take that call for a call of `set`, with its declared +`@keeps` and `@lends`. The crash of tslang.exe with `JsonValue`'s indexer was another bug, under every +model: the call made for `obj[key] = value` had no result even when `set` returns one, as `JsonValue.set` +does, so it disagreed with the function it calls and lowering to LLVM crashed now and then; it has the +setter's result now. Also, a method returning `this` that says what it keeps (`@keeps`) is no longer +told to say `@lends` too. Regression tests `tslang/test/tester/tests/import_class_keeps_indexer.ts` +(with `export_class_keeps_indexer.ts`) and `00class_indexer_set_result.ts`. diff --git a/tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md b/tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md index 0e4d02a15..299c6aeb1 100644 --- a/tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md +++ b/tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md @@ -5699,7 +5699,8 @@ a method typed with its class that returns `this` has it; families must agree on **Saying it.** A method whose result type is `this` says it: MLIRGen marks it (`ts.own_declared_returns_this`) and implies `@lends("this")` (`ts.own_implied_lends`). A function only this module calls drops the implied `@lends` (its callers go by its body; a declared one would want -every other fact declared too). A body is held to it: one that returns another object of the type +every other fact declared too), unless it says what it keeps (`@keeps`): it then declares its callers' +side, and the implied `@lends` is part of it. A body is held to it: one that returns another object of the type (`clone(): this { return new A() }`, valid TypeScript) is an error under own. A declaration (`lib.d.ts`) has no body, so its `: this` is what callers know, and the `-shared` declaration printer writes `: this` for such a method. There is no decorator: `: this` is the one way. @@ -5709,3 +5710,16 @@ call sees the one before it): a call with the fact whose argument is fresh, that keep, and that nothing uses after the call but its releases in the same block, loses those releases, and its result is `__own_fresh_result` - the temporary, now the result's to release. On an object someone owns the result stays a borrow, bounded by it. + +## 79. An indexer of a class from a library, 2026-11-09 + +`obj[key] = value` on a class with an indexer calls its `set`. For a class imported from a library +(`isDynamicImport`) the accessor is looked up by name: the call goes through +`ts.Cast(ts.SearchForAddressOfSymbol("C.set"))`, which the signature pass did not resolve, so the call +had no facts and the caller freed what a `@keeps` `set` kept (TSLANG_ISSUES 43: a double free, a hang +at exit). `searchedSymbol` resolves such a callee to the function the module declares by that name, +and its declared `@keeps` and `@lends` apply, as to `obj.set(key, value)`. + +Found with it, under every model: the call lowered for an indexer's `set` had no result, also when +`set` returns one (`set(key, value): this`), so the call disagreed with the function it calls and the +lowering to LLVM crashed now and then. It has the setter's results now, unused. diff --git a/tslang/lib/TypeScript/LowerToAffineLoops.cpp b/tslang/lib/TypeScript/LowerToAffineLoops.cpp index 1254c9f10..f88e91b26 100644 --- a/tslang/lib/TypeScript/LowerToAffineLoops.cpp +++ b/tslang/lib/TypeScript/LowerToAffineLoops.cpp @@ -1031,6 +1031,30 @@ struct ThisIndirectAccessorOpLowering : public TsPattern indexSetterResults(mlir::Type setterType) +{ + if (auto funcType = mlir::dyn_cast(setterType)) + { + return llvm::to_vector(funcType.getResults()); + } + + if (auto boundFuncType = mlir::dyn_cast(setterType)) + { + return llvm::to_vector(boundFuncType.getResults()); + } + + return {}; +} + +static mlir::SmallVector indexSetterResults(mlir::Operation *from, mlir::FlatSymbolRefAttr setter) +{ + auto funcOp = mlir::SymbolTable::lookupNearestSymbolFrom(from, setter); + return funcOp ? indexSetterResults(funcOp.getFunctionType()) : mlir::SmallVector{}; +} + struct ThisIndexAccessorOpLowering : public TsPattern { using TsPattern::TsPattern; @@ -1047,8 +1071,9 @@ struct ThisIndexAccessorOpLowering : public TsPattern(rewriter, thisIndexAccessorOp, loc, thisIndexAccessorOp.getSetAccessor().value(), - mlir::TypeRange{}, mlir::ValueRange{thisIndexAccessorOp.getThisVal(), + mlir::TypeRange{setterResults}, mlir::ValueRange{thisIndexAccessorOp.getThisVal(), thisIndexAccessorOp.getIndex(), thisIndexAccessorOp.getSetValue()}); } @@ -1092,7 +1117,8 @@ struct ThisIndirectIndexAccessorOpLowering : public TsPattern(rewriter, thisIndirectIndexAccessorOp, loc, TypeRange{}, thisIndirectIndexAccessorOp.getSetAccessor(), + auto setterResults = indexSetterResults(thisIndirectIndexAccessorOp.getSetAccessor().getType()); + createCall(rewriter, thisIndirectIndexAccessorOp, loc, TypeRange{setterResults}, thisIndirectIndexAccessorOp.getSetAccessor(), mlir::ValueRange{thisIndirectIndexAccessorOp.getThisVal(), thisIndirectIndexAccessorOp.getIndex(), thisIndirectIndexAccessorOp.getSetValue()}); } @@ -1209,7 +1235,7 @@ struct BoundIndirectIndexAccessorOpLowering : public TsPattern(loc, opaqueType, boundIndirectIndexAccessorOp.getSetAccessor()); auto methodOp = rewriter.create(loc, funcType, boundIndirectIndexAccessorOp.getSetAccessor()); - createCall(rewriter, boundIndirectIndexAccessorOp, loc, TypeRange{}, methodOp, mlir::ValueRange{thisOp, + createCall(rewriter, boundIndirectIndexAccessorOp, loc, TypeRange{funcType.getResults()}, methodOp, mlir::ValueRange{thisOp, boundIndirectIndexAccessorOp.getIndex(), boundIndirectIndexAccessorOp.getSetValue()}); } diff --git a/tslang/lib/TypeScript/OwnershipSignaturePass.cpp b/tslang/lib/TypeScript/OwnershipSignaturePass.cpp index 38977b138..5fcaac7b8 100644 --- a/tslang/lib/TypeScript/OwnershipSignaturePass.cpp +++ b/tslang/lib/TypeScript/OwnershipSignaturePass.cpp @@ -185,10 +185,12 @@ namespace own_signature findOpen(module); // the `@lends("this")` a result type `this` implies, on a function only this module calls: its - // callers go by its body, as for any other function here (spec 78) + // callers go by its body, as for any other function here (spec 78). Not when the function says + // what it keeps (`@keeps`): it then says what its callers give and get, and `this` is part of it. for (auto &entry : functions) { - if (entry.second->hasAttr(OWN_IMPLIED_LENDS_ATTR_NAME) && !entry.second.isDeclaration() && !open.contains(entry.second)) + if (entry.second->hasAttr(OWN_IMPLIED_LENDS_ATTR_NAME) && !entry.second.isDeclaration() && + !open.contains(entry.second) && !entry.second->hasAttr(OWN_DECLARED_KEEPS_ATTR_NAME)) { entry.second->removeAttr(OWN_DECLARED_LENDS_ATTR_NAME); } diff --git a/tslang/lib/TypeScript/OwnershipSignaturePass.h b/tslang/lib/TypeScript/OwnershipSignaturePass.h index 2be51893f..ede209991 100644 --- a/tslang/lib/TypeScript/OwnershipSignaturePass.h +++ b/tslang/lib/TypeScript/OwnershipSignaturePass.h @@ -675,6 +675,10 @@ class OwnershipSignaturePass : public mlir::PassWrapper(def); + auto searchOp = castOp ? castOp.getIn().getDefiningOp() + : mlir_ts::SearchForAddressOfSymbolOp(); + auto nameOp = searchOp ? searchOp->getOperand(0).getDefiningOp() : mlir_ts::ConstantOp(); + auto name = nameOp ? mlir::dyn_cast(nameOp.getValue()) : mlir::StringAttr(); + if (!name) + { + return {}; + } + + // only a function this module declares: its type is the one the call is made with + auto funcOp = functions.lookup(name.getValue()); + return funcOp ? funcOp.getSymNameAttr() : mlir::StringAttr(); + } + OwnershipSignaturePass::Callees OwnershipSignaturePass::resolve(mlir::Operation *op) { Callees callees; @@ -338,6 +355,13 @@ namespace own_signature { identifier = refOp.getIdentifierAttr().getAttr(); } + else if (auto symbol = searchedSymbol(def)) + { + // `ts.Cast(ts.SearchForAddressOfSymbol("C.set"))`: the indexer of a class imported from + // a library, whose accessors are looked up by name (issue 43); that function, as a call + // of `C.set` through its object + identifier = symbol; + } else if (isInstanceOfSlot(def)) { callees.known = true; diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 010d7608e..f4bc9213d 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -667,6 +667,7 @@ tslang_add_test(NAME test-compile-00-class-indexer COMMAND test-runner "${PROJEC tslang_add_test(NAME test-compile-00-class-indexer-super COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_indexer_super.ts") tslang_add_test(NAME test-compile-00-class-indexer-string-key COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_indexer_string_key.ts") tslang_add_test(NAME test-compile-00-class-indexer-union-key COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_indexer_union_key.ts") +tslang_add_test(NAME test-compile-00-class-indexer-set-result COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_indexer_set_result.ts") tslang_add_test(NAME test-compile-00-class-from-tuple COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_from_tuple.ts") tslang_add_test(NAME test-compile-00-class-access-control COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_access_control.ts") tslang_add_test(NAME test-compile-00-class-protected-constructor COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_protected_constr.ts") @@ -1341,6 +1342,7 @@ tslang_add_test(NAME test-jit-00-class-indexer COMMAND test-runner -jit "${PROJE tslang_add_test(NAME test-jit-00-class-indexer-super COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_indexer_super.ts") tslang_add_test(NAME test-jit-00-class-indexer-string-key COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_indexer_string_key.ts") tslang_add_test(NAME test-jit-00-class-indexer-union-key COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_indexer_union_key.ts") +tslang_add_test(NAME test-jit-00-class-indexer-set-result COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_indexer_set_result.ts") tslang_add_test(NAME test-jit-00-class-from-tuple COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_from_tuple.ts") tslang_add_test(NAME test-jit-00-class-access-control COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_access_control.ts") tslang_add_test(NAME test-jit-00-class-protected-constructor COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_protected_constr.ts") @@ -1786,9 +1788,13 @@ endforeach() # library's boundary under gc, rc and own tslang_add_test(NAME test-jit-00-class-indexer-string-key-own COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_indexer_string_key.ts") tslang_add_test(NAME test-jit-00-class-indexer-union-key-own COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_indexer_union_key.ts") +tslang_add_test(NAME test-jit-00-class-indexer-set-result-own COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_indexer_set_result.ts") foreach(string_indexer_mm gc rc own) tslang_add_test(NAME test-compile-shared-export-import-class-string-indexer-${string_indexer_mm} COMMAND test-runner -shared -mm=${string_indexer_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/import_class_string_indexer.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_class_string_indexer.ts") tslang_add_test(NAME test-jit-shared-export-import-class-string-indexer-${string_indexer_mm} COMMAND test-runner -jit -shared -mm=${string_indexer_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/import_class_string_indexer.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_class_string_indexer.ts") + # an indexer whose `set` keeps its value (TSLANG_ISSUES 43): the importer gives the value up + tslang_add_test(NAME test-compile-shared-export-import-class-keeps-indexer-${string_indexer_mm} COMMAND test-runner -shared -mm=${string_indexer_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/import_class_keeps_indexer.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_class_keeps_indexer.ts") + tslang_add_test(NAME test-jit-shared-export-import-class-keeps-indexer-${string_indexer_mm} COMMAND test-runner -jit -shared -mm=${string_indexer_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/import_class_keeps_indexer.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_class_keeps_indexer.ts") endforeach() # ...and of @cpp classes implemented here (their vtables C++'s), under every memory model tslang_add_test(NAME test-compile-00-cpp-class-values COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00cpp_class_values.ts") @@ -2491,6 +2497,7 @@ set(TSLANG_CORPUS 00class_indexer_super.ts 00class_indexer_string_key.ts 00class_indexer_union_key.ts + 00class_indexer_set_result.ts 00class_indexer.ts 00class_iterator_super.ts 00class_iterator.ts diff --git a/tslang/test/tester/tests/00class_indexer_set_result.ts b/tslang/test/tester/tests/00class_indexer_set_result.ts new file mode 100644 index 000000000..65d2a4a6f --- /dev/null +++ b/tslang/test/tester/tests/00class_indexer_set_result.ts @@ -0,0 +1,78 @@ +// An indexer whose `set` is a method that returns something (TSLANG_ISSUES 43): `obj[key] = value` +// calls it and drops the result. The call was made with no result, unlike the function it calls, +// which crashed the compiler now and then while lowering to LLVM. +class Item { + constructor(public n: number) {} +} + +class Chain { + private keys: string[] = []; + private values: Item[] = []; + + [key: string]: Item; + + @lends("this") + get(key: string): Item { + for (let i = 0; i < this.keys.length; i++) { + if (this.keys[i] == key) { + return this.values[i]; + } + } + + return this.values[0]; + } + + // returns the object it is called on + @keeps("value") + set(key: string, value: Item): this { + this.keys.push(key + ""); + this.values.push(value); + return this; + } + + get size() { + return this.keys.length; + } +} + +class Counted { + private values: number[] = []; + + [index: number]: number; + + get(index: number): number { + return this.values[index]; + } + + // returns a value of its own + set(index: number, value: number): number { + while (this.values.length <= index) { + this.values.push(0); + } + + const previous = this.values[index]; + this.values[index] = value; + return previous; + } +} + +function main() { + const chain = new Chain(); + let a = "a"; + let b = "b"; + chain[a] = new Item(1); + chain[b] = new Item(2); + chain.set("c", new Item(3)).set("d", new Item(4)); + assert(chain.size == 4); + assert(chain[a].n == 1); + assert(chain[b].n == 2); + assert(chain["d"].n == 4); + + const counted = new Counted(); + counted[2] = 5; + counted[2] = 7; + assert(counted[2] == 7); + assert(counted[0] == 0); + + print("done."); +} diff --git a/tslang/test/tester/tests/export_class_keeps_indexer.ts b/tslang/test/tester/tests/export_class_keeps_indexer.ts new file mode 100644 index 000000000..6eb7e1bba --- /dev/null +++ b/tslang/test/tester/tests/export_class_keeps_indexer.ts @@ -0,0 +1,38 @@ +// A class whose indexer's `set` keeps the value it is given (issue 43): imported from a library, +// `bag[key] = value` hands the value to `set` as `bag.set(key, value)` does, so under -mm=own the +// caller doesn't free it too. +namespace M { + export class Item { + constructor(public n: number) {} + } + + export class Bag { + private keys: string[] = []; + private values: Item[] = []; + + [key: string]: Item; + + @lends("this") + get(key: string): Item { + for (let i = 0; i < this.keys.length; i++) { + if (this.keys[i] == key) { + return this.values[i]; + } + } + + return this.values[0]; + } + + @lends("this") + @keeps("value") + set(key: string, value: Item): Bag { + this.keys.push(key + ""); + this.values.push(value); + return this; + } + + get size() { + return this.keys.length; + } + } +} diff --git a/tslang/test/tester/tests/import_class_keeps_indexer.ts b/tslang/test/tester/tests/import_class_keeps_indexer.ts new file mode 100644 index 000000000..359df046a --- /dev/null +++ b/tslang/test/tester/tests/import_class_keeps_indexer.ts @@ -0,0 +1,17 @@ +import './export_class_keeps_indexer' + +function main() { + const bag = new M.Bag(); + let a = "a"; + let b = "b"; + bag[a] = new M.Item(1); + assert(bag[a].n == 1); + bag[b] = new M.Item(2); + bag.set("c", new M.Item(3)); + + assert(bag.size == 3); + assert(bag[b].n == 2); + assert(bag["c"].n == 3); + + print("done."); +}