diff --git a/TSLANG_ISSUES.md b/TSLANG_ISSUES.md index 6c2ad3448..fa338d5e8 100644 --- a/TSLANG_ISSUES.md +++ b/TSLANG_ISSUES.md @@ -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 { [x: string]: T }`; + nothing extended, no decorator) is the same dictionary - `type Dictionary = { [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` in `f`, 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` was made as `Box`, so + `f(Box, Box)` 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 { [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(a: Box, b: - Box)` from `f(Box, Box)` 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(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 @@ -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, @@ -2231,11 +2249,6 @@ Still open, looked at: - -mm=own: a generator typed as an interface other than the library-instantiated Iterator - a program's own, or Iterable - 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. -- 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`, diff --git a/tslang/lib/TypeScript/MLIRGenClasses.cpp b/tslang/lib/TypeScript/MLIRGenClasses.cpp index 36f438952..76e05201d 100644 --- a/tslang/lib/TypeScript/MLIRGenClasses.cpp +++ b/tslang/lib/TypeScript/MLIRGenClasses.cpp @@ -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); }); @@ -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); }); diff --git a/tslang/lib/TypeScript/MLIRGenExpressions.cpp b/tslang/lib/TypeScript/MLIRGenExpressions.cpp index f017713d2..be79fc926 100644 --- a/tslang/lib/TypeScript/MLIRGenExpressions.cpp +++ b/tslang/lib/TypeScript/MLIRGenExpressions.cpp @@ -1794,6 +1794,22 @@ namespace mlirgen auto callExpr = callExpression->expression.as(); + // `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(); + 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(), 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` class's, take arrays only if (callExpr == SyntaxKind::PropertyAccessExpression diff --git a/tslang/lib/TypeScript/MLIRGenGenerics.cpp b/tslang/lib/TypeScript/MLIRGenGenerics.cpp index 161997e38..ad0d4baef 100644 --- a/tslang/lib/TypeScript/MLIRGenGenerics.cpp +++ b/tslang/lib/TypeScript/MLIRGenGenerics.cpp @@ -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 @@ -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)) { diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index 70240459e..c14e7236c 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -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 mlir::LogicalResult mlirGenInterfaceType(T newInterfacePtr, const GenContext &genContext) @@ -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` in `f`) 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` was + // made as `Box`: `f(Box, Box)` 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 typeParams, GenContext &genContext) + { + for (auto &typeParam : typeParams) + { + genContext.typeParamsWithArgs.erase(typeParam->getName()); + } + } + + // After: the alias of a parameter given another type parameter (`Box`'s T = S). Only then - a concrete + // argument made inside a generic prototype (`TypedArray` 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 typeParams, GenContext &genContext) + { + for (auto &typeParam : typeParams) + { + auto arg = genContext.typeParamsWithArgs.lookup(typeParam->getName()).second; + if (arg && isa(arg)) + { + genContext.typeAliasMap.erase(typeParam->getName()); + } + } + } + std::tuple zipTypeParameterWithArgument( mlir::Location location, llvm::StringMap> &pairs, const ts::TypeParameterDOM::TypePtr &typeParam, mlir::Type type, bool noExtendTest, diff --git a/tslang/lib/TypeScript/MLIRGenInterfaces.cpp b/tslang/lib/TypeScript/MLIRGenInterfaces.cpp index 77786862b..74cb5a950 100644 --- a/tslang/lib/TypeScript/MLIRGenInterfaces.cpp +++ b/tslang/lib/TypeScript/MLIRGenInterfaces.cpp @@ -1408,8 +1408,58 @@ namespace mlirgen return mlir::success(); } + // `interface D { [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 = { [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(); + 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) { 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) { diff --git a/tslang/lib/TypeScript/MLIRGenTypes.cpp b/tslang/lib/TypeScript/MLIRGenTypes.cpp index 378a7f848..50b2dd9ce 100644 --- a/tslang/lib/TypeScript/MLIRGenTypes.cpp +++ b/tslang/lib/TypeScript/MLIRGenTypes.cpp @@ -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` in `f` is f's T) - before the + // alias's own parameters shadow the names; a default, after, reads them + SmallVector 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)) { diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 0555c0cb0..5204fc252 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -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") @@ -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") diff --git a/tslang/test/tester/tests/00generator_symbol_iterator.ts b/tslang/test/tester/tests/00generator_symbol_iterator.ts new file mode 100644 index 000000000..28f71f326 --- /dev/null +++ b/tslang/test/tester/tests/00generator_symbol_iterator.ts @@ -0,0 +1,21 @@ +// A generator is its own iterator, as in JavaScript: `g[Symbol.iterator]()` is g, so a generator passes where +// something with [Symbol.iterator] is spelled out. It was "Can't resolve property 'iterator'". +function* count(n: number) { + for (let i = 1; i <= n; i++) yield i; +} + +function main() { + const it = count(3); + let s: number = 0; + for (const v of it[Symbol.iterator]()) s += v; + assert(s == 6, "a generator's [Symbol.iterator]() is the generator"); + + // partly consumed: the same generator, where it was + const g = count(4); + g.next(); + let rest: number = 0; + for (const v of g[Symbol.iterator]()) rest += v; + assert(rest == 9, "goes on from where it was"); + + print("done."); +} diff --git a/tslang/test/tester/tests/00generic_shadowed_type_param.ts b/tslang/test/tester/tests/00generic_shadowed_type_param.ts new file mode 100644 index 000000000..3915bc67f --- /dev/null +++ b/tslang/test/tester/tests/00generic_shadowed_type_param.ts @@ -0,0 +1,32 @@ +// A generic class instantiated inside a generic function with a type parameter of the same name as the class's +// own (`Box` in `f`, Box's parameter named T): the class's own shadows the function's. Before, `Box` +// was made as `Box`: `f(Box, Box)` inferred T = number | string and S = any, and read its +// second argument with the wrong layout (a crash). +class Box { + constructor(public v: T) {} +} + +interface Holder { + held: T; +} + +type Pair = { first: T, second: T }; + +function f(a: Box, b: Box) { + return typeof a.v + " " + typeof b.v + " " + b.v; +} + +function g(a: Holder, b: Holder) { + return typeof a.held + " " + typeof b.held; +} + +function h(a: Pair, b: Pair) { + return typeof a.first + " " + typeof b.second; +} + +function main() { + assert(f(new Box(1), new Box("x")) == "number string x", "class"); + assert(g({ held: 1.5 }, { held: "y" }) == "number string", "interface"); + assert(h({ first: 1.5, second: 2.5 }, { first: "a", second: "b" }) == "number string", "type alias"); + print("done."); +} diff --git a/tslang/test/tester/tests/00object_dictionary.ts b/tslang/test/tester/tests/00object_dictionary.ts index 5f3efc9f5..2114a4551 100644 --- a/tslang/test/tester/tests/00object_dictionary.ts +++ b/tslang/test/tester/tests/00object_dictionary.ts @@ -100,6 +100,21 @@ function sum(d: { [key: string]: number }) { return total; } +// an interface of a string index signature and nothing else is the same dictionary +interface Counts { + [name: string]: number; +} + +interface Table { + [key: string]: T; +} + +function total(c: Counts) { + let sum: number = 0; + for (const k in c) sum += c[k] ?? 0; + return sum; +} + function main() { const d: { [key: string]: number } = { a: 1, get: 5 }; const k = "x" + 1; @@ -143,5 +158,14 @@ function main() { assert(Object.keys(o).length == 9); assert(!("p2" in o)); + const counts: Counts = { a: 1, b: 2 }; + counts["c"] = 3; + assert(total(counts) == 6 && counts["z"] === undefined, "an interface of a string index signature"); + + const names: Table = {}; + const key = "k" + 1; + names[key] = "v1"; + assert(names["k1"] == "v1" && names["k2"] === undefined, "a generic one"); + print("done."); }