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
15 changes: 12 additions & 3 deletions tslang/lib/TypeScript/OwnershipSignaturePass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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()));
}
Expand Down
9 changes: 9 additions & 0 deletions tslang/lib/TypeScript/OwnershipSignaturePass.h
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,15 @@ class OwnershipSignaturePass : public mlir::PassWrapper<OwnershipSignaturePass,
// ...and those it says return a value their caller owns (SHARED_LIB_OWN_FRESH_RESULTS_ATTR_NAME)
llvm::StringSet<> 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);

Expand Down
6 changes: 6 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions tslang/test/tester/tests/export_record_stamp.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
27 changes: 27 additions & 0 deletions tslang/test/tester/tests/import_record_stamp.ts
Original file line number Diff line number Diff line change
@@ -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<T>(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.");
}
Loading