From 9b52032ad16dbf9626661d796525f939a29dd9b1 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Fri, 13 Nov 2026 01:26:24 +0000 Subject: [PATCH] -mm=own: an object literal holding another module's fresh instance borrows it JSON.stringify({ when: new Date(0) }) failed under -mm=own with "an object literal that holds a value it owns is moved only into a local or returned as it is". The literal's fields are classified (classifyRecordBoxes, in computeFacts) before the imported Date..new call was marked as a fresh result, so new Date(0) read as an unknown value. The imported `..new` and --own-facts fresh results are now marked before the facts as well. Co-Authored-By: Claude Opus 5.5 --- .../lib/TypeScript/OwnershipSignaturePass.cpp | 15 ++++++++--- .../lib/TypeScript/OwnershipSignaturePass.h | 9 +++++++ tslang/test/tester/CMakeLists.txt | 6 +++++ .../test/tester/tests/export_record_stamp.ts | 9 +++++++ .../test/tester/tests/import_record_stamp.ts | 27 +++++++++++++++++++ 5 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 tslang/test/tester/tests/export_record_stamp.ts create mode 100644 tslang/test/tester/tests/import_record_stamp.ts diff --git a/tslang/lib/TypeScript/OwnershipSignaturePass.cpp b/tslang/lib/TypeScript/OwnershipSignaturePass.cpp index 1491554f6..04cd85184 100644 --- a/tslang/lib/TypeScript/OwnershipSignaturePass.cpp +++ b/tslang/lib/TypeScript/OwnershipSignaturePass.cpp @@ -195,6 +195,17 @@ namespace own_signature entry.second->removeAttr(OWN_DECLARED_LENDS_ATTR_NAME); } } + // before the facts: an object literal holding `new Date(0)` borrows it as it does `new C()`, + // which classifyRecordBoxes decides from whether the value is fresh (the loop below marks it + // again, with the rest) + for (auto &call : calls) + { + if (isImportedFreshResult(call)) + { + call.op->setAttr(OWN_FRESH_RESULT_ATTR_NAME, mlir::UnitAttr::get(&getContext())); + } + } + // before the facts: a value kept for good is immortal, which keeps nothing of its owner findKeepers(module); decideKeptForGood(module); @@ -260,9 +271,7 @@ namespace own_signature // `new C()` of a class from a library built under own: see OWN_FRESH_RESULT_ATTR_NAME // ...and of a function another own module's object says returns one its caller owns // (--own-facts) - if (call.callees.imported && call.op->getNumResults() == 1 && - ((!call.callees.importedOverwrites && call.callees.importedName.ends_with("." NEW_METHOD_NAME)) || - importedFreshResults.contains(call.callees.importedName))) + if (isImportedFreshResult(call)) { call.op->setAttr(OWN_FRESH_RESULT_ATTR_NAME, mlir::UnitAttr::get(&getContext())); } diff --git a/tslang/lib/TypeScript/OwnershipSignaturePass.h b/tslang/lib/TypeScript/OwnershipSignaturePass.h index ede209991..5b494ab1e 100644 --- a/tslang/lib/TypeScript/OwnershipSignaturePass.h +++ b/tslang/lib/TypeScript/OwnershipSignaturePass.h @@ -607,6 +607,15 @@ class OwnershipSignaturePass : public mlir::PassWrapper importedFreshResults; + // `new C()` of a class from a library built under own, or a call of a function another own + // module's object says returns a value its caller owns (--own-facts): the caller owns the result + bool isImportedFreshResult(const Call &call) + { + return call.callees.known && call.callees.imported && call.op->getNumResults() == 1 && + ((!call.callees.importedOverwrites && call.callees.importedName.ends_with("." NEW_METHOD_NAME)) || + importedFreshResults.contains(call.callees.importedName)); + } + // The body's own share of a function's FieldDrops: its stores, not its calls. FieldDrops fieldDropsInBody(mlir_ts::FuncOp funcOp); diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index d04075238..4a39faf3b 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -1958,6 +1958,12 @@ add_test(NAME test-compile-class-field-capture-error COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/class_field_capture_error.ts") set_tests_properties(test-compile-class-field-capture-error PROPERTIES PASS_REGULAR_EXPRESSION "a member of class 'Counter' reads 'start', a local of the function the class is declared in" FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed|defined outside the region") +# an object literal holding a fresh instance of another module's class, given to a call (-mm=own: +# JSON.stringify({ when: new Date(0) })) +foreach(stamp_mm gc rc own) + tslang_add_test(NAME test-compile-shared-export-import-record-stamp-${stamp_mm} COMMAND test-runner -shared -mm=${stamp_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/import_record_stamp.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_record_stamp.ts") + tslang_add_test(NAME test-jit-shared-export-import-record-stamp-${stamp_mm} COMMAND test-runner -jit -shared -mm=${stamp_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/import_record_stamp.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_record_stamp.ts") +endforeach() # @keeps and @borrows across the boundary: the importer reads them from __decls (-mm=own), and the # other models ignore them foreach(markers_mm gc rc own) diff --git a/tslang/test/tester/tests/export_record_stamp.ts b/tslang/test/tester/tests/export_record_stamp.ts new file mode 100644 index 000000000..2e4b9e4aa --- /dev/null +++ b/tslang/test/tester/tests/export_record_stamp.ts @@ -0,0 +1,9 @@ +// A class an object literal holds in another module (import_record_stamp.ts), as DefaultLib's Date is +// held by `JSON.stringify({ when: new Date(0) })`. +export class Stamp { + constructor(public at: number) {} + + toString() { + return "stamp " + this.at; + } +} diff --git a/tslang/test/tester/tests/import_record_stamp.ts b/tslang/test/tester/tests/import_record_stamp.ts new file mode 100644 index 000000000..19d719886 --- /dev/null +++ b/tslang/test/tester/tests/import_record_stamp.ts @@ -0,0 +1,27 @@ +// An object literal holding a fresh instance of a class another module defines, given to a call: the +// literal borrows it, as it does a class of its own module's. Under -mm=own it failed with "an object +// literal that holds a value it owns is moved only into a local or returned as it is" - `new Date(0)` +// in `JSON.stringify({ when: new Date(0) })`: the imported `..new` was known to make a fresh value +// only after the literals had been classified. +import './export_record_stamp' + +function at(o: { when: Stamp }) { + return o.when.at; +} + +function name(o: T) { + return "" + typeof o; +} + +function both(o: { first: Stamp, second: Stamp }) { + return o.first.at + o.second.at; +} + +function main() { + assert(at({ when: new Stamp(3) }) == 3, "a literal argument holding an imported class"); + assert(name({ when: new Stamp(4) }) == "object" || name({ when: new Stamp(4) }) == "tuple", "to a generic"); + assert(both({ first: new Stamp(1), second: new Stamp(2) }) == 3, "two of them"); + const o = { when: new Stamp(5) }; + assert(at(o) == 5, "a local"); + print("done."); +}