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
71 changes: 71 additions & 0 deletions TSLANG_ISSUES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4006,3 +4006,74 @@ Not yet:
as a value; design questions without a ruling: overloads of a declared function (136) and class constructor (140).

Tests `00tslang_issue_214.ts` (all models).

215. Leftovers of 214 (2026-11-27). Fixed:
- `Record<string, T>` was an object with no fields in every model ("mapped type is empty for constrain: string"): the
literal given to it lost its fields, `Object.keys` gave none and `r["a"]` read nothing (131). It is `{ [key: string]:
T }`, the dictionary of T a string index signature is, both as the built-in Record (no default library) and as the
mapped type `{ [P in K]: T }` of K = string (the default library's Record). Without the dictionary class, as before.
Such a dictionary as a function's result type has its members generated where it is read (`Object.keys(f())` was
"'ObjectDictionary<!ts.number>.keys' does not reference a valid function": the alias is read once, in an early pass);
as a constraint (`T extends Record<string, X>`) an object of any fields meets it, as `{}` did; and `keyof` of one is
`string`, not the members of the class that holds it (the tsc sweep's 7 tests of these, failing with the change
alone, pass).
- A union of tuples cast to one of them (`<[number, string, string]>x` of `[number, string] | [number, string,
string]`) failed with nothing said ("failed statement"): each member is tested for and cast, and the shorter one
could not be made the longer - its cast read past its end. The member the value holds is cast, and each element a
shorter member lacks is filled (USER RULING 2026-11-27; TypeScript reads undefined there): "" for a string under
strict null checks and null without them, 0 for a number, false for a boolean, undefined where the type admits it
(`T | undefined`, `T?`, `any`), and null for a class, an interface or an array without strict null checks. One whose
type has nothing to fill it with - an object literal's, which is a value, or a class's, an interface's or an array's
under strict null checks - makes the cast a compile error ("can't cast '[number]' to '[number, number[]]': it has no
element 1 of 'number[]', and nothing fills it under strict null checks"). Only for tuples of elements: a union of
object shapes cast to a shape one member lacks a field of stays the compile error it was. A tuple cast to one with
more elements (not of a union) is a compile error that says so (tsc refuses it too).
- `x[1]` of such a union, "access expression is not applicable": an element at a constant index every member has,
after the same element types, is read where the first member has it.
- A member of Array of a union of tuples (`x.slice(1)`, `x.join(",")`, `forEach`, `map`) read the value as the first
member, in every model: a `[0, "c"]` held by `[number, string] | [number, string, string]` joined "0,c,null". It is
the array of the member the value holds, of the union of all members' element types. So is `[...x]` (it was "can't
estimate element of array").
- Under -mm=own a tuple read from a variable made an array (`t.slice(1)`, `t.forEach(f)` of `t: [number, string]`)
was refused ("an element ... would have two owners as an element of an array"): its strings are copied for the array,
as a tuple cast to another copies them (211). Another element owning memory stays refused.
- A union of object shapes or tuples cast to one of its members boxed the union in an `any` to read its tag, and
nothing gave the box back: about 40 bytes a cast under rc and -mm=own. The union's own tag is read.
- A union of tuples iterated (`for (const e of x)`) or read at a run-time index (`x[i]`), "access expression is not
applicable": the elements of the member it holds, and the element of that member (each member tested for, as a
cast is). `x[1] = v` stays refused, and says why: the element is read out of the member the union holds, a copy.
- Under -mm=rc an array made for a call and given as `this` to a method (`([i, s] as (number | string)[]).slice(1)`,
`t.slice(1)` of a tuple, `[...x]` of a union of tuples) was never given back: about 100 bytes a call (with the
compiler of c2a88259d too). Born uncounted, it gets the reference a new string has (markFreshBlockOwned) and is given
back where nothing takes it.
- Under -mm=own a spread literal given to a call (`cnt(...xs)`, the default library's `join`: `"".concat(...parts)`)
was never given back: about 100 bytes a call (with the compiler of c2a88259d too). It is given back as a literal
given to a call is (ownNewArrayArguments), with copies of the text pushed into it; one holding an object read from
elsewhere stays as it was.
- Under -mm=own no result of a default-library function was ever given back, held in a local or not
(`s.toUpperCase()` 16 bytes a call, `s.split("b")` 107, `(1.5).toFixed(2)` 64, `JSON.parse(t)` about 800; with the
compiler of c2a88259d too; rc freed them all): own did not read `@owned`, the declaration's word that its result is
the caller's (USER DECISION 2026-11-27, reversing 2026-11-18's rc only: what rc reads, own reads the same way, the two
models being alike). It does now, except where a declaration says it lends its result (`@lends`, or a result type
`this`: `JsonValue.get`, `ArrayBuffer.__bytes`), which own goes by - under rc that result is the caller's all the same.
Own's inference takes such a result for the caller's own too (OWN_FRESH_RESULT): a new value, not a part of what the
call was given - else `value.get("k").asString()` read the string's release, after `value`'s, as a use of the borrow
`get` lends. Each of those probes is flat under own, with the default library built by this compiler.

Not yet:
- Under -mm=own a union of tuples cast to a longer member is read where it is cast or held in a local (`const l =
<Long>x`), but a function can't return it whole (`return <Long>x`): "a value is merged from several branches". Each
branch borrows `x` - the member that is the target, or the shorter one filled - and own proves a merge of new values,
not a merge of borrows of one value.
- Under -mm=own a dictionary written in a loop over an array it is not (`for (const it of items) out[it] = 1`):
"'it' borrows an element but is used here after it may be released", of `Record<string, number>` and of `{ [k:
string]: number }` alike. The dictionary's `set` is known only to drop strings held anywhere (its drop facts are
`[-1, string]`), so any string read out of an array is taken for freed by it; facts naming what is under `this` would
tell `items` apart.
- Still from 214: 128 under own (a capture by value for value types), own `Constructor<T>` of `...args: any[]`; from
213: 56, 153, 159 under own; 131 (`Object.keys` of `object`, `Object.assign`, `defineProperty`, `create`), 108, 191's
callable interfaces, recursive conditional types, `Symbol` as a value; design questions without a ruling: overloads
of a declared function (136) and of a `declare class` constructor (140).

Tests `00tslang_issue_215.ts` (all models), `test-{rc,own}-owned-declarations` (the IR of `@owned` and `@lends` calls),
`tuple_union_cast_fill_error.ts`, `tuple_union_cast_fill_nonstrict.ts` (`--strict-null-checks=false`, all models).
6 changes: 4 additions & 2 deletions tslang/include/TypeScript/Defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -510,8 +510,10 @@
// it returns a reference its caller owns (§9.24), as an imported `export`ed function does. An
// unmarked declaration may be C, or TypeScript that hands back a pointer into a buffer
// (`convertNumber`): nothing can be said of its result, and its callers keep retaining. Said on a
// function with a body it is ignored - a body is read, not trusted. Only rc reads it: -mm=own goes by
// its declared facts (`@lends`, `@record`). A unit attribute on the FuncOp.
// function with a body it is ignored - a body is read, not trusted. rc and own read it alike (own: its
// result is the caller's to own, as a call of a function here that returns a new value is), except
// where a declaration says it lends its result (`@lends`, or a result type `this`): that is own's to
// go by. A unit attribute on the FuncOp.
// An array literal with a spread of run-time length (`[...xs]`, and the rest argument `f(...xs)` packs)
// is filled through a slot of its own, and what MLIRGen hands on is a read of that slot: this unit
// attribute on the `ts.Load` says it is the new array, born uncounted as a `ts.CreateArray` is. Under rc
Expand Down
122 changes: 120 additions & 2 deletions tslang/lib/TypeScript/MLIRGenAccessCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,21 @@ namespace mlirgen
return V(fieldRead);
}

// a member of Array reading a union of tuples (`x.slice(1)`): of the array of the member it holds
if (isArrayReadMember(name))
{
auto arrayValue = mlirGenUnionOfTuplesAsArray(location, objectValue, unionType, genContext);
if (arrayValue.failed())
{
return mlir::Value();
}

if (arrayValue.value)
{
return mlirGenPropertyAccessExpression(location, V(arrayValue), name, false, genContext);
}
}

// the same field in every member (or a method, dispatched where it is called): read as the first
auto frontType = mth.getFirstNonNullUnionType(unionType);
//auto casted = cast(location, frontType, objectValue, genContext);
Expand Down Expand Up @@ -704,6 +719,27 @@ namespace mlirgen

LLVM_DEBUG(llvm::dbgs() << "!! this type of function is : " << thisTypeFromFunc << "\n";);

// -mm=rc: a new array a method is called on (`[a, b].slice(1)`, `t.slice(1)` of a tuple made an array) is born
// uncounted, and nothing gave it back: about 100 bytes a call. It gets the standing a new string has
// (markFreshBlockOwned): a reference of its own, marked so, given back where nothing takes it (§9.30)
if (compileOptions.isRefCounted() && isa<mlir_ts::ArrayType>(thisValue.getType()))
{
auto source = thisValue;
while (auto castOp = source.getDefiningOp<mlir_ts::CastOp>())
{
source = castOp.getIn();
}

auto *definingOp = source.getDefiningOp();
auto isNewArray = isa_and_nonnull<mlir_ts::CreateArrayOp>(definingOp)
|| (isa_and_nonnull<mlir_ts::LoadOp>(definingOp) && definingOp->hasAttr(SPREAD_ARRAY_LITERAL_ATTR_NAME));
if (isNewArray && !definingOp->hasAttr(OWNED_RESULT_ATTR_NAME) && isa<mlir_ts::ArrayType>(source.getType()))
{
builder.create<mlir_ts::RetainOp>(location, source);
definingOp->setAttr(OWNED_RESULT_ATTR_NAME, builder.getUnitAttr());
}
}

// a key of an `any` is read at run time: a function of the same name found in some namespace is no member
// of it unless it takes an `any` (`foo.get = class {}` with `foo: any` found the library's generic
// `Reflect.get<T, K>(target: T, ...)`, whose `T` takes anything, and assigned to it)
Expand Down Expand Up @@ -1763,6 +1799,60 @@ namespace mlirgen
return lookup(0);
}

// The first member of a union of tuples, all of elements only, when each has the element at the constant index
// `index` holds and the same element types up to it: one laid out alike there, whichever member a value holds.
// No type otherwise.
mlir::Type MLIRGenImpl::unionTuplesCommonPrefixMember(mlir_ts::UnionType unionType, mlir::Value index)
{
auto constantOp = index.getDefiningOp<mlir_ts::ConstantOp>();
if (!constantOp)
{
return mlir::Type();
}

int64_t position = -1;
if (auto intAttr = dyn_cast<mlir::IntegerAttr>(constantOp.getValue()))
{
position = intAttr.getValue().getSExtValue();
}
else if (auto floatAttr = dyn_cast<mlir::FloatAttr>(constantOp.getValue());
floatAttr && floatAttr.getValueAsDouble() == (double)(int64_t)floatAttr.getValueAsDouble())
{
position = (int64_t)floatAttr.getValueAsDouble();
}

if (position < 0)
{
return mlir::Type();
}

SmallVector<mlir_ts::TupleType> tuples;
for (auto member : unionType.getTypes())
{
auto tupleType = dyn_cast<mlir_ts::TupleType>(mth.convertConstTupleTypeToTupleType(member));
if (!tupleType || (int64_t)tupleType.size() <= position
|| llvm::any_of(tupleType.getFields(), [](auto &field) { return !!field.id; }))
{
return mlir::Type();
}

tuples.push_back(tupleType);
}

for (auto tupleType : ArrayRef<mlir_ts::TupleType>(tuples).drop_front())
{
for (int64_t i = 0; i <= position; i++)
{
if (tupleType.getFieldInfo(i).type != tuples.front().getFieldInfo(i).type)
{
return mlir::Type();
}
}
}

return unionType.getTypes().front();
}

ValueOrLogicalResult MLIRGenImpl::mlirGenElementAccess(mlir::Location location, mlir::Value expression, mlir::Value argumentExpression, bool isConditionalAccess, const GenContext &genContext)
{
auto arrayType = expression.getType();
Expand Down Expand Up @@ -1792,6 +1882,20 @@ namespace mlirgen
expression = builder.create<mlir_ts::CastOp>(location, baseType, expression);
}
}
// `x[1]` of a union of tuples (`[number, string] | [number, string, string]`), each holding that element after
// the same elements: it is where the first member has it, whichever member the value holds (it was "access
// expression is not applicable")
else if (auto firstTuple = unionTuplesCommonPrefixMember(unionType, argumentExpression))
{
expression = builder.create<mlir_ts::GetValueFromUnionOp>(location, firstTuple, expression);
arrayType = firstTuple;
}
// `x[i]` of a run-time index: the element of the member the value holds (a value, as above: not a place
// to assign to)
else if (auto element = mlirGenUnionOfTuplesElementAt(location, expression, unionType, argumentExpression, genContext))
{
return *element;
}
}

if (isa<mlir_ts::LiteralType>(arrayType))
Expand Down Expand Up @@ -4119,12 +4223,21 @@ namespace mlirgen
mlir::Type MLIRGenImpl::instantiatedDictionaryType(mlir::Location location, mlir::Type type, const GenContext &genContext)
{
auto classInfo = dictionaryClassInfoOf(type, genContext);
if (!classInfo || classInfo->typeParamsWithArgs.size() != 1)
auto genericClassInfo = classInfo ? getGenericClassInfoByFullName(OBJECT_DICTIONARY_CLASS_NAME) : GenericClassInfo::TypePtr();
if (!genericClassInfo || genericClassInfo->typeParams.size() != 1)
{
return type;
}

auto valueType = std::get<1>(classInfo->typeParamsWithArgs.begin()->getValue());
// its value type, by the name of the class's type parameter: one instantiated inside a generic alias
// (`Record<string, T>`, the mapped type `{ [P in K]: T }`) carries the alias's type arguments as well
auto found = classInfo->typeParamsWithArgs.find(genericClassInfo->typeParams.front()->getName());
if (found == classInfo->typeParamsWithArgs.end())
{
return type;
}

auto valueType = std::get<1>(found->getValue());
auto instantiated = getObjectDictionaryType(location, valueType, genContext);
return instantiated ? instantiated : type;
}
Expand Down Expand Up @@ -4191,6 +4304,11 @@ namespace mlirgen
ValueOrLogicalResult MLIRGenImpl::mlirGenDictionaryCall(mlir::Location location, mlir::Value dictionary, StringRef method,
SmallVector<mlir::Value, 4> &operands, const GenContext &genContext)
{
// a dictionary whose type was found in an earlier pass (`f(): Record<string, T>`: the alias is read once) may
// have had its members generated nowhere: instantiated here, as for an entry read ("'ts.Call' op
// 'ObjectDictionary<!ts.number>.keys' does not reference a valid function" of `Object.keys(f())`)
instantiatedDictionaryType(location, dictionary.getType(), genContext);

auto methodResult = mlirGenPropertyAccessExpression(location, dictionary, method, genContext);
EXIT_IF_FAILED_OR_NO_VALUE(methodResult)
return mlirGenCall(location, V(methodResult), operands, genContext);
Expand Down
Loading
Loading