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
Original file line number Diff line number Diff line change
Expand Up @@ -5522,3 +5522,92 @@ objects with an arrow field each run in a flat 49 MB under own.
Not here: an arrow field reading a getter (`fn = () => this.prop` with `get prop()`) fails in every model,
because its arrow is kept as returning void (PR #744). A call through a field returning a string, under
`--no-default-lib`, is `borrows a field but is used here after ...` whatever the field holds, as before.

## 71. Copies of `any`, of arrays in `@copies`, and generators over immortal globals, 2026-10-30

The default library's untyped Map (`new Map()` is `Map<any, any>`) and `reduceRight` over arrays of
arrays were `not own` by design (§49, the containers ruling of 2026-10-26). USER decisions, 2026-10-30:
an `any` is copied as a string is (not counted: a counted box would put counting in every own program
that catches, since a caught value is an `any`); an `any` that holds an object stops the program where
it would be copied; an array is copied only in a function that says so.

- **An `any` is a copyable value** (`isCopyableValue`): §22's dry run, then copies for the retains it
could not erase, now takes an `any` beside a string. `ts.StringCopy` of an `any` lowers to a new box
of the same bytes (`{size, tag, data}`). A payload whose descriptor has no release routine owns
nothing and is copied with them; a string payload is copied in turn. Any other payload (a class or
object literal, an interface, a closure, an array or tuple holding blocks) has one owner, the box: the
copy stops the program, as a failed assert does, with "-mm=own cannot copy an 'any' that holds an
object; give the object a Shared<T>" (`own_any_object_copy_stops`). Nothing counts.
- **`@sharedElements` takes `any`** (and `unknown`, the same box): its elements are copied as strings
are. An object still goes in as Shared<T>, and the message names `any` among what may.
- **The nearest use** (`copyForRetain`): a retain whose value has several consuming uses (`f(s); f(s)`
boxing `s` twice, `push(s, s)`) copies for the first use after it in its block, as rc retained each
beside its use. `own_err_string_pushed_twice` pinned the old rule and asked to become a positive when
it widened: it is `own_string_pushed_twice`.
- **A constant boxed or printed is a block** (`isLiteral`): `<any>42` and a number cast to a string were
taken for literals that hold no block, so their release stayed after a move and `k.keep(42)` freed the
box twice. On main too.
- **`@copies`** (`OWN_DECLARED_COPIES_ATTR_NAME`, no arguments): in a function that says it, an array
whose elements own nothing (`number[]`, `isCopyableArray`) is copied as well: a new header over a copy
of the data block. A copy of an array can be told from it (a push to one is not a push to the other),
so it is not made anywhere else: `own_err_field_alias` and `own_err_two_owners` stay errors. The
default library's `reduce` and `reduceRight` say it: with no initial value their accumulator starts as
a copy of an element (`own_array_copies`).
- **A generator over immortal globals** (§25) may be kept anywhere (`checkBoundedResults`): `const it =
map.values()` at the top of a script. At least one bounded argument has to be one; a callback that
holds no block says nothing of the array `.map` reads (`own_err_generator_map_outlives`).

`own_any_copies` boxes one string into two keeping calls, fresh boxes into two, and strings and numbers
into a @sharedElements container, under own, rc, gc and none; own emits no counting. Not here: a
`const` string boxed once into a call that keeps it (`const s = "a" + "b"; c.has(s)`) is still `takes a
second reference`, on main too.

## 72. Elements taken out of a rest parameter, 2026-10-30

The array a caller gives a rest parameter borrows its elements when they are the caller's (§49). A
`pop` or `shift` of it was nevertheless a value of the function's own (`isFresh`): `this.data.push(
args.shift())` compiled, and `list.push(item)` freed `item` twice, as did `const first =
items.shift()` dropped. On main.

- **Taking and keeping keeps the parameter** (the signature pass's `localOwnedParams`): a `pop` or
`shift` of a rest parameter (`restParameterOf`: the last argument of a function whose type takes a
rest) whose result is kept makes the function keep the parameter. Its callers then move what they
give into the array, as for any kept parameter (`classifyArgumentArrays` leaves an array given to a
keeping call alone), so a local given is moved (`own_err_rest_take_used_after`).
- **The emptied array is released** (`takenOnEveryPath`): nothing takes the array itself, so the
function releases it at its exits, as it releases a parameter kept on some paths only.
- **Taking without keeping is an error** (`checkRestTakes`, `takenOutOfBorrowedRest`): what comes out of
a rest parameter the function does not keep is the caller's. "an element taken out of a rest parameter
is the caller's: -mm=own lets a function take one only when it keeps what it takes; read it by its
index instead" (`own_err_rest_take_dropped`).

`own_rest_takes` pushes, unshifts and splices objects into an Array<T>-like class (fresh ones and a
local), under own, rc, gc and none; own emits no counting. The default library's Array<T> does the
same. Not here: a function that *declares* `@keeps` on such a parameter skips the release
(`declaresCallerFacts`), so the library relies on inference, not the decorator.

## 73. Methods that share elements: `@sharesElements`, 2026-10-30

USER decision, 2026-10-30: the default library's Array<T> owns its objects (no `@sharedElements` on
Array; Map and Set keep it), and a method that gives an element a second place is an error where it is
called. All methods of a generic class are made with it, so `copyWithin`, `slice`, `concat`, `fill`,
`filter`, `filter2`, `toSorted`, `toReversed`, `with`, `entries`, `values` and the iterator failed for
Array<Item> whether called or not.

- **`@sharesElements`** (no arguments) on such a method or function: MLIRGen marks what it makes
(`OWN_CANNOT_SHARE_ATTR_NAME`, the type printed) when a type argument owns a block of its own and is
not a string, an `any` or a handle. That is a fact of the types, the same in every model (§4.3), and
only own reads it.
- **The body becomes a stop** (the signature pass, first, `stubCannotShare`): `assert(false, ...)` and
a default return, so nothing analyses it, and it keeps its place in the vtable. The functions the
removed body made for itself (a generator's `next`, named in its constant state record) go with it
once nothing else refers to them. What the declaration said of its parameters (`@borrows`) goes too.
- **A call is the error** (`checkCannotShare`): a call or reference to a marked function, by `callee`
or `identifier`: "'slice' would give an element of type 'Item' a second owner, which -mm=own cannot
do; hold the elements as 'Shared<Item>'".

`own_shares_elements` makes a class with a marked method for objects (the rest works) and for numbers
(the method works), under own, rc, gc and none; `own_err_shares_elements_called` calls it for objects.
The default library marks the functions above in `__Array` and in Array<T>; Array<T>'s push, unshift
and splice move elements as §72 says, and splice removes by swapping each to the end and popping it.

13 changes: 13 additions & 0 deletions tslang/include/TypeScript/Defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,19 @@
// that returns such a record has to say it. A unit attribute on the FuncOp.
#define RECORD_DECORATOR "record"
#define OWN_DECLARED_RECORD_ATTR_NAME "ts.own_declared_record"
// `@copies`: inside this function -mm=own may copy an array whose elements own nothing where it cannot
// prove one owner (own spec 71), as it copies a string anywhere. A copy of an array can be told from
// the array - a change to one is not a change to the other - so only a function that says so makes
// one: reduce's accumulator, which starts as an element. The other models ignore it. A unit attribute.
#define COPIES_DECORATOR "copies"
#define OWN_DECLARED_COPIES_ATTR_NAME "ts.own_declared_copies"
// `@sharesElements`, on a generic method or function that gives an element a second place (`slice`,
// `concat`, `fill`): made for a type argument that owns a block of its own and is not a string, an
// `any` or a Shared<T> (an object, an array), it is marked with that type (OWN_CANNOT_SHARE_ATTR_NAME,
// a string attribute). -mm=own cannot give such an element a second owner (own spec 73): the body
// stops the program if it is ever reached, and a call to it is an error. The other models ignore it.
#define SHARES_ELEMENTS_DECORATOR "sharesElements"
#define OWN_CANNOT_SHARE_ATTR_NAME "ts.own_cannot_share"
// `@immortalizes` names the parameters the function keeps for good, in an append-only global (a
// keeper, docs/superpowers/specs/2026-10-25-own-immortal-keepers-design.md): its callers make each
// argument immortal. Checked against a body; trusted on a declaration, a C function that keeps a
Expand Down
61 changes: 60 additions & 1 deletion tslang/lib/TypeScript/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,64 @@ class StringCopyOpLowering : public TsLlvmPattern<mlir_ts::StringCopyOp>
return rewriter.create<LLVM::ICmpOp>(loc, LLVM::ICmpPredicate::ne, pointer, rewriter.create<LLVM::ZeroOp>(loc, i8PtrTy));
};

// An `any` box: a new box of the same bytes. A payload that owns nothing (its descriptor has
// no release routine) is copied with them; a string payload is copied in turn, as above.
if (mlir::isa<mlir_ts::AnyType>(op.getIn().getType()))
{
AnyLogic al(op, rewriter, tch, loc, tsLlvmContext->compileOptions);
TypeDescriptorLogic tdl(rewriter, tch, loc);
auto boxCopy = clh.conditionalExpressionLowering(
loc, i8PtrTy, isSet(in),
[&](OpBuilder &, Location) -> mlir::Value {
auto dataOffset = rewriter.create<LLVM::PtrToIntOp>(loc, llvmIndexType, al.getDataPtrOfAny(rewriter.create<LLVM::ZeroOp>(loc, i8PtrTy)));
mlir::Value bytes = rewriter.create<LLVM::AddOp>(loc, llvmIndexType, ValueRange{dataOffset, al.getDataSizeOfAny(in)});
auto copy = ch.MemoryAlloc(bytes);
ch.MemoryCopy(copy, bytes, in, bytes);

auto tag = al.getTypeOfAny(in);
auto releasePtr = rewriter.create<LLVM::GEPOp>(loc, i8PtrTy, tdl.getRecordType(), tdl.getRecordPtrFromTag(tag),
ArrayRef<LLVM::GEPArg>{0, TYPE_DESCR_RELEASE});
auto release = rewriter.create<LLVM::LoadOp>(loc, i8PtrTy, releasePtr);
return clh.conditionalExpressionLowering(
loc, i8PtrTy, isSet(release),
[&](OpBuilder &, Location) -> mlir::Value {
return clh.conditionalExpressionLowering(
loc, i8PtrTy, tdl.isKind(tag, TYPE_KIND_STRING),
[&](OpBuilder &, Location) -> mlir::Value {
auto dataSlot = al.getDataPtrOfAny(copy);
auto text = rewriter.create<LLVM::LoadOp>(loc, i8PtrTy, dataSlot);
rewriter.create<LLVM::StoreOp>(loc, copyOf(text, isSet(text)), dataSlot);
return copy;
},
[&](OpBuilder &, Location) -> mlir::Value {
// an object payload has one owner, and the box being copied is it: the
// program stops, as a failed assert does (spec 71)
AssertLogic(op, rewriter, tch, loc, tsLlvmContext->compileOptions)
.check(rewriter.create<LLVM::ConstantOp>(loc, rewriter.getI1Type(), rewriter.getBoolAttr(false)),
"-mm=own cannot copy an 'any' that holds an object; give the object a Shared<T>");
return copy;
});
},
[&](OpBuilder &, Location) -> mlir::Value { return copy; });
},
[&](OpBuilder &, Location) -> mlir::Value { return in; });
rewriter.replaceOp(op, ValueRange{boxCopy});
return success();
}

// An array whose elements own nothing: a new header over a copy of the data block. A null
// array reads as an empty one, and its copy is an empty array.
if (auto arrayType = mlir::dyn_cast<mlir_ts::ArrayType>(op.getIn().getType()))
{
ArrayLayout al(op, rewriter, getTypeConverter(), tsLlvmContext->compileOptions);
auto length = al.length(arrayType, in);
auto bytes = al.bytesFor(arrayType, length);
auto data = ch.MemoryAlloc(bytes);
ch.MemoryCopy(data, bytes, al.data(arrayType, in), bytes);
rewriter.replaceOp(op, ValueRange{al.make(arrayType, data, length)});
return success();
}

if (mlir::isa<LLVM::LLVMPointerType>(in.getType()))
{
rewriter.replaceOp(op, ValueRange{copyOf(in, isSet(in))});
Expand Down Expand Up @@ -2311,7 +2369,8 @@ struct FuncOpLowering : public TsLlvmPattern<mlir_ts::FuncOp>
if (name == OWN_DECLARED_NO_DROPS_ATTR_NAME || name == OWN_DECLARED_KEEPS_ATTR_NAME ||
name == OWN_DECLARED_BORROWS_ATTR_NAME || name == OWN_DECLARED_LENDS_ATTR_NAME ||
name == OWN_DECLARED_RECORD_ATTR_NAME || name == OWN_DECLARED_PARAM_NAMES_ATTR_NAME ||
name == OWN_DECLARED_IMMORTALIZES_ATTR_NAME) {
name == OWN_DECLARED_IMMORTALIZES_ATTR_NAME || name == OWN_DECLARED_COPIES_ATTR_NAME ||
name == OWN_CANNOT_SHARE_ATTR_NAME) {
continue;
}

Expand Down
19 changes: 7 additions & 12 deletions tslang/lib/TypeScript/MLIRGenGenerics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1341,24 +1341,19 @@ namespace mlirgen
continue;
}

// an `any` (or `unknown`, the same box) is copied as a string is, payload and all
auto type = mth.stripLiteralType(std::get<1>(found->getValue()));
if (!type || mlir::isa<mlir_ts::NamedGenericType>(type) || mlir::isa<mlir_ts::StringType>(type) ||
MLIRTypeHelper::isSharedHandleType(type) || !mth.ownsHeapMemory(location, type))
mlir::isa<mlir_ts::AnyType, mlir_ts::UnknownType>(type) || MLIRTypeHelper::isSharedHandleType(type) ||
!mth.ownsHeapMemory(location, type))
{
continue;
}

auto diag = emitError(location) << "-mm=own: '" << genericClassInfo->name << "' cannot hold '" << to_print(type)
<< "' (its '" << typeParam->getName()
<< "'): an element has to be a value, a string or a Shared<T> handle; ";
if (mlir::isa<mlir_ts::AnyType, mlir_ts::UnknownType>(type))
{
diag << "give it one of those types";
}
else
{
diag << "use 'Shared<" << to_print(type) << ">'";
}
emitError(location) << "-mm=own: '" << genericClassInfo->name << "' cannot hold '" << to_print(type)
<< "' (its '" << typeParam->getName()
<< "'): an element has to be a value, a string, an any or a Shared<T> handle; use 'Shared<"
<< to_print(type) << ">'";

result = mlir::failure();
}
Expand Down
41 changes: 41 additions & 0 deletions tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3446,6 +3446,47 @@ class MLIRGenImpl
return;
}

// `@sharesElements` names nothing: made for an element type that owns a block of its own,
// the function is marked with it (spec 73)
if (name == SHARES_ELEMENTS_DECORATOR)
{
MLIRTypeHelper mth(builder.getContext(), compileOptions);
for (auto &entry : genContext.typeParamsWithArgs)
{
auto type = mth.stripLiteralType(std::get<1>(entry.getValue()));
if (!type || mlir::isa<mlir_ts::NamedGenericType, mlir_ts::StringType, mlir_ts::AnyType, mlir_ts::UnknownType>(type) ||
MLIRTypeHelper::isSharedHandleType(type) || !mth.ownsHeapMemory(location, type))
{
continue;
}

if (llvm::none_of(attrs, [](mlir::NamedAttribute &attr) { return attr.getName() == OWN_CANNOT_SHARE_ATTR_NAME; }))
{
attrs.push_back({builder.getStringAttr(OWN_CANNOT_SHARE_ATTR_NAME), builder.getStringAttr(to_print(type))});
}
}

return;
}

// `@copies` names nothing either: it lets -mm=own copy an array in the body (spec 71)
if (name == COPIES_DECORATOR)
{
if (!args.empty())
{
emitError(location) << "'@" COPIES_DECORATOR "' names no parameter";
result = mlir::failure();
return;
}

if (llvm::none_of(attrs, [](mlir::NamedAttribute &attr) { return attr.getName() == OWN_DECLARED_COPIES_ATTR_NAME; }))
{
attrs.push_back({builder.getStringAttr(OWN_DECLARED_COPIES_ATTR_NAME), builder.getUnitAttr()});
}

return;
}

if (name != KEEPS_DECORATOR && name != BORROWS_DECORATOR && name != LENDS_DECORATOR &&
name != IMMORTALIZES_DECORATOR)
{
Expand Down
Loading
Loading