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
31 changes: 31 additions & 0 deletions TSLANG_ISSUES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3041,3 +3041,34 @@ Looked at, still open:
borrow of an element cannot be one.

Tests `00own_generator_for_of_this_field.ts` (gc, rc, own).

183 (114, 94, 156, 2026-11-24). Fixed:
- `arguments.length` (114) counted the parameters the function declares, not the arguments given: `h(1)` of
`h(a, b?)` was 2. An optional parameter, or one with a default value, that the caller left out is not counted now
(mlirGenArguments: the count is worked out from the parameters given, and the `arguments` array is that long), in
a loop condition and a for-of over `arguments` too. A function with a rest parameter is as before.
- A generic arrow given to a parameter typed by a type parameter (94: `wrap(<T>(x: T) => x)`, `apply(<T>(x: T) =>
x)`): the arrow is instantiated for `any` and its type inferred from that (resolveGenericParamFromFunctionCall). A
rest parameter typed by a type parameter (`(...args: A) => T`) is inferred from the function given, `A` an array
of its parameters' type (tryInferFunction), and a function that takes its parameters one by one can be given as a
function type whose rest parameter is an array: an adapter reads them out of the array (fewerParametersThunk for a
named function or an arrow, functionValueAdapter for one read at run time, `{ x: (n: number) => n + 1 }.x`). 94's
example now compiles and runs (gc, rc).
- throw() of a generator paused in a `yield*` (156): the exception is handed to the delegate's throw(), as in
JavaScript. The delegate may catch it and go on yielding (the generator gives its next value), or finish (the
`yield*` is done and the generator goes on), or throw it on (its finally blocks run, then the generator's own try
statements see it). The loop over a delegate that has `throw` calls next() itself, and the resume check at its
yield puts what throw() gives in place of the next step (mlirGenYieldStar, mlirGenGeneratorResumeChecks).

Looked at, still open:
- `h(1, undefined)` of `h(a, b?)` gives 1 for `arguments.length` (JavaScript gives 2): an `undefined` given and one
left out are the same to the function.
- 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 `(number | string)[]`, not the tuple `[number, string]`, and the function is
refused.
- -mm=own: the function given as a rest-array function type when it is read at run time (an object literal's field,
94's example): the own rule for adapters of such values, with that error. throw() at a `yield*` under -mm=own still
throws at the yield (the delegate is exchanged into a local for each call there, 156).

Tests `00arguments_length_given.ts` (gc, rc, own), `00generic_arrow_through_generic.ts` (gc, rc),
`00generator_yield_star_throw.ts` (gc, rc).
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

- `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
- -mm=own: a value moved in some branches of an `if ... else if` with no `else` is released on the others; `typeof a === "string"` narrows a `const` holding an `any`; a call that throws an object through a `finally` that yields is a compile error, not a stop at run time
Expand Down
4 changes: 4 additions & 0 deletions tslang/include/TypeScript/MLIRLogic/MLIRGenContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,10 @@ struct GenContext
// In the loop a `yield*` makes over a delegate that has `return`: the variable holding it, whose return() a
// return() of the generator calls first at that yield (mlirGenGeneratorResumeChecks)
const char *yieldStarDelegate = nullptr;
// ...and where a throw() of the generator at that yield is handed to the delegate's throw() (TSLANG 183): the
// variable its result goes to, in place of the delegate's next(), and the flag that says next() is not called
const char *yieldStarResult = nullptr;
const char *yieldStarSkipNext = nullptr;
// out-of-band cancellation signal; mutable so stop() stays callable through the const& threading
mutable bool stopProcess = false;
mlir::SmallVector<std::unique_ptr<mlir::Diagnostic>> *postponedMessages = nullptr;
Expand Down
135 changes: 125 additions & 10 deletions tslang/lib/TypeScript/MLIRGenCast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3529,6 +3529,58 @@ 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)
{
return funcType && funcType.isVarArg() && !funcType.isCVarArg() && funcType.getNumInputs() > 0
? dyn_cast<mlir_ts::ArrayType>(funcType.getInputs().back())
: mlir_ts::ArrayType();
}

// 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.
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)
{
fixedCount = destInputs.size() - 1;
SmallVector<mlir::Type> destExpanded(destInputs.drop_back());
for (auto index = fixedCount; index < srcInputs.size(); index++)
{
destExpanded.push_back(restArray.getElementType());
}

if (adaptableSignature(srcInputs, srcResults, destExpanded, destResults, dropResult))
{
return true;
}

dropResult = false;
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
static void expandRestArguments(mlir::OpBuilder &builder, mlir::Location location, mlir::Type structIndexType,
mlir::ValueRange fixedArgs, mlir::Value rest, mlir_ts::ArrayType 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++)
{
expandedArgs.push_back(fixedArgs[index]);
}

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));
}
}

// The body of an adapter adaptableSignature allowed: calls `callee` with `leading` (a closure's captures, a
// method's `this`) and then `args` - as many as the callee takes, `undefined` for its optional ones past them -
// and returns its result as `destResults` says: as it is, widened, or dropped (released). Fails where an
Expand Down Expand Up @@ -3635,8 +3687,12 @@ namespace mlirgen
auto resFuncType = mth.GetFunctionType(type);
// a closure's own value is its function bound over the captures (a method's is bound to its object)
auto closure = boundOp && boundOp->hasAttr(OWNS_CAPTURE_ATTR_NAME);
// ...or one with a rest parameter, whose array holds what the function takes after the others (`(...args: A)
// => T` with A an array, given `(n: number) => number`, TSLANG 94): the adapter reads them out of it
auto restArray = restArrayOf(resFuncType);
if (!symbolRefOp || !inFuncType || !resFuncType || inFuncType.isVarArg() || inFuncType.isCVarArg()
|| resFuncType.isVarArg() || resFuncType.isCVarArg() || (boundOp && (!closure || inFuncType.getInputs().empty())))
|| (resFuncType.isVarArg() && !restArray) || resFuncType.isCVarArg()
|| (boundOp && (!closure || inFuncType.getInputs().empty())))
{
return mlir::Value();
}
Expand All @@ -3650,8 +3706,14 @@ namespace mlirgen

auto inInputs = inFuncType.getInputs();
auto srcInputs = boundOp ? inInputs.drop_front() : inInputs;

bool dropResult;
if (!adaptableSignature(srcInputs, inFuncType.getResults(), destInputs, resFuncType.getResults(), dropResult))
size_t fixedCount = destInputs.size();
auto adaptable = restArray
? adaptableToRestArray(srcInputs, inFuncType.getResults(), destInputs, resFuncType.getResults(), restArray,
dropResult, fixedCount)
: adaptableSignature(srcInputs, inFuncType.getResults(), destInputs, resFuncType.getResults(), dropResult);
if (!adaptable)
{
return mlir::Value();
}
Expand All @@ -3665,7 +3727,7 @@ namespace mlirgen
resInputs.append(destInputs.begin(), destInputs.end());

auto calleeName = symbolRefOp.getIdentifier();
auto thunkType = mlir_ts::FunctionType::get(builder.getContext(), resInputs, resFuncType.getResults(), false);
auto thunkType = mlir_ts::FunctionType::get(builder.getContext(), resInputs, resFuncType.getResults(), !!restArray);
// one for each type it is given as: the name is the type's, printed (deterministic, unlike its pointer)
std::string typeText;
llvm::raw_string_ostream typeStream(typeText);
Expand Down Expand Up @@ -3722,6 +3784,29 @@ namespace mlirgen

mlir::ValueRange leading = boundOp ? mlir::ValueRange(passed).take_front(1) : mlir::ValueRange();
mlir::ValueRange args = boundOp ? mlir::ValueRange(passed).drop_front(1) : mlir::ValueRange(passed);

// the rest array's elements, for the parameters past the others
SmallVector<mlir::Value> expandedArgs;
if (restArray)
{
auto restIndex = (boundOp ? 1u : 0u) + (unsigned)fixedCount;
mlir::Value rest;
if (restIndex < passed.size())
{
rest = passed[restIndex];
}
else
{
auto restParam = builder.create<mlir_ts::ParamOp>(
noLocation, mlir_ts::RefType::get(restArray), entryBlock.getArgument(restIndex),
builder.getBoolAttr(false), builder.getIndexAttr(restIndex + 1));
rest = builder.create<mlir_ts::LoadOp>(noLocation, restArray, restParam);
}

expandRestArguments(builder, noLocation, mth.getStructIndexType(), args, rest, restArray, fixedCount,
srcInputs.size(), expandedArgs);
args = mlir::ValueRange(expandedArgs);
}
if (mlir::failed(emitSignatureAdapterBody(noLocation, callee, leading, srcInputs, inFuncType.getResults(),
args, destResults,
hasResult ? entryOp.getReference() : mlir::Value(), genContext)))
Expand Down Expand Up @@ -3769,7 +3854,10 @@ namespace mlirgen

auto srcType = mth.GetFunctionType(valueType);
auto destType = mth.GetFunctionType(type);
if (!srcType || !destType || srcType.isVarArg() || srcType.isCVarArg() || destType.isVarArg() || destType.isCVarArg())
// a type whose rest parameter is an array the function's parameters are read out of (TSLANG 94)
auto restArray = restArrayOf(destType);
if (!srcType || !destType || srcType.isVarArg() || srcType.isCVarArg() || (destType.isVarArg() && !restArray)
|| destType.isCVarArg())
{
return mlir::Value();
}
Expand All @@ -3795,7 +3883,10 @@ namespace mlirgen
}

bool dropResult;
if (!adaptableSignature(srcInputs, srcType.getResults(), destInputs, destType.getResults(), dropResult))
size_t fixedCount = destInputs.size();
if (!(restArray ? adaptableToRestArray(srcInputs, srcType.getResults(), destInputs, destType.getResults(), restArray,
dropResult, fixedCount)
: adaptableSignature(srcInputs, srcType.getResults(), destInputs, destType.getResults(), dropResult)))
{
return mlir::Value();
}
Expand All @@ -3809,7 +3900,7 @@ namespace mlirgen

SmallVector<mlir::Type> thunkInputs{boxType};
thunkInputs.append(destInputs.begin(), destInputs.end());
auto thunkType = mlir_ts::FunctionType::get(builder.getContext(), thunkInputs, destType.getResults(), false);
auto thunkType = mlir_ts::FunctionType::get(builder.getContext(), thunkInputs, destType.getResults(), !!restArray);

// one adapter for each pair of types: the name is the types', printed (deterministic, unlike their pointers)
std::string typesText;
Expand Down Expand Up @@ -3861,8 +3952,17 @@ namespace mlirgen
callee = builder.create<mlir_ts::GetMethodOp>(noLocation, srcType, func);
}

mlir::ValueRange args = entryBlock.getArguments().drop_front();
SmallVector<mlir::Value> expandedArgs;
if (restArray)
{
expandRestArguments(builder, noLocation, mth.getStructIndexType(), args, args.back(), restArray, fixedCount,
srcInputs.size(), expandedArgs);
args = mlir::ValueRange(expandedArgs);
}

if (mlir::failed(emitSignatureAdapterBody(noLocation, callee, leading, srcInputs, srcType.getResults(),
entryBlock.getArguments().drop_front(), destResults,
args, destResults,
hasResult ? entryOp.getReference() : mlir::Value(), genContext)))
{
funcOp.erase();
Expand Down Expand Up @@ -4109,9 +4209,19 @@ namespace mlirgen
guardAsBoolean = true;
}

// ...and a function given as one whose rest parameter holds what it takes (`(n: number) => number` as
// `(...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()))
{
toRestArray = true;
}

// fewer parameters, more optional ones, a result given as `void` (`let f: () => void = n`) or
// widened (`() => string` as `() => string | number`): adaptableSignature
if (guardAsBoolean || test.result == MatchResultType::NotMatchArgCount
if (guardAsBoolean || toRestArray || test.result == MatchResultType::NotMatchArgCount
|| test.result == MatchResultType::NotMatchResultCount
|| test.result == MatchResultType::NotMatchResult)
{
Expand Down Expand Up @@ -4145,9 +4255,14 @@ namespace mlirgen
};

bool dropResult;
ownAdapterRefused = adaptableSignature(visibleInputs(valueType, srcType), srcType.getResults(),
size_t fixedCount;
auto restArray = restArrayOf(destType);
ownAdapterRefused = (restArray && !srcType.isVarArg() ? adaptableToRestArray(visibleInputs(valueType, srcType), srcType.getResults(),
visibleInputs(type, destType), destType.getResults(),
restArray, dropResult, fixedCount)
: adaptableSignature(visibleInputs(valueType, srcType), srcType.getResults(),
visibleInputs(type, destType), destType.getResults(),
dropResult);
dropResult));
}

emitFunctionTypeMismatch(location, valueType, type, test, ownAdapterRefused);
Expand Down
Loading
Loading