From b1fd5b82a4cb20e74e4acc4f39f559c89117c304 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Mon, 26 Oct 2026 16:29:06 +0000 Subject: [PATCH] new of a generic class: specialize by the defaults only when nothing infers the type arguments `new Set([1, 2])` made all of Set (its default, `Set`) beside the Set the constructor's arguments infer: the name was specialized by its defaults before the inference and the receiver were tried. Every model compiled the extra class; under -mm=own its methods failed (`any` elements), so every untyped Set failed to compile. Now, without type arguments but with constructor arguments or a receiver type, the name is resolved unspecialized, and the defaults apply only when neither gives the type arguments (`new Set()`). Test: test-generic-class-inference-no-default-specialization (no Pairs in 00generic_class_constructor_inference). Co-Authored-By: Claude Opus 5.5 --- docs/own-memory-model.md | 6 +++--- ...2026-10-26-own-generic-containers-design.md | 6 ++++-- tslang/lib/TypeScript/MLIRGenExpressions.cpp | 18 +++++++++++++++++- tslang/test/tester/CMakeLists.txt | 8 ++++++++ 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/docs/own-memory-model.md b/docs/own-memory-model.md index be1834f89..d88da77eb 100644 --- a/docs/own-memory-model.md +++ b/docs/own-memory-model.md @@ -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()`, - `new Set(["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`). + `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` elements, or build the result from copies. diff --git a/tslang/docs/superpowers/specs/2026-10-26-own-generic-containers-design.md b/tslang/docs/superpowers/specs/2026-10-26-own-generic-containers-design.md index 5e5cd3cde..b3dddb9af 100644 --- a/tslang/docs/superpowers/specs/2026-10-26-own-generic-containers-design.md +++ b/tslang/docs/superpowers/specs/2026-10-26-own-generic-containers-design.md @@ -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` beside `Set` (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` beside `Set` (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 diff --git a/tslang/lib/TypeScript/MLIRGenExpressions.cpp b/tslang/lib/TypeScript/MLIRGenExpressions.cpp index e7f6de2d1..6cb83f10f 100644 --- a/tslang/lib/TypeScript/MLIRGenExpressions.cpp +++ b/tslang/lib/TypeScript/MLIRGenExpressions.cpp @@ -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 beside the Set 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) @@ -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 @@ -1367,6 +1374,15 @@ namespace mlirgen } } + // neither gave the type: the defaults, as without the deferral + if (unspecialized && value == unspecialized) + { + SmallVector 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(value.getType())) { return NewArray(location, arrayType, newExpression->arguments, genContext); diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 2e9ae9b6d..39a88e0a5 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -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 beside Pairs. +add_test(NAME test-generic-class-inference-no-default-specialization + COMMAND $ --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[.]constructor" + FAIL_REGULAR_EXPRESSION "Pairs