diff --git a/TSLANG_ISSUES.md b/TSLANG_ISSUES.md index 0304ed489..c96d4c95b 100644 --- a/TSLANG_ISSUES.md +++ b/TSLANG_ISSUES.md @@ -47,115 +47,16 @@ tslang now checks the runtime's `TypeScriptAsyncRuntime.lib` before linking a li older than the compiler. `-v` is an alias for `--verbose`, which prints the linker's command line, so the linker's "use -v" hint works. -Items 31-33 are `-mm=own` limits found in the default library (TypeScriptCompilerDefaultLib), not in -native_ts_graphics. There the tests `thread_spawn.ts` and `atomics_wait.ts` are marked `// not own` -because of them. Each repro was compiled with `--emit=exe` (the JIT's default-library DLL didn't -link against this build) and runs under `-mm=gc` with the output shown. - -### 31. `-mm=own`: two closures can't capture the same array - -tslang (commit 94b8bdb2, built with uncommitted local changes). Found 2026-11-08. - -A closure passed to `Thread.spawn` moves what it captures, so a second closure capturing the same -`Int32Array` is a use after move. Threads can't share memory under `-mm=own`, which is what -`Atomics` is for. A single capture is fine: in the third repro the main thread still reads the -array after the thread captured it. The error names `'this value'`, not `counter`, and points at the -second arrow function rather than at `counter` inside it. - -Repro (`share_two.ts`): -```ts -// -mm=own: two threads that capture the same Int32Array -function main() { - const counter = new Int32Array([0]); - const a = Thread.spawn((): int => { Atomics.add(counter, 0, 1); return 0; }); - const b = Thread.spawn((): int => { Atomics.add(counter, 0, 1); return 0; }); - a.join(); - b.join(); - print(Atomics.load(counter, 0)); -} -``` - -`-mm=gc` prints `2`. `-mm=own`: -``` -share_two.ts:5:28: error: 'this value' is used here after its value was moved - const b = Thread.spawn((): int => { Atomics.add(counter, 0, 1); return 0; }); - ^ -share_two.ts:4:28: note: value moved here - const a = Thread.spawn((): int => { Atomics.add(counter, 0, 1); return 0; }); - ^ -``` - -The same in a loop (`share_loop.ts`), the usual way to start workers, gives a different error: -```ts -// -mm=own: threads spawned in a loop, each capturing the same Int32Array -function main() { - const counter = new Int32Array([0]); - const workers: ThreadHandle[] = []; - for (let t = 0; t < 4; t++) { - workers.push(Thread.spawn((): int => { Atomics.add(counter, 0, 1); return 0; })); - } - - for (const w of workers) w.join(); - print(Atomics.load(counter, 0)); -} -``` - -`-mm=gc` prints `4`. `-mm=own`: -``` -share_loop.ts:6:35: error: 'this value' is moved inside a loop but was made outside it; only a borrow could do that, and -mm=own cannot prove one yet - workers.push(Thread.spawn((): int => { Atomics.add(counter, 0, 1); return 0; })); - ^ -``` - -One capture compiles and runs under both models, printing `1` (`share_after.ts`): -```ts -// -mm=own: the main thread uses the Int32Array after a thread captured it -function main() { - const counter = new Int32Array([0]); - const a = Thread.spawn((): int => { Atomics.add(counter, 0, 1); return 0; }); - a.join(); - print(Atomics.load(counter, 0)); -} -``` - -Expected: a way to share one array between threads under `-mm=own`, such as `Shared` captured by -each closure, or a capture that borrows when the closure can't outlive the array. - -### 32. `-mm=own`: the error for a move in a loop doesn't say what was moved - -tslang (commit 94b8bdb2, built with uncommitted local changes). Found 2026-11-08. - -Both errors in 31 call the captured variable `'this value'`. In a program with several captures, -nothing says which one is moved. The error should name `counter` and point at its use inside the -arrow function. - -### 33. `-mm=own`: a thread can't return an object, and the error points into the default library - -tslang (commit 94b8bdb2, built with uncommitted local changes). Found 2026-11-08. - -`Thread.spawn` keeps the function's result in a field of `ThreadStart` for `join()` to return. -When `R` is a class, storing it is rejected. The error is reported in the default library's -`lib.generics.ts` (`this.result = result;` in `ThreadStart.run`) with no line in the user's program, -so nothing points at the `Thread.spawn` call that caused it. A string or a number works. - -Repro (`return_object.ts`): -```ts -// -mm=own: a thread whose function returns an object -class Box { - constructor(public value: int) {} -} - -function main() { - const t = Thread.spawn((): Box => new Box(42)); - print(t.join().value); -} -``` - -`-mm=gc` prints `42`. `-mm=own`: -``` -.\__build\defaultlib\generics/lib.generics.ts:857:13: error: 'this value' takes a second reference; -mm=own cannot prove a move or a borrow here yet -``` - -`const result = this.fn(); this.result = result;` moves a value the function just returned, which -the field should be able to take. Expected: the move is accepted. Failing that, the error should -also name the instantiating call in the user's file. +Items 31-33, `-mm=own` limits found in the default library on 2026-11-08, are fixed too, and +`thread_spawn.ts` and `atomics_wait.ts` there now run under every model. 31 (two closures couldn't +capture the same array): a closure that escapes owns what it captures, so that stays an error for a +plain object, and `Shared` is the way to share one. A `const` `Shared` is now captured by copy, so +any number of closures hold a counted handle each, in a loop too; under `-mm=own` a handle's count is +always atomic, since handles are what threads share. 32 (the error said `'this value'` and pointed at +the arrow): the error about a captured variable now names it, with or without `--di`, points at its +first use in the closure, and says to use a `const Shared`. 33 (`this.result = this.fn()` was refused +when `R` is a class): the result of a call through a callback is the caller's when every function that +can reach the call is defined in the module and none returns a borrow of its argument. A callback that +gives back its argument or what it captured is still refused. Regression tests +`tslang/test/tester/tests/00tslang_issues_31_33.ts` and `tslang/test/tester/own/own_err_capture_*.ts`, +`own_err_callback_result_*.ts`. diff --git a/tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md b/tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md index fba162415..9df4149e8 100644 --- a/tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md +++ b/tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md @@ -5611,3 +5611,33 @@ Array whether called or not. The default library marks the functions above in `__Array` and in Array; Array's push, unshift and splice move elements as §72 says, and splice removes by swapping each to the end and popping it. +## 74. What threads need: a callback's result, and a handle in several closures, 2026-11-08 + +`Thread.spawn` keeps its function in a field and the function's result in another, and threads that +share memory capture it (TSLANG_ISSUES 31-33). + +**A callback's result.** A call through a function value had no fresh result, so `this.result = +this.fn()` was a second reference. Where the signature pass has resolved the call to a closed callback +family (`Callees::viaCallback`), it knows every function that can be called: each returns its result +retained as any function does, and one that returns a borrow of a parameter says so in its facts. So +when every member is defined here and no member's facts have `borrows` or `record`, the call is +`__own_fresh_result`. A body that returns what it does not own (a capture, a global that can be +assigned) is refused where it is written, whichever way it is called. + +**A handle in several closures.** A `const` Shared has a slot (identity storage), which made it a +read-write variable, captured through its cell, and a cell has one owner. Nothing assigns a `const`, +so it is now captured by copy (`setCaptureByValue`): each box holds a counted handle of its own, and +the box's release gives it back. A `let` Shared keeps its cell. + +A box that borrows what it holds (a closure that does not escape, over a parameter's copy or beside a +cell) gives nothing back, so the retain made for a handle in it is erased too, although other handle +retains stay (spec 23.2), and a handle read from a local is then bounded like any other borrowed copy. + +**Counts.** Under own only handles are counted, and they are what threads share, so their counts are +always atomic (`CompileOptions::atomicRefCounts`). `Shared.share` stays erased: there is nothing to mark. + +**Names.** A captured variable's name is the box field's (`fieldName` of the fill's `ts.PropertyRef`), +which needs no `--di`. MLIRGen records where a closure first uses each capture +(`PassResult::outerVariableUses`) and puts it on the closure (`__capture_uses`, under own only); the +errors about a capture point there. The fill and its retain keep the arrow's location, so debug info +never holds a location from another function's scope. diff --git a/tslang/include/TypeScript/DataStructs.h b/tslang/include/TypeScript/DataStructs.h index 68cdf38fd..581738153 100644 --- a/tslang/include/TypeScript/DataStructs.h +++ b/tslang/include/TypeScript/DataStructs.h @@ -103,12 +103,13 @@ struct CompileOptions } // --atomic-rc: every reference count change is an atomic read-modify-write, so objects may - // be shared between threads under `rc`. Only `rc` keeps counts; `own` never counts at all. + // be shared between threads under `rc`. `own` counts only Shared handles, which exist to be + // shared, so it always counts them atomically; `Shared.share` has nothing to mark there. bool atomicRC = false; bool atomicRefCounts() const { - return isRefCounted() && atomicRC; + return (isRefCounted() && atomicRC) || memoryModel == MemoryModelOwn; } // Whether the ownership operations are live and lower to code - retains and releases under diff --git a/tslang/include/TypeScript/Defines.h b/tslang/include/TypeScript/Defines.h index 7fbfc0229..a6100daaa 100644 --- a/tslang/include/TypeScript/Defines.h +++ b/tslang/include/TypeScript/Defines.h @@ -112,6 +112,10 @@ // closure is built and the difference is known. #define OWNS_CAPTURE_ATTR_NAME "__owns_capture" +// On such a closure, under -mm=own: a dictionary from each captured variable's name to where the +// closure's body first uses it, which is where an error about the capture points. +#define CAPTURE_USES_ATTR_NAME "__capture_uses" + // On a `ts.CreateBoundFunction` that binds a method to an object: the object's type, which the // lowering casts `this` away from. Under rc the bound value counts the object through that type's // tag at CLOSURE_TYPE_INDEX, as an interface over it does. diff --git a/tslang/include/TypeScript/MLIRLogic/MLIRGenContext.h b/tslang/include/TypeScript/MLIRLogic/MLIRGenContext.h index 1d0ef665b..5e84ee81f 100644 --- a/tslang/include/TypeScript/MLIRLogic/MLIRGenContext.h +++ b/tslang/include/TypeScript/MLIRLogic/MLIRGenContext.h @@ -57,6 +57,8 @@ struct PassResult // a generator's next(): its {value, done} is Iterator's, so a finished one's undefined value is not in T bool iteratorResult = false; llvm::StringMap outerVariables; + // where the function first uses each of them, for the errors about the capture + llvm::StringMap outerVariableUses; SmallVector extraFieldsInThisContext; }; diff --git a/tslang/include/TypeScript/MLIRLogic/MLIRGenStore.h b/tslang/include/TypeScript/MLIRLogic/MLIRGenStore.h index 00e608cb2..c22ff4682 100644 --- a/tslang/include/TypeScript/MLIRLogic/MLIRGenStore.h +++ b/tslang/include/TypeScript/MLIRLogic/MLIRGenStore.h @@ -1338,6 +1338,8 @@ struct NamespaceInfo llvm::StringMap> captureVarsMap; + llvm::StringMap> captureUsesMap; + llvm::StringMap> localVarsInThisContextMap; llvm::StringMap> typeAliasMap; diff --git a/tslang/lib/TypeScript/MLIRGenFunctions.cpp b/tslang/lib/TypeScript/MLIRGenFunctions.cpp index c5859fcc3..8ac6ee5a2 100644 --- a/tslang/lib/TypeScript/MLIRGenFunctions.cpp +++ b/tslang/lib/TypeScript/MLIRGenFunctions.cpp @@ -922,6 +922,7 @@ namespace mlirgen } getCaptureVarsMap().insert({name, passResult->outerVariables}); + getCaptureUsesMap().insert({name, passResult->outerVariableUses}); funcProto->setHasCapturedVars(true); LLVM_DEBUG(llvm::dbgs() << "\n!! has captured vars, name: " << name << "\n";); @@ -2601,6 +2602,20 @@ namespace mlirgen // dropped - `apply(v => v.x + base.x, v)` - is released at the end of the block that // made it (§9.30), which is what gives the box back. boundFuncVal->setAttr(OWNS_CAPTURE_ATTR_NAME, builder.getUnitAttr()); + if (compileOptions.memoryModel == MemoryModelOwn) + { + if (auto captureUses = getCaptureUsesMap().find(name); captureUses != getCaptureUsesMap().end()) + { + SmallVector uses; + for (auto &use : captureUses->getValue()) + { + uses.push_back(builder.getNamedAttr(use.getKey(), use.getValue())); + } + + boundFuncVal->setAttr(CAPTURE_USES_ATTR_NAME, builder.getDictionaryAttr(uses)); + } + } + builder.create(location, boundFuncVal); boundFuncVal->setAttr(OWNED_RESULT_ATTR_NAME, builder.getUnitAttr()); diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index ccfd8de87..e7496612b 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -2216,6 +2216,14 @@ class MLIRGenImpl if (!isConst || varClass == VariableType::ConstRef || needsIdentityStorage) { varDecl->setReadWriteAccess(); + // A `const` Shared has a slot, but nothing assigns it: each closure takes a counted + // handle of its own, so any number of them can share the value, where a cell would + // have one owner (TSLANG_ISSUES 31). + if (isConst && !(varClass == VariableType::ConstRef) && MLIRTypeHelper::isSharedHandleType(type)) + { + varDecl->setCaptureByValue(); + } + // TODO: HACK: to mark var as local and ignore when capturing. Not a generator's alias // of its `this` parameter (buildGeneratorWrapperDeclaration), which its `next` // captures: it captures the parameter's storage @@ -16468,6 +16476,11 @@ class MLIRGenImpl return currentNamespace->captureVarsMap; } + auto getCaptureUsesMap() -> llvm::StringMap> & + { + return currentNamespace->captureUsesMap; + } + auto getLocalVarsInThisContextMap() -> llvm::StringMap> & { return currentNamespace->localVarsInThisContextMap; diff --git a/tslang/lib/TypeScript/MLIRGenVariables.cpp b/tslang/lib/TypeScript/MLIRGenVariables.cpp index d9d16f262..7a3337124 100644 --- a/tslang/lib/TypeScript/MLIRGenVariables.cpp +++ b/tslang/lib/TypeScript/MLIRGenVariables.cpp @@ -1185,6 +1185,7 @@ namespace mlirgen // special case, to prevent capturing ".a" because of reference to outer VaribleOp, which is hack (review // solution for it) genContext.passResult->outerVariables.insert({value.second->getName(), value.second}); + genContext.passResult->outerVariableUses.insert({value.second->getName(), mlir::LocationAttr(location)}); } // end of logic: outer vars diff --git a/tslang/lib/TypeScript/OwnershipInferenceClosures.cpp b/tslang/lib/TypeScript/OwnershipInferenceClosures.cpp index 17aaf6c09..4d75774cd 100644 --- a/tslang/lib/TypeScript/OwnershipInferenceClosures.cpp +++ b/tslang/lib/TypeScript/OwnershipInferenceClosures.cpp @@ -1455,10 +1455,11 @@ namespace own_inference auto root = rootOf(value); captured = ownerName(root); Chain chain; - if (holdsNoBlock(value) || placeReadOf(root) || borrowedParam(root) >= 0 || isHandle(root)) + if (holdsNoBlock(value) || placeReadOf(root) || borrowedParam(root) >= 0 || + (isHandle(root) && !fill.second->hasAttr(OWN_CAPTURE_BORROW_ATTR_NAME))) { // nothing to own, a read the read's check bounds, a parameter, or a handle, - // whose copy in the box is counted (spec 23.2) + // whose copy in the box is counted (spec 23.2) - unless the box borrows it } else if (auto slotLoad = slotLoadOf(root)) { diff --git a/tslang/lib/TypeScript/OwnershipInferenceDiagnostics.cpp b/tslang/lib/TypeScript/OwnershipInferenceDiagnostics.cpp index 4236a3cd9..968160e1f 100644 --- a/tslang/lib/TypeScript/OwnershipInferenceDiagnostics.cpp +++ b/tslang/lib/TypeScript/OwnershipInferenceDiagnostics.cpp @@ -5,6 +5,9 @@ #define DEBUG_TYPE "own" +// what to do about a value several closures, or several threads, need (TSLANG_ISSUES 31) +#define CLOSURES_SHARE_NOTE "a closure that escapes owns what it captures; closures can share a value held in a const Shared" + namespace own_inference { @@ -573,6 +576,90 @@ namespace own_inference signalPassFailure(); } + mlir_ts::StoreOp OwnershipInferencePass::captureFillOf(mlir::Operation *op) + { + auto isFill = [](mlir_ts::StoreOp storeOp) { + auto propertyRefOp = storeOp.getReference().getDefiningOp(); + return propertyRefOp && closureOfBox(propertyRefOp.getObjectRef()); + }; + + if (auto storeOp = mlir::dyn_cast(op)) + { + return isFill(storeOp) ? storeOp : mlir_ts::StoreOp(); + } + + if (!mlir::isa(op)) + { + return {}; + } + + // the retain, made where the box is filled (mlirGenResolveCapturedVars), of what goes into it + auto fillOf = [&](mlir::Value value) { + for (auto *user : value.getUsers()) + { + auto storeOp = mlir::dyn_cast(user); + if (storeOp && storeOp.getValue() == value && user->getLoc() == op->getLoc() && isFill(storeOp)) + { + return storeOp; + } + } + + return mlir_ts::StoreOp(); + }; + + auto value = op->getOperand(0); + if (auto storeOp = fillOf(value)) + { + return storeOp; + } + + // a copy of a local: its slot is read again for the box's field + if (auto loadOp = value.getDefiningOp()) + { + for (auto *user : loadOp.getReference().getUsers()) + { + auto readOp = mlir::dyn_cast(user); + if (auto storeOp = readOp ? fillOf(readOp.getResult()) : mlir_ts::StoreOp()) + { + return storeOp; + } + } + } + + return {}; + } + + OwnershipInferencePass::CaptureSite OwnershipInferencePass::captureSiteOf(mlir::Operation *op) + { + auto fill = captureFillOf(op); + if (!fill) + { + return {std::string(), op->getLoc()}; + } + + auto propertyRefOp = fill.getReference().getDefiningOp(); + CaptureSite site{fieldName(propertyRefOp), op->getLoc()}; + if (auto uses = closureOfBox(propertyRefOp.getObjectRef())->getAttrOfType(CAPTURE_USES_ATTR_NAME)) + { + if (auto use = uses.getAs(site.name)) + { + site.location = use; + } + } + + return site; + } + + std::string OwnershipInferencePass::namedByCapture(llvm::StringRef name, const CaptureSite &first, const CaptureSite &second) + { + if (name != "this value") + { + return name.str(); + } + + return !first.name.empty() ? first.name : !second.name.empty() ? second.name : name.str(); + } + void OwnershipInferencePass::reportUseAfterMove(mlir::Operation *use, mlir::Operation *move, llvm::StringRef name) { if (!reporting()) @@ -580,8 +667,16 @@ namespace own_inference return; } - auto diag = use->emitError("'") << name << "' is used here after its value was moved"; - diag.attachNote(move->getLoc()) << "value moved here"; + auto useSite = captureSiteOf(use); + auto moveSite = captureSiteOf(move); + auto diag = mlir::emitError(useSite.location) << "'" << namedByCapture(name, useSite, moveSite) + << "' is used here after its value was moved"; + diag.attachNote(moveSite.location) << "value moved here"; + if (!moveSite.name.empty()) + { + diag.attachNote() << CLOSURES_SHARE_NOTE; + } + signalPassFailure(); } @@ -592,9 +687,15 @@ namespace own_inference return; } - move->emitError("'") << name - << "' is moved inside a loop but was made outside it; only a borrow could do " - "that, and -mm=own cannot prove one yet"; + auto moveSite = captureSiteOf(move); + auto diag = mlir::emitError(moveSite.location) << "'" << namedByCapture(name, moveSite, moveSite) + << "' is moved inside a loop but was made outside it; only a borrow " + "could do that, and -mm=own cannot prove one yet"; + if (!moveSite.name.empty()) + { + diag.attachNote() << CLOSURES_SHARE_NOTE; + } + signalPassFailure(); } @@ -704,8 +805,12 @@ namespace own_inference return; } - auto diag = use->emitError("'") << name << "' is captured by a closure that escapes, and is still used here"; - diag.attachNote(capture->getLoc()) << "the closure takes it here"; + auto useSite = captureSiteOf(use); + auto captureSite = captureSiteOf(capture); + auto diag = mlir::emitError(useSite.location) << "'" << namedByCapture(name, captureSite, useSite) + << "' is captured by a closure that escapes, and is still used here"; + diag.attachNote(captureSite.location) << "the closure takes it here"; + diag.attachNote() << CLOSURES_SHARE_NOTE; signalPassFailure(); } @@ -755,9 +860,10 @@ namespace own_inference return; } - auto diag = move->emitError("'") << name - << "' is moved here on some paths only; -mm=own cannot release it " - "on the others yet"; + auto moveSite = captureSiteOf(move); + auto diag = mlir::emitError(moveSite.location) << "'" << namedByCapture(name, moveSite, moveSite) + << "' is moved here on some paths only; -mm=own cannot release it " + "on the others yet"; diag.attachNote(release->getLoc()) << "released here on every path"; if (usedAt) { diff --git a/tslang/lib/TypeScript/OwnershipInferencePass.cpp b/tslang/lib/TypeScript/OwnershipInferencePass.cpp index 485676272..4daeaeac2 100644 --- a/tslang/lib/TypeScript/OwnershipInferencePass.cpp +++ b/tslang/lib/TypeScript/OwnershipInferencePass.cpp @@ -353,10 +353,11 @@ namespace own_inference // a handle's retains and releases are rc's, and stay (spec 23.2); but not the release of a // yielded field's old value: the yield gave the state's count to the caller, who owns the - // record `next` returns and releases it + // record `next` returns and releases it; nor the retain for a box that borrows what it holds: + // that box gives nothing back toErase.remove_if([&](mlir::Operation *op) { auto releaseSlotOp = mlir::dyn_cast(op); - return touchesHandle(op) && !(releaseSlotOp && isYieldedField(releaseSlotOp.getSlot())); + return touchesHandle(op) && !(releaseSlotOp && isYieldedField(releaseSlotOp.getSlot())) && !captureRetains.contains(op); }); placeEdgeReleases(); // what owns nothing gives nothing back, wherever the moves put its releases (spec 36) diff --git a/tslang/lib/TypeScript/OwnershipInferencePass.h b/tslang/lib/TypeScript/OwnershipInferencePass.h index dcfbf6a08..2f1a4056d 100644 --- a/tslang/lib/TypeScript/OwnershipInferencePass.h +++ b/tslang/lib/TypeScript/OwnershipInferencePass.h @@ -1349,6 +1349,20 @@ class OwnershipInferencePass void reportGivenNotOwned(mlir::Operation *call, mlir::Value arg); + // What an error says about an op that fills a closure's capture box, or retains for it: the + // captured variable's name (the box's field has it, with or without --di) and where the + // closure's body first uses it (CAPTURE_USES_ATTR_NAME). For any other op, no name and its own + // location. + struct CaptureSite + { + std::string name; + mlir::Location location; + }; + + mlir_ts::StoreOp captureFillOf(mlir::Operation *op); + CaptureSite captureSiteOf(mlir::Operation *op); + static std::string namedByCapture(llvm::StringRef name, const CaptureSite &first, const CaptureSite &second); + void reportUseAfterMove(mlir::Operation *use, mlir::Operation *move, llvm::StringRef name); void reportMovedInLoop(mlir::Operation *move, llvm::StringRef name); diff --git a/tslang/lib/TypeScript/OwnershipSignaturePass.cpp b/tslang/lib/TypeScript/OwnershipSignaturePass.cpp index 09d94e69d..15708d037 100644 --- a/tslang/lib/TypeScript/OwnershipSignaturePass.cpp +++ b/tslang/lib/TypeScript/OwnershipSignaturePass.cpp @@ -262,6 +262,19 @@ namespace own_signature call.op->setAttr(OWN_FRESH_RESULT_ATTR_NAME, mlir::UnitAttr::get(&getContext())); } + // a callback's call, where every function that can reach it is defined here (spec 74): each + // returns its result retained, as a direct call's does, unless its facts say it borrows + // an argument. A body that returns anything else it does not own is refused on its own. + if (call.callees.viaCallback && !call.callees.imported && !call.callees.funcs.empty() && + call.op->getNumResults() == 1 && !isRecordResult(call.op) && + llvm::all_of(call.callees.funcs, [&](mlir_ts::FuncOp callee) { + auto facts = factsEffective.lookup(callee); + return facts.borrows.empty() && !facts.record; + })) + { + call.op->setAttr(OWN_FRESH_RESULT_ATTR_NAME, mlir::UnitAttr::get(&getContext())); + } + // a generator whose state object borrows from its arguments (not through a callback // family, whose member's parameters may be shifted) if (call.callees.funcs.size() == 1 && !call.callees.imported && !call.callees.viaCallback) diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index ba5179f46..d08a450f5 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -1801,6 +1801,11 @@ foreach(issues_mm gc rc none own) tslang_add_test(NAME test-jit-00-tslang-issue-30-${issues_mm} COMMAND test-runner -jit -mm=${issues_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/00tslang_issue_30.ts") tslang_add_test(NAME test-compile-00-tslang-issue-30-${issues_mm} COMMAND test-runner -mm=${issues_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/00tslang_issue_30.ts") endforeach() +# TSLANG_ISSUES 31, 33: a const Shared in several closures; a callback's result stored in a field +foreach(issues_mm gc rc none own) + tslang_add_test(NAME test-jit-00-tslang-issues-31-33-${issues_mm} COMMAND test-runner -jit -mm=${issues_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/00tslang_issues_31_33.ts") + tslang_add_test(NAME test-compile-00-tslang-issues-31-33-${issues_mm} COMMAND test-runner -mm=${issues_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/00tslang_issues_31_33.ts") +endforeach() # TSLANG 29: a @cdecl function takes a callback as a C function pointer; a C function with arguments # after the callback (a thread start) showed the three-pointer value it was given before if(WIN32) @@ -4848,11 +4853,11 @@ set(own_error_cases "own_err_field_alias|:9:11: error: '[^']*' is used here after its value was moved" "own_err_call_in_try_drops|:30:11: error: '[^']*' borrows a field but is used here after it may be released or overwritten" "own_err_closure_escape_used|:9:11: error: '[^']*' is captured by a closure that escapes, and is still used here" - "own_err_closure_escape_twice|:12:19: error: '[^']*' is captured by a closure that escapes, and is still used here" + "own_err_closure_escape_twice|:12:25: error: 'n' is captured by a closure that escapes, and is still used here" "own_err_closure_escape_param|:7:12: error: a closure that escapes owns what it captures, but -mm=own cannot move this variable into it: its value is the caller's" "own_err_closure_escape_inherited|:9:21: error: a closure that escapes owns what it captures, but -mm=own cannot move this variable into it: it is captured from an enclosing function" - "own_err_closure_escape_loop|:16:23: error: '[^']*' is moved inside a loop but was made outside it" - "own_err_closure_escape_some_paths|:17:23: error: '[^']*' is moved here on some paths only" + "own_err_closure_escape_loop|:16:29: error: 'a' is moved inside a loop but was made outside it" + "own_err_closure_escape_some_paths|:17:29: error: 'a' is moved here on some paths only" "own_err_closure_outlives_cell|:13:11: error: '[^']*' borrows '[^']*', which it captures, but runs here after '[^']*' is released" "own_err_closure_copy_moved|:13:11: error: '[^']*' borrows '[^']*', which it captures, but runs here after '[^']*' is released" "own_err_closure_param_assigned|:9:5: error: '[^']*' is a captured parameter and cannot be assigned under -mm=own: its value is the caller's" @@ -5123,6 +5128,10 @@ set(own_error_cases "own_err_swap_borrow_used_after|:11:11: error: '[^']*' borrows an element but is used here after it may be released or overwritten" "own_err_swap_call_borrow_used_after|:15:11: error: '[^']*' borrows an element but is used here after it may be released or overwritten" "own_err_callback_forwarded_drops|:15:17: error: 'this value' borrows an element but is used here after it may be released or overwritten" + "own_err_capture_two_closures|:14:37: error: 'thing' is used here after its value was moved" + "own_err_capture_in_loop|:15:44: error: 'thing' is moved inside a loop but was made outside it" + "own_err_callback_result_captured|:19:45: error: '[^']*' borrows a field and cannot be stored, returned or captured" + "own_err_callback_result_argument|:13:9: error: '[^']*' borrows a call's result and cannot be stored, returned or captured" "own_err_callback_forwarded_keeps|:32:18: error: '[^']*' keeps '#0' for good, but -mm=own cannot make every caller make it immortal: the function is called through a value" "own_err_optional_record_field_kept|:15:12: error: '[^']*' borrows the result of 'findIn' and cannot be stored, returned or captured" "own_err_generator_take_used_after|:14:9: error: '[^']*' borrows a field and cannot be stored, returned or captured" diff --git a/tslang/test/tester/own/own_err_callback_result_argument.ts b/tslang/test/tester/own/own_err_callback_result_argument.ts new file mode 100644 index 000000000..8a314f1fe --- /dev/null +++ b/tslang/test/tester/own/own_err_callback_result_argument.ts @@ -0,0 +1,26 @@ +// TSLANG_ISSUES 33: a result stored in a field must be the caller's; this callback gives back its +// argument. +class Box { + constructor(public value: int) {} +} + +class Apply { + result: R; + + constructor(public fn: (x: R) => R) {} + + run(x: R) { + this.result = this.fn(x); + } +} + +function same(x: Box): Box { + return x; +} + +function main() { + const box = new Box(1); + const apply = new Apply(same); + apply.run(box); + print(apply.result.value + box.value); +} diff --git a/tslang/test/tester/own/own_err_callback_result_captured.ts b/tslang/test/tester/own/own_err_callback_result_captured.ts new file mode 100644 index 000000000..fd9608662 --- /dev/null +++ b/tslang/test/tester/own/own_err_callback_result_captured.ts @@ -0,0 +1,22 @@ +// TSLANG_ISSUES 33: a result stored in a field must be the caller's; this callback hands back what +// its capture owns. +class Box { + constructor(public value: int) {} +} + +class Start { + result: R; + + constructor(public fn: () => R) {} + + run() { + this.result = this.fn(); + } +} + +function main() { + const box = new Box(1); + const start = new Start((): Box => box); + start.run(); + print(start.result.value + box.value); +} diff --git a/tslang/test/tester/own/own_err_capture_in_loop.ts b/tslang/test/tester/own/own_err_capture_in_loop.ts new file mode 100644 index 000000000..3a8321e37 --- /dev/null +++ b/tslang/test/tester/own/own_err_capture_in_loop.ts @@ -0,0 +1,19 @@ +// TSLANG_ISSUES 31, 32: an object captured by a closure that escapes, in a loop. The error names it +// and points at its use in the closure. +class Thing { + constructor(public value: int) {} +} + +class Keeper { + constructor(public f: () => int) {} +} + +function main() { + const thing = new Thing(1); + const keepers: Keeper[] = []; + for (let i = 0; i < 2; i++) { + keepers.push(new Keeper((): int => thing.value)); + } + + print(keepers[1].f()); +} diff --git a/tslang/test/tester/own/own_err_capture_two_closures.ts b/tslang/test/tester/own/own_err_capture_two_closures.ts new file mode 100644 index 000000000..39e12a37d --- /dev/null +++ b/tslang/test/tester/own/own_err_capture_two_closures.ts @@ -0,0 +1,16 @@ +// TSLANG_ISSUES 31, 32: an object captured by two closures that escape. The error names it and +// points at its use in the second closure. +class Thing { + constructor(public value: int) {} +} + +class Keeper { + constructor(public f: () => int) {} +} + +function main() { + const thing = new Thing(1); + const a = new Keeper((): int => thing.value); + const b = new Keeper((): int => thing.value); + print(a.f() + b.f()); +} diff --git a/tslang/test/tester/tests/00tslang_issues_31_33.ts b/tslang/test/tester/tests/00tslang_issues_31_33.ts new file mode 100644 index 000000000..4d646f160 --- /dev/null +++ b/tslang/test/tester/tests/00tslang_issues_31_33.ts @@ -0,0 +1,175 @@ +// TSLANG_ISSUES 31 and 33, without threads: what Thread.spawn does to a closure and its result. +// 31: under -mm=own a `const` Shared is captured by copy, so any number of closures that escape +// hold a counted handle each. 33: the result of a function called through a field is the caller's when +// every function that can be there makes its result, so it can be stored in a field. + +class Box { + constructor(public value: int) {} +} + +class Keeper { + constructor(public f: () => int) {} +} + +class Start { + result: R; + + constructor(public fn: () => R) {} + + run() { + this.result = this.fn(); + } + + runInTry() { + try { + const result = this.fn(); + this.result = result; + } catch (e) { + print("not reached"); + } + } + + drop() { + this.fn(); + } + + get(): R { + return this.result; + } +} + +function spawn(fn: () => R): Start { + return new Start(fn); +} + +function churn() { + // same size as what may have been freed, so a use after free reads one of these + const boxes: Box[] = []; + for (let i = 0; i < 16; i++) { + boxes.push(new Box(-1)); + } + + assert(boxes[15].value == -1); +} + +// Shared.count is -1 under the models that do not count +function expectCount(actual: number, expected: number) { + assert(actual == -1 || actual == expected); +} + +function runBoth(a: Keeper, b: Keeper) { + a.f(); + b.f(); +} + +function twoClosures(counter: Shared) { + const handle = counter; + const a = new Keeper((): int => { handle.value.value = handle.value.value + 1; return 0; }); + const b = new Keeper((): int => { handle.value.value = handle.value.value + 1; return 0; }); + expectCount(Shared.count(counter), 4); + runBoth(a, b); + churn(); + assert(handle.value.value == 2); +} + +function closuresShareOne() { + const counter = new Shared(new Box(0)); + twoClosures(counter); + expectCount(Shared.count(counter), 1); + assert(counter.value.value == 2); + + const a = new Keeper((): int => counter.value.value); + const b = new Keeper((): int => counter.value.value); + expectCount(Shared.count(counter), 3); + assert(a.f() + b.f() == 4); +} + +function closuresInLoop() { + const counter = new Shared(new Box(5)); + const keepers: Keeper[] = []; + for (let i = 0; i < 4; i++) { + keepers.push(new Keeper((): int => counter.value.value)); + } + + expectCount(Shared.count(counter), 5); + churn(); + let sum = 0; + for (const k of keepers) { + sum += k.f(); + } + + assert(sum == 20); +} + +function closureOutlivesTheLocal(): Keeper { + const counter = new Shared(new Box(7)); + return new Keeper((): int => counter.value.value); +} + +function callClosure(f: () => int): int { + return f(); +} + +function borrowingBoxes(s: Shared) { + // a closure that does not escape: over a parameter's handle, with a `let` beside it, inside a closure + assert(callClosure((): int => s.value.value) == 3); + let n = 1; + n = 2; + assert(callClosure((): int => s.value.value + n) == 5); + const keeper = new Keeper((): int => { + const inner = (): int => s.value.value * 2; + return inner(); + }); + assert(keeper.f() == 6); +} + +function makeBox(): Box { + return new Box(1); +} + +function resultsKept() { + const named = new Start(makeBox); + named.run(); + churn(); + assert(named.get().value == 1); + + const base = 41; + const captured = new Box(1); + const arrow = spawn((): Box => new Box(captured.value + base)); + arrow.run(); + // the second result replaces the first + arrow.runInTry(); + churn(); + assert(arrow.get().value == 42); + assert(captured.value == 1); + + // one nobody keeps is given back + for (let i = 0; i < 1000; i++) { + named.drop(); + } + + const text = spawn((): string => "a" + base); + text.run(); + assert(text.get() == "a41"); + + const number = spawn((): int => base + 1); + number.run(); + assert(number.get() == 42); +} + +function main() { + closuresShareOne(); + closuresInLoop(); + + const late = closureOutlivesTheLocal(); + churn(); + assert(late.f() == 7); + + const handle = new Shared(new Box(3)); + borrowingBoxes(handle); + expectCount(Shared.count(handle), 1); + + resultsKept(); + + print("done."); +}