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
6 changes: 3 additions & 3 deletions docs/own-memory-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -698,9 +698,9 @@ a reference gives it back on every path out of a function.

- The default library has an `-mm=own` build, and arrays, strings and their methods work. `Map`
and `Set` work with number, boolean and string keys and values (`new Map<string, number>()`,
`new Set<string>(["a", "b"])`): their iterators borrow the container, and strings go in as
copies. With `any` or object keys or values they do not compile yet, and neither does a `Map`
or `Set` made without type arguments (`new Set([1, 2])` also makes a `Set<any>`).
`new Set([1, 2])`): their iterators borrow the container, and strings go in as copies. With
`any` or object keys or values they do not compile yet, which includes a `new Map()` with no
type arguments and nothing to infer them from.
- Array methods that keep elements of the source in a new array (`filter` over class instances)
give each such element a second owner, which is an error by design. Use `Shared<T>` elements,
or build the result from copies.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ kinds:
counted as kept. It made `set` and `add` keeping calls, and a string given to two of them
(`m1.set(k, 1); m2.set(k, 2)`) has no retain to copy (spec 22.4): an error that the copy into the
tuple (rule 4) avoids, while no object case compiled with it either.
- `new Set([1, 2])` instantiates `Set<any>` beside `Set<si32>` (MLIRGen tries the default type
argument first, in every model), so an untyped container fails under own for the `any` case.
- `new Set([1, 2])` instantiated `Set<any>` beside `Set<si32>` (MLIRGen specialized by the default
type arguments before inferring them, in every model), so an untyped container failed under own
for the `any` case. Fixed after: the defaults are taken only when neither the constructor's
arguments nor the receiver give the type arguments.
- A callback that assigns a captured variable (`(s) => { r += s.n; }`) has no family, for a function
as much as a method: what it lends is still an error.
- A module-level `Date` with a method that changes it (`event.setDate(24)`) is "a value read out of
Expand Down
18 changes: 17 additions & 1 deletion tslang/lib/TypeScript/MLIRGenExpressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,13 @@ namespace mlirgen
mlir::Type type;
auto typeExpression = newExpression->expression;
////auto isNewArray = typeExpression == SyntaxKind::ElementAccessExpression && newExpression->arguments.isTextRangeEmpty();
auto result = mlirGen(typeExpression, newExpression->typeArguments, genContext);
// Without type arguments, the type may come from the constructor's arguments or the receiver
// (below): the generic is specialized by its defaults only when neither gives one. Done
// first, `new Set([1, 2])` made all of Set<any> beside the Set<si32> it is.
auto deferDefaults = newExpression->typeArguments.size() == 0 &&
(newExpression->arguments.size() > 0 || genContext.receiverType);
auto result = deferDefaults ? mlirGen(typeExpression, genContext)
: mlirGen(typeExpression, newExpression->typeArguments, genContext);
if (result.failed())
{
if (typeExpression == SyntaxKind::Identifier)
Expand All @@ -1302,6 +1308,7 @@ namespace mlirgen

EXIT_IF_FAILED_OR_NO_VALUE(result)
auto value = V(result);
auto unspecialized = deferDefaults ? value : mlir::Value();

// `new Set([2, 4, 6, 8])`: a generic class written without type arguments takes them from
// its constructor's arguments (see inferClassTypeFromConstructorArguments). Ahead of the
Expand Down Expand Up @@ -1367,6 +1374,15 @@ namespace mlirgen
}
}

// neither gave the type: the defaults, as without the deferral
if (unspecialized && value == unspecialized)
{
SmallVector<mlir::Value, 4> emptyOperands;
auto specialized = mlirGenSpecialized(location, value, newExpression->typeArguments, emptyOperands, genContext);
EXIT_IF_FAILED_OR_NO_VALUE(specialized)
value = V(specialized);
}

if (auto arrayType = dyn_cast<mlir_ts::ArrayType>(value.getType()))
{
return NewArray(location, arrayType, newExpression->arguments, genContext);
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 @@ -554,6 +554,14 @@ tslang_add_test(NAME test-compile-00-rest-args-pack-owned COMMAND test-runner "$
tslang_add_test(NAME test-compile-00-switch-char-string-cases COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00switch_char_string_cases.ts")
tslang_add_test(NAME test-compile-00-switch-default-fallthrough COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00switch_default_fallthrough.ts")
tslang_add_test(NAME test-compile-00-generic-class-constructor-inference COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00generic_class_constructor_inference.ts")
# A class inferred from its constructor's arguments is not also made with its defaults:
# `new Pairs([["one", 1]])` made all of Pairs<any, any> beside Pairs<string, si32>.
add_test(NAME test-generic-class-inference-no-default-specialization
COMMAND $<TARGET_FILE:tslang> --emit=mlir --no-default-lib
"${PROJECT_SOURCE_DIR}/test/tester/tests/00generic_class_constructor_inference.ts")
set_tests_properties(test-generic-class-inference-no-default-specialization
PROPERTIES PASS_REGULAR_EXPRESSION "Pairs<!ts[.]string,si32>[.]constructor"
FAIL_REGULAR_EXPRESSION "Pairs<!ts[.]any|error:|Stack dump")
tslang_add_test(NAME test-compile-00-generic-ref-inference COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00generic_ref_inference.ts")
tslang_add_test(NAME test-compile-00-generator-done-value-zero COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_done_value_zero.ts")
tslang_add_test(NAME test-compile-00-generator-yield-value COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_yield_value.ts")
Expand Down
Loading