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
12 changes: 12 additions & 0 deletions TSLANG_ISSUES.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,15 @@ the variable. 42 (`[data, this.data] = [this.data, data]` did not move a class i
every model, and `<T>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`.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <this>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.
Expand All @@ -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.
32 changes: 29 additions & 3 deletions tslang/lib/TypeScript/LowerToAffineLoops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,30 @@ struct ThisIndirectAccessorOpLowering : public TsPattern<mlir_ts::ThisIndirectAc
}
};

// The results of an indexer's `set`, which a class may declare as a method returning something
// (`set(key, value): this`): the call made for `obj[key] = value` has them too, unused, so that the
// call agrees with the function it calls.
static mlir::SmallVector<mlir::Type> indexSetterResults(mlir::Type setterType)
{
if (auto funcType = mlir::dyn_cast<mlir_ts::FunctionType>(setterType))
{
return llvm::to_vector(funcType.getResults());
}

if (auto boundFuncType = mlir::dyn_cast<mlir_ts::BoundFunctionType>(setterType))
{
return llvm::to_vector(boundFuncType.getResults());
}

return {};
}

static mlir::SmallVector<mlir::Type> indexSetterResults(mlir::Operation *from, mlir::FlatSymbolRefAttr setter)
{
auto funcOp = mlir::SymbolTable::lookupNearestSymbolFrom<mlir_ts::FuncOp>(from, setter);
return funcOp ? indexSetterResults(funcOp.getFunctionType()) : mlir::SmallVector<mlir::Type>{};
}

struct ThisIndexAccessorOpLowering : public TsPattern<mlir_ts::ThisIndexAccessorOp>
{
using TsPattern<mlir_ts::ThisIndexAccessorOp>::TsPattern;
Expand All @@ -1047,8 +1071,9 @@ struct ThisIndexAccessorOpLowering : public TsPattern<mlir_ts::ThisIndexAccessor
return mlir::failure();
}

auto setterResults = indexSetterResults(thisIndexAccessorOp, thisIndexAccessorOp.getSetAccessorAttr());
createCall<mlir_ts::CallOp>(rewriter, thisIndexAccessorOp, loc, thisIndexAccessorOp.getSetAccessor().value(),
mlir::TypeRange{}, mlir::ValueRange{thisIndexAccessorOp.getThisVal(),
mlir::TypeRange{setterResults}, mlir::ValueRange{thisIndexAccessorOp.getThisVal(),
thisIndexAccessorOp.getIndex(), thisIndexAccessorOp.getSetValue()});
}

Expand Down Expand Up @@ -1092,7 +1117,8 @@ struct ThisIndirectIndexAccessorOpLowering : public TsPattern<mlir_ts::ThisIndir
return mlir::failure();
}

createCall<mlir_ts::CallIndirectOp>(rewriter, thisIndirectIndexAccessorOp, loc, TypeRange{}, thisIndirectIndexAccessorOp.getSetAccessor(),
auto setterResults = indexSetterResults(thisIndirectIndexAccessorOp.getSetAccessor().getType());
createCall<mlir_ts::CallIndirectOp>(rewriter, thisIndirectIndexAccessorOp, loc, TypeRange{setterResults}, thisIndirectIndexAccessorOp.getSetAccessor(),
mlir::ValueRange{thisIndirectIndexAccessorOp.getThisVal(),
thisIndirectIndexAccessorOp.getIndex(), thisIndirectIndexAccessorOp.getSetValue()});
}
Expand Down Expand Up @@ -1209,7 +1235,7 @@ struct BoundIndirectIndexAccessorOpLowering : public TsPattern<mlir_ts::BoundInd
auto thisOp = rewriter.create<mlir_ts::GetThisOp>(loc, opaqueType, boundIndirectIndexAccessorOp.getSetAccessor());
auto methodOp = rewriter.create<mlir_ts::GetMethodOp>(loc, funcType, boundIndirectIndexAccessorOp.getSetAccessor());

createCall<mlir_ts::CallIndirectOp>(rewriter, boundIndirectIndexAccessorOp, loc, TypeRange{}, methodOp, mlir::ValueRange{thisOp,
createCall<mlir_ts::CallIndirectOp>(rewriter, boundIndirectIndexAccessorOp, loc, TypeRange{funcType.getResults()}, methodOp, mlir::ValueRange{thisOp,
boundIndirectIndexAccessorOp.getIndex(), boundIndirectIndexAccessorOp.getSetValue()});
}

Expand Down
6 changes: 4 additions & 2 deletions tslang/lib/TypeScript/OwnershipSignaturePass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 4 additions & 0 deletions tslang/lib/TypeScript/OwnershipSignaturePass.h
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,10 @@ class OwnershipSignaturePass : public mlir::PassWrapper<OwnershipSignaturePass,
// its `__own_no_drops`.
bool addImportedByAddress(mlir::Value callee, Callees &callees);

// `ts.Cast(ts.SearchForAddressOfSymbol(name))` of a constant name this module declares a
// function by: that function's symbol
mlir::StringAttr searchedSymbol(mlir::Operation *def);

Callees resolve(mlir::Operation *op);

// The function value a call goes through: a method of a class, virtual or not, a symbol, or
Expand Down
24 changes: 24 additions & 0 deletions tslang/lib/TypeScript/OwnershipSignatureResolve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,23 @@ namespace own_signature
return true;
}

mlir::StringAttr OwnershipSignaturePass::searchedSymbol(mlir::Operation *def)
{
auto castOp = mlir::dyn_cast_or_null<mlir_ts::CastOp>(def);
auto searchOp = castOp ? castOp.getIn().getDefiningOp<mlir_ts::SearchForAddressOfSymbolOp>()
: mlir_ts::SearchForAddressOfSymbolOp();
auto nameOp = searchOp ? searchOp->getOperand(0).getDefiningOp<mlir_ts::ConstantOp>() : mlir_ts::ConstantOp();
auto name = nameOp ? mlir::dyn_cast<mlir::StringAttr>(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;
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
78 changes: 78 additions & 0 deletions tslang/test/tester/tests/00class_indexer_set_result.ts
Original file line number Diff line number Diff line change
@@ -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.");
}
38 changes: 38 additions & 0 deletions tslang/test/tester/tests/export_class_keeps_indexer.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
17 changes: 17 additions & 0 deletions tslang/test/tester/tests/import_class_keeps_indexer.ts
Original file line number Diff line number Diff line change
@@ -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.");
}
Loading