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
62 changes: 62 additions & 0 deletions TSLANG_ISSUES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3847,3 +3847,65 @@ Not yet:
constructor (140).

Tests `00tslang_issue_211.ts` (all models), `own_grow_past_end_check.ts` (-mm=own).

212. Leftovers of 211's triage (2026-11-27). Fixed:
- 132: a generic class given for a constructor type (`fruitFactory1(Banana)` of `new (...args: any[]) => TFruit`,
`class Banana<T extends string>`): "Can't find class Banana". Its type arguments come from the constructor type's
parameters, as `new Banana(...)`'s come from its arguments (171), and a rest among them gives its element type to each
constructor parameter the others do not reach, the constructor's own rest included: `any` makes T `any` (TypeScript's
`Banana<any>`), `...args: "foo"[]` makes it a string. The tsc test contextualSignatureInstantiation4 compiles.
- 164: under -mm=own an interface narrowed to a class by `instanceof` (`if (v instanceof A) return v.a` of `v: IA` or
`IA | IB`): "'v' is a parameter, kept here". The narrowed value is the object the interface holds, a borrow as the
interface is; the cast also had a way for what is not an A (a new A of the interface's fields), never taken there,
which made the result a reference of its own. Not for a generic class, which `instanceof` answers for every G<T>.
00union_interfaces_instanceof.ts runs under -mm=own too.
- `v instanceof G` of a generic class narrowed to the generic itself, no type a value has: an interface `v` crashed the
compiler, a union "Can't resolve property". It narrows a union to its members that are a G<T> (or derive from one),
else not at all.
- 152: under -mm=own a record's strings spread into a dictionary (`const d: { [k: string]: string } = { ...rec }`) or
given to `Object.assign(d, rec)`: "a value that borrows a field cannot be stored". Each string read out of the record
is copied (strings are values). A literal given to Object.assign goes into a local of its own (ownedLiteralArgument),
which gives back what it holds: left in a slot nothing owned, it leaked once its strings were copied.
- 144: under -mm=own a `const` a function before it reads, holding a closure or an object (`const f = () => g(); const g
= () => 10;`): "'g' is captured by a closure, but -mm=own cannot tell who owns its value here". Made one of the
function's variables (declared with no value, TSLANG 172/174), it is marked an owned local before anything captures
it, and its declaration gives it the value it owns. One whose value is a borrow (`const q = p`, `const g = this.c`)
stays refused, as before ("kept here", "borrows 'this.c'").
- 191: an array of a union said to be an array of members of it (`x.slice(1) as readonly string[]` of `[number,
string] | [number, string, string]`, tsc sliceResultCast): "cast from array type ... is not supported" with no
location, at lowering. It is a new array, each element read as the member it is said to be (castArrayElementwise),
for an assertion (`as`, `<T>`) of a union with a tag only: TypeScript refuses `const s: string[] = m`, which stays
as it was, and a union with no tag (classes) is cast as it is, the same array.
- 139: a class given to an interface of a construct signature only (`interface PtCtor { new (x: number): Pt }`,
`make(P)`; `const c: Constructor<A> = A`): "the class 'P' itself can't be given as interface" (TSLANG 204's clear
error for an internal one). It is made over an object of one field, `new`, a function calling the class's
constructor and returning its object as the signature's result. An interface with other members as well is still
refused (class_itself_interface_error.ts). Not under -mm=own, which refuses a construct signature's result, through a
type literal (`{ new (x: number): Pt }`) as well: "a value takes a second reference".
- A leak under -mm=rc of the result of every call through a function value (a callback, an object literal's method,
an interface method an object literal's function field backs), in every program linking the default library since
DefaultLib #108: its `Temporal.Now` functions lacked `@owned`, the only heap results there the compiler could not
classify, and one is enough to turn off the rule that such a call hands its result over (DefaultLib #122). A call of
a function an object literal holds (`o.make(i)`, the literal a constant of the functions' names) names its callee
now, so its result is classified whatever else the program links, under -mm=own too (it leaked there).

Not yet:
- Under -mm=own an interface method an object literal's function field backs (`const m: Mk = { make: (x) => new
P(x) }; m.make(i)`) leaks its result: the thunk calling the field cannot know whose result it is, and -mm=own never
takes a call through a value for one handing its result over.
- Under -mm=rc `new c(i)` of a type literal's construct signature (`c: { new (x: number): Pt }`) given a class crashes
at run time (before this batch too).
- Under -mm=own a tuple literal holding a new string given to a union of tuples (`const x: [number, string] | [number,
string, string] = [i, "a" + i, "b"]`): "a value that borrows a field cannot be stored"; `x.slice(1)` of such a
parameter: "an element ... would have two owners".
- Under -mm=own `make(L)` of `class L<T> { constructor(...items: T[]) }` for `new (...args: number[]) => X`: "'items'
is a parameter, kept here" (gc runs it); and a `let` captured by a closure and assigned after (`let g = () => 10;
const f = () => g(); g = () => 11;`): "takes a second reference".
- The `any` dictionary of a spread record (`const d: { [k: string]: any } = { ...rec }`) leaks under -mm=own (before
this batch too).
- Still from the triage: 117, 131 (Object.keys of `object`, Object.defineProperty, Object.create), 128, 56, 140 `await`
in `finally`, 108, 191's callable interfaces naming each other, recursive conditional types, `Symbol` as a value, own
153 and 159; design questions without a ruling: overloads of a declared function (136) and class constructor (140).

Tests `00tslang_issue_212.ts`, `00tslang_issue_212_dictionary.ts` (all models), `00tslang_issue_212_ctor_interface.ts`
(gc, rc, none); `00union_interfaces_instanceof.ts` under -mm=own; `class_itself_interface_error.ts` changed.
13 changes: 12 additions & 1 deletion tslang/lib/TypeScript/MLIRGenAccessCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4396,8 +4396,19 @@ namespace mlirgen
return mlir::failure();
}

// under -mm=own a string read out of the record is copied (strings are values): the dictionary keeps what it is
// given, and the record still holds its own - stored, the read was "a value that borrows a field" (TSLANG 152)
auto fieldValue = V(fieldResult);
if (compileOptions.memoryModel == MemoryModelOwn && isa<mlir_ts::StringType>(fieldValue.getType())
&& isa_and_nonnull<mlir_ts::LoadOp, mlir_ts::ExtractPropertyOp>(fieldValue.getDefiningOp()))
{
auto copy = builder.create<mlir_ts::StringCopyOp>(spreadLocation, fieldValue.getType(), fieldValue);
copy->setAttr(OWNED_RESULT_ATTR_NAME, builder.getUnitAttr());
fieldValue = copy;
}

auto fieldKey = builder.create<mlir_ts::ConstantOp>(spreadLocation, getStringType(), name);
if (mlir::failed(mlirGenDictionarySet(spreadLocation, dictionary, fieldKey, V(fieldResult), genContext)))
if (mlir::failed(mlirGenDictionarySet(spreadLocation, dictionary, fieldKey, fieldValue, genContext)))
{
return mlir::failure();
}
Expand Down
85 changes: 84 additions & 1 deletion tslang/lib/TypeScript/MLIRGenCast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,17 @@ namespace mlirgen
return (name && name.getValue().starts_with(".")) || interfaceInfo->findField(field.id);
});

// narrowed by `v instanceof C` (TSLANG 164): it is a C, the test said so. The object it holds is all the cast is -
// a borrow of v, as v is. With the way for what is not a C, never taken there, the result was a new C in one
// branch, so -mm=own took the other for a reference of its own: a parameter `v` was kept ("cannot make the
// callers give it up"). Not a generic class's: `v instanceof G` is true of any G<T>, which this one may not be
auto narrowedClassInfo = getClassInfoByFullName(classType.getName().getValue());
if (castCheckedByInstanceOf && narrowedClassInfo && !narrowedClassInfo->originClassType)
{
auto thisPtr = builder.create<mlir_ts::ExtractInterfaceThisOp>(location, getOpaqueType(), value);
return ValueOrLogicalResult(builder.create<mlir_ts::CastOp>(location, classType, thisPtr));
}

auto classRefVal = builder.create<mlir_ts::ClassRefOp>(
location, classType, mlir::FlatSymbolRefAttr::get(builder.getContext(), classType.getName().getValue()));
auto isInstance = mlirGenInstanceOfInterface(location, value, classRefVal, genContext);
Expand Down Expand Up @@ -1927,6 +1938,39 @@ namespace mlirgen
// 'ts.ClassRef'"). Its type is `typeof A`, or a constructor type (`new (...args: any[]) => A`).
if (value.getDefiningOp<mlir_ts::ClassRefOp>())
{
// ...but an interface of a construct signature only (`interface PtCtor { new (x: number): Pt }`,
// TSLANG 139) is made over an object of one field, `new`: the class's constructor called as `new
// P(...)` calls it (generatingStaticNewCtorForClass), as a type literal's construct signature is given
// the class (castClassToTuple). Its P is made the interface's Pt as the object is cast to it
auto classInfo = getClassInfoByFullName(classType.getName().getValue());
auto onlyConstructSignature = interfaceInfo->hasNew && interfaceInfo->fields.empty()
&& interfaceInfo->accessors.empty() && interfaceInfo->indexes.empty() && interfaceInfo->extends.empty()
&& interfaceInfo->methods.size() == 1 && interfaceInfo->methods.front().name == NEW_CTOR_METHOD_NAME;
auto constructorMethod = classInfo ? classInfo->findMethod(CONSTRUCTOR_NAME) : nullptr;
auto memberResultType = onlyConstructSignature
? mth.getReturnTypeFromFuncRef(interfaceInfo->methods.front().funcType) : mlir::Type();
if (onlyConstructSignature && classInfo && !classInfo->isAbstract && constructorMethod && memberResultType)
{
// (the object it makes is returned as the signature's result, `Pt`: one returning a P was "can't
// assign '(p0: number) => P' to '(p0: number) => Pt'")
auto wrapperType = getFunctionType(constructorMethod->funcType.getInputs().slice(1) /*no this*/,
{memberResultType}, constructorMethod->funcType.isVarArg());
auto wrapperName = generateSynthMethodToCallNewCtor(location, classInfo, interfaceInfo->fullName, 0,
wrapperType, 0, genContext);
if (wrapperName.empty())
{
return mlir::failure();
}

mlir::Value newCtorValue = builder.create<mlir_ts::SymbolRefOp>(
location, wrapperType, mlir::FlatSymbolRefAttr::get(builder.getContext(), wrapperName));
SmallVector<mlir_ts::FieldInfo> ctorFields{{MLIRHelper::TupleFieldName(NEW_CTOR_METHOD_NAME, builder.getContext()),
newCtorValue.getType(), false, mlir_ts::AccessLevel::Public}};
auto ctorObject = builder.create<mlir_ts::CreateTupleOp>(location, getTupleType(ctorFields),
mlir::ValueRange{newCtorValue});
return cast(location, interfaceType, ctorObject, genContext);
}

// (said in a discovery pass too: the statement fails there, and is not made again)
emitError(location) << "the class '" << to_print(valueType) << "' itself can't be given as interface '"
<< to_print(interfaceType) << "': give it an instance, or declare it as `typeof "
Expand Down Expand Up @@ -2719,15 +2763,54 @@ namespace mlirgen
auto srcElementType = srcArrayType.getElementType();
auto elementType = arrayType.getElementType();

// (the assertion is this cast's, not the casts that build the new array's elements)
auto asserted = castIsAssertion;
MLIRValueGuard<bool> assertionGuard(castIsAssertion);
castIsAssertion = false;

// ...and an array of any other elements given as `any[]` (`("a" | "b")[]` for a parameter
// `ReadonlyArray<T>` inferred `any`): each element is boxed on its own
auto boxesElements = [&]() {
return isa<mlir_ts::AnyType>(elementType) && !isa<mlir_ts::AnyType>(srcElementType)
&& !isa<mlir_ts::ArrayType>(srcElementType);
};

// ...and an array of a union said to be an array of members of it (`x.slice(1) as readonly string[]` of
// `[number, string] | [number, string, string]`, TSLANG 191): each element is read as the member it is said to be.
// The array itself cannot be: its elements are unions, laid out as such ("cast from array type ... is not
// supported" when it was lowered). Only asserted: TypeScript refuses `const s: string[] = m`
auto narrowsElements = [&]() {
if (!asserted)
{
return false;
}

// (a union with no tag - classes, `T | null` of a reference - is laid out as its members are: cast as it is,
// the same array, as before)
auto srcUnionType = dyn_cast<mlir_ts::UnionType>(srcElementType);
mlir::Type srcBaseType;
if (!srcUnionType || !mth.isUnionTypeNeedsTag(location, srcUnionType, srcBaseType))
{
return false;
}

SmallVector<mlir::Type> members;
if (auto unionType = dyn_cast<mlir_ts::UnionType>(elementType))
{
members.append(unionType.getTypes().begin(), unionType.getTypes().end());
}
else
{
members.push_back(elementType);
}

return llvm::all_of(members, [&](mlir::Type member) {
return llvm::is_contained(srcUnionType.getTypes(), member);
});
};

if (srcElementType == elementType
|| !((isNumericSource(srcElementType) && isNumericTarget(elementType)) || boxesElements()))
|| !((isNumericSource(srcElementType) && isNumericTarget(elementType)) || boxesElements() || narrowsElements()))
{
return std::nullopt;
}
Expand Down
12 changes: 11 additions & 1 deletion tslang/lib/TypeScript/MLIRGenExpressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,9 @@ namespace mlirgen
return reinterpreted;
}

// (asserted: an array of a union may be read as an array of members of it, castArrayElementwise)
MLIRValueGuard<bool> assertionGuard(castIsAssertion);
castIsAssertion = isa<mlir_ts::ArrayType>(typeInfo) && isa<mlir_ts::ArrayType>(exprValue.getType());
CAST_A(castedValue, location, typeInfo, exprValue, genContext);
return castedValue;
}
Expand Down Expand Up @@ -442,6 +445,9 @@ namespace mlirgen
return reinterpreted;
}

// (asserted: an array of a union may be read as an array of members of it, castArrayElementwise)
MLIRValueGuard<bool> assertionGuard(castIsAssertion);
castIsAssertion = isa<mlir_ts::ArrayType>(typeInfo) && isa<mlir_ts::ArrayType>(exprValue.getType());
CAST_A(castedValue, location, typeInfo, exprValue, genContext);
return castedValue;
}
Expand Down Expand Up @@ -1947,7 +1953,11 @@ namespace mlirgen

auto sourceResult = mlirGen(source, genContext);
EXIT_IF_FAILED_OR_NO_VALUE(sourceResult)
if (mlir::failed(mlirGenSpreadIntoDictionary(loc(source), target, V(sourceResult), genContext)))
// -mm=own: a literal given (`Object.assign(d, { a: "z" + i })`) goes into a local of its own, which gives
// back what it holds once its strings are copied into the dictionary: left in a slot nothing owns, they
// were never given back
auto sourceValue = ownedLiteralArgument(loc(source), source, V(sourceResult), genContext);
if (mlir::failed(mlirGenSpreadIntoDictionary(loc(source), target, sourceValue, genContext)))
{
return mlir::failure();
}
Expand Down
Loading
Loading