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
33 changes: 23 additions & 10 deletions TSLANG_ISSUES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2138,15 +2138,26 @@ reads `T | undefined`, under -mm=own the dictionary owns its values). Fixed:
the library's Map throws was made while the imported file was read as declarations, a declaration, then made
public. It is made in full there now, as a specialization is (makeAnyKeysAdapter).

- Later: an interface of a string index signature and nothing else (`interface Dictionary<T> { [x: string]: T }`;
nothing extended, no decorator) is the same dictionary - `type Dictionary<T> = { [x: string]: T }`
(isStringIndexSignatureOnly). It was a record of `get`/`set` functions nothing could fill. A class may still
`implements` it (nothing to implement); without the default library's dictionary it stays the interface it
was. Open: another interface `extends` it ("indexer is not declared" at an index of that one).
- Later: a generic class, interface or type alias instantiated inside a generic function with a type parameter of
the same name as one of its own (`Box<S>` in `f<T, S>`, Box's parameter named T) took the function's: the
instance's context starts from the function's type parameters and aliases, a type parameter as an argument did
not replace the T there, and an alias is looked up first - `Box<S>` was made as `Box<T>`, so
`f(Box<number>, Box<string>)` inferred T = number | string, S = any, and read its second argument with the wrong
layout (a crash). The declaration's own parameters shadow the enclosing ones now (shadowTypeParameters); a
type alias's arguments are read before. Test `00generic_shadowed_type_param.ts`.

Still open, looked at:
- An interface with only a string index signature (`interface Dictionary<T> { [x: string]: T }`) is still an
interface: the dictionary is for the type literal (and so for what is written with it).
- -mm=own: a local moved inside two `if`s one after the other (`if (a) keep(k); if (b) keep(k);`) is "moved here on
some paths only" - so the dictionary copies its keys rather than taking the caller's; a spread of a record into a
dictionary is refused (its fields would have two owners).
- A spread of a dictionary into one of another value type is an error for now: inferring `f<T, S>(a: Box<T>, b:
Box<S>)` from `f(Box<number>, Box<string>)` gives T = number | string and S = any, and the call then reads the
second argument with the wrong layout (a crash) - a generic-inference bug of its own.
- A spread of a dictionary into one of another value type is an error for now. The generic-inference bug it
waited on is fixed (above); what is left is the default library's `objectDictionaryAssign<T>(target, source)`
taking two type parameters, a library change to land first.
- `a[a.length] = x` on an array did not grow it (gc wrote past the end; rc and own crashed). Fixed under gc, rc and
none: the element store grows the array to `i + 1` first when `i` is at or past the length
(mlirGenGrowArrayForWrite, SetLengthOf: the slots in between read as the element type's default; a negative
Expand Down Expand Up @@ -2220,6 +2231,13 @@ Tests: `00object_dictionary.ts`, `00own_param_exclusive_moves.ts` (compile, JIT,
an error in the lowering rather than a resume somewhere else. Tests `generator_yield_in_finally.ts`
(diagnostic-location), `00generator_finally.ts`.

- Later: a generator is its own iterator - `g[Symbol.iterator]()` (`[Symbol.asyncIterator]` of an async one) is g
itself, made at the call: a generator's object has a step and no member of that name. It was "Can't resolve
property 'iterator'". A method `[Symbol.iterator]() { return this; }` on the generator object was tried before
and backed out: a generator made inside another's machinery (an iterator's builtin map/filter) resolved that
`this` through the outer one's alias, and under -mm=own a method returning `this` is refused. Test
`00generator_symbol_iterator.ts` (compile, JIT, rc, own).

Still open, looked at:
- A yield in a finally block (an error, above): supporting it needs the finally lowered as straight code after the
try when it yields - a catch-all keeping the exception, the way out recorded (end, throw, return, break,
Expand All @@ -2231,11 +2249,6 @@ Still open, looked at:
- -mm=own: a generator typed as an interface other than the library-instantiated Iterator<T> - a program's own, or
Iterable<T> - is "a value takes a second reference; -mm=own cannot prove a move or a borrow here yet" at its
`for...of` (before these changes too), and so is an array given as an Iterable<T>.
- A generator as its own iterator (`gen[Symbol.iterator]()`: "field "iterator" can't be found"): a method
`[Symbol.iterator]() { return this; }` on the generator object was tried - a generator made inside another's
machinery (the builtin map/filter of an iterator) then resolved that `this` through the outer one's alias
("can't resolve name: .this"), and under -mm=own a method returning `this` is refused. Likewise
`[Symbol.asyncIterator]` of an async generator.

Tests: `00iterables.ts`, `00generator_typed_yield.ts` (compile, JIT, rc),
diagnostic-location `generator_return_of_other_type.ts`; `00generator_next_value.ts`, `00object_method_arity.ts`,
Expand Down
14 changes: 14 additions & 0 deletions tslang/lib/TypeScript/MLIRGenClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1834,6 +1834,13 @@ namespace mlirgen
// `implements` handling only accepts a genuine InterfaceType - properly
// supporting a TupleType target would need synthesizing an anonymous
// interface from it, a separate, scoped feature. Fail cleanly for now.
// An interface of a string index signature only is a dictionary (isStringIndexSignatureOnly):
// for a class, what it says is only a type check - nothing to implement.
if (dictionaryClassInfoOf(type, genContext))
{
return;
}

emitError(loc(implementingType)) << "class cannot implement type: " << to_print(type);
});

Expand Down Expand Up @@ -5149,6 +5156,13 @@ genContext);
// type-alias-resolved TupleType target, not a genuine InterfaceType) - this
// function builds the vtable definition for whatever `implements` already
// registered, so it hits the same real gap from a different stage.
// A dictionary (an interface of a string index signature only): nothing to implement.
if (dictionaryClassInfoOf(type, genContext))
{
success = true;
return;
}

emitError(loc(implementingType)) << "class cannot implement type: " << to_print(type);
});

Expand Down
16 changes: 16 additions & 0 deletions tslang/lib/TypeScript/MLIRGenExpressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1794,6 +1794,22 @@ namespace mlirgen

auto callExpr = callExpression->expression.as<Expression>();

// `g[Symbol.iterator]()` of a generator (`[Symbol.asyncIterator]` of an async one): the generator itself,
// as JavaScript's generators are their own iterators. Its object has a step and no member of that name;
// a method returning `this` on it was resolved through an outer generator's alias when one generator is
// made inside another's machinery, and under -mm=own is refused - the expression itself is not.
if (callExpression->arguments.size() == 0 && callExpr == SyntaxKind::ElementAccessExpression)
{
auto elementAccess = callExpr.as<ElementAccessExpression>();
auto wellKnown = wellKnownSymbolName(elementAccess->argumentExpression);
if ((wellKnown == SYMBOL_ITERATOR || wellKnown == SYMBOL_ASYNC_ITERATOR) &&
evaluateProperty(elementAccess->expression, GENERATOR_STEP, genContext) &&
!evaluateProperty(elementAccess->expression, wellKnown.str(), genContext))
{
return mlirGen(elementAccess->expression.as<Expression>(), genContext);
}
}

// `a.concat(x, [y, z])`: an argument that is not an array is one element, as `[x]` - the array's
// concat (lib.generics), and the default library's `Array<T>` class's, take arrays only
if (callExpr == SyntaxKind::PropertyAccessExpression
Expand Down
4 changes: 4 additions & 0 deletions tslang/lib/TypeScript/MLIRGenGenerics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1757,8 +1757,10 @@ namespace mlirgen
GenContext genericTypeGenContext(genContext);
genericTypeGenContext.instantiateSpecializedFunction = false;
auto typeParams = genericClassInfo->typeParams;
shadowTypeParameters(typeParams, genericTypeGenContext);
auto [result, hasAnyNamedGenericType] = zipTypeParametersWithArguments(
location, typeParams, typeArguments, genericTypeGenContext.typeParamsWithArgs, genContext);
shadowTypeParameterAliases(typeParams, genericTypeGenContext);
if (mlir::failed(result) && hasAnyNamedGenericType == IsGeneric::NoDefaults)
{
// can't instantiate generic type, so check if normal type without generic types exists
Expand Down Expand Up @@ -2144,8 +2146,10 @@ namespace mlirgen

GenContext genericTypeGenContext(genContext);
auto typeParams = genericInterfaceInfo->typeParams;
shadowTypeParameters(typeParams, genericTypeGenContext);
auto [result, hasAnyNamedGenericType] = zipTypeParametersWithArguments(
location, typeParams, typeArguments, genericTypeGenContext.typeParamsWithArgs, genContext);
shadowTypeParameterAliases(typeParams, genericTypeGenContext);
if (mlir::failed(result) || (hasAnyNamedGenericType == IsGeneric::True && !allowNamedGenerics)
|| typeArgumentsTooLong(location, genericTypeGenContext.typeParamsWithArgs))
{
Expand Down
31 changes: 31 additions & 0 deletions tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -17835,6 +17835,8 @@ class MLIRGenImpl
HeritageClause heritageClause, int &orderWeight, bool declareClass,
const GenContext &genContext);

bool isStringIndexSignatureOnly(InterfaceDeclaration interfaceDeclarationAST, const GenContext &genContext);

mlir::LogicalResult mlirGen(InterfaceDeclaration interfaceDeclarationAST, const GenContext &genContext);

template <typename T> mlir::LogicalResult mlirGenInterfaceType(T newInterfacePtr, const GenContext &genContext)
Expand Down Expand Up @@ -18589,6 +18591,35 @@ class MLIRGenImpl
const ts::TypeParameterDOM::TypePtr &typeParam, mlir::Type type, const GenContext &genContext,
bool reportFailure = true);

// A generic declaration instantiated inside another generic (`Box<S>` in `f<T, S>`) starts from the
// enclosing one's type parameters and aliases: its own of the same name shadow those. Left in, `Box`'s T
// was f's T (a type parameter as an argument does not replace what is there, and the prototype of a
// generic function names each of its parameters as an alias of itself, looked up first), so `Box<S>` was
// made as `Box<T>`: `f(Box<number>, Box<string>)` inferred T = number | string and S = any, and read its
// second argument with the wrong layout. Before the arguments are zipped: the parameters.
void shadowTypeParameters(llvm::ArrayRef<TypeParameterDOM::TypePtr> typeParams, GenContext &genContext)
{
for (auto &typeParam : typeParams)
{
genContext.typeParamsWithArgs.erase(typeParam->getName());
}
}

// After: the alias of a parameter given another type parameter (`Box<S>`'s T = S). Only then - a concrete
// argument made inside a generic prototype (`TypedArray<u8>` there) keeps the alias as before: generated
// there in full, the instance was taken for done, and the module that uses it had none of its members.
void shadowTypeParameterAliases(llvm::ArrayRef<TypeParameterDOM::TypePtr> typeParams, GenContext &genContext)
{
for (auto &typeParam : typeParams)
{
auto arg = genContext.typeParamsWithArgs.lookup(typeParam->getName()).second;
if (arg && isa<mlir_ts::NamedGenericType>(arg))
{
genContext.typeAliasMap.erase(typeParam->getName());
}
}
}

std::tuple<mlir::LogicalResult, IsGeneric> zipTypeParameterWithArgument(
mlir::Location location, llvm::StringMap<std::pair<TypeParameterDOM::TypePtr, mlir::Type>> &pairs,
const ts::TypeParameterDOM::TypePtr &typeParam, mlir::Type type, bool noExtendTest,
Expand Down
50 changes: 50 additions & 0 deletions tslang/lib/TypeScript/MLIRGenInterfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1408,8 +1408,58 @@ namespace mlirgen
return mlir::success();
}

// `interface D<T> { [k: string]: T }`: a string index signature and nothing else, nothing extended, no
// decorator - the dictionary the same type literal is (getTupleType(TypeLiteralNode)), so it is
// `type D<T> = { [k: string]: T }`. As an interface it was a record of `get`/`set` functions nothing could
// fill.
bool MLIRGenImpl::isStringIndexSignatureOnly(InterfaceDeclaration interfaceDeclarationAST, const GenContext &genContext)
{
// without the default library's dictionary, the interface it was (an object given as it is a clear error)
if (!getGenericClassInfoByFullName(OBJECT_DICTIONARY_CLASS_NAME))
{
return false;
}

if (interfaceDeclarationAST->members.size() != 1 || interfaceDeclarationAST->heritageClauses.size() > 0)
{
return false;
}

auto member = interfaceDeclarationAST->members.front();
if (member != SyntaxKind::IndexSignature)
{
return false;
}

auto indexSignature = member.as<IndexSignatureDeclaration>();
if (indexSignature->parameters.size() != 1 || !indexSignature->type ||
!indexSignature->parameters.front()->type ||
indexSignature->parameters.front()->type != SyntaxKind::StringKeyword)
{
return false;
}

auto decorated = false;
iterateDecorators(interfaceDeclarationAST, genContext, [&](StringRef, SmallVector<StringRef>) { decorated = true; });
return !decorated;
}

mlir::LogicalResult MLIRGenImpl::mlirGen(InterfaceDeclaration interfaceDeclarationAST, const GenContext &genContext)
{
if (isStringIndexSignatureOnly(interfaceDeclarationAST, genContext))
{
NodeFactory nf(NodeFactoryFlags::None);
auto typeLiteral = nf.createTypeLiteralNode(interfaceDeclarationAST->members);
typeLiteral->pos = interfaceDeclarationAST->pos;
typeLiteral->_end = interfaceDeclarationAST->_end;
auto typeAlias = nf.createTypeAliasDeclaration(interfaceDeclarationAST->modifiers, interfaceDeclarationAST->name,
interfaceDeclarationAST->typeParameters, typeLiteral);
typeAlias->pos = interfaceDeclarationAST->pos;
typeAlias->_end = interfaceDeclarationAST->_end;
typeAlias->parent = interfaceDeclarationAST->parent;
return mlirGen(typeAlias, genContext);
}

// do not proceed for Generic Interfaces for declaration
if (interfaceDeclarationAST->typeParameters.size() > 0 && genContext.typeParamsWithArgs.size() == 0)
{
Expand Down
19 changes: 18 additions & 1 deletion tslang/lib/TypeScript/MLIRGenTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -694,9 +694,26 @@ namespace mlirgen
auto typeParams = std::get<0>(genericTypeAliasInfo);
auto typeNode = std::get<1>(genericTypeAliasInfo);

// the arguments are the reference's, read where it is (`Pair<T>` in `f<T>` is f's T) - before the
// alias's own parameters shadow the names; a default, after, reads them
SmallVector<mlir::Type> typeArgs;
for (auto typeArgument : typeReferenceAST->typeArguments)
{
auto typeArg = getType(typeArgument, genContext);
if (!typeArg)
{
return mlir::Type();
}

typeArgs.push_back(typeArg);
}

shadowTypeParameters(typeParams, genericTypeGenContext);

auto [result, hasAnyNamedGenericType] =
zipTypeParametersWithArguments(location, typeParams, typeReferenceAST->typeArguments,
zipTypeParametersWithArguments(location, typeParams, typeArgs,
genericTypeGenContext.typeParamsWithArgs, genericTypeGenContext);
shadowTypeParameterAliases(typeParams, genericTypeGenContext);

if (mlir::failed(result))
{
Expand Down
8 changes: 8 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,8 @@ tslang_add_test(NAME test-compile-00-object-method-arity COMMAND test-runner "${
tslang_add_test(NAME test-compile-00-generator-next-value COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_next_value.ts")
tslang_add_test(NAME test-compile-00-generator-return-throw COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_return_throw.ts")
tslang_add_test(NAME test-compile-00-generator-finally COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_finally.ts")
tslang_add_test(NAME test-compile-00-generator-symbol-iterator COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_symbol_iterator.ts")
tslang_add_test(NAME test-compile-00-generic-shadowed-type-param COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00generic_shadowed_type_param.ts")
tslang_add_test(NAME test-compile-00-any-undefined-field COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_undefined_field.ts")
tslang_add_test(NAME test-compile-00-array-write-grows COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00array_write_grows.ts")
tslang_add_test(NAME test-compile-00-constructor-type-value COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts")
Expand Down Expand Up @@ -1318,6 +1320,12 @@ tslang_add_test(NAME test-jit-own-00-generator-return-throw COMMAND test-runner
tslang_add_test(NAME test-jit-00-generator-finally COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_finally.ts")
tslang_add_test(NAME test-jit-rc-00-generator-finally COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_finally.ts")
tslang_add_test(NAME test-jit-own-00-generator-finally COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_finally.ts")
tslang_add_test(NAME test-jit-00-generator-symbol-iterator COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_symbol_iterator.ts")
tslang_add_test(NAME test-jit-rc-00-generator-symbol-iterator COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_symbol_iterator.ts")
tslang_add_test(NAME test-jit-own-00-generator-symbol-iterator COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_symbol_iterator.ts")
tslang_add_test(NAME test-jit-00-generic-shadowed-type-param COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00generic_shadowed_type_param.ts")
tslang_add_test(NAME test-jit-rc-00-generic-shadowed-type-param COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00generic_shadowed_type_param.ts")
tslang_add_test(NAME test-jit-own-00-generic-shadowed-type-param COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00generic_shadowed_type_param.ts")
tslang_add_test(NAME test-jit-00-any-undefined-field COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_undefined_field.ts")
tslang_add_test(NAME test-jit-rc-00-any-undefined-field COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_undefined_field.ts")
tslang_add_test(NAME test-jit-own-00-any-undefined-field COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_undefined_field.ts")
Expand Down
Loading
Loading