From 158468be377f1c8748acdbc075e993df58c69a61 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Tue, 10 Nov 2026 01:42:38 +0000 Subject: [PATCH] A cast helper in a namespace, union widening, own's instanceOf and a returned union parameter (TSLANG 44-46) 44: the helpers a cast makes from source (___cast, ___unbox, ...) are declared in the root namespace; the one made before is now erased there too, so code in a namespace no longer gets the helper made for another type (`flag && a && b` threw "Can't cast from union type"). 45: a union widened to one that has all its members is a plain cast, not a ___cast call whose result -mm=own took for a value stored. -mm=own takes a class's ..instanceOf called through the vtable for a call that destroys nothing, an exported class's too. 46 (-mm=own): a function returning its union parameter, given a new object, freed it twice. The temporary taken over is the object behind its views, and its release may be in a block only the call leads to. `x instanceof A` of a borrowed union is a test that keeps nothing. Co-Authored-By: Claude Opus 5.5 --- TSLANG_ISSUES.md | 35 +++++++++ tslang/lib/TypeScript/MLIRGenCast.cpp | 13 ++++ tslang/lib/TypeScript/MLIRGenImpl.h | 28 ++----- .../OwnershipInferenceBorrowers.cpp | 10 +++ .../lib/TypeScript/OwnershipSignaturePass.cpp | 23 +++++- .../TypeScript/OwnershipSignatureResolve.cpp | 9 +++ tslang/test/tester/CMakeLists.txt | 10 +++ tslang/test/tester/tests/00tslang_issue_44.ts | 75 ++++++++++++++++++ tslang/test/tester/tests/00tslang_issue_45.ts | 77 +++++++++++++++++++ tslang/test/tester/tests/00tslang_issue_46.ts | 75 ++++++++++++++++++ 10 files changed, 331 insertions(+), 24 deletions(-) create mode 100644 tslang/test/tester/tests/00tslang_issue_44.ts create mode 100644 tslang/test/tester/tests/00tslang_issue_45.ts create mode 100644 tslang/test/tester/tests/00tslang_issue_46.ts diff --git a/TSLANG_ISSUES.md b/TSLANG_ISSUES.md index b6aa95a89..fd1d74686 100644 --- a/TSLANG_ISSUES.md +++ b/TSLANG_ISSUES.md @@ -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 `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. diff --git a/tslang/lib/TypeScript/MLIRGenCast.cpp b/tslang/lib/TypeScript/MLIRGenCast.cpp index b72369f60..a61f9ec63 100644 --- a/tslang/lib/TypeScript/MLIRGenCast.cpp +++ b/tslang/lib/TypeScript/MLIRGenCast.cpp @@ -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(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; diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index 4154574e7..ebff71989 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -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); @@ -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 { diff --git a/tslang/lib/TypeScript/OwnershipInferenceBorrowers.cpp b/tslang/lib/TypeScript/OwnershipInferenceBorrowers.cpp index bbced418e..fb53528a6 100644 --- a/tslang/lib/TypeScript/OwnershipInferenceBorrowers.cpp +++ b/tslang/lib/TypeScript/OwnershipInferenceBorrowers.cpp @@ -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(user); + castOp && mlir::isa(castOp.getIn().getType()) && + mlir::isa(castOp.getType()) && isBorrow(user, pending.value)) + { + continue; + } + llvm::SmallVector same; llvm::SmallVector renewedAt; if (passesOn(use, same, &renewedAt)) diff --git a/tslang/lib/TypeScript/OwnershipSignaturePass.cpp b/tslang/lib/TypeScript/OwnershipSignaturePass.cpp index 5fcaac7b8..1491554f6 100644 --- a/tslang/lib/TypeScript/OwnershipSignaturePass.cpp +++ b/tslang/lib/TypeScript/OwnershipSignaturePass.cpp @@ -891,6 +891,12 @@ namespace own_signature receiver = object; } + // ...seen through its views: `same(new A())` to an `A | null` parameter is given + // `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(); @@ -901,6 +907,21 @@ 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()); + 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(use) && use->getBlock() == user->getBlock(); + }); + }; + llvm::SmallVector releases; auto temporary = llvm::all_of(receiver.getUsers(), [&](mlir::Operation *user) { if (user == op) @@ -908,7 +929,7 @@ namespace own_signature return true; } - if (mlir::isa(user) && user->getBlock() == op->getBlock() && op->isBeforeInBlock(user)) + if (mlir::isa(user) && releasedAfter(user)) { releases.push_back(user); return true; diff --git a/tslang/lib/TypeScript/OwnershipSignatureResolve.cpp b/tslang/lib/TypeScript/OwnershipSignatureResolve.cpp index f9637ca10..78b88c240 100644 --- a/tslang/lib/TypeScript/OwnershipSignatureResolve.cpp +++ b/tslang/lib/TypeScript/OwnershipSignatureResolve.cpp @@ -332,6 +332,15 @@ namespace own_signature if (auto refOp = mlir::dyn_cast_or_null(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)) { diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index f4bc9213d..ac67c9478 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -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) diff --git a/tslang/test/tester/tests/00tslang_issue_44.ts b/tslang/test/tester/tests/00tslang_issue_44.ts new file mode 100644 index 000000000..bc5c77358 --- /dev/null +++ b/tslang/test/tester/tests/00tslang_issue_44.ts @@ -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."); +} diff --git a/tslang/test/tester/tests/00tslang_issue_45.ts b/tslang/test/tester/tests/00tslang_issue_45.ts new file mode 100644 index 000000000..d9f6fdb95 --- /dev/null +++ b/tslang/test/tester/tests/00tslang_issue_45.ts @@ -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."); +} diff --git a/tslang/test/tester/tests/00tslang_issue_46.ts b/tslang/test/tester/tests/00tslang_issue_46.ts new file mode 100644 index 000000000..d698e6a75 --- /dev/null +++ b/tslang/test/tester/tests/00tslang_issue_46.ts @@ -0,0 +1,75 @@ +// TSLANG_ISSUES 46 (-mm=own): a function returning its union parameter, given a new object, freed the +// object twice. `same(new A())` to an `A | null` parameter is given `new A()`: the result +// was made the caller's own while the object's release stayed. The releases taken over are the +// object's now, after the call in its block or in one only the call leads to. And `x instanceof A` +// of a borrowed union (the result of such a call, or a field read) is a test that keeps nothing. + +class A { + v = 1; +} + +class B { } + +function same(v: A | null): A | null { + return v; +} + +function sameTagged(v: boolean | A): boolean | A { + return v; +} + +function widen(v: null | boolean | A): null | boolean | A | B { + return v; +} + +function make(): A { + return new A(); +} + +class Holder { + f: A | null = new A(); + + has(): boolean { + const x = this.f; + return x instanceof A; + } +} + +function main() { + // a new object given straight to the call + const x = same(new A()); + assert(x instanceof A, "same(new A())"); + + const t = sameTagged(new A()); + assert(t instanceof A, "sameTagged(new A())"); + + const w = widen(new A()); + assert(w instanceof A, "widen(new A())"); + assert(widen(true) === true, "widen(true)"); + assert(widen(null) === null, "widen(null)"); + + // a named object, used after the call: the result borrows it + const a = new A(); + const y = same(a); + assert(a instanceof A && y instanceof A && a.v == 1, "same(a), a used after"); + + // what a call returns, given on + const z = same(make()); + assert(z instanceof A, "same(make())"); + + // in a loop + let sum = 0; + for (let i = 0; i < 3; i++) { + const e = same(new A()); + if (e) { + sum += e.v; + } + } + + assert(sum == 3, "same(new A()) in a loop"); + + // a field read tested with instanceof + assert(new Holder().has(), "instanceof of a borrowed field"); + + print("done."); +}