Skip to content

@cpp class | null tested for truth with a boolean (h && cond): no instanceof - #765

Merged
ASDAlexander77 merged 1 commit into
mainfrom
cpp-union-truthiness
Nov 1, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
cpp-union-truthiness

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Fixes TSLANG_ISSUES 23: if (h && n == 3) with h a @cpp class | null failed with "instanceof can't be used with a @cpp class" and "'___cast' not all generic types can be identified".

h && n == 3 is typed Handle | null | boolean; making it a boolean goes through castFromUnion, whose generated ___cast tests every class member with instanceof, which a @cpp class refuses (its objects are C++ objects with no type information).

  • The class tests are now one writeClassTests lambda (used by the null-target and default branches).
  • When the union has a @cpp class and the class need not be told apart (the only class of the union, or tested for truth), it emits return t;: typeof has narrowed t enough, and truthiness is the null test.
  • Otherwise instanceof is emitted only for the non-@cpp classes.

Test: tests/00cpp_union_truth.ts (hand-made C++ object): h && n == 3, enabled && h with a handle and null, and two @cpp classes in a union with a boolean tested for truth. Registered compile/JIT default + own, and in the rc/none corpus. Full release ctest passes.

Issue 22 (one-field @value T | null) was already fixed by #763; checked with the current build.

🤖 Generated with Claude Code

…nger asks instanceof

`h && n == 3` with h a `Handle | null` (@cpp) is a union of the class, null
and boolean; castFromUnion's ___cast helper tested each class member with
instanceof, which a @cpp class refuses (its objects carry no type info).
When the class need not be told apart - the only class of the union, or the
union tested for truth - typeof has narrowed t enough: `return t`. Otherwise
only the non-@cpp classes are tested. TSLANG_ISSUES 23.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit f8b3540 into main Nov 1, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the cpp-union-truthiness branch November 1, 2026 11:51
ASDAlexander77 added a commit that referenced this pull request Nov 1, 2026
* TSLANG_ISSUES: 22, one-field @value T | null crash; 23, h && cond with a @cpp class | null

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* TSLANG_ISSUES: 22 and 23 fixed (#763, #765)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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