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
35 changes: 35 additions & 0 deletions TSLANG_ISSUES.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,38 @@ does, so it disagreed with the function it calls and lowering to LLVM crashed no
setter's result now. Also, a method returning `this` that says what it keeps (`@keeps`) is no longer
told to say `@lends` too. Regression tests `tslang/test/tester/tests/import_class_keeps_indexer.ts`
(with `export_class_keeps_indexer.ts`) and `00class_indexer_set_result.ts`.

Items 44 and 45, found on 2026-11-10 in native_ts_graphics' `examples/nri/variable_shading_nri.ts`, are
fixed. 44 (`flag && a && b` of two classes in a namespace threw "Can't cast from union type"): the helpers
a cast makes from source (`___cast`, `___unbox`, `___unbox_interface`, `___check_tuple`, `___bin_op_*`,
`___union_eq_*`) are declared in the root namespace, but the one made for the cast before was erased
from the namespace of the code asking for the new one, and from the root only when that code was outside
every namespace. In a namespace the old `___cast`, made for `null | boolean | A`, stayed and was
instantiated for `null | boolean | A | B`, so a `B` reached its `throw`. The old helper is now erased from
the root namespace, where the new one is declared. 45 (`-mm=own` refused `flag && a && b` of two classes,
in a namespace or not: "borrows the result of '___cast<...>'"): the second `&&` widened `null | boolean |
A` to `null | boolean | A | B` through a `___cast` call, whose result own took for a value stored. A union
widened to one that has all its members is now a plain cast, the value keeping its tag and its bytes, as
the lowering already did for it. With that, `!this.flag && texture && descriptor` in a method of an
exported class (every class in a namespace is) was still refused, "borrows a field but is used here
after it may be released or overwritten": `___cast` tests an instance with its class's `..instanceOf`,
called through the vtable, and for a class another module may extend own took that call for one that
may destroy anything. A class's `..instanceOf` is the compiler's own, which destroys nothing, called
through the vtable as through the `.instanceOf` slot. Regression tests
`tslang/test/tester/tests/00tslang_issue_44.ts` and `00tslang_issue_45.ts`, under every model.

Item 46, found on 2026-11-10 while testing 45, is fixed. 46 (`-mm=own`: a function that returns its
union parameter, given a new object, freed it twice; the exe hung or crashed at exit, while the JIT run
passed). `function same(v: A | null): A | null { return v; }` called as `same(new A())`, and as much
with `boolean | A`, `null | boolean | A` widened to `null | boolean | A | B`, or a named object no longer
used. On v0.0-pre-alpha121 too: its exe exits with 0xC0000374 (heap corruption). A call whose result is
its argument itself (spec 78) takes a temporary over: the temporary's releases go and the result is the
caller's own. The argument here is `<A | null>new A()`, a view, and the check looked at the view's users,
which hold no release: the result was made the caller's own while the object's release stayed. The
temporary is the object behind its views now. And the object's release may be in another block of the
function (a condition after the call, `x instanceof A`, splits it): one the call leads to, where the
result is released too, counts as after it. With the result left a borrow when the object is used after
the call, `x instanceof A` of the borrowed union was refused ("borrows ... and cannot be stored"), as it
was of a field read (`const x = this.f; x instanceof A`) before: the object seen as an opaque pointer,
whose vtable is read for its class's `.instanceOf`, is a test that keeps nothing, as for an owned value.
Regression test `tslang/test/tester/tests/00tslang_issue_46.ts`, JIT and compiled, under every model.
13 changes: 13 additions & 0 deletions tslang/lib/TypeScript/MLIRGenCast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2138,6 +2138,19 @@ namespace mlirgen
mlir::Type baseType;
if (!toAny && mth.isUnionTypeNeedsTag(location, unionType, baseType))
{
// widened to a union that has every member of this one (`flag && a && b` widens
// `null | boolean | A` to `null | boolean | A | B`): the value keeps its tag and its
// bytes, which the lowering carries over; tested at run time by `___cast` instead,
// its result was a new value that -mm=own took for one stored (TSLANG 45)
if (auto resUnionType = dyn_cast<mlir_ts::UnionType>(type))
{
auto resTypes = resUnionType.getTypes();
if (llvm::all_of(unionType.getTypes(), [&](mlir::Type member) { return llvm::is_contained(resTypes, member); }))
{
return std::nullopt;
}
}

if (auto result = castFromNullableUnion(location, type, value, unionType, genContext))
{
return *result;
Expand Down
28 changes: 5 additions & 23 deletions tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -16684,27 +16684,6 @@ class MLIRGenImpl
\
return false;

#define removeLogic(S) \
MLIRNamespaceGuard ng(currentNamespace); \
do \
{ \
auto res = currentNamespace->S.count(name); \
if (res > 0) \
{ \
currentNamespace->S.erase(name.str()); \
return true; \
} \
\
if (!currentNamespace->isFunctionNamespace) \
{ \
return false; \
} \
\
currentNamespace = currentNamespace->parentNamespace; \
} while (true); \
\
return false;

auto getNamespaceByFullName(StringRef fullName) -> NamespaceInfo::TypePtr
{
return fullNamespacesMap.lookup(fullName);
Expand Down Expand Up @@ -16756,10 +16735,13 @@ class MLIRGenImpl
existLogic(genericFunctionMap);
}

// the helpers made from source (`___cast`, `___unbox`, ...) are declared by parsePartialStatements
// in the root namespace, wherever the code that asks for one is: the one made before is erased
// there, or code in a namespace would get it back for another type (TSLANG 44)
auto removeGenericFunctionMap(StringRef name) -> bool
{
removeLogic(genericFunctionMap);
}
return rootNamespace->genericFunctionMap.erase(name);
}

auto getGlobalsNamespaceFullName() -> llvm::StringRef
{
Expand Down
10 changes: 10 additions & 0 deletions tslang/lib/TypeScript/OwnershipInferenceBorrowers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,16 @@ namespace own_inference
continue;
}

// `u instanceof A` of a borrowed union (spec 61): the object as an opaque pointer,
// whose vtable is read for its class's `.instanceOf` - a test that keeps nothing
// (isBorrow), not a view whose vtable read would keep it (TSLANG 46)
if (auto castOp = mlir::dyn_cast<mlir_ts::CastOp>(user);
castOp && mlir::isa<mlir_ts::ClassType>(castOp.getIn().getType()) &&
mlir::isa<mlir_ts::OpaqueType>(castOp.getType()) && isBorrow(user, pending.value))
{
continue;
}

llvm::SmallVector<mlir::Value> same;
llvm::SmallVector<mlir::Operation *, 2> renewedAt;
if (passesOn(use, same, &renewedAt))
Expand Down
23 changes: 22 additions & 1 deletion tslang/lib/TypeScript/OwnershipSignaturePass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,12 @@ namespace own_signature
receiver = object;
}

// ...seen through its views: `same(new A())` to an `A | null` parameter is given
// `<A | null>new A()`, and the releases to take over are the object's. Judged on the
// view, whose only user is the call, the result was made fresh while the object's own
// release stayed: freed twice (TSLANG 46).
receiver = rootOf(receiver);

// the caller's own: fresh, or what a call that borrows nothing returns (another module's
// `JsonValue.newObject()`), which the caller releases
auto *producer = receiver.getDefiningOp();
Expand All @@ -901,14 +907,29 @@ namespace own_signature
continue;
}

// a release after the call: in its block, or in one only the call leads to (a condition
// after it, `x instanceof A`, splits the function's blocks) where the result is released
// too, so the result's release is there to take the temporary's place
mlir::DominanceInfo dominance(op->getParentOfType<mlir_ts::FuncOp>());
auto releasedAfter = [&](mlir::Operation *user) {
if (user->getBlock() == op->getBlock())
{
return op->isBeforeInBlock(user);
}

return dominance.properlyDominates(op, user) && llvm::any_of(op->getResult(0).getUsers(), [&](mlir::Operation *use) {
return mlir::isa<mlir_ts::ReleaseOp>(use) && use->getBlock() == user->getBlock();
});
};

llvm::SmallVector<mlir::Operation *> releases;
auto temporary = llvm::all_of(receiver.getUsers(), [&](mlir::Operation *user) {
if (user == op)
{
return true;
}

if (mlir::isa<mlir_ts::ReleaseOp>(user) && user->getBlock() == op->getBlock() && op->isBeforeInBlock(user))
if (mlir::isa<mlir_ts::ReleaseOp>(user) && releasedAfter(user))
{
releases.push_back(user);
return true;
Expand Down
9 changes: 9 additions & 0 deletions tslang/lib/TypeScript/OwnershipSignatureResolve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,15 @@ namespace own_signature
if (auto refOp = mlir::dyn_cast_or_null<mlir_ts::ThisVirtualSymbolRefOp>(def))
{
identifier = refOp.getIdentifierAttr().getAttr();
// a class's `..instanceOf`, called through its vtable (`___cast` tests an instance so):
// the compiler's own in every class, which destroys nothing, as the `.instanceOf` slot
// read below - whoever overrides it, an exported class's too (TSLANG 45)
if (identifier.getValue().ends_with("..instanceOf"))
{
callees.known = true;
callees.instanceOf = true;
return;
}
// a @final receiver's slot holds the method named: no family, one function
if (!refOp->hasAttr(FINAL_RECEIVER_ATTR_NAME))
{
Expand Down
10 changes: 10 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1835,6 +1835,16 @@ 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 44: a cast helper made for code in a namespace is not the one made for the cast before
# TSLANG_ISSUES 45: a union widened to one with all its members is a plain cast; -mm=own takes an
# exported class's `..instanceOf` for a call that destroys nothing
# TSLANG_ISSUES 46: a function returning its union parameter, given a new object, freed it twice
foreach(issues_mm gc rc none own)
foreach(issues_test 00tslang_issue_44 00tslang_issue_45 00tslang_issue_46)
tslang_add_test(NAME test-jit-${issues_test}-${issues_mm} COMMAND test-runner -jit -mm=${issues_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/${issues_test}.ts")
tslang_add_test(NAME test-compile-${issues_test}-${issues_mm} COMMAND test-runner -mm=${issues_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/${issues_test}.ts")
endforeach()
endforeach()
# TSLANG_ISSUES 37, 38: a program declares its own Symbol, as a function or as a class
foreach(issues_mm gc rc none own)
foreach(symbol_test 00tslang_issue_37 00tslang_issue_38)
Expand Down
75 changes: 75 additions & 0 deletions tslang/test/tester/tests/00tslang_issue_44.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// TSLANG_ISSUES 44: the helpers made from source for a cast (`___cast`, `___unbox`, ...) are declared in
// the root namespace, and the one made for the previous cast was erased only when the code was outside a
// namespace. In a namespace the old helper stayed, so `flag && a && b` cast `null | boolean | A | B`
// with the body made for `null | boolean | A`, and a `B` threw "Can't cast from union type".

class A { }
class B { }
class C { }

namespace NS {
export function test(flag: boolean, a: A | null, b: B | null): boolean {
if (flag && a && b) {
return true;
}
return false;
}

export function test3(flag: boolean, a: A | null, b: B | null, c: C | null): boolean {
if (flag && a && b && c) {
return true;
}
return false;
}

export namespace Inner {
export function test(a: A | null, flag: boolean, c: C | null): boolean {
if (a && flag && c) {
return true;
}
return false;
}
}

// classes of the namespace, tested in a method, as native_ts_graphics' variable_shading_nri.ts does
export class Texture { }
export class Descriptor { }

export class Pass {
cleared = false;
texture: Texture | null = null;
descriptor: Descriptor | null = null;

ready(): boolean {
const texture = this.texture;
const descriptor = this.descriptor;
if (!this.cleared && texture && descriptor) {
return true;
}
return false;
}
}
}

function main() {
assert(NS.test(true, new A(), new B()), "a && b of two classes in a namespace");
assert(!NS.test(true, new A(), null), "b is null");
assert(!NS.test(true, null, new B()), "a is null");
assert(!NS.test(false, new A(), new B()), "flag is false");

assert(NS.test3(true, new A(), new B(), new C()), "three classes in a namespace");
assert(!NS.test3(true, new A(), new B(), null), "c is null");

assert(NS.Inner.test(new A(), true, new C()), "a nested namespace");
assert(!NS.Inner.test(new A(), false, new C()), "a nested namespace, flag is false");

const pass = new NS.Pass();
assert(!pass.ready(), "a method: no texture yet");
pass.texture = new NS.Texture();
pass.descriptor = new NS.Descriptor();
assert(pass.ready(), "a method testing classes of the namespace");
pass.cleared = true;
assert(!pass.ready(), "a method: cleared");

print("done.");
}
77 changes: 77 additions & 0 deletions tslang/test/tester/tests/00tslang_issue_45.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// TSLANG_ISSUES 45: `flag && a && b` widens `null | boolean | A` to `null | boolean | A | B`. That went
// through a `___cast` call, whose result -mm=own took for a value stored ("borrows the result of
// '___cast<...>'"); it is a plain cast now, the value keeping its tag. And `___cast` tests an instance
// with its class's `..instanceOf`, called through the vtable: for an exported class -mm=own took that
// call for one that may release a field read before it.

class A { }
class B { }
class C { }

function both(flag: boolean, a: A | null, b: B | null): boolean {
if (flag && a && b) {
return true;
}
return false;
}

function three(a: A | null, flag: boolean, c: C | null): boolean {
return !!(a && flag && c);
}

// a widened union keeps what it holds
function widen(v: null | boolean | A): null | boolean | A | B {
return v;
}

function describe(v: null | boolean | A | B): string {
if (v === null) return "null";
if (typeof v == "boolean") return v ? "true" : "false";
if (v instanceof A) return "A";
if (v instanceof B) return "B";
return "?";
}

export class Texture { }
export class Descriptor { }

export class Pass {
cleared = false;
texture: Texture | null = null;
descriptor: Descriptor | null = null;

ready(): boolean {
const texture = this.texture;
const descriptor = this.descriptor;
if (!this.cleared && texture && descriptor) {
return true;
}
return false;
}
}

function main() {
assert(both(true, new A(), new B()), "a && b");
assert(!both(true, new A(), null), "b is null");
assert(!both(true, null, new B()), "a is null");
assert(!both(false, new A(), new B()), "flag is false");

assert(three(new A(), true, new C()), "!!(a && flag && c)");
assert(!three(new A(), false, new C()), "!!(a && flag && c), flag is false");
assert(!three(null, true, new C()), "!!(a && flag && c), a is null");

assert(describe(widen(null)) == "null", "widened null");
assert(describe(widen(true)) == "true", "widened true");
assert(describe(widen(false)) == "false", "widened false");
assert(describe(widen(new A())) == "A", "widened A");

const pass = new Pass();
assert(!pass.ready(), "no texture yet");
pass.texture = new Texture();
pass.descriptor = new Descriptor();
assert(pass.ready(), "fields of exported classes tested in a method");
pass.cleared = true;
assert(!pass.ready(), "cleared");

print("done.");
}
Loading
Loading