From 675feadd61a7e211a32ea73822a8a8ff8e102ee0 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Wed, 28 Oct 2026 23:55:50 +0000 Subject: [PATCH 1/2] An object whose method names a field an interface cast would widen is an error `{ count: 0, inc() { this.count = this.count + 1; } }` cast to an interface with `count: number` was given a clone with `count` widened to f64, while `inc` kept writing the s32 layout into it: `c.count` read 4.94066e-324 after one call, in every model. The cast of an object with methods to an interface now reports an error when a field it would convert is one the object's members name through `this` (or they use `this` otherwise), naming the field and both types and suggesting `0.0` or `0`. MLIRGen records those names for each object literal's storage (recordObjectLiteralThisFields); an object from another module has no record, so any method of it counts. Objects whose methods never name the field (00interface_object5) and objects of fields only are still cloned, with the warning. export_object_literal_untyped now gives `count: 0.0`, and its importer asserts the count read through the interface. Tests: 00interface_method_object_field_type, interface-cast/method_object_narrow_field. Co-Authored-By: Claude Opus 5.5 --- tslang/lib/TypeScript/MLIRGenCast.cpp | 45 +++++++++++++++++++ tslang/lib/TypeScript/MLIRGenExpressions.cpp | 1 + tslang/lib/TypeScript/MLIRGenImpl.h | 29 ++++++++++++ tslang/test/tester/CMakeLists.txt | 6 ++- .../method_object_narrow_field.ts | 14 ++++++ .../00interface_method_object_field_type.ts | 34 ++++++++++++++ .../tests/export_object_literal_untyped.ts | 4 +- .../tests/import_object_literal_untyped.ts | 1 + 8 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 tslang/test/tester/interface-cast/method_object_narrow_field.ts create mode 100644 tslang/test/tester/tests/00interface_method_object_field_type.ts diff --git a/tslang/lib/TypeScript/MLIRGenCast.cpp b/tslang/lib/TypeScript/MLIRGenCast.cpp index 44a8845af..639d909fd 100644 --- a/tslang/lib/TypeScript/MLIRGenCast.cpp +++ b/tslang/lib/TypeScript/MLIRGenCast.cpp @@ -3202,6 +3202,51 @@ namespace mlirgen return mlir::failure(); } + // An object literal's members take this very block for `this`, laid out as it is: given the + // clone through the interface, a member writing a field the clone widened (`count: 0`, an + // s32, for `count: number`) writes it as the narrow type into the wider slot, and the + // interface reads garbage. Such an object cannot be cast; its field has to be the + // interface's type. One whose members never name that field through `this` can. An object + // made in another module has no record here: any method of it may. + auto isMethod = [](mlir::Type type) { + return isa(type); + }; + auto thisFields = objectLiteralThisFields.find(storageTuple); + auto recorded = thisFields != objectLiteralThisFields.end(); + auto namedThroughThis = [&](mlir::Attribute id) { + auto name = dyn_cast(id); + return !recorded || !name || thisFields->second.contains(name.getValue()) || thisFields->second.contains(""); + }; + if (recorded || llvm::any_of(storageTuple.getFields(), [&](auto &field) { return isMethod(field.type); })) + { + for (auto &field : storageTuple.getFields()) + { + auto found = llvm::find_if(fields, [&](auto &cloneField) { return cloneField.id == field.id; }); + if (found == fields.end() || found->type == field.type || isMethod(field.type) || + !namedThroughThis(field.id)) + { + continue; + } + + auto diag = emitError(location) << "field '"; + if (auto name = dyn_cast(field.id)) + { + diag << name.getValue(); + } + else + { + diag << field.id; + } + + diag << "' of the object is '" << to_print(field.type) + << "' but interface '" << interfaceInfo->fullName << "' declares '" + << to_print(found->type) + << "': the object's methods would not see the converted field. Give the field " + "the interface's type (for a number, a literal such as `0.0`, or `0`)"; + return mlir::failure(); + } + } + auto newInterfaceTupleType = getTupleType(fields); CAST_A(unboxed, location, newInterfaceTupleType, in, genContext); diff --git a/tslang/lib/TypeScript/MLIRGenExpressions.cpp b/tslang/lib/TypeScript/MLIRGenExpressions.cpp index 405cbcadd..70c221ec2 100644 --- a/tslang/lib/TypeScript/MLIRGenExpressions.cpp +++ b/tslang/lib/TypeScript/MLIRGenExpressions.cpp @@ -2480,6 +2480,7 @@ namespace mlirgen // final type, update objectStorageType.setFields(oli.fieldInfos); + recordObjectLiteralThisFields(oli, getTupleType(oli.fieldInfos)); if (mlir::failed(mlirGenObjectLiteralMethodBodies(oli, genContext))) { diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index 5a545de2c..fcffba4ec 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -11623,6 +11623,32 @@ class MLIRGenImpl // given with no receiver may be built for an interface, whose vtable takes the literal's methods, and so // may one made for an interface. An arrow whose untyped parameters have no function type to take theirs // from stays a method too. + // The fields an object literal's members name through `this` (`this.count`), and "" when one uses + // `this` otherwise (whole, or `this[k]`), by the literal's storage: a cast to an interface that widens + // such a field gives the members a clone they would read and write in the narrow type + // (castObjectToInterface) + void recordObjectLiteralThisFields(ObjectLiteralInfo &oli, mlir::Type storageType) + { + auto &names = objectLiteralThisFields[storageType]; + size_t thisCount = 0; + size_t fieldCount = 0; + FilterVisitorAST thisVisitor(SyntaxKind::ThisKeyword, [&](auto) { thisCount++; }); + FilterVisitorAST accessVisitor(SyntaxKind::PropertyAccessExpression, [&](auto node) { + auto access = node.template as(); + if (access->expression == SyntaxKind::ThisKeyword) + { + fieldCount++; + names.insert(MLIRHelper::getName(access->name)); + } + }); + thisVisitor.visit(oli.objectLiteral); + accessVisitor.visit(oli.objectLiteral); + if (thisCount > fieldCount) + { + names.insert(""); + } + } + void markClosureMembers(ObjectLiteralInfo &oli, const GenContext &genContext) { oli.closureMembers.clear(); @@ -16362,6 +16388,9 @@ class MLIRGenImpl // (interfaceVTableNameForClass), from the discovery pass on: see castAnyToInterface llvm::StringSet<> classInterfaceVTablesCast; + // the fields object literals' members name through `this`, by storage (recordObjectLiteralThisFields) + llvm::DenseMap> objectLiteralThisFields; + // the @cpp methods returning a C++ record (by function name): the record's type llvm::StringMap cppVTableHiddenReturns; // those of them whose hidden pointer goes before `this` (the Itanium ABI) diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 78f848e9b..cc536a15e 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -754,6 +754,7 @@ tslang_add_test(NAME test-compile-01-spread COMMAND test-runner "${PROJECT_SOURC tslang_add_test(NAME test-compile-00-types-unknown1 COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00types_unknown1.ts") tslang_add_test(NAME test-compile-00-any-to-tuple COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_to_tuple.ts") tslang_add_test(NAME test-compile-00-any-literal-to-interface COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_literal_to_interface.ts") +tslang_add_test(NAME test-compile-00-interface-method-object-field-type COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00interface_method_object_field_type.ts") tslang_add_test(NAME test-compile-00-unknown-round-trip COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00unknown_round_trip.ts") tslang_add_test(NAME test-compile-00-types-indexed-access-type COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00types_indexedaccesstype.ts") tslang_add_test(NAME test-compile-00-types-mapped-type COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00types_mappedtype.ts") @@ -1378,6 +1379,7 @@ tslang_add_test(NAME test-jit-01-spread COMMAND test-runner -jit "${PROJECT_SOUR tslang_add_test(NAME test-jit-00-types-unknown1 COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00types_unknown1.ts") tslang_add_test(NAME test-jit-00-any-to-tuple COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_to_tuple.ts") tslang_add_test(NAME test-jit-00-any-literal-to-interface COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_literal_to_interface.ts") +tslang_add_test(NAME test-jit-00-interface-method-object-field-type COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00interface_method_object_field_type.ts") tslang_add_test(NAME test-jit-00-unknown-round-trip COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00unknown_round_trip.ts") tslang_add_test(NAME test-jit-00-types-indexed-access-type COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00types_indexedaccesstype.ts") tslang_add_test(NAME test-jit-00-types-mapped-type COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00types_mappedtype.ts") @@ -2480,6 +2482,7 @@ set(TSLANG_CORPUS 00types_unknown1.ts 00any_to_tuple.ts 00any_literal_to_interface.ts + 00interface_method_object_field_type.ts 00unknown_round_trip.ts 00types_utility.ts 00types.ts @@ -3478,7 +3481,8 @@ set_tests_properties(test-compile-interface-cast-empty # field of another type, has no slot to copy. set(interface_cast_error_cases "method_field|interface 'F' can't be cast to interface 'M': member 'g' is a method of one and a field of the other" - "field_type|interface 'S1' can't be cast to interface 'T1': member 'b' has another type") + "field_type|interface 'S1' can't be cast to interface 'T1': member 'b' has another type" + "method_object_narrow_field|field 'count' of the object is 's32' but interface 'Counter' declares 'number'") foreach(interface_cast_error_case ${interface_cast_error_cases}) string(FIND "${interface_cast_error_case}" "|" interface_cast_error_sep) string(SUBSTRING "${interface_cast_error_case}" 0 ${interface_cast_error_sep} interface_cast_error_name) diff --git a/tslang/test/tester/interface-cast/method_object_narrow_field.ts b/tslang/test/tester/interface-cast/method_object_narrow_field.ts new file mode 100644 index 000000000..9f0be2b5c --- /dev/null +++ b/tslang/test/tester/interface-cast/method_object_narrow_field.ts @@ -0,0 +1,14 @@ +// An object with methods cast to an interface that declares one of its fields wider (`count: 0` is an +// s32, `count: number` an f64): the cast would give the interface a clone with the field widened, and +// the methods, which take the object as it is laid out, would write the narrow value into the wide slot. +interface Counter { + count: number; + inc(): void; +} + +function main() { + const counterObj = { count: 0, inc() { this.count = this.count + 1; } }; + const c: Counter = counterObj; + c.inc(); + print(c.count); +} diff --git a/tslang/test/tester/tests/00interface_method_object_field_type.ts b/tslang/test/tester/tests/00interface_method_object_field_type.ts new file mode 100644 index 000000000..f2249f1a5 --- /dev/null +++ b/tslang/test/tester/tests/00interface_method_object_field_type.ts @@ -0,0 +1,34 @@ +// An object with methods cast to an interface keeps one block when its fields have the interface's +// types: the methods and the interface see the same field (`count: 0.0`, `0`). One whose +// narrower field a method names through `this` is an error (interface-cast/method_object_narrow_field.ts): +// a clone with the field widened was written by the methods in the narrow type, and read back as garbage. +// An object of fields only, or whose methods do not name the field (00interface_object5.ts), is still +// copied, its field widened. +interface Counter { + count: number; + inc(): void; +} + +interface Point { + x: number; +} + +function main() { + const a = { count: 0.0, inc() { this.count = this.count + 1; } }; + const ca: Counter = a; + ca.inc(); + ca.inc(); + assert(ca.count == 2, "a float literal field"); + assert(a.count == 2, "the same block"); + + const b = { count: 0, inc() { this.count = this.count + 1; } }; + const cb: Counter = b; + cb.inc(); + assert(cb.count == 1, "a cast literal field"); + + const p = { x: 3 }; + const ip: Point = p; + assert(ip.x == 3, "an object of fields only, widened"); + + print("done."); +} diff --git a/tslang/test/tester/tests/export_object_literal_untyped.ts b/tslang/test/tester/tests/export_object_literal_untyped.ts index c3c7054e1..baf610b04 100644 --- a/tslang/test/tester/tests/export_object_literal_untyped.ts +++ b/tslang/test/tester/tests/export_object_literal_untyped.ts @@ -11,5 +11,7 @@ namespace A { // boxing fix: the importer's @dllimport reconstruction must box this // back to match, both for casting to an interface AND for reading the // global's fields directly (no cast at all). - export var counterObj = { count: 0, inc() { this.count = this.count + 1; } }; + // `count: 0.0`: an integer literal is an s32, which the interface's `number` field would widen, + // and a cast of an object whose method writes that field is an error (castObjectToInterface) + export var counterObj = { count: 0.0, inc() { this.count = this.count + 1; } }; } diff --git a/tslang/test/tester/tests/import_object_literal_untyped.ts b/tslang/test/tester/tests/import_object_literal_untyped.ts index 35b9bb5c6..b62204d1b 100644 --- a/tslang/test/tester/tests/import_object_literal_untyped.ts +++ b/tslang/test/tester/tests/import_object_literal_untyped.ts @@ -15,5 +15,6 @@ assert(A.counterObj.count == 2); var c: A.Counter = A.counterObj; c.inc(); print(c.count); +assert(c.count == 3); print("done."); From a11b227358f4ff6947ddfb77a403d62a03b9273c Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Thu, 29 Oct 2026 00:08:57 +0000 Subject: [PATCH 2/2] An imported boxed object is the exporter's object, not a copy of it An untyped object literal with methods is exported boxed: its global holds a pointer to the object (@boxed in the declarations). The importer loaded that pointer and then copied the whole object into its own variable, so `A.counterObj` in the importer was a snapshot: its method calls and writes never reached the exporter's object, and a cast to an interface copied the snapshot again (`c.inc()` left `A.counterObj.count` behind). The importer's variable is now the pointer itself, an object type, as the exporter's is. import_object_literal_untyped asserts the count read through the import and through a function of the exporter after the interface's call. Co-Authored-By: Claude Opus 5.5 --- tslang/lib/TypeScript/MLIRGenVariables.cpp | 18 ++++++++++-------- .../tests/export_object_literal_untyped.ts | 3 +++ .../tests/import_object_literal_untyped.ts | 4 ++++ 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/tslang/lib/TypeScript/MLIRGenVariables.cpp b/tslang/lib/TypeScript/MLIRGenVariables.cpp index c1c2cd5f0..9c14b8fda 100644 --- a/tslang/lib/TypeScript/MLIRGenVariables.cpp +++ b/tslang/lib/TypeScript/MLIRGenVariables.cpp @@ -878,17 +878,19 @@ namespace mlirgen { // the resolved symbol's storage is a single boxed // pointer (ObjectType lowers to `ptr`) to the real - // data, not the data inline - load the pointer first, - // then load through it, instead of reading - // sizeof(fieldType) bytes directly at the symbol's own - // address (which would read past a lone 8-byte slot). - // See DeclarationPrinter.cpp's @boxed emission. + // data, not the data inline - load the pointer, + // instead of reading sizeof(fieldType) bytes directly + // at the symbol's own address (which would read past a + // lone 8-byte slot). The variable is that pointer, as + // the exporter's is: a copy of the data it points to + // was a snapshot, which the exporter's object never saw + // a write to, nor a method call, and a cast to an + // interface copied again. See DeclarationPrinter.cpp's + // @boxed emission. auto boxedType = getObjectType(fieldType); auto refToBoxed = cast(location, mlir_ts::RefType::get(boxedType), referenceToStaticFieldOpaque, genContext); auto boxedPtr = V(builder.create(location, boxedType, refToBoxed)); - auto refToTyped = V(cast(location, mlir_ts::RefType::get(fieldType), boxedPtr, genContext)); - auto valueOfField = builder.create(location, fieldType, refToTyped); - return std::make_tuple(valueOfField.getType(), V(valueOfField), TypeProvided::Yes); + return std::make_tuple(boxedPtr.getType(), boxedPtr, TypeProvided::Yes); } auto refToTyped = cast(location, mlir_ts::RefType::get(fieldType), referenceToStaticFieldOpaque, genContext); diff --git a/tslang/test/tester/tests/export_object_literal_untyped.ts b/tslang/test/tester/tests/export_object_literal_untyped.ts index baf610b04..2121ed8f2 100644 --- a/tslang/test/tester/tests/export_object_literal_untyped.ts +++ b/tslang/test/tester/tests/export_object_literal_untyped.ts @@ -14,4 +14,7 @@ namespace A { // `count: 0.0`: an integer literal is an s32, which the interface's `number` field would widen, // and a cast of an object whose method writes that field is an error (castObjectToInterface) export var counterObj = { count: 0.0, inc() { this.count = this.count + 1; } }; + + // the exporter's own view of the object, after the importer changed it + export function countOf() { return counterObj.count; } } diff --git a/tslang/test/tester/tests/import_object_literal_untyped.ts b/tslang/test/tester/tests/import_object_literal_untyped.ts index b62204d1b..dfef3e999 100644 --- a/tslang/test/tester/tests/import_object_literal_untyped.ts +++ b/tslang/test/tester/tests/import_object_literal_untyped.ts @@ -16,5 +16,9 @@ var c: A.Counter = A.counterObj; c.inc(); print(c.count); assert(c.count == 3); +// the interface and the import are one object, the exporter's: the importer's variable is its +// boxed pointer, not a copy of what it points to +assert(A.counterObj.count == 3); +assert(A.countOf() == 3); print("done.");