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
21 changes: 21 additions & 0 deletions TSLANG_ISSUES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3072,3 +3072,24 @@ Looked at, still open:

Tests `00arguments_length_given.ts` (gc, rc, own), `00generic_arrow_through_generic.ts` (gc, rc),
`00generator_yield_star_throw.ts` (gc, rc).

184 (183's leftovers, 2026-11-24). Fixed:
- A function whose parameters have different types given to `(...args: A) => T` (`call2((a: number, b: string) =>
b + a, 1, "x")`): `A` is inferred as the tuple of them, `[number, string]`, as tsc infers it (tryInferFunction),
and the arguments given for `...args: A` are taken as that tuple, not inferred again as an array of their union. A
tuple of unnamed elements extends an array whose element each element extends (`[number, string] extends
unknown[]`, extendsType). The adapters read the function's parameters out of a rest tuple as out of a rest array
(restArrayOf, expandRestArguments). Parameters of one type are an array, as before. Works in every model for a
function or an arrow given directly.
- -mm=own: throw() of a generator paused in a `yield*` of a generator made there (`yield* inner()`) is handed to the
delegate's throw() (183 for the other models), for elements that own nothing (a number, a boolean), as the
generator's return() under -mm=own. The delegate is exchanged into a local of next() for the call and back, and
what was thrown is exchanged out of the generator's field into a local first: given from the field, the call was
taken for one that may overwrite the field it borrows.

Looked at, still open:
- -mm=own: throw() at a `yield*` of strings or objects throws at the yield, as before: the step's result would be one
of two calls' ("a value is merged from several branches"). And a delegate named by a parameter (`yield* it`).
- A delegate whose type has `throw` but not `return` is not handed the exception (the library's Generator has both).

Tests `00generic_arrow_through_generic.ts` (gc, rc; tuple cases added), `00generator_yield_star_throw.ts` (own added).
1 change: 1 addition & 0 deletions docs/whats-new.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# What's new

- A function with parameters of different types can be given to `(...args: A) => T` (`A` is inferred as a tuple, `[number, string]`); under -mm=own, throw() of a generator paused in a `yield*` of numbers is handed to the delegate too
- `arguments.length` counts the arguments given, not the parameters declared; a generic arrow can be given to a generic parameter (`wrap(<T>(x: T) => x)`), and a function to `(...args: A) => T`; throw() of a generator paused in a `yield*` is handed to the delegate's throw(), which may catch it and go on yielding
- -mm=own: `for (const s of this.items) yield s` works in a generator method: the field is read again at each step (a field assigned during the loop is followed, unlike JavaScript's for-of)
- -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
Expand Down
21 changes: 21 additions & 0 deletions tslang/include/TypeScript/MLIRLogic/MLIRTypeHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -3544,6 +3544,27 @@ class MLIRTypeHelper
}
}

// a tuple (`[number, string]`, its elements unnamed - not an object's fields) extends an array whose element
// each of its elements extends (`A extends unknown[]` inferred as a tuple, TSLANG 184)
if (auto srcTuple = dyn_cast<mlir_ts::TupleType>(srcType))
{
auto extArray = dyn_cast<mlir_ts::ArrayType>(extendType);
if (extArray && srcTuple.size() > 0
&& llvm::all_of(srcTuple.getFields(), [](mlir_ts::FieldInfo field) { return !field.id; }))
{
for (auto field : srcTuple.getFields())
{
auto result = extendsType(location, field.type, extArray.getElementType(), typeParamsWithArgs);
if (result != ExtendsResult::True)
{
return result;
}
}

return ExtendsResult::True;
}
}

// Special case when we have string type (widen from Literal Type)
if (auto literalType = dyn_cast<mlir_ts::LiteralType>(extendType))
{
Expand Down
70 changes: 50 additions & 20 deletions tslang/lib/TypeScript/MLIRGenCast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3529,26 +3529,50 @@ namespace mlirgen
return !(sameResults && srcInputs.size() == destInputs.size());
}

// The array a function type's rest parameter is (`(...args: A) => T` with A `number[]`), null where it has none
static mlir_ts::ArrayType restArrayOf(mlir_ts::FunctionType funcType)
// What a function type's rest parameter is, an array (`(...args: A) => T` with A `number[]`) or a tuple (A
// `[number, string]`), null where it has none
static mlir::Type restArrayOf(mlir_ts::FunctionType funcType)
{
return funcType && funcType.isVarArg() && !funcType.isCVarArg() && funcType.getNumInputs() > 0
? dyn_cast<mlir_ts::ArrayType>(funcType.getInputs().back())
: mlir_ts::ArrayType();
if (!funcType || !funcType.isVarArg() || funcType.isCVarArg() || funcType.getNumInputs() == 0)
{
return mlir::Type();
}

auto rest = funcType.getInputs().back();
return isa<mlir_ts::ArrayType, mlir_ts::TupleType>(rest) ? rest : mlir::Type();
}

// the type of the rest parameter's element at `index`: the array's element, the tuple's field (null past its end)
static mlir::Type restElementType(mlir::Type rest, size_t index)
{
if (auto arrayType = dyn_cast<mlir_ts::ArrayType>(rest))
{
return arrayType.getElementType();
}

auto tupleType = cast<mlir_ts::TupleType>(rest);
return index < tupleType.size() ? tupleType.getFieldInfo(index).type : mlir::Type();
}

// adaptableSignature for a function given as a type with a rest array (TSLANG 94): the parameters the function
// takes past the type's fixed ones are elements of the array. With the same parameters as those elements there
// is still an adapter to make: it reads them out of the array. `fixedCount` is the number of fixed ones.
// adaptableSignature for a function given as a type with a rest array or tuple (TSLANG 94, 184): the parameters
// the function takes past the type's fixed ones are elements of it. With the same parameters as those elements
// there is still an adapter to make: it reads them out of it. `fixedCount` is the number of fixed ones.
static bool adaptableToRestArray(mlir::ArrayRef<mlir::Type> srcInputs, mlir::TypeRange srcResults,
mlir::ArrayRef<mlir::Type> destInputs, mlir::TypeRange destResults,
mlir_ts::ArrayType restArray, bool &dropResult, size_t &fixedCount)
mlir::Type restArray, bool &dropResult, size_t &fixedCount)
{
fixedCount = destInputs.size() - 1;
SmallVector<mlir::Type> destExpanded(destInputs.drop_back());
for (auto index = fixedCount; index < srcInputs.size(); index++)
{
destExpanded.push_back(restArray.getElementType());
auto elementType = restElementType(restArray, index - fixedCount);
if (!elementType)
{
dropResult = false;
return false;
}

destExpanded.push_back(elementType);
}

if (adaptableSignature(srcInputs, srcResults, destExpanded, destResults, dropResult))
Expand All @@ -3560,10 +3584,10 @@ namespace mlirgen
return llvm::equal(srcInputs, destExpanded) && srcResults == destResults;
}

// The arguments an adapter gives a function from the type's fixed ones and its rest array `rest`: the fixed ones,
// then an element of the array for each parameter the function takes past them
// The arguments an adapter gives a function from the type's fixed ones and its rest array or tuple `rest`: the
// fixed ones, then an element of it for each parameter the function takes past them
static void expandRestArguments(mlir::OpBuilder &builder, mlir::Location location, mlir::Type structIndexType,
mlir::ValueRange fixedArgs, mlir::Value rest, mlir_ts::ArrayType restArray,
mlir::ValueRange fixedArgs, mlir::Value rest, mlir::Type restArray,
size_t fixedCount, size_t srcCount, SmallVectorImpl<mlir::Value> &expandedArgs)
{
for (auto index = size_t(0); index < std::min((size_t)fixedArgs.size(), fixedCount); index++)
Expand All @@ -3573,11 +3597,18 @@ namespace mlirgen

for (auto index = fixedCount; index < srcCount; index++)
{
auto position =
builder.create<mlir_ts::ConstantOp>(location, structIndexType, builder.getI32IntegerAttr((int)(index - fixedCount)));
auto elementRef = builder.create<mlir_ts::ElementRefOp>(
location, mlir_ts::RefType::get(restArray.getElementType()), rest, position);
expandedArgs.push_back(builder.create<mlir_ts::LoadOp>(location, restArray.getElementType(), elementRef));
auto elementIndex = (int64_t)(index - fixedCount);
auto elementType = restElementType(restArray, elementIndex);
if (isa<mlir_ts::TupleType>(restArray))
{
expandedArgs.push_back(builder.create<mlir_ts::ExtractPropertyOp>(location, elementType, rest,
builder.getDenseI64ArrayAttr({elementIndex})));
continue;
}

auto position = builder.create<mlir_ts::ConstantOp>(location, structIndexType, builder.getI32IntegerAttr((int)elementIndex));
auto elementRef = builder.create<mlir_ts::ElementRefOp>(location, mlir_ts::RefType::get(elementType), rest, position);
expandedArgs.push_back(builder.create<mlir_ts::LoadOp>(location, elementType, elementRef));
}
}

Expand Down Expand Up @@ -4213,8 +4244,7 @@ namespace mlirgen
// `(...args: number[]) => number`, TSLANG 94)
auto toRestArray = false;
if (auto srcFuncType = mth.GetFunctionType(valueType), destFuncType = mth.GetFunctionType(type);
srcFuncType && destFuncType && !srcFuncType.isVarArg() && destFuncType.isVarArg()
&& destFuncType.getNumInputs() > 0 && isa<mlir_ts::ArrayType>(destFuncType.getInputs().back()))
srcFuncType && destFuncType && !srcFuncType.isVarArg() && restArrayOf(destFuncType))
{
toRestArray = true;
}
Expand Down
Loading
Loading