Skip to content

A cast helper in a namespace, union widening, own's instanceOf and a returned union parameter (TSLANG 44-46) - #803

Merged
ASDAlexander77 merged 1 commit into
mainfrom
tslang-issues-44-46
Nov 10, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
tslang-issues-44-46

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Fixes TSLANG_ISSUES 44, 45 and 46, found while porting native_ts_graphics (examples/nri/variable_shading_nri.ts).

44: flag && a && b in a namespace threw "Can't cast from union type"

The helpers a cast builds from source (___cast, ___unbox, ___unbox_interface, ___check_tuple, ___bin_op_*, ___union_eq_*) are declared in the root namespace, but removeGenericFunctionMap erased the previous one only from the namespaces it walked: inside NS it stopped at NS. The stale ___cast (made for null | boolean | A) was then instantiated for null | boolean | A | B, and a B reached its throw. The previous helper is now erased from the root namespace.

45: -mm=own refused flag && a && b of two classes

  • A union widened to one that holds all its members is now a plain ts.Cast (the lowering already re-tags it), not a ___cast call whose result own treated as a stored value.
  • ___cast tests an instance with its class's ..instanceOf through the vtable (ThisVirtualSymbolRef). For an exported class (every class in a namespace) own resolved that as an open family and assumed it may destroy anything, so !this.cleared && texture && descriptor was refused. A class's ..instanceOf is the compiler's own and destroys nothing, as the .instanceOf slot read already was.

46: -mm=own freed a returned union parameter twice

function same(v: A | null): A | null { return v; } called as same(new A()): the compiled exe hung or crashed at exit (alpha121: 0xC0000374); JIT passed.

  • Spec 78's hand-over looked at the users of the argument <A | null>new A(), a view with no release, so it made the result fresh while the object's own release stayed. It now looks at the object behind its views.
  • The object's release may sit in another block (a condition after the call splits the function); a release in a block the call dominates, where the result is released too, counts.
  • With the result left a borrow, x instanceof A of a borrowed union was refused, as it already was for a field read (const x = this.f; x instanceof A). The cast to an opaque pointer whose vtable is read for .instanceOf is now a test that keeps nothing in the borrow walk, as isBorrow already said.

Tests

  • New 00tslang_issue_44.ts, 00tslang_issue_45.ts, 00tslang_issue_46.ts, JIT and compiled under gc, rc, none and own.
  • Full ctest on Windows Release: 6937 passed, 0 failed.
  • Not run: Linux, the Debug build, the DefaultLib suite.

🤖 Generated with Claude Code

…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 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit f6d2249 into main Nov 10, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the tslang-issues-44-46 branch November 10, 2026 02:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant