diff --git a/tslang/lib/TypeScript/OwnershipSignaturePass.cpp b/tslang/lib/TypeScript/OwnershipSignaturePass.cpp index 2021be678..1fcc41db3 100644 --- a/tslang/lib/TypeScript/OwnershipSignaturePass.cpp +++ b/tslang/lib/TypeScript/OwnershipSignaturePass.cpp @@ -1309,11 +1309,16 @@ namespace own_signature setFacts(funcOp, effective); funcOp->removeAttr(OWN_FACTS_LOST_ATTR_NAME); funcOp->removeAttr(OWN_PARAMS_RELEASED_ATTR_NAME); - // a signature's `@keeps` is checked against the body as it is (checkDeclared) + // a signature's `@keeps` is checked against the body as it is (checkDeclared). But a rest + // parameter it names is the array the callers move their elements into (spec 72): the body + // takes the elements out, and the emptied array is released as an inferred keep's is + auto ownFunc = mlir::dyn_cast(funcOp); + auto funcType = ownFunc ? mlir::dyn_cast(ownFunc.getFunctionType()) : mlir_ts::FunctionType(); + auto restIndex = funcType && funcType.isVarArg() ? static_cast(funcType.getNumInputs()) - 1 : -1; llvm::SmallVector released; for (auto index : effective.owned) { - if (!declaresCallerFacts(funcOp) && !llvm::is_contained(takenParams.lookup(funcOp), index)) + if ((!declaresCallerFacts(funcOp) || index == restIndex) && !llvm::is_contained(takenParams.lookup(funcOp), index)) { released.push_back(index); } diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 512fe0bdf..33a0af095 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -4089,6 +4089,8 @@ foreach(own_test own_fresh_string own_fresh_array own_return_new own_try_local o own_captured_string_callback own_callback_and_call own_callback_captured own_literal_closures own_literal_variable_closures own_temporary_field_take own_union_narrowed_calls own_named_field_take own_let_field_take own_literal_captures own_literal_object_captures own_literal_interface_captures own_literal_let_captures own_interface_generic_method own_back_reference own_field_function_result own_callback_shapes own_callback_extended own_callback_interface own_generic_specializations own_callback_cast own_callback_interface_slot + own_any_copies own_array_copies own_string_pushed_twice own_rest_takes own_shares_elements + own_rest_keeps_declared) own_any_copies own_array_copies own_string_pushed_twice own_rest_takes own_shares_elements) tslang_add_test(NAME test-jit-own-${own_test} COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/own/${own_test}.ts") tslang_add_test(NAME test-compile-own-${own_test} COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/own/${own_test}.ts") @@ -4127,7 +4129,7 @@ foreach(keeps_mm rc gc none) own_generator_take own_rest_array_borrows own_string_copies own_captured_string_callback own_callback_and_call own_callback_captured own_literal_closures own_literal_variable_closures own_temporary_field_take own_union_narrowed_calls own_named_field_take own_let_field_take own_literal_captures own_literal_object_captures own_literal_interface_captures own_literal_let_captures own_interface_generic_method own_back_reference own_field_function_result own_callback_shapes own_callback_extended own_callback_interface - own_generic_specializations own_callback_cast own_callback_interface_slot) + own_generic_specializations own_callback_cast own_callback_interface_slot own_rest_keeps_declared) tslang_add_test(NAME test-jit-${keeps_mm}-${keeps_test} COMMAND test-runner -jit -mm=${keeps_mm} "${PROJECT_SOURCE_DIR}/test/tester/own/${keeps_test}.ts") tslang_add_test(NAME test-compile-${keeps_mm}-${keeps_test} COMMAND test-runner -mm=${keeps_mm} "${PROJECT_SOURCE_DIR}/test/tester/own/${keeps_test}.ts") endforeach() @@ -4193,6 +4195,13 @@ add_test(NAME test-own-caught-value-call set_tests_properties(test-own-caught-value-call PROPERTIES PASS_REGULAR_EXPRESSION "ts.Func" FAIL_REGULAR_EXPRESSION "error|Stack dump") +# A rest parameter its function says it keeps (`@keeps`) is released once emptied under own, as an +# inferred keep's is (spec 72); no run can see the leak, so the IR is read. +add_test(NAME test-own-rest-keeps-declared-released + COMMAND ${CMAKE_COMMAND} -DTSLANG=$ + "-DFILE=${PROJECT_SOURCE_DIR}/test/tester/own/own_rest_keeps_declared.ts" + -P "${CMAKE_CURRENT_SOURCE_DIR}/own-rest-keeps-declared.cmake") + # A read through an interface field a getter may fill is freed after its last use under own, on the # getter's path only (own_interface_getter_read.ts): the release is placed by inference. add_test(NAME test-own-interface-getter-read-released diff --git a/tslang/test/tester/own-rest-keeps-declared.cmake b/tslang/test/tester/own-rest-keeps-declared.cmake new file mode 100644 index 000000000..02fffd54d --- /dev/null +++ b/tslang/test/tester/own-rest-keeps-declared.cmake @@ -0,0 +1,28 @@ +# Under own a rest parameter its function says it keeps (`@keeps("args")`, own_rest_keeps_declared.ts) is +# released at the function's exit once its elements are moved out, as an inferred keep's is (spec 72). No run +# shows a leak, so the IR is read (--emit=mlir-affine): `List.push` releases its rest array before it returns. +cmake_minimum_required(VERSION 3.17.3) +execute_process(COMMAND "${TSLANG}" --emit=mlir-affine --no-default-lib -mm=own "${FILE}" + OUTPUT_VARIABLE out ERROR_VARIABLE err RESULT_VARIABLE result) +# the IR is printed to stderr +string(APPEND out "${err}") +if(NOT result EQUAL 0) + message(FATAL_ERROR "tslang failed (${result}):\n${out}") +endif() + +# the body of `List.push`, from its `ts.Func` to the next one +string(FIND "${out}" "ts.Func @List.push " begin) +if(begin EQUAL -1) + message(FATAL_ERROR "no function `List.push` in:\n${out}") +endif() +string(SUBSTRING "${out}" ${begin} -1 body) +string(SUBSTRING "${body}" 1 -1 rest) +string(FIND "${rest}" "ts.Func @" end) +if(NOT end EQUAL -1) + math(EXPR end "${end} + 1") + string(SUBSTRING "${body}" 0 ${end} body) +endif() + +if(NOT body MATCHES "\"ts[.]ReleaseSlot\"[(]%[0-9]+[)] : [(]!ts[.]ref 0) this.data.push(args.shift()); + return this.data.length; + } +} + +function churn() { + const hold: Item[] = []; + for (let i = 0; i < 64; i++) hold.push(new Item("churn" + i)); +} + +function main() { + const list = new List(); + list.push(new Item("a"), new Item("b")); + + // a local moves into the call + const c = new Item("c"); + list.push(c); + churn(); + + let s = ""; + for (const item of list.data) s += item.name; + assert(s == "abc", "pushed"); + + print("done."); +}